public void UpdateRedis(bool serverTerminated = false)
{
var gameServerStatusData = new GameServerStatusData();
gameServerStatusData.taskArn = this.taskDataArnWithContainer;
gameServerStatusData.currentPlayers = this.server.GetPlayerCount();
gameServerStatusData.maxPlayers = Server.maxPlayers;
gameServerStatusData.publicIP = this.publicIP;
gameServerStatusData.port = Server.port;
gameServerStatusData.serverTerminated = serverTerminated;
gameServerStatusData.gameSessionsHosted = Server.hostedGameSessions;
var lambdaConfig = new AmazonLambdaConfig()
{
RegionEndpoint = this.regionEndpoint
};
lambdaConfig.MaxErrorRetry = 0; //Don't do retries on failures
var lambdaClient = new Amazon.Lambda.AmazonLambdaClient(lambdaConfig);
// Option 1. If TCPListener is not ready yet, update as not ready
if (!this.server.IsReady())
{
Debug.Log("Updating as not ready yet to Redis");
gameServerStatusData.ready = false;
gameServerStatusData.serverInUse = false;
}
// Option 2. If not full yet but, update our status as ready
else if (this.server.IsReady() && this.server.GetPlayerCount() < Server.maxPlayers)
{
Debug.Log("Updating as ready to Redis");
gameServerStatusData.ready = true;
gameServerStatusData.serverInUse = false;
}
// Option 3. If full, make sure the available key is deleted in Redis and update the full key
else
{
Debug.Log("Updating as full to Redis");
gameServerStatusData.ready = true;
gameServerStatusData.serverInUse = true;
}
// Call Lambda function to update status
var request = new Amazon.Lambda.Model.InvokeRequest()
{
FunctionName = "FargateGameServersUpdateGameServerData",
Payload = JsonConvert.SerializeObject(gameServerStatusData),
InvocationType = InvocationType.Event
};
// NOTE: We could catch response to validate it was successful and do something useful with that information
lambdaClient.InvokeAsync(request);
}