-
Notifications
You must be signed in to change notification settings - Fork 10
/
lobby.go
94 lines (80 loc) · 3.22 KB
/
lobby.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2015 TF2Stadium
// Use of this source code is governed by the GPLv3
// that can be found in the COPYING file.
//Package hooks contains event hooks, which are called after specific events, like lobby joins and logins.
package hooks
import (
"fmt"
"strconv"
"time"
"github.com/TF2Stadium/Helen/controllers/broadcaster"
chelpers "github.com/TF2Stadium/Helen/controllers/controllerhelpers"
"github.com/TF2Stadium/Helen/controllers/socket/sessions"
"github.com/TF2Stadium/Helen/models/chat"
"github.com/TF2Stadium/Helen/models/lobby"
"github.com/TF2Stadium/Helen/models/player"
"github.com/TF2Stadium/Helen/routes/socket"
"github.com/TF2Stadium/wsevent"
)
func AfterLobbyJoin(so *wsevent.Client, lob *lobby.Lobby, player *player.Player) {
room := fmt.Sprintf("%s_private", GetLobbyRoom(lob.ID))
//make all sockets join the private room, given the one the player joined the lobby on
//might close, so lobbyStart and lobbyReadyUp can be sent to other tabs
sockets, _ := sessions.GetSockets(player.SteamID)
for _, so := range sockets {
socket.AuthServer.Join(so, room)
}
if lob.State == lobby.InProgress { // player is a substitute
lob.AfterPlayerNotInGameFunc(player, 5*time.Minute, func() {
// if player doesn't join game server in 5 minutes,
// substitute them
message := player.Alias() + " has been reported for not joining the game within 5 minutes"
chat.SendNotification(message, int(lob.ID))
lob.Substitute(player)
})
}
broadcaster.SendMessage(player.SteamID, "lobbyJoined", lobby.DecorateLobbyData(lob, false))
}
func AfterLobbyLeave(lob *lobby.Lobby, player *player.Player, kicked bool, notReady bool) {
event := lobby.LobbyEvent{
ID: lob.ID,
Kicked: kicked,
NotReady: notReady,
}
broadcaster.SendMessage(player.SteamID, "lobbyLeft", event)
sockets, _ := sessions.GetSockets(player.SteamID)
//player might have connected from multiple tabs, remove all of them from the room
for _, so := range sockets {
socket.AuthServer.Leave(so, fmt.Sprintf("%s_private", GetLobbyRoom(lob.ID)))
}
}
func AfterLobbySpec(server *wsevent.Server, so *wsevent.Client, player *player.Player, lob *lobby.Lobby) {
//remove socket from room of the previous lobby the socket was spectating (if any)
lobbyID, ok := sessions.GetSpectating(so.ID)
if ok {
server.Leave(so, fmt.Sprintf("%d_public", lobbyID))
sessions.RemoveSpectator(so.ID)
if player != nil {
prevLobby, _ := lobby.GetLobbyByID(lobbyID)
prevLobby.RemoveSpectator(player, true)
}
}
server.Join(so, fmt.Sprintf("%d_public", lob.ID))
chelpers.BroadcastScrollback(so, lob.ID)
sessions.SetSpectator(so.ID, lob.ID)
}
func AfterLobbySpecLeave(so *wsevent.Client, lob *lobby.Lobby) {
socket.AuthServer.Leave(so, fmt.Sprintf("%s_public", GetLobbyRoom(lob.ID)))
sessions.RemoveSpectator(so.ID)
}
func GetLobbyRoom(lobbyid uint) string {
return strconv.FormatUint(uint64(lobbyid), 10)
}
//Not really broadcast, since it sends each client a different LobbyStart JSON
func BroadcastLobbyStart(lob *lobby.Lobby) {
for _, slot := range lob.GetAllSlots() {
player, _ := player.GetPlayerByID(slot.PlayerID)
connectInfo := lobby.DecorateLobbyConnect(lob, player, slot.Slot)
broadcaster.SendMessage(player.SteamID, "lobbyStart", connectInfo)
}
}