Exemplo n.º 1
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
}
Exemplo n.º 2
0
func (Admin) AdminChangeRole(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"`
		Role    *string `json:"role"`
	}

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

	role, ok := helpers.RoleMap[*args.Role]
	if !ok || role == helpers.RoleAdmin {
		return helpers.NewTPError("Invalid role parameter", 0).Encode()
	}

	otherPlayer, err := models.GetPlayerBySteamId(*args.Steamid)
	if err != nil {
		return helpers.NewTPError("Player not found.", 0).Encode()
	}

	currPlayer, _ := chelpers.GetPlayerSocket(so.Id())

	models.LogAdminAction(currPlayer.ID, helpers.ActionChangeRole, otherPlayer.ID)

	// actual change happens
	otherPlayer.Role = role
	db.DB.Save(&otherPlayer)

	// rewrite session data. THIS WON'T WRITE A COOKIE SO IT ONLY WORKS WITH
	// STORES THAT STORE DATA IN COOKIES (AND NOT ONLY SESSION ID).
	session, sesserr := chelpers.GetSessionHTTP(so.Request())
	if sesserr == nil {
		session.Values["role"] = role
		session.Save(so.Request(), FakeResponseWriter{})
	}

	return chelpers.EmptySuccessJS
}