createPlayerSession method

Future<CreatePlayerSessionOutput> createPlayerSession({
  1. required String gameSessionId,
  2. required String playerId,
  3. String? playerData,
})

Reserves an open player slot in an active game session. Before a player can be added, a game session must have an ACTIVE status, have a creation policy of ALLOW_ALL, and have an open player slot. To add a group of players to a game session, use CreatePlayerSessions. When the player connects to the game server and references a player session ID, the game server contacts the Amazon GameLift service to validate the player reservation and accept the player.

To create a player session, specify a game session ID, player ID, and optionally a string of player data. If successful, a slot is reserved in the game session for the player and a new PlayerSession object is returned. Player sessions cannot be updated.

Available in Amazon GameLift Local.

May throw InternalServiceException. May throw UnauthorizedException. May throw InvalidGameSessionStatusException. May throw GameSessionFullException. May throw TerminalRoutingStrategyException. May throw InvalidRequestException. May throw NotFoundException.

Parameter gameSessionId : A unique identifier for the game session to add a player to.

Parameter playerId : A unique identifier for a player. Player IDs are developer-defined.

Parameter playerData : Developer-defined information related to a player. Amazon GameLift does not use this data, so it can be formatted as needed for use in the game.

Implementation

Future<CreatePlayerSessionOutput> createPlayerSession({
  required String gameSessionId,
  required String playerId,
  String? playerData,
}) async {
  ArgumentError.checkNotNull(gameSessionId, 'gameSessionId');
  _s.validateStringLength(
    'gameSessionId',
    gameSessionId,
    1,
    256,
    isRequired: true,
  );
  ArgumentError.checkNotNull(playerId, 'playerId');
  _s.validateStringLength(
    'playerId',
    playerId,
    1,
    1024,
    isRequired: true,
  );
  _s.validateStringLength(
    'playerData',
    playerData,
    1,
    2048,
  );
  final headers = <String, String>{
    'Content-Type': 'application/x-amz-json-1.1',
    'X-Amz-Target': 'GameLift.CreatePlayerSession'
  };
  final jsonResponse = await _protocol.send(
    method: 'POST',
    requestUri: '/',
    exceptionFnMap: _exceptionFns,
    // TODO queryParams
    headers: headers,
    payload: {
      'GameSessionId': gameSessionId,
      'PlayerId': playerId,
      if (playerData != null) 'PlayerData': playerData,
    },
  );

  return CreatePlayerSessionOutput.fromJson(jsonResponse.body);
}