Example #1
0
        internal static async Task <SessionInfo> CreateOrJoinGameSession(GameSessionRequest sessionParams)
        {
            // find or create a game session
            using (MySqlConnection conn = new MySqlConnection(_connStr))
            {
                await conn.OpenAsync();

                using (MySqlCommand cmd = new MySqlCommand("createOrJoinGameSession", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new MySqlParameter("@param_player_id", sessionParams.PlayerId));

                    using (var cursor = await cmd.ExecuteReaderAsync())
                    {
                        cursor.Read();

                        var id   = Convert.ToInt64(cursor["id"]);
                        var guid = Guid.Parse(cursor["guid"].ToString());

                        return(new SessionInfo
                        {
                            Id = id,
                            Guid = guid
                        });
                    }
                }
            }
        }
Example #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            [OrchestrationClient] DurableOrchestrationClient starter,
            ILogger log)
        {
            // get request params
            var postData = await req.ReadAsStringAsync();

            GameSessionRequest sessionParams = null;

            try
            {
                sessionParams = JsonConvert.DeserializeObject <GameSessionRequest>(postData);
            }
            catch (JsonException ex)
            {
                return(new BadRequestResult());
            }

            // get playerId
            if (ValidateRequest(sessionParams))
            {
                log.LogInformation($"The player identifier provided is: {sessionParams.PlayerId}");
            }
            else
            {
                return(new BadRequestResult());
            }

            // query db to join or create session
            SessionInfo sessionInfo = await DataClient.CreateOrJoinGameSession(sessionParams);

            var sessionOrchestrator = await starter.GetStatusAsync(sessionInfo.Guid.ToString());

            if (sessionOrchestrator == null)
            {
                // create session
                var    initialGameState = new GameSession(sessionInfo.Id, sessionInfo.Guid, sessionParams.PlayerId);
                string instanceId       = await starter.StartNewAsync("GameSession", sessionInfo.Guid.ToString(), initialGameState);

                log.LogInformation($"Started game session for player {sessionParams.PlayerId} with ID = '{instanceId}'.");
            }
            else
            {
                // send 'player join' event to session orchestrator
                await starter.RaiseEventAsync(sessionInfo.Guid.ToString(), Constants.PlayerJoinEventTag, sessionParams.PlayerId);
            }

            return(new OkObjectResult(starter.CreateHttpManagementPayload(sessionInfo.Guid.ToString())));
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            [OrchestrationClient] DurableOrchestrationClient starter,
            ILogger log)
        {
            // get request params
            var postData = await req.ReadAsStringAsync();

            GameSessionRequest sessionParams = null;

            try
            {
                sessionParams = JsonConvert.DeserializeObject <GameSessionRequest>(postData);
            }
            catch (JsonException ex)
            {
                return(new BadRequestResult());
            }

            // get playerId
            int?playerId = null;

            if (sessionParams != null)
            {
                playerId = sessionParams.PlayerId;
                log.LogInformation($"The player identifier provided is: {playerId}");
            }
            else
            {
                return(new BadRequestResult());
            }

            // query db for session matching params
            string matchResult = null;

            if (playerId.HasValue)
            {
                matchResult = await DataClient.GetJoinableGameSessionGuid(playerId.Value.ToString());
            }

            if (matchResult == null)
            {
                // create session
                var    initialGameState = new GameSession(playerId.Value);
                string instanceId       = await starter.StartNewAsync("GameSession", initialGameState);

                // write game to db so other players can discover it
                await DataClient.AddGameSession(initialGameState, instanceId);

                log.LogInformation($"Started game session for player {playerId} with ID = '{instanceId}'.");

                matchResult = instanceId;
            }
            else
            {
                // send 'player join' event to session orchestrator
                await starter.RaiseEventAsync(matchResult, Constants.PlayerJoinEventTag, playerId);
            }

            return(new OkObjectResult(matchResult));
        }
Example #4
0
 public static bool ValidateRequest(GameSessionRequest req)
 {
     return(req != null && req.PlayerId != 0);
 }