Exemple #1
0
        private async Task AppendMatchPlayerWeaponTargets(UpdatedMatchPlayer updatedMatchPlayer)
        {
            var updatedWeapons = await GetWeaponsToUpdate(updatedMatchPlayer);

            foreach (var updatedWeapon in updatedWeapons)
            {
                await AppendWeaponTargets(updatedWeapon);
            }

            var newWeapons = (updatedWeapons == null
                    ? updatedMatchPlayer.Body?.Weapons
                    : updatedMatchPlayer.Body?.Weapons?.Except(updatedWeapons.Select(uw => uw.Body)))
                             ?.ToList();

            if (newWeapons != null && newWeapons.Any())
            {
                var newWeaponModels = newWeapons.Select(w => new WeaponModel
                {
                    MatchPlayerId = updatedMatchPlayer.Model.Id,
                    Name          = w.Name
                });

                await this.dbContext.Weapon.AddRangeAsync(newWeaponModels);
            }
        }
Exemple #2
0
        private async Task <List <UpdatedWeapon> > GetWeaponsToUpdate(UpdatedMatchPlayer updatedMatchPlayer)
        {
            // Find existing weapons
            var updatedWeapons = updatedMatchPlayer.Model.Weapons?.Join(
                updatedMatchPlayer.Body.Weapons,
                w => w.Name,
                w => w.Name,
                (wm, wb) => new UpdatedWeapon {
                Model = wm, Body = wb
            }).ToList()
                                 ?? new List <UpdatedWeapon>();

            // Find new weapons (non existing)
            var newWeapons = updatedMatchPlayer.Body.Weapons
                             .Except(updatedWeapons.Select(uw => uw.Body))
                             .Select(w => new UpdatedWeapon
            {
                Model = new WeaponModel
                {
                    MatchPlayerId = updatedMatchPlayer.Model.Id,
                    Name          = w.Name
                },
                Body = w
            }).ToList();

            if (newWeapons.Count > 0)
            {
                // Insert new match players to the DB
                await this.dbContext.Weapon.AddRangeAsync(newWeapons.Select(uw => uw.Model));

                // Save the new match players to the DB
                await this.dbContext.SaveChangesAsync();

                // Add the list of new match players to the list of updated match players
                updatedWeapons.AddRange(newWeapons);
            }

            return(updatedWeapons);
        }