createPlayerSessions method

Future<CreatePlayerSessionsOutput> createPlayerSessions({
  1. required String gameSessionId,
  2. required List<String> playerIds,
  3. Map<String, String>? playerDataMap,
})

Reserves open slots in a game session for a group of players. Before players 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 single player to a game session, use CreatePlayerSession. When a 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 player sessions, specify a game session ID, a list of player IDs, and optionally a set of player data strings. If successful, a slot is reserved in the game session for each player and a set of new PlayerSession objects 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 players to.

Parameter playerIds : List of unique identifiers for the players to be added.

Parameter playerDataMap : Map of string pairs, each specifying a player ID and a set of developer-defined information related to the player. Amazon GameLift does not use this data, so it can be formatted as needed for use in the game. Player data strings for player IDs not included in the PlayerIds parameter are ignored.

Implementation

Future<CreatePlayerSessionsOutput> createPlayerSessions({
  required String gameSessionId,
  required List<String> playerIds,
  Map<String, String>? playerDataMap,
}) async {
  ArgumentError.checkNotNull(gameSessionId, 'gameSessionId');
  _s.validateStringLength(
    'gameSessionId',
    gameSessionId,
    1,
    256,
    isRequired: true,
  );
  ArgumentError.checkNotNull(playerIds, 'playerIds');
  final headers = <String, String>{
    'Content-Type': 'application/x-amz-json-1.1',
    'X-Amz-Target': 'GameLift.CreatePlayerSessions'
  };
  final jsonResponse = await _protocol.send(
    method: 'POST',
    requestUri: '/',
    exceptionFnMap: _exceptionFns,
    // TODO queryParams
    headers: headers,
    payload: {
      'GameSessionId': gameSessionId,
      'PlayerIds': playerIds,
      if (playerDataMap != null) 'PlayerDataMap': playerDataMap,
    },
  );

  return CreatePlayerSessionsOutput.fromJson(jsonResponse.body);
}