Before you can get or create a game session or manage game state in your Server Game function, you need to:
Game session stores connection information, game, players, operators identifiers that are used in every request. Game session can be permament for games that need to store data during whole game lifecycle (ex. slot machines) or temporary where every game round new game session is created (ex. card games, multiplayer games). Every game session can stores custom specific for game data - game state - for saving, updateing any different types of data that game need during the play.
After successful authorization and check for no active game session, you can create a new game session with default game state:
// custom game state
public class MyGameState
{
public bool IsGameOver;
public int NumberField;
public string StringField;
public Dictionary<string, int> DictionaryField;
}
...
public async Task<MessageResult> OnAuthSuccess(SessionMessage message)
{
if (message.GameSession == null)
{
// create new default custom game state
var defaultGameState = new MyGameState();
// create new permanent
var gameSession = await _fireball.CreatePermanentGameSession(defaultGameState, message);
// update session message with new game session data
message.UpdateGameSession(gameSession);
}
return await _fireball.SendSessionToClient(message);
}
Note: Player can have multiply different active game sessions inside one game if needed.
Get game session is by providing game session id:
public async Task<MessageResult> OnAuthSuccess(SessionMessage message)
{
if (message.GameSession != null)
{
var gameSession = await _fireball.GetGameSession(message.GameSession);
...
}
return await _fireball.SendSessionToClient(message);
}
If game have multiply active game sessions, the newest active game session id will be automatically retrieved during authorization process in SessionMessage
.
If you need to get all active game sessions for Player:
public async Task<MessageResult> OnAuthSuccess(SessionMessage message)
{
if (message.GameSession != null)
{
var gameSessionsList = await _fireball.GetAllGameSessions(message);
foreach (var gameSession in gameSessionsList)
{
...
}
}
return await _fireball.SendSessionToClient(message);
}
// custom game state
public class MyGameState
{
public bool IsGameOver;
public int NumberField;
public string StringField;
public Dictionary<string, int> DictionaryField;
}
var gameState = await _fireball.GetGameState<MyGameState>(message.GameSession);
// create new game state
var gameState = new MyGameState();
// update game state fields
gameState.NumberField = 123;
gameState.DictionaryField.Add("test", 5);
// save updated game state into game session
await _fireball.SaveGameState<MyGameState>(message.GameSession, gameState);
$.<field_name>.<sub_field>.<sub_field>
): // update number field directly
await _fireball.UpdateGameState(message.GameSession, "$.NumberField", 456);
// update dictionary sungle key-value entry
await _fireball.UpdateGameState(message.GameSession, "$.DictionaryField.test", 6);
Closing active game session by game session id:
// get all game sessions
var gameSessionsList = await _fireball.GetAllGameSessions(message);
foreach (var gameSession in gameSessionsList)
{
// get game state from game session
var gameState = gameSession.ParseGameState<MyGameState>();
// close game session with game over game state
if (gameState.IsGameOver)
{
bool closed = await _fireball.CloseGameSession(gameSession.Id);
}
}
You can create a new timed game session that will be automatically closed in specified time:
// create new default custom game state
var defaultGameState = new MyGameState();
// create date when game session with be automatically closed
var timeEnd = new DateTime().AddHours(24);
// create new permanent
var gameSession = await _fireball.CreateTimedGameSession(defaultGameState, timeEnd, message);
More details coming soon!