예제 #1
0
        public async Task <bool> SaveConsensusPostAsync(ScouterApi.Models.EventModel scoreEvent)
        {
            const string partitionKey = "/gameId";

            // Update the new events's date
            scoreEvent.UpdatedOn = DateTime.UtcNow.ToString();

            try
            {
                // Map API data object to Data
                var scoreDTO = _mapper.Map <Scouter.Data.EventModelDTO>(scoreEvent);

                using (var db = new CosmosUtil <Scouter.Data.EventModelDTO>("consensus", partitionKey: partitionKey))
                {
                    //Check if the item is already exist, and then replace it
                    var oldScores = await db.GetItemsAsync(
                        $"SELECT * FROM c WHERE c.gameId = '{scoreEvent.GameId}' and c.account = '{scoreEvent.Account}'");

                    if (oldScores.Count() > 0)
                    {
                        // delete any old items except the last one
                        for (var i = 0; i < oldScores.Count() - 1; i++)
                        {
                            await db.DeleteItemAsync(oldScores.ToArray()[i].Id.ToString(), oldScores.ToArray()[i].GameId.ToString());
                        }

                        //Update precision for the event's time
                        foreach (var item in scoreEvent.Events)
                        {
                            item.EventTime = Decimal.Round(item.EventTime, 2);
                        }

                        //Replace the Scores document
                        await db.ReplaceItemAsync(scoreDTO, oldScores.Last().Id.ToString(), partitionKey : scoreEvent.GameId.ToString());
                    }
                    else
                    {
                        // Create a new one
                        await db.AddItemAsync(scoreDTO, partitionKey : scoreEvent.GameId.ToString());
                    }

                    return(true);
                }
            }
            catch (Exception e)
            {
                LogUtil.LogError(this._logger, e.Message, nameof(this.SaveConsensusPostAsync));
                Console.WriteLine(e);
                throw;
            }
        }
예제 #2
0
        public async Task <bool> SaveGoldCirclePostAsync([FromBody] GoldCircleModel goldCircle)
        {
            const string partitionKey = "/gameId";

            try
            {
                using (var db = new CosmosUtil <GoldCircleModel>("GoldCircle", partitionKey: partitionKey))
                {
                    // Update the new events's date
                    goldCircle.UpdatedOn = DateTime.UtcNow.ToString();

                    //Check if the item is already exist, and then replace it
                    var oldGoldenCircle = await db.GetItemsAsync(
                        $"SELECT * FROM c WHERE c.gameId = '{goldCircle.GameId}'");

                    if (oldGoldenCircle.Count() > 0)
                    {
                        // delete any old items except the last one
                        for (var i = 0; i < oldGoldenCircle.Count() - 1; i++)
                        {
                            await db.DeleteItemAsync(oldGoldenCircle.ToArray()[i].Id.ToString(), oldGoldenCircle.ToArray()[i].GameId.ToString());
                        }

                        //Replace the Gold Circle document
                        await db.ReplaceItemAsync(goldCircle, oldGoldenCircle.Last().Id.ToString(), partitionKey : goldCircle.GameId.ToString());
                    }
                    else
                    {
                        // Create a new one
                        await db.AddItemAsync(goldCircle, partitionKey : goldCircle.GameId.ToString());
                    }
                    return(true);
                }
            }
            catch (Exception e)
            {
                LogUtil.LogError(this._logger, e.Message, nameof(this.SaveGoldCirclePostAsync));
                Console.WriteLine(e);
                throw;
            }
        }
예제 #3
0
        public async Task <bool> PostAsync([FromBody] ScouterApi.Models.EventModel scoreEvent)
        {
            const string partitionKey = "/gameId";

            // Update the new events's date
            scoreEvent.UpdatedOn = DateTime.UtcNow.ToString();

            try
            {
                // Map API data object to Data
                var scoreDTO = _mapper.Map <Scouter.Data.EventModelDTO>(scoreEvent);

                IEnumerable <Scouter.Data.EventModelDTO> gameScores = null;

                using (var db = new CosmosUtil <Scouter.Data.EventModelDTO>("events", partitionKey: partitionKey))
                {
                    // Get all game scores
                    gameScores = await db.GetItemsAsync(
                        $"SELECT * FROM c WHERE c.gameId = '{scoreEvent.GameId}'");

                    //Check if the item is already exist, and then replace it
                    var oldScores = gameScores.Where(s => s.Account == scoreEvent.Account).ToList();
                    //var oldScores = await db.GetItemsAsync(
                    //    $"SELECT * FROM c WHERE c.gameId = '{scoreEvent.GameId}' and c.account = '{scoreEvent.Account}'");
                    if (oldScores.Count() > 0)
                    {
                        // delete any old items except the last one
                        for (var i = 0; i < oldScores.Count() - 1; i++)
                        {
                            await db.DeleteItemAsync(oldScores.ToArray()[i].Id.ToString(), oldScores.ToArray()[i].GameId.ToString());
                        }

                        //Update precision for the event's time
                        foreach (var item in scoreEvent.Events)
                        {
                            item.EventTime = Decimal.Round(item.EventTime, 2);
                        }

                        //Replace the Scores document
                        await db.ReplaceItemAsync(scoreDTO, oldScores.Last().Id.ToString(), partitionKey : scoreEvent.GameId.ToString());
                    }
                    else
                    {
                        // Create a new one
                        await db.AddItemAsync(scoreDTO, partitionKey : scoreEvent.GameId.ToString());
                    }
                }

                if (scoreDTO.IsMaster)
                {
                    // Calculate the game results and save it in the Database
                    var gameResults = ScoresProcessor.ProcessResults(gameScores).ToList();
                    if (gameResults == null)
                    {
                        return(false);
                    }

                    // Save to the database
                    using (var db = new CosmosUtil <ResultsModel>("results", partitionKey: partitionKey))
                    {
                        // Get all game scores
                        var results = await db.GetItemsAsync(
                            $"SELECT * FROM c WHERE c.gameId = '{scoreEvent.GameId}'");

                        if (results.Count() > 0)
                        {
                            // Replace any old items with the new ones
                            foreach (var item in results)
                            {
                                //Delete old document
                                await db.DeleteItemAsync(item.Id.ToString(), partitionKey : scoreEvent.GameId.ToString());
                            }
                        }

                        // Create or update the results
                        await db.UpsertArrayAsync(gameResults, partitionKey : scoreEvent.GameId.ToString());
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                LogUtil.LogError(this._logger, e.Message, nameof(this.PostAsync));
                Console.WriteLine(e);
                throw;
            }
        }