func ReadyTimeoutListener() { for { id := <-readyUpLobbyID go func() { tick := time.After(time.Second * 30) lobby := &Lobby{} helpers.LockRecord(lobby.ID, lobby) lobby.readyUpTimestamp = time.Now().Unix() helpers.UnlockRecord(lobby.ID, lobby) <-tick db.DB.First(lobby, id) if lobby.State != LobbyStateInProgress { helpers.LockRecord(lobby.ID, lobby) defer helpers.UnlockRecord(lobby.ID, lobby) err := lobby.RemoveUnreadyPlayers() if err != nil { helpers.Logger.Critical(err.Error()) } lobby.UnreadyAllPlayers() if err != nil { helpers.Logger.Critical(err.Error()) } lobby.State = LobbyStateWaiting lobby.Save() } }() } }
func LobbyClose(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, lobbyCloseFilters, func(params map[string]interface{}) string { player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) lobbyid := params["id"].(uint) lob, tperr := models.GetLobbyById(uint(lobbyid)) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } if player.SteamId != lob.CreatedBySteamID && player.Role != helpers.RoleAdmin { bytes, _ := chelpers.BuildFailureJSON("Player not authorized to close lobby.", 1).Encode() return string(bytes) } if lob.State == models.LobbyStateEnded { bytes, _ := chelpers.BuildFailureJSON("Lobby already closed.", -1).Encode() return string(bytes) } helpers.LockRecord(lob.ID, lob) lob.Close(true) helpers.UnlockRecord(lob.ID, lob) chelpers.StopLogger(lobbyid) models.BroadcastLobbyList() // has to be done manually for now 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 PlayerNotReady(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, playerUnreadyFilter, func(_ map[string]interface{}) string { player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } 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.UnreadyPlayer(player) lobby.RemovePlayer(player) helpers.UnlockRecord(lobby.ID, lobby) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } lobby.UnreadyAllPlayers() bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) }) }
func PlayerUnready(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, playerUnreadyFilter, func(_ map[string]interface{}) string { player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } 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) } helpers.LockRecord(lobby.ID, lobby) tperr = lobby.UnreadyPlayer(player) helpers.UnlockRecord(lobby.ID, lobby) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) }) }
func LobbySpectatorJoin(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, lobbySpectatorJoinFilters, func(params map[string]interface{}) string { lobbyid := params["id"].(uint) player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } var lob *models.Lobby lob, tperr = models.GetLobbyById(uint(lobbyid)) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } if id, _ := player.GetLobbyId(); id != lobbyid { helpers.LockRecord(lob.ID, lob) tperr = lob.AddSpectator(player) helpers.UnlockRecord(lob.ID, lob) } bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } chelpers.AfterLobbySpec(so, lob) models.BroadcastLobbyToUser(lob, player.SteamId) return string(bytes) }) }
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 LobbyKick(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, lobbyKickFilters, func(params map[string]interface{}) string { steamid := params["steamid"].(string) ban := params["ban"].(bool) lobbyid := params["id"].(uint) var self bool selfSteamid := chelpers.GetSteamId(so.Id()) // TODO check authorization, currently can kick anyone if steamid == "" { self = true steamid = selfSteamid } //player to kick player, tperr := models.GetPlayerBySteamId(steamid) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } lob, tperr := models.GetLobbyById(uint(lobbyid)) if tperr != nil { bytes, _ := chelpers.BuildFailureJSON(tperr.Error(), -1).Encode() return string(bytes) } if !self && selfSteamid != lob.CreatedBySteamID { // TODO proper authorization checks bytes, _ := chelpers.BuildFailureJSON( "Not authorized to remove players", 1).Encode() return string(bytes) } _, err := lob.GetPlayerSlot(player) helpers.LockRecord(lob.ID, lob) defer helpers.UnlockRecord(lob.ID, lob) var spec bool if err == nil { lob.RemovePlayer(player) } else if player.IsSpectatingId(lob.ID) { spec = true lob.RemoveSpectator(player) } else { bytes, _ := chelpers.BuildFailureJSON("Player neither playing nor spectating", 2).Encode() return string(bytes) } if ban { lob.BanPlayer(player) } if !spec { chelpers.AfterLobbyLeave(so, lob, player) } else { chelpers.AfterLobbySpecLeave(so, lob) } if !self { broadcaster.SendMessage(steamid, "sendNotification", fmt.Sprintf("You have been removed from Lobby #%d", lobbyid)) } bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) }) }
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) }) }