Esempio n. 1
0
//SocketInit initializes the websocket connection for the provided socket
func SocketInit(so *wsevent.Client) error {
	chelpers.AuthenticateSocket(so.ID, so.Request)
	loggedIn := chelpers.IsLoggedInSocket(so.ID)
	if loggedIn {
		steamid := chelpers.GetSteamId(so.ID)
		sessions.AddSocket(steamid, so)
	}

	if loggedIn {
		hooks.AfterConnect(socket.AuthServer, so)

		player, err := models.GetPlayerBySteamID(chelpers.GetSteamId(so.ID))
		if err != nil {
			logrus.Warning(
				"User has a cookie with but a matching player record doesn't exist: %s",
				chelpers.GetSteamId(so.ID))
			chelpers.DeauthenticateSocket(so.ID)
			hooks.AfterConnect(socket.UnauthServer, so)
			return ErrRecordNotFound
		}

		hooks.AfterConnectLoggedIn(so, player)
	} else {
		hooks.AfterConnect(socket.UnauthServer, so)
		so.EmitJSON(helpers.NewRequest("playerSettings", "{}"))
		so.EmitJSON(helpers.NewRequest("playerProfile", "{}"))
	}

	so.EmitJSON(helpers.NewRequest("socketInitialized", "{}"))

	return nil
}
Esempio n. 2
0
func onDisconnect(id string) {
	//defer helpers.Logger.Debug("Disconnected from Socket")
	defer chelpers.DeauthenticateSocket(id)
	if chelpers.IsLoggedInSocket(id) {
		steamid := chelpers.GetSteamId(id)
		broadcaster.RemoveSocket(steamid)
		player, tperr := models.GetPlayerBySteamId(steamid)
		if tperr != nil || player == nil {
			helpers.Logger.Error(tperr.Error())
			return
		}

		ids, tperr := player.GetSpectatingIds()
		if tperr != nil {
			helpers.Logger.Error(tperr.Error())
			return
		}

		for _, id := range ids {
			lobby, _ := models.GetLobbyById(id)
			err := lobby.RemoveSpectator(player, true)
			if err != nil {
				helpers.Logger.Error(err.Error())
				continue
			}
			//helpers.Logger.Debug("removing %s from %d", player.SteamId, id)
		}

	}
}
Esempio n. 3
0
func SocketInit(server *wsevent.Server, noauth *wsevent.Server, so *wsevent.Client) error {
	chelpers.AuthenticateSocket(so.Id(), so.Request())
	loggedIn := chelpers.IsLoggedInSocket(so.Id())
	if loggedIn {
		steamid := chelpers.GetSteamId(so.Id())
		broadcaster.SetSocket(steamid, so)
	}

	if loggedIn {
		chelpers.AfterConnect(server, so)

		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()))
			chelpers.DeauthenticateSocket(so.Id())
			so.Close()
			return ErrRecordNotFound
		}

		chelpers.AfterConnectLoggedIn(server, so, player)
	} else {
		chelpers.AfterConnect(noauth, so)
		so.EmitJSON(helpers.NewRequest("playerSettings", "{}"))
		so.EmitJSON(helpers.NewRequest("playerProfile", "{}"))
	}

	so.EmitJSON(helpers.NewRequest("socketInitialized", "{}"))

	return nil
}
Esempio n. 4
0
//OnDisconnect is connected when a player with a given socketID disconnects
func OnDisconnect(socketID string) {
	pprof.Clients.Add(-1)
	defer chelpers.DeauthenticateSocket(socketID)
	if chelpers.IsLoggedInSocket(socketID) {
		steamid := chelpers.GetSteamId(socketID)
		sessions.RemoveSocket(socketID, steamid)
		player, tperr := models.GetPlayerBySteamID(steamid)
		if tperr != nil || player == nil {
			logrus.Error(tperr.Error())
			return
		}

		ids, tperr := player.GetSpectatingIds()
		if tperr != nil {
			logrus.Error(tperr.Error())
			return
		}

		for _, id := range ids {
			//if this _specific_ socket is spectating this lobby, remove them from it
			//player might be spectating other lobbies in another tab, but we don't care
			if sessions.IsSpectating(socketID, id) {
				lobby, _ := models.GetLobbyByID(id)
				err := lobby.RemoveSpectator(player, true)
				if err != nil {
					logrus.Error(err.Error())
					continue
				}
				sessions.RemoveSpectator(socketID)
				//logrus.Debug("removing %s from %d", player.SteamId, id)
			}
		}

		id, _ := player.GetLobbyID(true)
		//if player is in a waiting lobby, and hasn't connected for > 30 seconds,
		//remove him from it. Here, connected = player isn't connected from any tab/window
		if id != 0 && sessions.ConnectedSockets(player.SteamID) == 0 {
			time.AfterFunc(time.Second*30, func() {
				if !sessions.IsConnected(player.SteamID) {
					//player may have changed lobbies during this time
					//fetch lobby ID again
					id, err := player.GetLobbyID(true)
					if err != nil {
						return
					}

					lobby := &models.Lobby{}
					db.DB.First(lobby, id)
					lobby.RemovePlayer(player)
				}
			})
		}
	}

}
Esempio n. 5
0
func SocketInit(so socketio.Socket) {
	chelpers.AuthenticateSocket(so.Id(), so.Request())
	if chelpers.IsLoggedInSocket(so.Id()) {
		steamid := chelpers.GetSteamId(so.Id())
		broadcaster.SetSocket(steamid, so)
	}

	so.On("disconnection", func() {
		chelpers.DeauthenticateSocket(so.Id())
		if chelpers.IsLoggedInSocket(so.Id()) {
			steamid := chelpers.GetSteamId(so.Id())
			broadcaster.RemoveSocket(steamid)
		}
		helpers.Logger.Debug("on disconnect")
	})

	so.On("authenticationTest", chelpers.FilterRequest(so, chelpers.FilterParams{FilterLogin: true},
		func(_ map[string]interface{}) string {
			return "authenticated"
		}))

	helpers.Logger.Debug("on connection")
	chelpers.AfterConnect(so)

	loggedIn := chelpers.IsLoggedInSocket(so.Id())
	if loggedIn {
		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
		}

		chelpers.AfterConnectLoggedIn(so, player)
	} else {
		so.Emit("playerSettings", "{}")
		so.Emit("playerProfile", "{}")
	}

	// LOBBY CREATE
	so.On("lobbyCreate", handler.LobbyCreate(so))

	so.On("serverVerify", handler.ServerVerify(so))

	so.On("lobbyClose", handler.LobbyClose(so))

	so.On("lobbyJoin", handler.LobbyJoin(so))

	if loggedIn {
		so.On("lobbySpectatorJoin", handler.LobbySpectatorJoin(so))
	} else {
		so.On("lobbySpectatorJoin", handler.LobbyNoLoginSpectatorJoin(so))
	}
	so.On("lobbyKick", handler.LobbyKick(so))

	so.On("playerReady", handler.PlayerReady(so))

	so.On("playerNotReady", handler.PlayerNotReady(so))

	so.On("playerSettingsGet", handler.PlayerSettingsGet(so))

	so.On("playerSettingsSet", handler.PlayerSettingsSet(so))

	so.On("playerProfile", handler.PlayerProfile(so))

	so.On("chatSend", handler.ChatSend(so))

	so.On("adminChangeRole", handler.AdminChangeRole(so))

	so.On("requestLobbyListData", handler.RequestLobbyListData(so))

	//Debugging handlers
	if config.Constants.ServerMockUp {
		so.On("debugLobbyFill", handler.DebugLobbyFill(so))
		so.On("debugLobbyReady", handler.DebugLobbyReady(so))
		so.On("debugGetAllLobbies", handler.DebugRequestAllLobbies(so))
		so.On("debugRequestLobbyStart", handler.DebugRequestLobbyStart(so))
	}

	so.Emit("socketInitialized", "")
}
Esempio n. 6
0
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)
		})))
}