func TestPlayerInfoFetching(t *testing.T) { testhelpers.CleanupDB() if config.Constants.SteamDevApiKey == "your steam dev api key" { return } // disable mock mode because we're actually testing it config.Constants.SteamApiMockUp = false player, playErr := models.NewPlayer("76561197999073985") assert.Nil(t, playErr) assert.Equal(t, "http://steamcommunity.com/id/nonagono/", player.Profileurl) assert.Regexp(t, "(.*)steamcommunity/public/images/avatars/(.*).jpg", player.Avatar) assert.True(t, player.GameHours >= 3000) player.Stats.PlayedCountSet(models.LobbyTypeSixes, 3) player.Stats.PlayedCountSet(models.LobbyTypeHighlander, 7) player.Stats.PlayedCountIncrease(models.LobbyTypeSixes) // sixes: 3 -> 4 assert.Equal(t, 4, player.Stats.PlayedCountGet(models.LobbyTypeSixes)) assert.Equal(t, 7, player.Stats.PlayedCountGet(models.LobbyTypeHighlander)) database.DB.Save(player) player2, err := models.GetPlayerWithStats(player.SteamId) assert.Nil(t, err) assert.Equal(t, 4, player2.Stats.PlayedCountGet(models.LobbyTypeSixes)) assert.Equal(t, 7, player2.Stats.PlayedCountGet(models.LobbyTypeHighlander)) assert.Equal(t, "http://steamcommunity.com/id/nonagono/", player2.Profileurl) }
func (Player) PlayerProfile(server *wsevent.Server, so *wsevent.Client, data []byte) []byte { reqerr := chelpers.FilterRequest(so, 0, true) if reqerr != nil { return reqerr.Encode() } var args struct { Steamid string `json:"steamid"` } err := chelpers.GetParams(data, &args) if err != nil { return helpers.NewTPErrorFromError(err).Encode() } steamid := args.Steamid if steamid == "" { steamid = chelpers.GetSteamId(so.Id()) } player, playErr := models.GetPlayerWithStats(steamid) if playErr != nil { return playErr.Encode() } result := models.DecoratePlayerProfileJson(player) resp, _ := chelpers.BuildSuccessJSON(result).Encode() return resp }
func (Player) PlayerProfile(so *wsevent.Client, args struct { Steamid *string `json:"steamid"` }) interface{} { steamid := *args.Steamid if steamid == "" { steamid = chelpers.GetSteamId(so.ID) } player, playErr := models.GetPlayerWithStats(steamid) if playErr != nil { return playErr } result := models.DecoratePlayerProfileJson(player) return chelpers.NewResponse(result) }
func PlayerProfile(so socketio.Socket) func(string) string { return chelpers.FilterRequest(so, playerProfileFilter, func(params map[string]interface{}) string { steamid := params["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 := models.DecoratePlayerProfileJson(player) resp, _ := chelpers.BuildSuccessJSON(result).Encode() return string(resp) }) }
func AfterConnectLoggedIn(so *wsevent.Client, player *models.Player) { if time.Since(player.UpdatedAt) >= time.Hour*1 { player.UpdatePlayerInfo() player.Save() } lobbyID, err := player.GetLobbyID(false) if err == nil { lobby, _ := models.GetLobbyByIDServer(lobbyID) AfterLobbyJoin(so, lobby, player) AfterLobbySpec(socket.AuthServer, so, lobby) models.BroadcastLobbyToUser(lobby, chelpers.GetSteamId(so.ID)) slot := &models.LobbySlot{} err := db.DB.Where("lobby_id = ? AND player_id = ?", lobby.ID, player.ID).First(slot).Error if err == nil { if lobby.State == models.LobbyStateInProgress { broadcaster.SendMessage(player.SteamID, "lobbyStart", models.DecorateLobbyConnect(lobby, player.Name, slot.Slot)) } else if lobby.State == models.LobbyStateReadyingUp && !slot.Ready { data := struct { Timeout int64 `json:"timeout"` }{lobby.ReadyUpTimeLeft()} broadcaster.SendMessage(player.SteamID, "lobbyReadyUp", data) } } } settings, err2 := player.GetSettings() if err2 == nil { broadcaster.SendMessage(player.SteamID, "playerSettings", models.DecoratePlayerSettingsJson(settings)) } profilePlayer, err3 := models.GetPlayerWithStats(player.SteamID) if err3 == nil { broadcaster.SendMessage(player.SteamID, "playerProfile", models.DecoratePlayerProfileJson(profilePlayer)) } }
func AfterConnectLoggedIn(server *wsevent.Server, so *wsevent.Client, player *models.Player) { lobbyIdPlaying, err := player.GetLobbyId() if err == nil { lobby, _ := models.GetLobbyByIdServer(lobbyIdPlaying) AfterLobbyJoin(server, so, lobby, player) AfterLobbySpec(server, so, lobby) models.BroadcastLobbyToUser(lobby, GetSteamId(so.Id())) slot := &models.LobbySlot{} err := db.DB.Where("lobby_id = ? AND player_id = ?", lobby.ID, player.ID).First(slot).Error if err == nil { if lobby.State == models.LobbyStateInProgress && !models.IsPlayerInServer(player.SteamId) { bytes, _ := json.Marshal(models.DecorateLobbyConnect(lobby, player.Name, slot.Class)) broadcaster.SendMessage(player.SteamId, "lobbyStart", string(bytes)) } else if lobby.State == models.LobbyStateReadyingUp && !slot.Ready { data := struct { Timeout int64 `json:"timeout"` }{lobby.ReadyUpTimeLeft()} bytes, _ := json.Marshal(data) broadcaster.SendMessage(player.SteamId, "lobbyReadyUp", string(bytes)) } } } settings, err2 := player.GetSettings() if err2 == nil { bytes, _ := json.Marshal(models.DecoratePlayerSettingsJson(settings)) broadcaster.SendMessage(player.SteamId, "playerSettings", string(bytes)) } profilePlayer, err3 := models.GetPlayerWithStats(player.SteamId) if err3 == nil { bytes, _ := json.Marshal(models.DecoratePlayerProfileJson(profilePlayer)) broadcaster.SendMessage(player.SteamId, "playerProfile", string(bytes)) } }
func AfterConnectLoggedIn(so socketio.Socket, player *models.Player) { lobbyIdPlaying, err := player.GetLobbyId() if err == nil { lobby, _ := models.GetLobbyById(lobbyIdPlaying) AfterLobbyJoin(so, lobby, player) AfterLobbySpec(so, lobby) models.BroadcastLobbyToUser(lobby, GetSteamId(so.Id())) slot := &models.LobbySlot{} err := db.DB.Where("lobby_id = ? AND player_id = ?", lobby.ID, player.ID).First(slot).Error if err == nil { if lobby.State == models.LobbyStateInProgress && !slot.InGame { bytes, _ := models.DecorateLobbyConnectJSON(lobby).Encode() broadcaster.SendMessage(player.SteamId, "lobbyStart", string(bytes)) } else if lobby.State == models.LobbyStateReadyingUp && !slot.Ready { left := simplejson.New() left.Set("timeout", lobby.ReadyUpTimeLeft()) bytes, _ := left.Encode() broadcaster.SendMessage(player.SteamId, "lobbyReadyUp", string(bytes)) } } } settings, err2 := player.GetSettings() if err2 == nil { json := models.DecoratePlayerSettingsJson(settings) bytes, _ := json.Encode() broadcaster.SendMessage(player.SteamId, "playerSettings", string(bytes)) } profilePlayer, err3 := models.GetPlayerWithStats(player.SteamId) if err3 == nil { json := models.DecoratePlayerProfileJson(profilePlayer) bytes, _ := json.Encode() broadcaster.SendMessage(player.SteamId, "playerProfile", string(bytes)) } }
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) }))) }