コード例 #1
0
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = nameof(GetOngoingGame))]
                                              HttpRequestMessage req, TraceWriter log)
        {
            using (var analytic = new AnalyticService(new RequestTelemetry
            {
                Name = nameof(GetOngoingGame)
            }))
            {
                try
                {
                    var email = req.GetQueryNameValuePairs().FirstOrDefault(kvp => kvp.Key == "email").Value;

                    if (string.IsNullOrWhiteSpace(email))
                    {
                        return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "email address required"));
                    }

                    using (var client = new CosmosDataService())
                    {
                        var game = client.GetOngoingGame(email);

                        return(req.CreateResponse(HttpStatusCode.OK, game as Object));
                    }
                }
                catch (Exception e)
                {
                    // track exceptions that occur
                    analytic.TrackException(e);

                    return(req.CreateErrorResponse(HttpStatusCode.BadRequest, e));
                }
            }
        }
コード例 #2
0
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = nameof(GetGame))]
                                              HttpRequestMessage req, TraceWriter log)
        {
            using (var analytic = new AnalyticService(new RequestTelemetry
            {
                Name = nameof(GetGame)
            }))
            {
                try
                {
                    var gameId = req.GetQueryNameValuePairs().FirstOrDefault(kvp => kvp.Key == "id").Value;

                    using (var client = new CosmosDataService())
                    {
                        var game = client.GetItemAsync <Game>(gameId).Result;

                        return(req.CreateResponse(HttpStatusCode.OK, game));
                    }
                }
                catch (Exception e)
                {
                    analytic.TrackException(e);

                    return(req.CreateErrorResponse(HttpStatusCode.BadRequest, e));
                }
            }
        }
コード例 #3
0
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = nameof(GetGameByEntryCode))]
                                              HttpRequestMessage req, TraceWriter log)
        {
            using (var analytic = new AnalyticService(new RequestTelemetry
            {
                Name = nameof(GetGameByEntryCode)
            }))
            {
                try
                {
                    var kvps      = req.GetQueryNameValuePairs();
                    var email     = kvps.FirstOrDefault(kvp => kvp.Key == "email").Value;
                    var entryCode = kvps.FirstOrDefault(kvp => kvp.Key == "entryCode").Value;

                    using (var client = new CosmosDataService())
                    {
                        var existingGame = client.GetOngoingGame(email);

                        if (existingGame != null)
                        {
                            return(req.CreateErrorResponse(HttpStatusCode.Conflict, "User already has an ongoing game"));
                        }

                        var openGame = client.GetGameByEntryCode(entryCode);

                        return(req.CreateResponse(HttpStatusCode.OK, openGame));
                    }
                }
                catch (Exception e)
                {
                    // track exceptions that occur
                    analytic.TrackException(e);
                    return(req.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message, e));
                }
            }
        }
コード例 #4
0
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = nameof(SaveGame))]
                                              HttpRequestMessage req, TraceWriter log)
        {
            using (var analytic = new AnalyticService(new RequestTelemetry
            {
                Name = nameof(SaveGame)
            }))
            {
                var json    = req.Content.ReadAsStringAsync().Result;
                var jobject = JsonConvert.DeserializeObject <JObject>(json);

                var action    = jobject["action"].ToString();
                var arguments = jobject["arguments"].ToObject <Dictionary <string, string> >();
                var game      = jobject["game"].ToObject <Game>();
                arguments = arguments ?? new Dictionary <string, string>();

                //Need to validate this player is not already part of another ongoing game or the coordinator of this game
                if (game.EntryCode == null)
                {
                    //Let's hope this is generally random. Best to confirm the code is not already in used but I'm lazy
                    game.EntryCode = Math.Abs(game.Id.GetHashCode()).ToString().Substring(0, 6);
                }

                Game savedGame   = null;
                bool isEndOfgame = false;
                try
                {
                    using (var client = new CosmosDataService())
                    {
                        if (!game.IsPersisted)
                        {
                            client.InsertItemAsync(game).Wait();
                        }
                        else
                        {
                            var existingGame = client.GetItemAsync <Game>(game.Id).Result;
                            if (existingGame.TS != game.TS)
                            {
                                return(req.CreateErrorResponse(HttpStatusCode.Conflict, "Unable to save game - version conflict. Please pull the latest version and reapply your changes."));
                            }

                            if (action == GameUpdateAction.EndGame && existingGame.HasEnded)
                            {
                                return(req.CreateResponse(HttpStatusCode.OK));
                            }

                            if (action == GameUpdateAction.StartGame)
                            {
                                game.StartDate = DateTime.UtcNow;
                            }

                            if (action == GameUpdateAction.EndGame)
                            {
                                isEndOfgame = true;
                            }

                            bool isWinningAcquisition = false;
                            if (action == GameUpdateAction.AcquireTreasure)
                            {
                                //Need to evaluate the game first before we save as there might be a winner
                                var teamId = arguments["teamId"];
                                isWinningAcquisition = game.EvaluateGameForWinner(teamId);

                                if (isWinningAcquisition)
                                {
                                    isEndOfgame = true;
                                }
                            }

                            if (isEndOfgame)
                            {
                                game.EndDate = DateTime.UtcNow;
                                var teams = game.Teams.OrderByDescending(t => t.TotalPoints).ToArray();

                                if (teams[0].TotalPoints == teams[1].TotalPoints)
                                {
                                    game.WinnningTeamId = null;                                     //Draw
                                }
                                else
                                {
                                    game.WinnningTeamId = teams[0].Id;
                                }
                            }

                            client.UpdateItemAsync(game).Wait();

                            if (action == GameUpdateAction.StartGame)
                            {
                                SetEndGameTimer(game, analytic);
                            }

                            if (isEndOfgame)
                            {
                                SendTargetedNotifications(game, GameUpdateAction.EndGame, arguments);
                            }
                            else
                            {
                                SendTargetedNotifications(game, action, arguments);
                            }
                        }

                        savedGame = client.GetItemAsync <Game>(game.Id).Result;                        //Comment out at some point if not needed
                    }

                    return(req.CreateResponse(HttpStatusCode.OK, savedGame));
                }
                catch (Exception e)
                {
                    // track exceptions that occur
                    analytic.TrackException(e);
                    return(req.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message, e));
                }
            }
        }