func DebugLobbyFill(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, debugLobbyFillFilter, func(param map[string]interface{}) string { id := param["id"].(uint) lobby, _ := models.GetLobbyById(id) var players []*models.Player for i := 1; i < int(lobby.Type)*2; i++ { steamid := "DEBUG" + strconv.FormatUint(uint64(time.Now().Unix()), 10) + strconv.Itoa(i) player, _ := models.NewPlayer(steamid) player.Debug = true player.Save() players = append(players, player) lobby.AddPlayer(player, i) } lobby.State = models.LobbyStateReadyingUp lobby.Save() room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobby.ID)) broadcaster.SendMessageToRoom(room, "lobbyReadyUp", "") lobby.ReadyUpTimeoutCheck() bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) }) }
func (Chat) ChatSend(server *wsevent.Server, so *wsevent.Client, data []byte) []byte { reqerr := chelpers.FilterRequest(so, 0, true) if reqerr != nil { return reqerr.Encode() } var args struct { Message *string `json:"message"` Room *int `json:"room"` } err := chelpers.GetParams(data, &args) if err != nil { return helpers.NewTPErrorFromError(err).Encode() } player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if tperr != nil { return tperr.Encode() } //helpers.Logger.Debug("received chat message: %s %s", *args.Message, player.Name) if *args.Room > 0 { spec := player.IsSpectatingId(uint(*args.Room)) //Check if player has either joined, or is spectating lobby lobbyId, tperr := player.GetLobbyId() if tperr != nil && !spec && lobbyId != uint(*args.Room) { return helpers.NewTPError("Player is not in the lobby.", 5).Encode() } } else { // else room is the lobby list room *args.Room, _ = strconv.Atoi(config.Constants.GlobalChatRoom) } if (*args.Message)[0] == '\n' { return helpers.NewTPError("Cannot send messages prefixed with newline", 4).Encode() } if len(*args.Message) > 120 { return helpers.NewTPError("Message too long", 4).Encode() } message := models.NewChatMessage(*args.Message, *args.Room, player) db.DB.Save(message) bytes, _ := json.Marshal(message) broadcaster.SendMessageToRoom(fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(uint(*args.Room))), "chatReceive", string(bytes)) if strings.HasPrefix(*args.Message, "!admin") { chelpers.SendToSlack(*args.Message, player.Name, player.SteamId) } return chelpers.EmptySuccessJS }
func DebugRequestLobbyStart(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, debugRequestLobbyStartFilter, func(params map[string]interface{}) string { lobby, _ := models.GetLobbyById(params["id"].(uint)) bytes, _ := models.DecorateLobbyConnectJSON(lobby).Encode() room := fmt.Sprintf("%s_private", chelpers.GetLobbyRoom(lobby.ID)) broadcaster.SendMessageToRoom(room, "lobbyStart", string(bytes)) bytes, _ = chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) }) }
func PlayerReady(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, playerReadyFilter, func(_ map[string]interface{}) string { steamid := chelpers.GetSteamId(so.Id()) player, tperr := models.GetPlayerBySteamId(steamid) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } lobbyid, tperr := player.GetLobbyId() if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } lobby, tperr := models.GetLobbyById(lobbyid) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } if lobby.State != models.LobbyStateReadyingUp { bytes, _ := helpers.NewTPError("Lobby hasn't been filled up yet.", 4).ErrorJSON().Encode() return string(bytes) } helpers.LockRecord(lobby.ID, lobby) tperr = lobby.ReadyPlayer(player) defer helpers.UnlockRecord(lobby.ID, lobby) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } if lobby.IsEveryoneReady() { lobby.State = models.LobbyStateInProgress lobby.Save() bytes, _ := models.DecorateLobbyConnectJSON(lobby).Encode() room := fmt.Sprintf("%s_private", chelpers.GetLobbyRoom(lobby.ID)) broadcaster.SendMessageToRoom(room, "lobbyStart", string(bytes)) models.BroadcastLobbyList() } bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) }) }
func ChatSend(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, chatSendFilter, func(params map[string]interface{}) string { message := params["message"].(string) room := params["room"].(int) player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } helpers.Logger.Debug("received chat message: %s %s", message, player.Name) spec := player.IsSpectatingId(uint(room)) //Check if player has either joined, or is spectating lobby lobbyId, tperr := player.GetLobbyId() if room > 0 { if tperr != nil && !spec && lobbyId != uint(room) { bytes, _ := chelpers.BuildFailureJSON("Player is not in the lobby.", 5).Encode() return string(bytes) } } else { // else room is the lobby list room room, _ = strconv.Atoi(config.Constants.GlobalChatRoom) } chatMessage := simplejson.New() // TODO send proper timestamps chatMessage.Set("timestamp", time.Now().Unix()) chatMessage.Set("message", message) chatMessage.Set("room", room) chatMessage.Set("player", models.DecoratePlayerSummaryJson(player)) bytes, _ := chatMessage.Encode() broadcaster.SendMessageToRoom(fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(uint(room))), "chatReceive", string(bytes)) resp, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() chelpers.LogChat(uint(room), player.Name, player.SteamId, message) chelpers.AddScrollbackMessage(uint(room), string(bytes)) return string(resp) }) }
func (Lobby) LobbyJoin(server *wsevent.Server, so *wsevent.Client, data []byte) []byte { reqerr := chelpers.FilterRequest(so, authority.AuthAction(0), true) if reqerr != nil { return reqerr.Encode() } var args struct { Id *uint `json:"id"` Class *string `json:"class"` Team *string `json:"team" valid:"red,blu"` } if err := chelpers.GetParams(data, &args); err != nil { return helpers.NewTPErrorFromError(err).Encode() } //helpers.Logger.Debug("id %d class %s team %s", *args.Id, *args.Class, *args.Team) player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if tperr != nil { return tperr.Encode() } lob, tperr := models.GetLobbyById(*args.Id) if tperr != nil { return tperr.Encode() } if lob.State == models.LobbyStateEnded { return helpers.NewTPError("Cannot join a closed lobby.", -1).Encode() } //Check if player is in the same lobby var sameLobby bool if id, err := player.GetLobbyId(); err == nil && id == *args.Id { sameLobby = true } slot, tperr := models.LobbyGetPlayerSlot(lob.Type, *args.Team, *args.Class) if tperr != nil { return tperr.Encode() } if prevId, err := player.GetLobbyId(); err != nil { server.RemoveClient(so.Id(), fmt.Sprintf("%d_public", prevId)) server.RemoveClient(so.Id(), fmt.Sprintf("%d_private", prevId)) } tperr = lob.AddPlayer(player, slot, *args.Team, *args.Class) if tperr != nil { return tperr.Encode() } if !sameLobby { chelpers.AfterLobbyJoin(server, so, lob, player) } if lob.IsFull() { lob.State = models.LobbyStateReadyingUp lob.ReadyUpTimestamp = time.Now().Unix() + 30 lob.Save() tick := time.After(time.Second * 30) id := lob.ID stop := make(chan struct{}) go func() { select { case <-tick: lobby := &models.Lobby{} db.DB.First(lobby, id) if lobby.State != models.LobbyStateInProgress { err := lobby.RemoveUnreadyPlayers() if err != nil { helpers.Logger.Error("RemoveUnreadyPlayers: ", err.Error()) err = nil } err = lobby.UnreadyAllPlayers() if err != nil { helpers.Logger.Error("UnreadyAllPlayers: ", err.Error()) } lobby.State = models.LobbyStateWaiting lobby.Save() } case <-stop: return } }() room := fmt.Sprintf("%s_private", chelpers.GetLobbyRoom(lob.ID)) broadcaster.SendMessageToRoom(room, "lobbyReadyUp", `{"timeout":30}`) models.BroadcastLobbyList() } err := models.AllowPlayer(*args.Id, player.SteamId, *args.Team+*args.Class) if err != nil { helpers.Logger.Error(err.Error()) } if lob.State == models.LobbyStateInProgress { bytes, _ := json.Marshal(models.DecorateLobbyConnect(lob, player.Name, *args.Class)) broadcaster.SendMessage(player.SteamId, "lobbyStart", string(bytes)) } return chelpers.EmptySuccessJS }
func handleEvent(event map[string]interface{}) { switch event["name"] { case "playerDisc": slot := &models.LobbySlot{} lobbyid := event["lobbyId"].(uint) steamId := event["steamId"].(string) player, _ := models.GetPlayerBySteamId(steamId) db.DB.Where("player_id = ? AND lobby_id = ?", player.ID, lobbyid).First(slot) helpers.LockRecord(slot.ID, slot) slot.InGame = false db.DB.Save(slot) helpers.UnlockRecord(slot.ID, slot) room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid)) broadcaster.SendMessageToRoom(room, "sendNotification", fmt.Sprintf("%s has disconected from the server .", player.Name)) go func() { t := time.After(time.Minute * 2) <-t lobby, _ := models.GetLobbyById(lobbyid) slot := &models.LobbySlot{} db.DB.Where("player_id = ? AND lobby_id = ?", player.ID, lobbyid).First(slot) if !slot.InGame { helpers.LockRecord(lobby.ID, lobby) defer helpers.UnlockRecord(lobby.ID, lobby) lobby.RemovePlayer(player) broadcaster.SendMessage(player.SteamId, "sendNotification", "You have been removed from the lobby.") } }() case "playerConn": slot := &models.LobbySlot{} lobbyid := event["lobbyId"].(uint) steamId := event["steamId"].(string) player, _ := models.GetPlayerBySteamId(steamId) err := db.DB.Where("player_id = ? AND lobby_id = ?", player.ID, lobbyid).First(slot).Error if err == nil { //else, player isn't in the lobby, will be kicked by Pauling helpers.LockRecord(slot.ID, slot) slot.InGame = true db.DB.Save(slot) helpers.UnlockRecord(slot.ID, slot) } case "playerRep": lobbyid := event["lobbyId"].(uint) steamId := event["steamId"].(string) player, _ := models.GetPlayerBySteamId(steamId) var slot *models.LobbySlot db.DB.Where("player_id = ? AND lobby_id = ?", player.ID, lobbyid).Find(slot) slot.NeedSub = true db.DB.Save(slot) room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid)) broadcaster.SendMessageToRoom(room, "sendNotification", fmt.Sprintf("%s has been reported.", player.Name)) case "discFromServer": lobbyid := event["lobbyId"].(uint) lobby, _ := models.GetLobbyById(lobbyid) helpers.LockRecord(lobby.ID, lobby) lobby.Close(false) helpers.UnlockRecord(lobby.ID, lobby) room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid)) broadcaster.SendMessageToRoom(room, "sendNotification", "Disconnected from Server.") case "matchEnded": lobbyid := event["lobbyId"].(uint) lobby, _ := models.GetLobbyById(lobbyid) helpers.LockRecord(lobby.ID, lobby) lobby.UpdateStats() lobby.Close(false) helpers.UnlockRecord(lobby.ID, lobby) room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid)) broadcaster.SendMessageToRoom(room, "sendNotification", "Lobby Ended.") case "getServers": var lobbies []*models.Lobby var activeStates = []models.LobbyState{models.LobbyStateWaiting, models.LobbyStateInProgress} db.DB.Model(&models.Lobby{}).Where("state IN (?)", activeStates).Find(&lobbies) for _, lobby := range lobbies { info := models.ServerBootstrap{ LobbyId: lobby.ID, Info: lobby.ServerInfo, } for _, player := range lobby.BannedPlayers { info.BannedPlayers = append(info.BannedPlayers, player.SteamId) } for _, slot := range lobby.Slots { var player *models.Player db.DB.Find(player, slot.PlayerId) info.Players = append(info.Players, player.SteamId) } models.Pauling.Call("Pauling.SetupVerifier", &info, &struct{}{}) } } }
func eventListener(eventChanMap map[string](chan map[string]interface{})) { for { select { case event := <-eventChanMap["playerDisc"]: lobbyid := event["lobbyId"].(uint) steamId := event["steamId"].(string) player, _ := models.GetPlayerBySteamId(steamId) lobby, _ := models.GetLobbyById(lobbyid) lobby.SetNotInGame(player) helpers.Logger.Debug("#%d, player %s<%s> disconnected", lobby.ID, player.Name, player.SteamId) room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid)) broadcaster.SendMessageToRoom(room, "sendNotification", fmt.Sprintf(`{"notification": "%s has disconected from the server."}`, player.Name)) t := time.After(time.Minute * 2) go func() { <-t lobby, _ := models.GetLobbyById(lobbyid) ingame, err := lobby.IsPlayerInGame(player) if err != nil { helpers.Logger.Error(err.Error()) } if !ingame { sub, _ := models.NewSub(lobby.ID, player.SteamId) db.DB.Save(sub) models.BroadcastSubList() lobby.RemovePlayer(player) } }() case event := <-eventChanMap["playerConn"]: lobbyid := event["lobbyId"].(uint) steamId := event["steamId"].(string) player, _ := models.GetPlayerBySteamId(steamId) lobby, _ := models.GetLobbyById(lobbyid) lobby.SetInGame(player) case event := <-eventChanMap["playerSub"]: lobbyid := event["lobbyId"].(uint) steamId := event["steamId"].(string) sub, err := models.NewSub(lobbyid, steamId) if err != nil { helpers.Logger.Error(err.Error()) continue } db.DB.Save(sub) models.BroadcastSubList() player, _ := models.GetPlayerBySteamId(steamId) room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid)) broadcaster.SendMessageToRoom(room, "sendNotification", fmt.Sprintf(`{"notification": "%s has been reported."}`, player.Name)) //helpers.Logger.Debug("#%d: Reported player %s<%s>", // lobbyid, player.Name, player.SteamId) case event := <-eventChanMap["discFromServer"]: lobbyid := event["lobbyId"].(uint) lobby, _ := models.GetLobbyByIdServer(lobbyid) helpers.Logger.Debug("#%d: Lost connection to %s", lobby.ID, lobby.ServerInfo.Host) lobby.Close(false) room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid)) broadcaster.SendMessageToRoom(room, "sendNotification", `{"notification": "Lobby Closed (Connection to server lost)."}`) case event := <-eventChanMap["matchEnded"]: lobbyid := event["lobbyId"].(uint) lobby, _ := models.GetLobbyByIdServer(lobbyid) helpers.Logger.Debug("#%d: Match Ended", lobbyid) lobby.UpdateStats() lobby.Close(false) room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid)) broadcaster.SendMessageToRoom(room, "sendNotification", `{"notification": ""Lobby Ended."}`) case <-eventChanMap["getServers"]: var lobbies []*models.Lobby var activeStates = []models.LobbyState{models.LobbyStateWaiting, models.LobbyStateInProgress} db.DB.Preload("ServerInfo").Model(&models.Lobby{}).Where("state IN (?)", activeStates).Find(&lobbies) for _, lobby := range lobbies { info := models.ServerBootstrap{ LobbyId: lobby.ID, Info: lobby.ServerInfo, } for _, player := range lobby.BannedPlayers { info.BannedPlayers = append(info.BannedPlayers, player.SteamId) } for _, slot := range lobby.Slots { var player = &models.Player{} db.DB.Find(player, slot.PlayerId) info.Players = append(info.Players, player.SteamId) } models.Pauling.Call("Pauling.SetupVerifier", &info, &struct{}{}) } } } }
func LobbyJoin(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, lobbyJoinFilters, func(params map[string]interface{}) string { player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } lobbyid := params["id"].(uint) classString := params["class"].(string) teamString := params["team"].(string) lob, tperr := models.GetLobbyById(uint(lobbyid)) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } //Check if player is in the same lobby var sameLobby bool if id, err := player.GetLobbyId(); err == nil && id == lobbyid { sameLobby = true } slot, tperr := models.LobbyGetPlayerSlot(lob.Type, teamString, classString) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } helpers.LockRecord(lob.ID, lob) defer helpers.UnlockRecord(lob.ID, lob) tperr = lob.AddPlayer(player, slot) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } if !sameLobby { chelpers.AfterLobbyJoin(so, lob, player) } if lob.IsFull() { lob.State = models.LobbyStateReadyingUp lob.Save() lob.ReadyUpTimeoutCheck() room := fmt.Sprintf("%s_private", chelpers.GetLobbyRoom(lob.ID)) broadcaster.SendMessageToRoom(room, "lobbyReadyUp", `{"timeout":30}`) models.BroadcastLobbyList() } models.BroadcastLobbyToUser(lob, player.SteamId) bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) }) }