Пример #1
0
func (Lobby) LobbyLeave(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"`
	}
	if err := chelpers.GetParams(data, &args); err != nil {
		return helpers.NewTPErrorFromError(err).Encode()
	}

	steamId := chelpers.GetSteamId(so.Id())

	lob, player, tperr := removePlayerFromLobby(*args.Id, steamId)
	if tperr != nil {
		return tperr.Encode()
	}

	chelpers.AfterLobbyLeave(server, so, lob, player)

	return chelpers.EmptySuccessJS
}
Пример #2
0
func (Lobby) LobbyBan(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"`
		Steamid *string `json:"steamid"`
	}

	if err := chelpers.GetParams(data, &args); err != nil {
		return helpers.NewTPErrorFromError(err).Encode()
	}

	steamId := *args.Steamid
	selfSteamId := chelpers.GetSteamId(so.Id())

	if steamId == selfSteamId {
		return helpers.NewTPError("Player can't kick himself.", -1).Encode()
	}
	if ok, tperr := playerCanKick(*args.Id, selfSteamId); !ok {
		return tperr.Encode()
	}

	lob, player, tperr := removePlayerFromLobby(*args.Id, steamId)
	if tperr != nil {
		return tperr.Encode()
	}

	lob.BanPlayer(player)

	so, _ = broadcaster.GetSocket(player.SteamId)
	chelpers.AfterLobbyLeave(server, so, lob, player)

	broadcaster.SendMessage(steamId, "sendNotification",
		fmt.Sprintf(`{"notification": "You have been removed from Lobby #%d"}`, *args.Id))

	return chelpers.EmptySuccessJS
}
Пример #3
0
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)
		})
}