Example #1
0
        private async Task <Dictionary <long, Player> > UpdatePlayerInformation_Internal(IEnumerable <long> playerIds, Func <IQueryable <Player>, IQueryable <Player> > query = null)
        {
            if (query == null)
            {
                query = q => q;
            }
            var data = await RequestPlayerInformation(playerIds.ToArray());

            var players = await _db.GetOrCreateAsync(playerIds, p => p.SteamId, id => new Player {
                SteamId = id
            }, query : query);

            players.ForEach(p => p.Update(data[p.SteamId]));
            await _db.SaveChangesAsync();

            return(players.ToDictionary(p => p.SteamId));
        }
Example #2
0
        private async Task <ActionResult> UpdateUnitData(string dataString, string type = "unit")
        {
            if (string.IsNullOrWhiteSpace(dataString))
            {
                return(Json(new MissingArgumentFailure()));
            }
            if (!dataString.TryToJson(out JsonDocument parsedUnitData))
            {
                return(Json(new InvalidRequestFailure()));
            }
            var unitData = parsedUnitData.RootElement;

            try
            {
                var strategy = _db.Database.CreateExecutionStrategy();
                await strategy.ExecuteAsync(async() =>
                {
                    using var transaction = await _db.Database.BeginTransactionAsync();
                    var properties        = unitData.EnumerateObject().Where(p => !string.IsNullOrWhiteSpace(p.Name));
                    var unitNames         = properties.Select(p => p.Name);
                    var units             = await _db.GetOrCreateAsync(unitNames, u => u.Name, name => CreateUnitOrBuilder(name, type));
                    foreach (var(data, unit) in properties.Select(p => p.Value).Zip(units))
                    {
                        unit.UpdateValues(data);
                    }
                    await _db.SaveChangesAsync();

                    // Remove all old links between abilities and units
                    var sqlUnitNames  = string.Join(", ", unitNames.Select(u => $"'{u}'"));
                    var usedAbilities = new List <string>();
                    await _db.Database.ExecuteSqlRawAsync($"DELETE FROM UnitAbilities WHERE UnitName IN ({sqlUnitNames})");
                    foreach (var unitObject in unitData.EnumerateObject())
                    {
                        for (int i = 1; i <= 24; i++)
                        {
                            string abilityName = unitObject.Value.GetValueOrDefault($"Ability{i}");
                            if (!string.IsNullOrWhiteSpace(abilityName))
                            {
                                var newAbility = new UnitAbility
                                {
                                    UnitName    = unitObject.Name,
                                    AbilityName = abilityName,
                                    Slot        = i
                                };
                                _db.UnitAbilities.Add(newAbility);
                                usedAbilities.Add(abilityName);
                            }
                        }
                    }
                    if (usedAbilities.Count > 0)
                    {
                        await _db.GetOrCreateAsync(usedAbilities, a => a.Name, CreateAbility);
                    }
                    await _db.SaveChangesAsync();
                    lock (_dbLock)
                    {
                        transaction.Commit();
                    }
                });

                return(Json(new { Success = true }));
            }
            catch (Exception e)
            {
                LoggingUtil.Error($"Updating {type} data failed");
                await _fileLogger.LogToFile($"update_{type}_data", new Dictionary <string, object> {
                    { "exception", e.ToString() },
                    { "errorSource", e.Source },
                    { "targetSite", e.TargetSite }
                });

                return(Json(new { Success = false }));
            }
        }