func TestGetSpectatingIds(t *testing.T) { testhelpers.CleanupDB() player, _ := models.NewPlayer("asdf") database.DB.Save(player) specIds, specErr := player.GetSpectatingIds() assert.Nil(t, specErr) assert.Equal(t, len(specIds), 0) //assert.Equal(t, []uint{lobby.ID, lobby2.ID}, specIds) lobby1 := models.NewLobby("cp_badlands", models.LobbyTypeSixes, "ugc", models.ServerRecord{}, 1, false) database.DB.Save(lobby1) lobby1.AddSpectator(player) specIds, specErr = player.GetSpectatingIds() assert.Nil(t, specErr) assert.Equal(t, specIds[0], lobby1.ID) lobby2 := models.NewLobby("cp_badlands", models.LobbyTypeSixes, "ugc", models.ServerRecord{}, 1, false) database.DB.Save(lobby2) lobby2.AddSpectator(player) specIds, specErr = player.GetSpectatingIds() assert.Nil(t, specErr) assert.Equal(t, []uint{lobby1.ID, lobby2.ID}, specIds) }
func TestIsSpectating(t *testing.T) { testhelpers.CleanupDB() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, "ugc", models.ServerRecord{}, 1, false) database.DB.Save(lobby) lobby2 := models.NewLobby("cp_badlands", models.LobbyTypeSixes, "ugc", models.ServerRecord{}, 1, false) database.DB.Save(lobby2) player, _ := models.NewPlayer("asdf") database.DB.Save(player) isSpectating := player.IsSpectatingId(lobby.ID) assert.False(t, isSpectating) lobby.AddSpectator(player) isSpectating = player.IsSpectatingId(lobby.ID) assert.True(t, isSpectating) lobby2.AddSpectator(player) isSpectating2 := player.IsSpectatingId(lobby2.ID) assert.True(t, isSpectating2) specIds, specErr := player.GetSpectatingIds() assert.Nil(t, specErr) assert.Equal(t, []uint{lobby.ID, lobby2.ID}, specIds) lobby.RemoveSpectator(player, false) isSpectating = player.IsSpectatingId(lobby.ID) assert.False(t, isSpectating) }
func TestLobbyAdd(t *testing.T) { migrations.TestCleanup() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0) lobby.Save() var players []*models.Player for i := 0; i < 12; i++ { player, playErr := models.NewPlayer("p" + fmt.Sprint(i)) assert.Nil(t, playErr) player.Save() players = append(players, player) } // add player err := lobby.AddPlayer(players[0], 0) assert.Nil(t, err) slot, err2 := lobby.GetPlayerSlot(players[0]) assert.Equal(t, slot, 0) assert.Nil(t, err2) id, err3 := lobby.GetPlayerIdBySlot(0) assert.Equal(t, id, players[0].ID) assert.Nil(t, err3) // try to switch slots err = lobby.AddPlayer(players[0], 1) assert.Nil(t, err) slot, err2 = lobby.GetPlayerSlot(players[0]) assert.Equal(t, slot, 1) assert.Nil(t, err2) // this should be empty now id, err3 = lobby.GetPlayerIdBySlot(0) assert.NotNil(t, err3) // try to add a second player to the same slot err = lobby.AddPlayer(players[1], 1) assert.NotNil(t, err) // try to add a player to a wrong slot slot err = lobby.AddPlayer(players[2], 55) assert.NotNil(t, err) lobby2 := models.NewLobby("cp_granary", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0) lobby2.Save() // try to add a player while they're in another lobby err = lobby.AddPlayer(players[0], 1) assert.NotNil(t, err) }
func TestLobbyRemove(t *testing.T) { migrations.TestCleanup() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0) lobby.Save() player, playErr := models.NewPlayer("1235") assert.Nil(t, playErr) player.Save() // add player err := lobby.AddPlayer(player, 0) assert.Nil(t, err) // remove player err = lobby.RemovePlayer(player) assert.Nil(t, err) // this should be empty now _, err2 := lobby.GetPlayerIdBySlot(0) assert.NotNil(t, err2) // can add player again err = lobby.AddPlayer(player, 0) assert.Nil(t, err) }
func TestSpectators(t *testing.T) { migrations.TestCleanup() player, playErr := models.NewPlayer("apple") assert.Nil(t, playErr) player.Save() player2, playErr2 := models.NewPlayer("testing1") assert.Nil(t, playErr2) player2.Save() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0) lobby.Save() err := lobby.AddSpectator(player) assert.Nil(t, err) var specs []models.Player db.DB.Model(lobby).Association("Spectators").Find(&specs) assert.Equal(t, 1, len(specs)) err = lobby.AddSpectator(player2) assert.Nil(t, err) specs = nil db.DB.Model(lobby).Association("Spectators").Find(&specs) assert.Equal(t, 2, len(specs)) assert.Equal(t, true, specs[0].IsSpectatingId(lobby.ID)) err = lobby.RemoveSpectator(player) assert.Nil(t, err) specs = nil db.DB.Model(lobby).Association("Spectators").Find(&specs) assert.Equal(t, 1, len(specs)) // adding the same player again should not increase the count err = lobby.AddSpectator(player2) specs = nil db.DB.Model(lobby).Association("Spectators").Find(&specs) assert.Equal(t, 1, len(specs)) // players in lobby should be removed from it if added as spectator lobby.AddPlayer(player, 10) err = lobby.AddSpectator(player) assert.Nil(t, err) _, terr := lobby.GetPlayerSlot(player) assert.NotNil(t, terr) // adding a player should remove them from spectators lobby.AddPlayer(player2, 11) specs = nil db.DB.Model(lobby).Association("Spectators").Find(&specs) assert.Equal(t, 0, len(specs)) }
func LobbyCreate(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, lobbyCreateFilters, func(params map[string]interface{}) string { player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) mapName := params["mapName"].(string) lobbytypestring := params["type"].(string) league := params["league"].(string) server := params["server"].(string) rconPwd := params["rconpwd"].(string) whitelist := int(params["whitelist"].(uint)) mumble := params["mumbleRequired"].(bool) var playermap = map[string]models.LobbyType{ "debug": models.LobbyTypeDebug, "sixes": models.LobbyTypeSixes, "highlander": models.LobbyTypeHighlander, } lobbytype, _ := playermap[lobbytypestring] randBytes := make([]byte, 6) rand.Read(randBytes) serverPwd := base64.URLEncoding.EncodeToString(randBytes) //TODO what if playermap[lobbytype] is nil? info := models.ServerRecord{ Host: server, RconPassword: rconPwd, ServerPassword: serverPwd} err := models.VerifyInfo(info) if err != nil { return err.Error() } lob := models.NewLobby(mapName, lobbytype, league, info, whitelist, mumble) lob.CreatedBySteamID = player.SteamId lob.Save() err = lob.SetupServer() if err != nil { bytes, _ := err.(*helpers.TPError).ErrorJSON().Encode() return string(bytes) } lob.State = models.LobbyStateWaiting lob.Save() lobby_id := simplejson.New() lobby_id.Set("id", lob.ID) bytes, _ := chelpers.BuildSuccessJSON(lobby_id).Encode() return string(bytes) }) }
func TestLobbyCreation(t *testing.T) { migrations.TestCleanup() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "testip", ""}, 0) lobby.Save() lobby2, _ := models.GetLobbyById(lobby.ID) assert.Equal(t, lobby.ID, lobby2.ID) assert.Equal(t, lobby.ServerInfo.Host, lobby2.ServerInfo.Host) assert.Equal(t, lobby.ServerInfo.ID, lobby2.ServerInfo.ID) //testing password creation assert.Equal(t, len(lobby.Server.ServerPassword), 8) lobby3 := models.NewLobby("cp_process_final", models.LobbyTypeSixes, models.ServerRecord{0, "testip", ""}, 0) lobby3.Save() assert.NotEqual(t, lobby.Server.ServerPassword, lobby3.Server.ServerPassword) lobby.MapName = "cp_granary" lobby.Save() db.DB.First(lobby2) assert.Equal(t, "cp_granary", lobby2.MapName) }
func TestUnreadyPlayer(t *testing.T) { migrations.TestCleanup() player, playErr := models.NewPlayer("testing") assert.Nil(t, playErr) player.Save() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0) lobby.Save() lobby.AddPlayer(player, 0) lobby.ReadyPlayer(player) lobby.UnreadyPlayer(player) ready, err := lobby.IsPlayerReady(player) assert.Equal(t, ready, false) assert.Nil(t, err) }
func TestLobbyCreation(t *testing.T) { testhelpers.CleanupDB() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, "ugc", models.ServerRecord{0, "testip", "", ""}, 0, false) lobby.Save() lobby2, _ := models.GetLobbyById(lobby.ID) assert.Equal(t, lobby.ID, lobby2.ID) assert.Equal(t, lobby.ServerInfo.Host, lobby2.ServerInfo.Host) assert.Equal(t, lobby.ServerInfo.ID, lobby2.ServerInfo.ID) lobby.MapName = "cp_granary" lobby.Save() db.DB.First(lobby2) assert.Equal(t, "cp_granary", lobby2.MapName) }
func TestUnreadyAllPlayers(t *testing.T) { testhelpers.CleanupDB() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, "", models.ServerRecord{0, "", "", ""}, 0, false) lobby.Save() for i := 0; i < 12; i++ { player, playErr := models.NewPlayer(strconv.Itoa(i)) assert.Nil(t, playErr) player.Save() lobby.AddPlayer(player, i) lobby.ReadyPlayer(player) } err := lobby.UnreadyAllPlayers() assert.Nil(t, err) ready := lobby.IsEveryoneReady() assert.Equal(t, ready, false) }
func TestIsSpectating(t *testing.T) { migrations.TestCleanup() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{}, 1) database.DB.Save(lobby) player, _ := models.NewPlayer("asdf") database.DB.Save(player) isSpectating := player.IsSpectatingId(lobby.ID) assert.False(t, isSpectating) lobby.AddSpectator(player) isSpectating = player.IsSpectatingId(lobby.ID) assert.True(t, isSpectating) lobby.RemoveSpectator(player) isSpectating = player.IsSpectatingId(lobby.ID) assert.False(t, isSpectating) }
func TestRemoveUnreadyPlayers(t *testing.T) { testhelpers.CleanupDB() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, "", models.ServerRecord{0, "", "", ""}, 0, false) lobby.Save() for i := 0; i < 12; i++ { player, playErr := models.NewPlayer(strconv.Itoa(i)) assert.Nil(t, playErr) player.Save() lobby.AddPlayer(player, i) } err := lobby.RemoveUnreadyPlayers() assert.Nil(t, err) for i := 0; i < 12; i++ { _, err := lobby.GetPlayerIdBySlot(i) assert.NotNil(t, err) } }
func TestReadyPlayer(t *testing.T) { testhelpers.CleanupDB() player, playErr := models.NewPlayer("testing") assert.Nil(t, playErr) player.Save() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, "", models.ServerRecord{0, "", "", ""}, 0, false) lobby.Save() lobby.AddPlayer(player, 0) lobby.ReadyPlayer(player) ready, err := lobby.IsPlayerReady(player) assert.Equal(t, ready, true) assert.Nil(t, err) lobby.UnreadyPlayer(player) lobby.ReadyPlayer(player) ready, err = lobby.IsPlayerReady(player) assert.Equal(t, ready, true) assert.Nil(t, err) }
func TestIsEveryoneReady(t *testing.T) { migrations.TestCleanup() player, playErr := models.NewPlayer("0") assert.Nil(t, playErr) player.Save() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0) lobby.Save() lobby.AddPlayer(player, 0) lobby.ReadyPlayer(player) assert.Equal(t, lobby.IsEveryoneReady(), false) for i := 1; i < 12; i++ { player, playErr = models.NewPlayer(strconv.Itoa(i)) assert.Nil(t, playErr) player.Save() lobby.AddPlayer(player, i) lobby.ReadyPlayer(player) } assert.Equal(t, lobby.IsEveryoneReady(), true) }
func TestLobbyBan(t *testing.T) { migrations.TestCleanup() lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0) lobby.Save() player, playErr := models.NewPlayer("1235") assert.Nil(t, playErr) player.Save() // add player err := lobby.AddPlayer(player, 0) assert.Nil(t, err) // ban player err = lobby.RemovePlayer(player) lobby.BanPlayer(player) assert.Nil(t, err) // should not be able to add again err = lobby.AddPlayer(player, 5) assert.NotNil(t, err) }
func CreateLobby() *models.Lobby { lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, "etf2l", models.ServerRecord{}, "0", false, "", "") lobby.Save() return lobby }
func (Lobby) LobbyCreate(_ *wsevent.Server, so *wsevent.Client, data []byte) []byte { reqerr := chelpers.FilterRequest(so, authority.AuthAction(0), true) if reqerr != nil { bytes, _ := json.Marshal(reqerr) return bytes } var args struct { Map *string `json:"map"` Type *string `json:"type" valid:"debug,6s,highlander,4v4,ultiduo,bball"` League *string `json:"league" valid:"ugc,etf2l,esea,asiafortress,ozfortress"` Server *string `json:"server"` RconPwd *string `json:"rconpwd"` WhitelistID *uint `json:"whitelistID"` Mumble *bool `json:"mumbleRequired"` } err := chelpers.GetParams(data, &args) if err != nil { return helpers.NewTPErrorFromError(err).Encode() } player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) var playermap = map[string]models.LobbyType{ "debug": models.LobbyTypeDebug, "6s": models.LobbyTypeSixes, "highlander": models.LobbyTypeHighlander, "ultiduo": models.LobbyTypeUltiduo, "bball": models.LobbyTypeBball, "4v4": models.LobbyTypeFours, } lobbyType := playermap[*args.Type] var count int db.DB.Table("server_records").Where("host = ?", *args.Server).Count(&count) if count != 0 { return helpers.NewTPError("A lobby is already using this server.", -1).Encode() } randBytes := make([]byte, 6) rand.Read(randBytes) serverPwd := base64.URLEncoding.EncodeToString(randBytes) //TODO what if playermap[lobbytype] is nil? info := models.ServerRecord{ Host: *args.Server, RconPassword: *args.RconPwd, ServerPassword: serverPwd} // err = models.VerifyInfo(info) // if err != nil { // bytes, _ := helpers.NewTPErrorFromError(err).Encode() // return string(bytes) // } lob := models.NewLobby(*args.Map, lobbyType, *args.League, info, int(*args.WhitelistID), *args.Mumble) lob.CreatedBySteamID = player.SteamId lob.RegionCode, lob.RegionName = chelpers.GetRegion(*args.Server) if (lob.RegionCode == "" || lob.RegionName == "") && config.Constants.GeoIP != "" { return helpers.NewTPError("Couldn't find region server.", 1).Encode() } lob.Save() err = lob.SetupServer() if err != nil { qerr := db.DB.Where("id = ?", lob.ID).Delete(&models.Lobby{}).Error if qerr != nil { helpers.Logger.Warning(qerr.Error()) } db.DB.Delete(&lob.ServerInfo) return helpers.NewTPErrorFromError(err).Encode() } lob.State = models.LobbyStateWaiting lob.Save() reply_str := struct { ID uint `json:"id"` }{lob.ID} models.FumbleLobbyCreated(lob) bytes, _ := chelpers.BuildSuccessJSON(reply_str).Encode() return bytes }
func (Lobby) LobbyCreate(so *wsevent.Client, args struct { Map *string `json:"map"` Type *string `json:"type" valid:"debug,6s,highlander,4v4,ultiduo,bball"` League *string `json:"league" valid:"ugc,etf2l,esea,asiafortress,ozfortress"` Server *string `json:"server"` RconPwd *string `json:"rconpwd"` WhitelistID *string `json:"whitelistID"` Mumble *bool `json:"mumbleRequired"` Password *string `json:"password" empty:"-"` SteamGroupWhitelist *string `json:"steamGroupWhitelist" empty:"-"` Requirements *struct { Classes map[string]Requirement `json:"classes,omitempty"` General Requirement `json:"general,omitempty"` } `json:"requirements" empty:"-"` }) interface{} { player := chelpers.GetPlayerFromSocket(so.ID) if banned, until := player.IsBannedWithTime(models.PlayerBanCreate); banned { str := fmt.Sprintf("You've been banned from creating lobbies till %s", until.Format(time.RFC822)) return helpers.NewTPError(str, -1) } var steamGroup string if *args.SteamGroupWhitelist != "" && !rSteamGroup.MatchString(*args.SteamGroupWhitelist) { return helpers.NewTPError("Invalid Steam group URL", -1) } else if rSteamGroup.MatchString(*args.SteamGroupWhitelist) { steamGroup = rSteamGroup.FindStringSubmatch(*args.SteamGroupWhitelist)[1] } var playermap = map[string]models.LobbyType{ "debug": models.LobbyTypeDebug, "6s": models.LobbyTypeSixes, "highlander": models.LobbyTypeHighlander, "ultiduo": models.LobbyTypeUltiduo, "bball": models.LobbyTypeBball, "4v4": models.LobbyTypeFours, } lobbyType := playermap[*args.Type] var count int db.DB.Table("server_records").Where("host = ?", *args.Server).Count(&count) if count != 0 { return helpers.NewTPError("A lobby is already using this server.", -1) } randBytes := make([]byte, 6) rand.Read(randBytes) serverPwd := base64.URLEncoding.EncodeToString(randBytes) //TODO what if playermap[lobbytype] is nil? info := models.ServerRecord{ Host: *args.Server, RconPassword: *args.RconPwd, ServerPassword: serverPwd} // err = models.VerifyInfo(info) // if err != nil { // bytes, _ := helpers.NewTPErrorFromError(err).Encode() // return string(bytes) // } lob := models.NewLobby(*args.Map, lobbyType, *args.League, info, *args.WhitelistID, *args.Mumble, steamGroup, *args.Password) lob.CreatedBySteamID = player.SteamID lob.RegionCode, lob.RegionName = chelpers.GetRegion(*args.Server) if (lob.RegionCode == "" || lob.RegionName == "") && config.Constants.GeoIP != "" { return helpers.NewTPError("Couldn't find region server.", 1) } lob.Save() err := lob.SetupServer() if err != nil { //lobby setup failed, delete lobby and corresponding server record qerr := db.DB.Where("id = ?", lob.ID).Delete(&models.Lobby{}).Error if qerr != nil { logrus.Warning(qerr.Error()) } db.DB.Delete(&lob.ServerInfo) return helpers.NewTPErrorFromError(err) } lob.State = models.LobbyStateWaiting lob.Save() models.FumbleLobbyCreated(lob) if args.Requirements != nil { for class, requirement := range (*args.Requirements).Classes { if requirement.Restricted.Blu { err := newRequirement("blu", class, requirement, lob) if err != nil { return err } } if requirement.Restricted.Red { err := newRequirement("red", class, requirement, lob) if err != nil { return err } } } if args.Requirements.General.Hours != 0 || args.Requirements.General.Lobbies != 0 { general := &models.Requirement{ LobbyID: lob.ID, Hours: args.Requirements.General.Hours, Lobbies: args.Requirements.General.Lobbies, Slot: -1, } general.Save() } } return chelpers.NewResponse( struct { ID uint `json:"id"` }{lob.ID}) }
func SocketInit(so socketio.Socket) { chelpers.AuthenticateSocket(so.Id(), so.Request()) if chelpers.IsLoggedInSocket(so.Id()) { steamid := chelpers.GetSteamId(so.Id()) SteamIdSocketMap[steamid] = &so } so.On("disconnection", func() { chelpers.DeauthenticateSocket(so.Id()) if chelpers.IsLoggedInSocket(so.Id()) { steamid := chelpers.GetSteamId(so.Id()) delete(SteamIdSocketMap, steamid) } helpers.Logger.Debug("on disconnect") }) so.On("authenticationTest", chelpers.AuthFilter(so.Id(), func(val string) string { return "authenticated" })) helpers.Logger.Debug("on connection") so.Join("-1") //room for global chat if chelpers.IsLoggedInSocket(so.Id()) { player, err := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if err != nil { helpers.Logger.Warning("User has a cookie with but a matching player record doesn't exist: %s", chelpers.GetSteamId(so.Id())) return } lobbyid, err := player.GetLobbyId() if err != nil { so.Join(strconv.FormatUint(uint64(lobbyid), 10)) } } var lobbyCreateParams = map[string]chelpers.Param{ "mapName": chelpers.Param{Type: chelpers.PTypeString}, "type": chelpers.Param{Type: chelpers.PTypeString}, "server": chelpers.Param{Type: chelpers.PTypeString}, "rconpwd": chelpers.Param{Type: chelpers.PTypeString}, "whitelist": chelpers.Param{Type: chelpers.PTypeInt}, "mumbleRequired": chelpers.Param{Type: chelpers.PTypeBool}, } so.On("lobbyCreate", chelpers.AuthFilter(so.Id(), chelpers.JsonVerifiedFilter(lobbyCreateParams, func(js *simplejson.Json) string { player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) mapName, _ := js.Get("mapName").String() lobbytypestring, _ := js.Get("type").String() server, _ := js.Get("server").String() rconPwd, _ := js.Get("rconpwd").String() whitelist, err := js.Get("whitelist").Int() var playermap = map[string]models.LobbyType{ "sixes": models.LobbyTypeSixes, "highlander": models.LobbyTypeHighlander, } lobbytype, ok := playermap[lobbytypestring] if !ok { bytes, _ := chelpers.BuildFailureJSON("Lobby type invalid.", -1).Encode() return string(bytes) } //mumble, _ := js.Get("mumbleRequired").Bool() //TODO: Configure server here //TODO what if playermap[lobbytype] is nil? lob := models.NewLobby(mapName, lobbytype, models.ServerRecord{Host: server, RconPassword: rconPwd}, whitelist) lob.CreatedBy = *player err = lob.Save() if err != nil { bytes, _ := err.(*helpers.TPError).ErrorJSON().Encode() return string(bytes) } // setup server info go func() { err := lob.TrySettingUp() if err != nil { SendMessage(chelpers.GetSteamId(so.Id()), "sendNotification", err.Error()) } else { // for debug SendMessage(chelpers.GetSteamId(so.Id()), "sendNotification", fmt.Sprintf("Lobby %d created successfully", lob.ID)) } }() lobby_id := simplejson.New() lobby_id.Set("id", lob.ID) bytes, _ := chelpers.BuildSuccessJSON(lobby_id).Encode() return string(bytes) }))) var lobbyCloseParams = map[string]chelpers.Param{ "id": chelpers.Param{Type: chelpers.PTypeInt}, } so.On("lobbyClose", chelpers.AuthFilter(so.Id(), chelpers.JsonVerifiedFilter(lobbyCloseParams, func(js *simplejson.Json) string { player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) lobbyid, _ := js.Get("id").Uint64() lob, tperr := models.GetLobbyById(uint(lobbyid)) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } if player.ID != lob.CreatedByID { 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) } lob.Close() bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) }))) var lobbyJoinParams = map[string]chelpers.Param{ "id": chelpers.Param{Type: chelpers.PTypeInt}, "class": chelpers.Param{Type: chelpers.PTypeString}, "team": chelpers.Param{Type: chelpers.PTypeString}, } so.On("lobbyJoin", chelpers.AuthFilter(so.Id(), chelpers.JsonVerifiedFilter(lobbyJoinParams, func(js *simplejson.Json) string { player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } lobbyid, _ := js.Get("id").Uint64() classString, _ := js.Get("class").String() teamString, _ := js.Get("team").String() lob, tperr := models.GetLobbyById(uint(lobbyid)) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } slot, tperr := chelpers.GetPlayerSlot(lob.Type, teamString, classString) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } tperr = lob.AddPlayer(player, slot) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } so.Join(strconv.FormatUint(lobbyid, 10)) bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) }))) var lobbyRemovePlayerParams = map[string]chelpers.Param{ "id": chelpers.Param{Type: chelpers.PTypeInt}, "steamid": chelpers.Param{Type: chelpers.PTypeString, Default: ""}, "ban": chelpers.Param{Type: chelpers.PTypeBool, Default: false}, } so.On("lobbyRemovePlayer", chelpers.AuthFilter(so.Id(), chelpers.JsonVerifiedFilter(lobbyRemovePlayerParams, func(js *simplejson.Json) string { steamid, _ := js.Get("steamid").String() ban, _ := js.Get("ban").Bool() lobbyid, _ := js.Get("id").Int() self := false // TODO check authorization, currently can kick anyone if steamid == "" || steamid == chelpers.GetSteamId(so.Id()) { self = true steamid = chelpers.GetSteamId(so.Id()) } 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 && lob.CreatedByID != player.ID { // TODO proper authorization checks bytes, _ := chelpers.BuildFailureJSON("Not authorized to remove players", 1).Encode() return string(bytes) } _, err := lob.GetPlayerSlot(player) if err == nil { lob.RemovePlayer(player) } else if player.IsSpectatingId(lob.ID) { lob.RemoveSpectator(player) } else { bytes, _ := chelpers.BuildFailureJSON("Player neither playing nor spectating", 2).Encode() return string(bytes) } if ban { lob.BanPlayer(player) } so.Leave(strconv.FormatInt(int64(lobbyid), 10)) bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) }))) so.On("playerReady", chelpers.AuthFilter(so.Id(), func(val string) 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) } tperr = lobby.ReadyPlayer(player) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } if lobby.IsEveryoneReady() { bytes, _ := decorators.GetLobbyConnectJSON(lobby).Encode() SendMessageToRoom(strconv.FormatUint(uint64(lobby.ID), 10), "lobbyStart", string(bytes)) } bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) })) so.On("playerUnready", chelpers.AuthFilter(so.Id(), func(val string) 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) } tperr = lobby.UnreadyPlayer(player) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(bytes) })) var lobbyJoinSpectatorParams = map[string]chelpers.Param{ "id": chelpers.Param{Type: chelpers.PTypeInt}, } so.On("lobbySpectatorJoin", chelpers.AuthFilter(so.Id(), chelpers.JsonVerifiedFilter(lobbyJoinSpectatorParams, func(js *simplejson.Json) string { lobbyid, _ := js.Get("id").Uint64() player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } lob, tperr := models.GetLobbyById(uint(lobbyid)) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() tperr = lob.AddSpectator(player) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } lob.Save() return string(bytes) }))) var playerSettingsGetParams = map[string]chelpers.Param{ "key": chelpers.Param{Type: chelpers.PTypeString, Default: ""}, } so.On("playerSettingsGet", chelpers.AuthFilter(so.Id(), chelpers.JsonVerifiedFilter(playerSettingsGetParams, func(js *simplejson.Json) string { player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) key, _ := js.Get("key").String() var err error var settings []models.PlayerSetting var setting models.PlayerSetting if key == "" { settings, err = player.GetSettings() } else { setting, err = player.GetSetting(key) settings = append(settings, setting) } if err != nil { bytes, _ := chelpers.BuildFailureJSON(err.Error(), 0).Encode() return string(bytes) } result := decorators.GetPlayerSettingsJson(settings) resp, _ := chelpers.BuildSuccessJSON(result).Encode() return string(resp) }))) var playerSettingsSetParams = map[string]chelpers.Param{ "key": chelpers.Param{Type: chelpers.PTypeString}, "value": chelpers.Param{Type: chelpers.PTypeString}, } so.On("playerSettingsSet", chelpers.AuthFilter(so.Id(), chelpers.JsonVerifiedFilter(playerSettingsSetParams, func(js *simplejson.Json) string { player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) key, _ := js.Get("key").String() value, _ := js.Get("value").String() err := player.SetSetting(key, value) if err != nil { bytes, _ := chelpers.BuildFailureJSON(err.Error(), 0).Encode() return string(bytes) } resp, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(resp) }))) var playerProfileParams = map[string]chelpers.Param{ "steamid": chelpers.Param{Type: chelpers.PTypeString, Default: ""}, } so.On("playerProfile", chelpers.AuthFilter(so.Id(), chelpers.JsonVerifiedFilter(playerProfileParams, func(js *simplejson.Json) string { steamid, _ := js.Get("steamid").String() if steamid == "" { steamid = chelpers.GetSteamId(so.Id()) } player, playErr := models.GetPlayerWithStats(steamid) if playErr != nil { bytes, _ := chelpers.BuildFailureJSON(playErr.Error(), 0).Encode() return string(bytes) } result := decorators.GetPlayerProfileJson(player) resp, _ := chelpers.BuildSuccessJSON(result).Encode() return string(resp) }))) var chatSendParams = map[string]chelpers.Param{ "message": chelpers.Param{Type: chelpers.PTypeString}, "room": chelpers.Param{Type: chelpers.PTypeInt}, } so.On("chatSend", chelpers.AuthFilter(so.Id(), chelpers.JsonVerifiedFilter(chatSendParams, func(js *simplejson.Json) string { message, _ := js.Get("message").String() room, _ := js.Get("room").Int() player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id())) if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } //Check if player has either joined, or is spectating lobby lobbyId, tperr := player.GetLobbyId() if room > 0 { // if room is a lobby room if tperr != nil { bytes, _ := tperr.ErrorJSON().Encode() return string(bytes) } else if lobbyId != uint(room) && !player.IsSpectatingId(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 = -1 } t := time.Now() chatMessage := simplejson.New() // TODO send proper timestamps chatMessage.Set("timestamp", strconv.Itoa(t.Hour())+strconv.Itoa(t.Minute())) chatMessage.Set("message", html.EscapeString(message)) chatMessage.Set("room", room) user := simplejson.New() user.Set("id", player.SteamId) user.Set("name", player.Name) chatMessage.Set("user", user) bytes, _ := chatMessage.Encode() so.BroadcastTo(strconv.Itoa(room), "chatReceive", string(bytes)) resp, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode() return string(resp) }))) }