Ejemplo n.º 1
0
        public List <CommandInstruction> GenerateOptions(Dictionary <Scaffold.Village, Scaffold.CurrentVillage> availableVillages, Scaffold.Village target)
        {
            var result           = new List <CommandInstruction>();
            var travelCalculator = new TravelCalculator(worldSpeed, unitSpeed);

            if (availableVillages == null)
            {
                throw new ArgumentNullException(nameof(availableVillages));
            }

            foreach ((var source, var currentVillage) in availableVillages.Tupled())
            {
                if (source == null)
                {
                    throw new ArgumentNullException("source");
                }
                if (currentVillage == null)
                {
                    throw new ArgumentNullException("currentVillage");
                }

                var villageArmy = ArmyConvert.ArmyToJson(currentVillage.ArmyAtHome);
                if (villageArmy == null || villageArmy.IsEmpty())
                {
                    continue;
                }

                foreach (var permutation in ArmyPermutations(villageArmy))
                {
                    if (Requirements.Any(r => !r.MeetsRequirement(worldSpeed, unitSpeed, source.Coordinates(), target.Coordinates(), permutation)))
                    {
                        continue;
                    }

                    var travelTroopType = travelCalculator.TravelTroopType(permutation);

                    result.Add(new CommandInstruction
                    {
                        SendFrom   = source.VillageId,
                        SendTo     = target.VillageId,
                        TroopType  = travelTroopType,
                        TravelTime = travelCalculator.CalculateTravelTime(travelTroopType, source, target)
                    });
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task<IActionResult> GetVillageArmy(long villageId, int? morale)
        {
            if (!await CanReadVillage(villageId))
                return StatusCode(401);

            PreloadWorldData();

            var uploadHistory = await Profile("Get user upload history", () =>
                context.UserUploadHistory.Where(h => h.Uid == CurrentUserId).FirstOrDefaultAsync()
            );

            var validationInfo = UploadRestrictionsValidate.ValidateInfo.FromMapRestrictions(CurrentUser, uploadHistory);
            List<String> needsUpdateReasons = UploadRestrictionsValidate.GetNeedsUpdateReasons(CurrentServerTime, validationInfo);

            if (needsUpdateReasons != null && needsUpdateReasons.Any())
            {
                return StatusCode(423, needsUpdateReasons.Select(r => Translate(r)).ToList()); // Status code "Locked"
            }

            //  Start getting village data

            var currentVillage = await Profile("Get current village", () => (
                from cv in CurrentSets.CurrentVillage
                                    .Include(v => v.ArmyAtHome)
                                    .Include(v => v.ArmyOwned)
                                    .Include(v => v.ArmyStationed)
                                    .Include(v => v.ArmyTraveling)
                                    .Include(v => v.ArmyRecentLosses)
                                    .Include(v => v.CurrentBuilding)
                where cv.VillageId == villageId
                select cv
            ).FirstOrDefaultAsync());

            var commandsToVillage = await Profile("Get commands to village", () => (
                from command in CurrentSets.Command
                                            .Include(c => c.Army)
                where command.TargetVillageId == villageId
                where !command.IsReturning
                where command.LandsAt > CurrentServerTime
                select command
            ).ToListAsync());

            var latestConquerTimestamp = await Profile("Get latest conquer", () => (
                from conquer in CurrentSets.Conquer
                where conquer.VillageId == villageId
                orderby conquer.UnixTimestamp descending
                select conquer.UnixTimestamp
            ).FirstOrDefaultAsync());
            
            var jsonData = new JSON.VillageData();

            //  Return empty data if no data is available for the village
            if (currentVillage == null)
                return Ok(jsonData);

            Profile("Populate JSON data", () =>
            {
                if (currentVillage.ArmyOwned?.LastUpdated != null)
                {
                    jsonData.OwnedArmy = ArmyConvert.ArmyToJson(currentVillage.ArmyOwned);
                    jsonData.OwnedArmySeenAt = currentVillage.ArmyOwned.LastUpdated;
                }

                if (currentVillage.ArmyRecentLosses?.LastUpdated != null)
                {
                    jsonData.RecentlyLostArmy = ArmyConvert.ArmyToJson(currentVillage.ArmyRecentLosses);
                    jsonData.RecentlyLostArmySeenAt = currentVillage.ArmyRecentLosses.LastUpdated;
                }

                if (currentVillage.ArmyStationed?.LastUpdated != null)
                {
                    jsonData.StationedArmy = ArmyConvert.ArmyToJson(currentVillage.ArmyStationed);
                    jsonData.StationedSeenAt = currentVillage.ArmyStationed.LastUpdated;
                }

                if (currentVillage.ArmyTraveling?.LastUpdated != null)
                {
                    jsonData.TravelingArmy = ArmyConvert.ArmyToJson(currentVillage.ArmyTraveling);
                    jsonData.TravelingSeenAt = currentVillage.ArmyTraveling.LastUpdated;
                }

                if (currentVillage.ArmyAtHome?.LastUpdated != null)
                {
                    jsonData.AtHomeArmy = ArmyConvert.ArmyToJson(currentVillage.ArmyAtHome);
                    jsonData.AtHomeSeenAt = currentVillage.ArmyAtHome.LastUpdated;
                }

                jsonData.LastLoyalty = currentVillage.Loyalty;
                jsonData.LastLoyaltySeenAt = currentVillage.LoyaltyLastUpdated;

                if (currentVillage.Loyalty != null)
                {
                    var loyaltyCalculator = new LoyaltyCalculator(CurrentWorldSettings.GameSpeed);
                    jsonData.PossibleLoyalty = loyaltyCalculator.PossibleLoyalty(currentVillage.Loyalty.Value, CurrentServerTime - currentVillage.LoyaltyLastUpdated.Value);
                }

                jsonData.LastBuildings = BuildingConvert.CurrentBuildingToJson(currentVillage.CurrentBuilding);
                jsonData.LastBuildingsSeenAt = currentVillage.CurrentBuilding?.LastUpdated;

                if (currentVillage.CurrentBuilding?.LastUpdated != null)
                {
                    var constructionCalculator = new ConstructionCalculator();
                    jsonData.PossibleBuildings = constructionCalculator.CalculatePossibleBuildings(jsonData.LastBuildings, CurrentServerTime - currentVillage.CurrentBuilding.LastUpdated.Value);
                }

                if (currentVillage.ArmyStationed?.LastUpdated != null)
                {
                    var battleSimulator = new BattleSimulator();
                    short wallLevel = currentVillage.CurrentBuilding?.Wall ?? 20;
                    short hqLevel = currentVillage.CurrentBuilding?.Main ?? 20;

                    if (currentVillage.CurrentBuilding != null)
                        wallLevel += new ConstructionCalculator().CalculateLevelsInTimeSpan(BuildingType.Wall, hqLevel, wallLevel, CurrentServerTime - currentVillage.CurrentBuilding.LastUpdated.Value);

                    var nukeEstimation = battleSimulator.EstimateRequiredNukes(jsonData.StationedArmy, wallLevel, CurrentWorldSettings.ArchersEnabled, morale ?? 100);

                    jsonData.NukesRequired = nukeEstimation.NukesRequired;
                    jsonData.LastNukeLossPercent = (int)(nukeEstimation.LastNukeLossesPercent);
                }

                //  Might have CurrentArmy entries but they're just empty/null - not based on any report data
                if (jsonData.OwnedArmy != null && (jsonData.OwnedArmySeenAt == null || jsonData.OwnedArmy.Count == 0))
                {
                    jsonData.OwnedArmy = null;
                    jsonData.OwnedArmySeenAt = null;
                }

                if (jsonData.StationedArmy != null && (jsonData.StationedSeenAt == null || jsonData.StationedArmy.Count == 0))
                {
                    jsonData.StationedArmy = null;
                    jsonData.StationedSeenAt = null;
                }

                if (jsonData.TravelingArmy != null && (jsonData.TravelingSeenAt == null || jsonData.TravelingArmy.Count == 0))
                {
                    jsonData.TravelingArmy = null;
                    jsonData.TravelingSeenAt = null;
                }

                if (jsonData.RecentlyLostArmy != null && (jsonData.RecentlyLostArmySeenAt == null || jsonData.RecentlyLostArmy.Count == 0))
                {
                    jsonData.RecentlyLostArmy = null;
                    jsonData.RecentlyLostArmySeenAt = null;
                }


                var armyCalculator = new RecruitmentCalculator(2, jsonData.LastBuildings);
                DateTime? localArmyLastSeenAt = null;
                int? availableArmyPopulation = null;

                if (jsonData.StationedArmy != null)
                {
                    localArmyLastSeenAt = jsonData.StationedSeenAt.Value;
                    var existingPop = ArmyStats.CalculateTotalPopulation(jsonData.StationedArmy);
                    availableArmyPopulation = Math.Max(0, armyCalculator.MaxPopulation - existingPop);
                }

                var conquerTime = DateTimeOffset.FromUnixTimeMilliseconds(latestConquerTimestamp).UtcDateTime;

                bool useConquer = false;
                if (localArmyLastSeenAt == null)
                    useConquer = true;
                else
                    useConquer = conquerTime > localArmyLastSeenAt.Value;

                if (useConquer)
                {
                    localArmyLastSeenAt = conquerTime;
                    availableArmyPopulation = armyCalculator.MaxPopulation;
                }

                //  Add recruitment estimations
                if (localArmyLastSeenAt != null)
                {
                    var timeSinceSeen = CurrentServerTime - localArmyLastSeenAt.Value;
                    armyCalculator.MaxPopulation = availableArmyPopulation.Value;

                    //  No point in estimating troops if there's been 2 weeks since we saw stationed troops
                    if (timeSinceSeen.TotalDays < 14)
                    {
                        jsonData.PossibleRecruitedOffensiveArmy = armyCalculator.CalculatePossibleOffenseRecruitment(timeSinceSeen);
                        jsonData.PossibleRecruitedDefensiveArmy = armyCalculator.CalculatePossibleDefenseRecruitment(timeSinceSeen);
                    }
                }

                //  Add command summaries
                jsonData.DVs = new Dictionary<long, int>();
                jsonData.Fakes = new List<long>();
                jsonData.Nukes = new List<long>();
                jsonData.Nobles = new List<long>();

                jsonData.Players = commandsToVillage.Select(c => c.SourcePlayerId).Distinct().ToList();

                foreach (var command in commandsToVillage.Where(c => c.Army != null))
                {
                    var army = ArmyConvert.ArmyToJson(command.Army);
                    var offensivePop = ArmyStats.CalculateTotalPopulation(army.OfType(JSON.UnitBuild.Offensive));
                    var defensivePop = ArmyStats.CalculateTotalPopulation(army.OfType(JSON.UnitBuild.Defensive));
                    var isNoble = command.Army.Snob > 0 && command.IsAttack;

                    bool isFake = false;
                    bool isNuke = false;
                    if (!army.Values.Any(cnt => cnt > 1) && !isNoble)
                    {
                        isFake = true;
                    }
                    else if (command.IsAttack && offensivePop > 10000)
                    {
                        isNuke = true;
                    }

                    if (isFake)
                        jsonData.Fakes.Add(command.CommandId);
                    else if (isNuke)
                        jsonData.Nukes.Add(command.CommandId);
                    else if (defensivePop > 3000 && !command.IsAttack)
                        jsonData.DVs.Add(command.CommandId, defensivePop);

                    if (isNoble)
                        jsonData.Nobles.Add(command.CommandId);
                }
            });

            return Ok(jsonData);
        }
Ejemplo n.º 3
0
        public async Task<IActionResult> GetMapTags(int x, int y, int width, int height)
        {
            var uploadHistory = await context.UserUploadHistory.Where(u => u.Uid == CurrentUserId).FirstOrDefaultAsync();
            var validationInfo = UploadRestrictionsValidate.ValidateInfo.FromMapRestrictions(CurrentUser, uploadHistory);
            var needsUpdateReasons = UploadRestrictionsValidate.GetNeedsUpdateReasons(CurrentServerTime, validationInfo);

            if (needsUpdateReasons != null && needsUpdateReasons.Any())
            {
                return StatusCode(423, needsUpdateReasons.Select(r => Translate(r)).ToList()); // Status code "Locked"
            }

            var vaultTribes = await Profile("Get tribe IDs", () => (
                from user in CurrentSets.User
                join player in CurrentSets.Player on user.PlayerId equals player.PlayerId
                where player.TribeId != null && user.Enabled
                select player.TribeId.Value
            ).Distinct().ToListAsync());

            var enemyTribes = await CurrentSets.EnemyTribe.Select(et => et.EnemyTribeId).ToListAsync();

            var villageData = await Profile("Get village data", () => (
                from currentVillage in CurrentSets.CurrentVillage
                                                  .Include(cv => cv.ArmyOwned)
                                                  .Include(cv => cv.ArmyTraveling)
                                                  .Include(cv => cv.ArmyStationed)
                                                  .Include(cv => cv.CurrentBuilding)

                join village in CurrentSets.Village on currentVillage.VillageId equals village.VillageId
                join player in CurrentSets.Player on village.PlayerId equals player.PlayerId
                where CurrentUserIsAdmin || player.TribeId == null || !vaultTribes.Contains(player.TribeId.Value)

                // Right now we're not doing lazy-loading of data in the script, so this is just a potentially-costly
                // and pointless calculation.
                //where village.X >= x && village.Y >= y && village.X <= x + width && village.Y <= y + height
                select new { CurrentVillage = currentVillage, player.PlayerId, player.TribeId }
            ).ToListAsync());

            var ownVillageData = await Profile("Get own village data", () => (
                from village in CurrentSets.Village
                join currentVillage in CurrentSets.CurrentVillage on village.VillageId equals currentVillage.VillageId
                where village.PlayerId == CurrentPlayerId
                select new { Village = village, currentVillage.ArmyAtHome }
            ).ToListAsync());

            var validVillages = villageData.Where(vd => vd.PlayerId != CurrentPlayerId).ToList();
            var validVillageIds = villageData.Select(d => d.CurrentVillage.VillageId).ToList();

            var returningCommandData = await Profile("Get returning command data", () => (
                from command in CurrentSets.Command
                where command.ReturnsAt > CurrentServerTime && command.ArmyId != null && command.IsReturning
                select new { command.SourceVillageId, command.TargetVillageId, command.Army }
            ).ToListAsync());

            var sendingCommandData = await Profile("Get sent command data", () => (
                from command in CurrentSets.Command
                where command.ReturnsAt > CurrentServerTime && command.ArmyId != null && !command.IsReturning
                select new { command.SourceVillageId, command.TargetVillageId, command.Army }
            ).ToListAsync());

            var returningCommandsBySourceVillageId = validVillageIds.ToDictionary(id => id, id => new List<Scaffold.CommandArmy>());
            foreach (var command in returningCommandData)
            {
                if (returningCommandsBySourceVillageId.ContainsKey(command.SourceVillageId))
                    returningCommandsBySourceVillageId[command.SourceVillageId].Add(command.Army);
            }

            var commandsByTargetVillageId = validVillageIds.ToDictionary(id => id, id => new List<Scaffold.CommandArmy>());
            foreach (var command in sendingCommandData)
            {
                if (commandsByTargetVillageId.ContainsKey(command.TargetVillageId))
                    commandsByTargetVillageId[command.TargetVillageId].Add(command.Army);
            }

            var result = new Dictionary<long, JSON.VillageTags>();

            var tribeIds = validVillages.Select(vv => vv.TribeId).Where(t => t != null).Select(t => t.Value).Distinct();
            var tribeNames = await Profile("Get tribe names", () => (
                from tribe in CurrentSets.Ally
                where tribeIds.Contains(tribe.TribeId)
                select new { tribe.Tag, tribe.TribeId }
            ).ToListAsync());

            var tribeNamesById = tribeIds.ToDictionary(tid => tid, tid => tribeNames.First(tn => tn.TribeId == tid).Tag.UrlDecode());

            Profile("Generate JSON tags", () =>
            {
                foreach (var data in validVillages)
                {
                    var village = data.CurrentVillage;
                    var tag = new JSON.VillageTags();
                    tag.TribeName = data.TribeId == null ? null : tribeNamesById[data.TribeId.Value];
                    tag.IsEnemyTribe = data.TribeId == null ? false : enemyTribes.Contains(data.TribeId.Value);

                    tag.WallLevel = data.CurrentVillage.CurrentBuilding?.Wall;
                    tag.WallLevelSeenAt = data.CurrentVillage.CurrentBuilding?.LastUpdated;

                    tag.WatchtowerLevel = data.CurrentVillage.CurrentBuilding?.Watchtower;
                    tag.WatchtowerSeenAt = data.CurrentVillage.CurrentBuilding?.LastUpdated;

                    bool IsNuke(Scaffold.IScaffoldArmy army)
                    {
                        var jsonArmy = ArmyConvert.ArmyToJson(army);
                        if (BattleSimulator.TotalAttackPower(jsonArmy) < 3.5e5)
                            return false;

                        if (ArmyStats.CalculateTotalPopulation(jsonArmy, TroopType.Axe, TroopType.Light, TroopType.Marcher) < 4000)
                            return false;

                        //  Check HC nuke
                        if (army.Light < 100)
                        {
                            return ArmyStats.CalculateTotalPopulation(jsonArmy, TroopType.Axe, TroopType.Heavy, TroopType.Marcher) > 15000 && army.Axe > army.Heavy;
                        }
                        else
                        {
                            //  13k pop, ie 5k axe, 2k lc
                            return ArmyStats.CalculateTotalPopulation(jsonArmy, TroopType.Axe, TroopType.Light, TroopType.Marcher) > 13000;
                        }
                    }

                    tag.ReturningTroopsPopulation = returningCommandsBySourceVillageId[data.CurrentVillage.VillageId].Sum((army) => ArmyStats.CalculateTotalPopulation(ArmyConvert.ArmyToJson(army)));
                    tag.NumTargettingNukes = commandsByTargetVillageId[data.CurrentVillage.VillageId].Count(army => IsNuke(army));

                    if (village.ArmyStationed?.LastUpdated != null)
                    {
                        // 1 DV is approx. 1.7m total defense power
                        var stationed = village.ArmyStationed;
                        var defensePower = BattleSimulator.TotalDefensePower(ArmyConvert.ArmyToJson(stationed));
                        tag.IsStacked = defensePower > 1.7e6;
                        tag.StackDVs = defensePower / (float)1.7e6;
                        tag.StackSeenAt = village.ArmyStationed.LastUpdated;
                    }

                    var validArmies = new[] {
                        village.ArmyOwned,
                        village.ArmyTraveling,
                        village.ArmyStationed
                    }.Where(a => a?.LastUpdated != null && !ArmyConvert.ArmyToJson(a).IsEmpty()).ToList();

                    var nukeArmy = (
                            from army in validArmies
                            where IsNuke(army)
                            orderby army.LastUpdated.Value descending
                            select army
                        ).FirstOrDefault();

                    var nobleArmy = (
                            from army in validArmies
                            where army.Snob > 0
                            orderby army.LastUpdated.Value descending
                            select army
                        ).FirstOrDefault();

                    if (nukeArmy != null)
                    {
                        tag.HasNuke = true;
                        tag.NukeSeenAt = nukeArmy.LastUpdated;
                    }

                    if (nobleArmy != null)
                    {
                        tag.HasNobles = true;
                        tag.NoblesSeenAt = nobleArmy.LastUpdated;
                    }

                    result.Add(village.VillageId, tag);
                }
            });

            return Ok(result);
        }
Ejemplo n.º 4
0
        public async Task<IActionResult> GetCommandsRegardingVillage(long villageId)
        {
            if (!await CanReadVillage(villageId))
                return StatusCode(401);

            var uploadHistory = await context.UserUploadHistory.Where(u => u.Uid == CurrentUserId).FirstOrDefaultAsync();
            var validationInfo = UploadRestrictionsValidate.ValidateInfo.FromMapRestrictions(CurrentUser, uploadHistory);
            var needsUpdateReasons = UploadRestrictionsValidate.GetNeedsUpdateReasons(CurrentServerTime, validationInfo);

            if (needsUpdateReasons != null && needsUpdateReasons.Any())
            {
                return StatusCode(423, needsUpdateReasons.Select(r => Translate(r)).ToList()); // Status code "Locked"
            }

            var commandsFromVillage = await Profile("Get commands from village", () => (
                    from command in CurrentSets.Command
                                               .FromWorld(CurrentWorldId)
                                               .Include(c => c.Army)
                    where command.SourceVillageId == villageId
                    where command.ReturnsAt > CurrentServerTime
                    orderby command.ReturnsAt ascending
                    select command
                ).ToListAsync());

            foreach (var command in commandsFromVillage.Where(c => !c.IsReturning))
            {
                if (command.LandsAt <= CurrentServerTime)
                    command.IsReturning = true;
            }

            await context.SaveChangesAsync();
            

            var targetVillageIds = commandsFromVillage.Select(c => c.TargetVillageId).Distinct();
            var targetVillages = await Profile("Get other villages", () => (
                    from village in CurrentSets.Village
                    where targetVillageIds.Contains(village.VillageId)
                    select village
                ).ToListAsync());

            var targetVillagesById = targetVillages.ToDictionary(v => v.VillageId, v => v);

            var result = new JSON.VillageCommandSet();

            if (commandsFromVillage != null && commandsFromVillage.Count > 0)
            {
                result.CommandsFromVillage = new List<JSON.VillageCommand>();

                foreach (var command in commandsFromVillage)
                {
                    var commandData = new JSON.VillageCommand();
                    commandData.CommandId = command.CommandId;
                    commandData.LandsAt = command.LandsAt;
                    commandData.ReturnsAt = command.ReturnsAt.Value;
                    commandData.Army = ArmyConvert.ArmyToJson(command.Army);
                    commandData.IsReturning = command.IsReturning;
                    commandData.TroopType = command.TroopType;
                    commandData.OtherVillageId = command.TargetVillageId;

                    var otherVillage = targetVillagesById[command.TargetVillageId];
                    commandData.OtherVillageName = otherVillage.VillageName.UrlDecode();
                    commandData.OtherVillageCoords = $"{otherVillage.X}|{otherVillage.Y}";

                    result.CommandsFromVillage.Add(commandData);
                }
            }

            return Ok(result);
        }
Ejemplo n.º 5
0
        public async Task<IActionResult> PostCurrentArmy([FromBody]JSON.PlayerArmy currentArmySetJson)
        {
            if (!ModelState.IsValid)
                return ValidationProblem(ModelState);

            if (currentArmySetJson.TroopData.Count == 0)
                return Ok();

            var villageIds = currentArmySetJson.TroopData.Select(a => a.VillageId.Value).ToList();

            var scaffoldCurrentVillages = await Profile("Get existing scaffold current villages", () => (
                from cv in CurrentSets.CurrentVillage
                                        .Include(v => v.ArmyOwned)
                                        .Include(v => v.ArmyAtHome)
                                        .Include(v => v.ArmyStationed)
                                        .Include(v => v.ArmySupporting)
                                        .Include(v => v.ArmyTraveling)
                where villageIds.Contains(cv.VillageId)
                select cv
            ).ToListAsync());

            var villagesWithPlayerIds = await Profile("Get village player IDs", () => (
                from v in CurrentSets.Village
                where villageIds.Contains(v.VillageId)
                select new { v.PlayerId, v.VillageId }
            ).ToListAsync());

            var villageIdsByPlayerId = villagesWithPlayerIds.ToDictionary(v => v.VillageId, v => v.PlayerId);

            var mappedScaffoldVillages = villageIds.ToDictionary(id => id, id => scaffoldCurrentVillages.SingleOrDefault(cv => cv.VillageId == id));
            var missingScaffoldVillageIds = mappedScaffoldVillages.Where(kvp => kvp.Value == null).Select(kvp => kvp.Key).ToList();

            var missingVillageData = mappedScaffoldVillages.Values.Count(v => v == null) == 0
                ? new List<Scaffold.Village>()
                : await Profile("Get missing village data", () => (
                        from v in CurrentSets.Village
                        where missingScaffoldVillageIds.Contains(v.VillageId)
                        select v
                    ).ToListAsync()
                );

            var mappedMissingVillageData = missingVillageData.ToDictionary(vd => vd.VillageId, vd => vd);

            //  Get or make CurrentVillage

            Profile("Populating missing village data", () =>
            {
                foreach (var missingVillageId in missingScaffoldVillageIds)
                {
                    var village = mappedMissingVillageData[missingVillageId];
                    var newCurrentVillage = new Scaffold.CurrentVillage();
                    newCurrentVillage.VillageId = missingVillageId;
                    newCurrentVillage.WorldId = CurrentWorldId;
                    newCurrentVillage.AccessGroupId = CurrentAccessGroupId;

                    context.CurrentVillage.Add(newCurrentVillage);

                    mappedScaffoldVillages[missingVillageId] = newCurrentVillage;
                }
            });

            Profile("Generate scaffold armies", () =>
            {
                foreach (var armySetJson in currentArmySetJson.TroopData)
                {
                    var currentVillage = mappedScaffoldVillages[armySetJson.VillageId.Value];
                    var villagePlayerId = villageIdsByPlayerId[currentVillage.VillageId];

                    if (!Configuration.Security.AllowUploadArmyForNonOwner
                            && villagePlayerId != CurrentPlayerId)
                    {
                        context.InvalidDataRecord.Add(MakeInvalidDataRecord(
                            JsonConvert.SerializeObject(currentArmySetJson),
                            $"Attempted to upload current army to village {villagePlayerId} but that village is not owned by the requestor"
                        ));
                    }

                    var fullArmy = armySetJson.AtHome + armySetJson.Traveling + armySetJson.Supporting;
                    currentVillage.ArmyOwned = ArmyConvert.JsonToArmy(fullArmy, CurrentWorldId, currentVillage.ArmyOwned, context, emptyIfNull: true);
                    currentVillage.ArmyStationed = ArmyConvert.JsonToArmy(armySetJson.Stationed, CurrentWorldId, currentVillage.ArmyStationed, context, emptyIfNull: true);
                    currentVillage.ArmyTraveling = ArmyConvert.JsonToArmy(armySetJson.Traveling, CurrentWorldId, currentVillage.ArmyTraveling, context, emptyIfNull: true);
                    currentVillage.ArmyAtHome = ArmyConvert.JsonToArmy(armySetJson.AtHome, CurrentWorldId, currentVillage.ArmyAtHome, context, emptyIfNull: true);
                    currentVillage.ArmySupporting = ArmyConvert.JsonToArmy(armySetJson.Supporting, CurrentWorldId, currentVillage.ArmySupporting, context, emptyIfNull: true);


                    currentVillage.ArmyOwned.LastUpdated = CurrentServerTime;
                    currentVillage.ArmyStationed.LastUpdated = CurrentServerTime;
                    currentVillage.ArmyTraveling.LastUpdated = CurrentServerTime;
                    currentVillage.ArmyAtHome.LastUpdated = CurrentServerTime;
                    currentVillage.ArmySupporting.LastUpdated = CurrentServerTime;
                }
            });

            var currentPlayer = await EFUtil.GetOrCreateCurrentPlayer(context, CurrentPlayerId, CurrentWorldId, CurrentAccessGroupId);
            currentPlayer.CurrentPossibleNobles = currentArmySetJson.PossibleNobles;

            await Profile("Save changes", () => context.SaveChangesAsync());

            //  Run upload history update in separate query to prevent creating multiple history
            //  entries
            var userUploadHistory = await EFUtil.GetOrCreateUserUploadHistory(context, CurrentUserId);
            userUploadHistory.LastUploadedTroopsAt = CurrentServerTime;
            await context.SaveChangesAsync();

            return Ok();
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> GetTroopsSummary(int fangMinCats, int fangMaxPop)
        {
            //  Dear jesus this is such a mess

            context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

            if (!CurrentUserIsAdmin)
            {
                var authRecord = MakeFailedAuthRecord("User is not admin");
                context.Add(authRecord);
                await context.SaveChangesAsync();

                return(Unauthorized());
            }

            //  This is a mess because of different classes for Player, CurrentPlayer, etc

            //  Get all CurrentVillages from the user's tribe - list of (Player, CurrentVillage)
            //  (This returns a lot of data and will be slow)
            var tribeVillages = await(
                from player in CurrentSets.Player
                join user in CurrentSets.User on player.PlayerId equals user.PlayerId
                join village in CurrentSets.Village on player.PlayerId equals village.PlayerId
                join currentVillage in CurrentSets.CurrentVillage
                on village.VillageId equals currentVillage.VillageId
                into currentVillage
                where user.Enabled && !user.IsReadOnly
                where player.TribeId == CurrentTribeId || !Configuration.Security.RestrictAccessWithinTribes
                select new { player, villageId = village.VillageId, currentVillage = currentVillage.FirstOrDefault(), X = village.X.Value, Y = village.Y.Value }
                ).ToListAsync();

            var currentPlayers = await(
                //  Get all CurrentPlayer data for the user's tribe (separate from global 'Player' table
                //      so we can also output stats for players that haven't uploaded anything yet)
                from currentPlayer in CurrentSets.CurrentPlayer
                join player in CurrentSets.Player on currentPlayer.PlayerId equals player.PlayerId
                join user in CurrentSets.User on player.PlayerId equals user.PlayerId
                where user.Enabled && !user.IsReadOnly
                where player.TribeId == CurrentTribeId || !Configuration.Security.RestrictAccessWithinTribes
                select currentPlayer
                ).ToListAsync();

            var uploadHistory = await(
                //  Get user upload history
                from history in context.UserUploadHistory
                join user in CurrentSets.User on history.Uid equals user.Uid
                join player in CurrentSets.Player on user.PlayerId equals player.PlayerId
                where player.TribeId == CurrentTribeId || !Configuration.Security.RestrictAccessWithinTribes
                where user.Enabled && !user.IsReadOnly
                select new { playerId = player.PlayerId, history }
                ).ToListAsync();

            var enemyVillages = await(
                //  Get enemy villages
                from tribe in CurrentSets.EnemyTribe
                join player in CurrentSets.Player on tribe.EnemyTribeId equals player.TribeId
                join village in CurrentSets.Village on player.PlayerId equals village.PlayerId
                select new { village.VillageId, X = village.X.Value, Y = village.Y.Value }
                ).ToListAsync();

            // Need to load armies separately since `join into` doesn't work right with .Include()
            var armyIds = tribeVillages
                          .Where(v => v.currentVillage != null)
                          .SelectMany(v => new[] {
                v.currentVillage.ArmyAtHomeId,
                v.currentVillage.ArmyOwnedId,
                v.currentVillage.ArmyRecentLossesId,
                v.currentVillage.ArmyStationedId,
                v.currentVillage.ArmySupportingId,
                v.currentVillage.ArmyTravelingId
            })
                          .Where(id => id != null)
                          .Select(id => id.Value)
                          .ToList();

            var allArmies = await context.CurrentArmy.Where(a => armyIds.Contains(a.ArmyId)).ToDictionaryAsync(a => a.ArmyId, a => a);

            foreach (var village in tribeVillages.Where(v => v.currentVillage != null))
            {
                Scaffold.CurrentArmy FindArmy(long?armyId) => armyId == null ? null : allArmies.GetValueOrDefault(armyId.Value);

                var cv = village.currentVillage;
                cv.ArmyAtHome       = FindArmy(cv.ArmyAtHomeId);
                cv.ArmyOwned        = FindArmy(cv.ArmyOwnedId);
                cv.ArmyRecentLosses = FindArmy(cv.ArmyRecentLossesId);
                cv.ArmyStationed    = FindArmy(cv.ArmyStationedId);
                cv.ArmySupporting   = FindArmy(cv.ArmySupportingId);
                cv.ArmyTraveling    = FindArmy(cv.ArmyTravelingId);
            }

            var currentPlayerIds = currentPlayers.Select(p => p.PlayerId).ToList();

            var villageIds         = tribeVillages.Select(v => v.villageId).Distinct().ToList();
            var attackedVillageIds = await Profile("Get incomings", () => (
                                                       from command in CurrentSets.Command
                                                       where villageIds.Contains(command.TargetVillageId) && command.IsAttack && command.LandsAt > CurrentServerTime && !command.IsReturning
                                                       where !currentPlayerIds.Contains(command.SourcePlayerId)
                                                       select command.TargetVillageId
                                                       ).ToListAsync());

            var attackCommands = await Profile("Get attack details", () => (
                                                   from command in CurrentSets.Command.Include(c => c.Army)
                                                   where villageIds.Contains(command.SourceVillageId) && command.IsAttack && command.LandsAt > CurrentServerTime
                                                   where command.TargetPlayerId != null
                                                   select new { command.SourceVillageId, command.Army }
                                                   ).ToListAsync());

            var attackingVillageIds = attackCommands.Select(c => c.SourceVillageId).ToList();

            var tribeIds = tribeVillages.Select(tv => tv.player.TribeId)
                           .Where(tid => tid != null)
                           .Distinct()
                           .Select(tid => tid.Value)
                           .ToList();

            //  Collect villages grouped by owner
            var villagesByPlayer = tribeVillages
                                   .Select(v => v.player)
                                   .Distinct()
                                   .ToDictionary(
                p => p,
                p => tribeVillages.Where(v => v.player == p)
                .Select(tv => tv.currentVillage)
                .Where(cv => cv != null)
                .ToList()
                );

            var villageIdsByPlayer = villagesByPlayer.ToDictionary(
                kvp => kvp.Key,
                kvp => kvp.Value.Select(v => v.VillageId).ToList()
                );

            var uploadHistoryByPlayer = uploadHistory
                                        .Select(h => h.playerId)
                                        .Distinct()
                                        .ToDictionary(
                p => p,
                p => uploadHistory.Where(h => h.playerId == p)
                .Select(h => h.history)
                .FirstOrDefault()
                );

            //  Get all support data for the tribe
            //  'villageIds' tends to be large, so this will be a slow query
            var villagesSupport = await(
                from support in CurrentSets.CurrentVillageSupport
                .Include(s => s.SupportingArmy)
                where villageIds.Contains(support.SourceVillageId)
                select support
                ).ToListAsync();



            //  Get support data by player Id, and sorted by target tribe ID
            var playersById = tribeVillages.Select(tv => tv.player).Distinct().ToDictionary(p => p.PlayerId, p => p);

            var tribeIdsByVillage = tribeVillages.ToDictionary(
                v => v.villageId,
                v => v.player.TribeId ?? -1
                );

            //  Get tribes being supported that are not from vault
            var nonTribeVillageIds = villagesSupport.Select(s => s.TargetVillageId).Distinct().Except(villageIds).ToList();

            var nonTribeTargetTribesByVillageId = await(
                from village in CurrentSets.Village
                join player in CurrentSets.Player on village.PlayerId equals player.PlayerId
                join ally in CurrentSets.Ally on player.TribeId equals ally.TribeId
                where nonTribeVillageIds.Contains(village.VillageId)
                select new { village.VillageId, ally.TribeId }
                ).ToDictionaryAsync(d => d.VillageId, d => d.TribeId);

            foreach (var entry in nonTribeTargetTribesByVillageId)
            {
                tribeIdsByVillage.Add(entry.Key, entry.Value);
            }

            tribeIds = tribeIds.Concat(nonTribeTargetTribesByVillageId.Values.Distinct()).Distinct().ToList();

            var villagesSupportByPlayerId = new Dictionary <long, List <Scaffold.CurrentVillageSupport> >();
            var villagesSupportByPlayerIdByTargetTribeId = new Dictionary <long, Dictionary <long, List <Scaffold.CurrentVillageSupport> > >();


            //  Only check support with players that have registered villas
            foreach (var player in currentPlayers.Where(p => playersById.ContainsKey(p.PlayerId)))
            {
                var supportFromPlayer = villagesSupport.Where(
                    s => villageIdsByPlayer[playersById[player.PlayerId]].Contains(s.SourceVillageId)
                    ).ToList();

                villagesSupportByPlayerId.Add(player.PlayerId, supportFromPlayer);

                var supportByTribe = tribeIds.ToDictionary(tid => tid, _ => new List <Scaffold.CurrentVillageSupport>());
                supportByTribe.Add(-1, new List <Scaffold.CurrentVillageSupport>());

                foreach (var support in supportFromPlayer)
                {
                    var targetTribeId = tribeIdsByVillage.GetValueOrDefault(support.TargetVillageId, -1);
                    supportByTribe[targetTribeId].Add(support);
                }

                villagesSupportByPlayerIdByTargetTribeId.Add(player.PlayerId, supportByTribe);
            }

            var numIncomingsByPlayer      = new Dictionary <long, int>();
            var numAttacksByPlayer        = new Dictionary <long, int>();
            var numAttackingFangsByPlayer = new Dictionary <long, int>();
            var villageOwnerIdById        = tribeVillages.ToDictionary(v => v.villageId, v => v.player.PlayerId);

            foreach (var target in attackedVillageIds)
            {
                var playerId = villageOwnerIdById[target];
                if (!numIncomingsByPlayer.ContainsKey(playerId))
                {
                    numIncomingsByPlayer[playerId] = 0;
                }
                numIncomingsByPlayer[playerId]++;
            }

            foreach (var source in attackingVillageIds)
            {
                var playerId = villageOwnerIdById[source];
                if (!numAttacksByPlayer.ContainsKey(playerId))
                {
                    numAttacksByPlayer[playerId] = 0;
                }
                numAttacksByPlayer[playerId]++;
            }

            bool IsFang(JSON.Army army, bool ignorePop = false) =>
            army != null &&
            army.ContainsKey(JSON.TroopType.Catapult) &&
            army[JSON.TroopType.Catapult] >= fangMinCats &&
            (ignorePop || ArmyStats.CalculateTotalPopulation(army, ArmyStats.OffensiveTroopTypes) <= fangMaxPop);

            foreach (var command in attackCommands)
            {
                var playerId = villageOwnerIdById[command.SourceVillageId];
                if (!numAttackingFangsByPlayer.ContainsKey(playerId))
                {
                    numAttackingFangsByPlayer[playerId] = 0;
                }

                if (IsFang(command.Army))
                {
                    numAttackingFangsByPlayer[playerId]++;
                }
            }

            var villagesNearEnemy = new HashSet <long>();

            foreach (var village in tribeVillages)
            {
                var nearbyEnemyVillage = enemyVillages.FirstOrDefault(v =>
                {
                    var distance = Model.Coordinate.Distance(v.X, v.Y, village.X, village.Y);
                    return(distance < 10);
                });

                if (nearbyEnemyVillage != null)
                {
                    villagesNearEnemy.Add(village.villageId);
                }
            }

            var maxNoblesByPlayer = currentPlayers.ToDictionary(p => p.PlayerId, p => p.CurrentPossibleNobles);

            //  Get tribe labels
            var tribeNames = await(
                from tribe in CurrentSets.Ally
                where tribeIds.Contains(tribe.TribeId)
                select new { tribe.Tag, tribe.TribeId }
                ).ToListAsync();

            var tribeNamesById = tribeNames.ToDictionary(tn => tn.TribeId, tn => tn.Tag.UrlDecode());

            var jsonData = new List <JSON.PlayerSummary>();

            foreach (var kvp in villagesByPlayer.OrderBy(kvp => kvp.Key.TribeId).ThenBy(kvp => kvp.Key.PlayerName))
            {
                var    player         = kvp.Key;
                String playerName     = player.PlayerName;
                String tribeName      = tribeNamesById.GetValueOrDefault(player.TribeId ?? -1);
                var    playerVillages = kvp.Value;

                var playerHistory = uploadHistoryByPlayer.GetValueOrDefault(player.PlayerId);
                var playerSummary = new JSON.PlayerSummary
                {
                    PlayerName          = playerName.UrlDecode(),
                    PlayerId            = player.PlayerId,
                    TribeName           = tribeName,
                    UploadedAt          = playerHistory?.LastUploadedTroopsAt ?? new DateTime(),
                    UploadedReportsAt   = playerHistory?.LastUploadedReportsAt ?? new DateTime(),
                    UploadedIncomingsAt = playerHistory?.LastUploadedIncomingsAt ?? new DateTime(),
                    UploadedCommandsAt  = playerHistory?.LastUploadedCommandsAt ?? new DateTime(),
                    NumNobles           = playerVillages.Select(v => v.ArmyOwned?.Snob ?? 0).Sum(),
                    NumIncomings        = numIncomingsByPlayer.GetValueOrDefault(player.PlayerId, 0),
                    NumAttackCommands   = numAttacksByPlayer.GetValueOrDefault(player.PlayerId, 0),
                    FangsTraveling      = numAttackingFangsByPlayer.GetValueOrDefault(player.PlayerId, 0)
                };

                playerSummary.UploadAge = CurrentServerTime - playerSummary.UploadedAt;

                if (maxNoblesByPlayer.ContainsKey(player.PlayerId))
                {
                    playerSummary.MaxPossibleNobles = maxNoblesByPlayer[player.PlayerId];
                }

                //  General army data
                foreach (var village in playerVillages.Where(v => v.ArmyOwned != null && v.ArmyTraveling != null && v.ArmyAtHome != null))
                {
                    var armyOwned     = ArmyConvert.ArmyToJson(village.ArmyOwned);
                    var armyTraveling = ArmyConvert.ArmyToJson(village.ArmyTraveling);
                    var armyAtHome    = ArmyConvert.ArmyToJson(village.ArmyAtHome);

                    if (IsFang(armyAtHome, true) && !ArmyStats.IsNuke(armyOwned, 0.75))
                    {
                        playerSummary.FangsOwned++;
                    }

                    if (ArmyStats.IsOffensive(village.ArmyOwned))
                    {
                        playerSummary.NumOffensiveVillages++;

                        var offensivePower = BattleSimulator.TotalAttackPower(armyOwned);

                        if (ArmyStats.IsNuke(armyOwned))
                        {
                            playerSummary.NukesOwned++;
                        }
                        else if (ArmyStats.IsNuke(armyOwned, 0.75))
                        {
                            playerSummary.ThreeQuarterNukesOwned++;
                        }
                        else if (ArmyStats.IsNuke(armyOwned, 0.5))
                        {
                            playerSummary.HalfNukesOwned++;
                        }
                        else if (ArmyStats.IsNuke(armyOwned, 0.25))
                        {
                            playerSummary.QuarterNukesOwned++;
                        }

                        if (ArmyStats.IsNuke(armyTraveling))
                        {
                            playerSummary.NukesTraveling++;
                        }
                        else if (IsFang(armyTraveling))
                        {
                            playerSummary.FangsTraveling++;
                        }
                    }
                    else
                    {
                        playerSummary.NumDefensiveVillages++;

                        var ownedDefensivePower     = BattleSimulator.TotalDefensePower(armyOwned);
                        var atHomeDefensivePower    = BattleSimulator.TotalDefensePower(armyAtHome);
                        var travelingDefensivePower = BattleSimulator.TotalDefensePower(armyTraveling);

                        playerSummary.DVsAtHome += atHomeDefensivePower / (float)ArmyStats.FullDVDefensivePower;
                        if (!villagesNearEnemy.Contains(village.VillageId))
                        {
                            playerSummary.DVsAtHomeBackline += atHomeDefensivePower / (float)ArmyStats.FullDVDefensivePower;
                        }

                        playerSummary.DVsOwned     += ownedDefensivePower / (float)ArmyStats.FullDVDefensivePower;
                        playerSummary.DVsTraveling += travelingDefensivePower / (float)ArmyStats.FullDVDefensivePower;
                    }
                }

                //  Support data
                var playerSupport = villagesSupportByPlayerId.GetValueOrDefault(player.PlayerId);
                if (playerSupport != null)
                {
                    //  Support where the target is one of the players' own villages
                    foreach (var support in playerSupport.Where(s => playerVillages.Any(v => v.VillageId == s.TargetVillageId)))
                    {
                        playerSummary.DVsSupportingSelf += BattleSimulator.TotalDefensePower(support.SupportingArmy) / (float)ArmyStats.FullDVDefensivePower;
                    }

                    //  Support where the target isn't any of the players' own villages
                    foreach (var support in playerSupport.Where(s => playerVillages.All(v => v.VillageId != s.TargetVillageId)))
                    {
                        playerSummary.DVsSupportingOthers += BattleSimulator.TotalDefensePower(support.SupportingArmy) / (float)ArmyStats.FullDVDefensivePower;
                    }

                    playerSummary.SupportPopulationByTargetTribe = new Dictionary <string, int>();

                    foreach (var(tribeId, supportToTribe) in villagesSupportByPlayerIdByTargetTribeId[player.PlayerId])
                    {
                        var supportedTribeName     = tribeNamesById.GetValueOrDefault(tribeId, Translate("UNKNOWN"));
                        var totalSupportPopulation = 0;
                        foreach (var support in supportToTribe)
                        {
                            totalSupportPopulation += ArmyStats.CalculateTotalPopulation(ArmyConvert.ArmyToJson(support.SupportingArmy));
                        }

                        playerSummary.SupportPopulationByTargetTribe.Add(supportedTribeName, totalSupportPopulation);
                    }
                }

                jsonData.Add(playerSummary);
            }

            return(Ok(jsonData));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Post([FromBody] JSON.Report jsonReport)
        {
            if (ModelState.IsValid)
            {
                if (!Configuration.Security.ReportIgnoreExpectedPopulationBounds &&
                    !ArmyValidate.MeetsPopulationRestrictions(jsonReport.AttackingArmy))
                {
                    context.InvalidDataRecord.Add(MakeInvalidDataRecord(
                                                      JsonConvert.SerializeObject(jsonReport),
                                                      "Troops in attacking army exceed possible village population"
                                                      ));
                    return(BadRequest());
                }

                if (!Configuration.Security.ReportIgnoreExpectedPopulationBounds &&
                    !ArmyValidate.MeetsPopulationRestrictions(jsonReport.TravelingTroops))
                {
                    context.InvalidDataRecord.Add(MakeInvalidDataRecord(
                                                      JsonConvert.SerializeObject(jsonReport),
                                                      "Troops in traveling army exceed possible village population"
                                                      ));
                }

                if (jsonReport.OccurredAt.Value > CurrentServerTime)
                {
                    context.InvalidDataRecord.Add(MakeInvalidDataRecord(
                                                      JsonConvert.SerializeObject(jsonReport),
                                                      "The report 'OccurredAt' is in the future"
                                                      ));
                    //  Return 200/OK to trick malicious actors
                    return(Ok());
                }

                bool isDuplicate    = false;
                var  scaffoldReport = await Profile("Find existing report by ID", () => (
                                                        from report in CurrentSets.Report.IncludeReportData()
                                                        where report.ReportId == jsonReport.ReportId.Value
                                                        select report
                                                        ).FirstOrDefaultAsync()
                                                    );

                if (scaffoldReport == null)
                {
                    await Profile("Find existing report by contents", async() =>
                    {
                        var reportsMatchingDetails = await(
                            from report in CurrentSets.Report.IncludeReportData()
                            where report.OccuredAt == jsonReport.OccurredAt
                            where report.AttackerPlayerId == jsonReport.AttackingPlayerId
                            where report.AttackerVillageId == jsonReport.AttackingVillageId
                            where report.DefenderPlayerId == jsonReport.DefendingPlayerId
                            where report.DefenderVillageId == jsonReport.DefendingVillageId
                            select report
                            ).ToListAsync();

                        var existingDuplicate = reportsMatchingDetails.FirstOrDefault((r) =>
                                                                                      jsonReport.AttackingArmy == r.AttackerArmy &&
                                                                                      jsonReport.DefendingArmy == r.DefenderArmy &&
                                                                                      jsonReport.AttackingArmyLosses == r.AttackerLossesArmy &&
                                                                                      jsonReport.DefendingArmyLosses == r.DefenderLossesArmy &&
                                                                                      jsonReport.TravelingTroops == r.DefenderTravelingArmy
                                                                                      );

                        isDuplicate = existingDuplicate != null;
                    });
                }

                var tx = BuildTransaction();
                context.Transaction.Add(tx);

                if (isDuplicate)
                {
                    var isIgnored = await context.IgnoredReport.AnyAsync(r => r.ReportId == jsonReport.ReportId.Value);

                    if (!isIgnored)
                    {
                        context.IgnoredReport.Add(new IgnoredReport
                        {
                            AccessGroupId = CurrentAccessGroupId,
                            ReportId      = jsonReport.ReportId.Value,
                            WorldId       = CurrentWorldId
                        });
                    }
                }
                else
                {
                    Profile("Populate scaffold report", () =>
                    {
                        if (scaffoldReport == null)
                        {
                            scaffoldReport               = new Scaffold.Report();
                            scaffoldReport.WorldId       = CurrentWorldId;
                            scaffoldReport.AccessGroupId = CurrentAccessGroupId;
                            context.Report.Add(scaffoldReport);
                        }
                        else
                        {
                            var existingJsonReport = ReportConvert.ModelToJson(scaffoldReport);

                            if (existingJsonReport != jsonReport && scaffoldReport.TxId.HasValue)
                            {
                                context.ConflictingDataRecord.Add(new Scaffold.ConflictingDataRecord
                                {
                                    ConflictingTx = tx,
                                    OldTxId       = scaffoldReport.TxId.Value
                                });
                            }
                        }

                        jsonReport.ToModel(CurrentWorldId, scaffoldReport, context);

                        scaffoldReport.Tx = tx;
                    });

                    if (jsonReport.AttackingPlayerId != null)
                    {
                        await Profile("Update command troop type", async() =>
                        {
                            var lostAllTroops = jsonReport.AttackingArmy == jsonReport.AttackingArmyLosses;

                            var command = await Model.UtilQuery.FindCommandForReport(scaffoldReport, context);

                            if (command == null && !lostAllTroops && (jsonReport.Loyalty == null || jsonReport.Loyalty > 0))
                            {
                                //  WARNING - This will auto-generate a command with a random ID,
                                //      if a new TW command is uploaded with the given ID any backtime
                                //      calculations for this old command will get screwy
                                try
                                {
                                    await context.SaveChangesAsync();
                                }
                                catch (Exception e)
                                {
                                    throw e;
                                }

                                command                 = new Scaffold.Command();
                                command.Tx              = tx;
                                command.WorldId         = CurrentWorldId;
                                command.AccessGroupId   = CurrentAccessGroupId;
                                command.IsReturning     = true;
                                command.FirstSeenAt     = CurrentServerTime;
                                command.IsAttack        = true;
                                command.SourcePlayerId  = jsonReport.AttackingPlayerId.Value;
                                command.TargetPlayerId  = jsonReport.DefendingPlayerId;
                                command.SourceVillageId = jsonReport.AttackingVillageId.Value;
                                command.TargetVillageId = jsonReport.DefendingVillageId.Value;
                                command.LandsAt         = jsonReport.OccurredAt.Value;

                                bool madeCommand = false;

                                //  Need to auto-generate a random command ID
                                while (!madeCommand)
                                {
                                    try
                                    {
                                        command.CommandId = Random.NextLong >> 14;
                                        context.Add(command);
                                        await context.SaveChangesAsync();
                                        madeCommand = true;
                                    }
                                    catch (Exception) { }
                                }
                            }

                            if (command != null)
                            {
                                JSON.TroopType?slowestType = null;
                                float slowestSpeed         = -1;
                                foreach (var troopType in jsonReport.AttackingArmy.Where(kvp => kvp.Value > 0).Select(kvp => kvp.Key))
                                {
                                    var travelSpeed = Native.ArmyStats.TravelSpeed[troopType];
                                    if (slowestType == null)
                                    {
                                        slowestType  = troopType;
                                        slowestSpeed = travelSpeed;
                                    }
                                    else if (travelSpeed > slowestSpeed)
                                    {
                                        slowestType  = troopType;
                                        slowestSpeed = travelSpeed;
                                    }
                                }

                                var attackingVillage = await CurrentSets.Village
                                                       .FromWorld(CurrentWorldId)
                                                       .Where(v => v.VillageId == jsonReport.AttackingVillageId)
                                                       .FirstOrDefaultAsync();

                                var defendingVillage = await CurrentSets.Village
                                                       .FromWorld(CurrentWorldId)
                                                       .Where(v => v.VillageId == jsonReport.DefendingVillageId)
                                                       .FirstOrDefaultAsync();

                                var travelCalculator = new Features.Simulation.TravelCalculator(CurrentWorldSettings.GameSpeed, CurrentWorldSettings.UnitSpeed);
                                var travelTime       = travelCalculator.CalculateTravelTime(slowestType.Value, attackingVillage, defendingVillage);

                                command.TroopType = slowestType.Value.ToTroopString();

                                command.Army = ArmyConvert.JsonToArmy(jsonReport.AttackingArmy - jsonReport.AttackingArmyLosses, CurrentWorldId, command.Army, context);
                                if (command.Army != null)
                                {
                                    command.Army.WorldId = CurrentWorldId;
                                }
                                command.ReturnsAt   = scaffoldReport.OccuredAt + travelTime;
                                command.IsReturning = true;
                            }
                        });
                    }
                }

                //if (jsonReport.Loyalty <= 0)
                //{
                //    var conquer = new Scaffold.Conquer
                //    {
                //        WorldId = CurrentWorldId,
                //        OldOwner = jsonReport.DefendingPlayerId,
                //        NewOwner = jsonReport.AttackingPlayerId,
                //        VillageId = jsonReport.DefendingVillageId,
                //        UnixTimestamp = new DateTimeOffset(jsonReport.OccurredAt.Value).ToUnixTimeSeconds()
                //    };

                //    context.Add(conquer);
                //}

                await Profile("Save changes", () => context.SaveChangesAsync());

                //  Run upload history update in separate query to prevent creating multiple history
                //  entries
                var userUploadHistory = await EFUtil.GetOrCreateUserUploadHistory(context, CurrentUserId);

                userUploadHistory.LastUploadedReportsAt = CurrentServerTime;
                await context.SaveChangesAsync();

                return(Ok());
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> GetIncomingTags([FromBody] List <long> incomingsIds)
        {
            // Preload world data since we need world settings within queries below
            PreloadWorldData();
            //  Lots of data read but only updating some of it; whenever we do SaveChanges it checks
            //  for changes against all queried objects. Disable tracking by default and track explicitly if necessary
            context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

            var incomingData = await Profile("Get existing commands", () => (
                                                 from command in CurrentSets.Command
                                                 .Include(c => c.SourceVillage)
                                                 let currentVillage = command.SourceVillage.CurrentVillage.FirstOrDefault(cv => cv.AccessGroupId == CurrentAccessGroupId)
                                                                      where incomingsIds.Contains(command.CommandId)
                                                                      select new { Command = command, CurrentVillage = currentVillage }
                                                 ).ToListAsync()
                                             );

            if (incomingData == null)
            {
                return(NotFound());
            }

            //  Load in actual CurrentArmy data for incomings
            //  (Didn't need this previously but EF Core can be dumb, .Include on a `join .. into` doesn't actually include the given properties)
            {
                IEnumerable <long> SelectCurrentArmyIds(Scaffold.CurrentVillage currentVillage)
                {
                    if (currentVillage == null)
                    {
                        yield break;
                    }

                    if (currentVillage.ArmyOwnedId != null)
                    {
                        yield return(currentVillage.ArmyOwnedId.Value);
                    }

                    if (currentVillage.ArmyStationedId != null)
                    {
                        yield return(currentVillage.ArmyStationedId.Value);
                    }

                    if (currentVillage.ArmyTravelingId != null)
                    {
                        yield return(currentVillage.ArmyTravelingId.Value);
                    }
                }

                var currentArmyIds = incomingData.SelectMany(d => SelectCurrentArmyIds(d.CurrentVillage)).ToList();
                var currentArmies  = await CurrentSets.CurrentArmy.Where(army => currentArmyIds.Contains(army.ArmyId)).ToDictionaryAsync(a => a.ArmyId, a => a);

                foreach (var village in incomingData.Select(d => d.CurrentVillage).Where(v => v != null))
                {
                    if (village.ArmyOwnedId != null)
                    {
                        village.ArmyOwned = currentArmies[village.ArmyOwnedId.Value];
                    }
                    if (village.ArmyStationedId != null)
                    {
                        village.ArmyStationed = currentArmies[village.ArmyStationedId.Value];
                    }
                    if (village.ArmyTravelingId != null)
                    {
                        village.ArmyTraveling = currentArmies[village.ArmyTravelingId.Value];
                    }
                }
            }

            var uploadHistory = await Profile("Get user upload history", () =>
                                              context.UserUploadHistory.Where(h => h.Uid == CurrentUserId).FirstOrDefaultAsync()
                                              );

            var           validationInfo     = UploadRestrictionsValidate.ValidateInfo.FromTaggingRestrictions(CurrentUser, uploadHistory);
            List <String> needsUpdateReasons = UploadRestrictionsValidate.GetNeedsUpdateReasons(CurrentServerTime, validationInfo);

            if (needsUpdateReasons != null && needsUpdateReasons.Any())
            {
                return(StatusCode(423, needsUpdateReasons.Select(r => Translate(r)).ToList())); // Status code "Locked"
            }

            //  NOTE - We pull data for all villas requested but only return data for villas not in vaultOwnedVillages,
            //  should stop querying that other data at some point
            var commandSourceVillageIds = incomingData.Select(inc => inc.Command.SourceVillageId).Distinct().ToList();
            var commandTargetVillageIds = incomingData.Select(inc => inc.Command.TargetVillageId).Distinct().ToList();

            var relevantVillages = await(
                from village in CurrentSets.Village
                where commandSourceVillageIds.Contains(village.VillageId) || commandTargetVillageIds.Contains(village.VillageId)
                select new { village.PlayerId, village.VillageId, village.VillageName, X = village.X.Value, Y = village.Y.Value }
                ).ToDictionaryAsync(v => v.VillageId, v => v);

            var sourcePlayerIds = relevantVillages.Values.Where(v => commandSourceVillageIds.Contains(v.VillageId)).Select(v => v.PlayerId ?? 0).ToList();

            //  Don't do any tagging for villages owned by players registered with the vault (so players in other tribes
            //  also using the vault can't infer villa builds)
            var vaultOwnedVillages = await Profile("Get villages owned by vault users", () => (
                                                       from user in CurrentSets.User
                                                       join village in CurrentSets.Village on user.PlayerId equals village.PlayerId
                                                       where user.Enabled
                                                       select village.VillageId
                                                       ).ToListAsync());

            var sourcePlayerNames = await Profile("Get player names", () => (
                                                      from player in CurrentSets.Player
                                                      where sourcePlayerIds.Contains(player.PlayerId)
                                                      select new { player.PlayerId, player.PlayerName }
                                                      ).ToDictionaryAsync(p => p.PlayerId, p => p.PlayerName));

            var countsByVillage = await Profile("Get command counts", () => (
                                                    from command in CurrentSets.Command
                                                    where !command.IsReturning && command.LandsAt > CurrentServerTime
                                                    group command by command.SourceVillageId into villageCommands
                                                    select new { VillageId = villageCommands.Key, Count = villageCommands.Count() }
                                                    ).ToDictionaryAsync(vc => vc.VillageId, vc => vc.Count));

            var travelCalculator = new Features.Simulation.TravelCalculator(CurrentWorldSettings.GameSpeed, CurrentWorldSettings.UnitSpeed);

            DateTime CommandLaunchedAt(Scaffold.Command command) => command.LandsAt - travelCalculator.CalculateTravelTime(
                (command.TroopType ?? "ram").ToTroopType(),
                relevantVillages[command.SourceVillageId].X, relevantVillages[command.SourceVillageId].Y,
                relevantVillages[command.TargetVillageId].X, relevantVillages[command.TargetVillageId].Y
                );

            var earliestLaunchTime = incomingData.Select(inc => CommandLaunchedAt(inc.Command)).DefaultIfEmpty(CurrentServerTime).Min();

            var commandsReturningByVillageId = await Profile("Process returning commands for all source villages", async() =>
            {
                var commandSeenThreshold = earliestLaunchTime - TimeSpan.FromDays(1);

                var sentCommands = await Profile("Query returning commands for all source villages", () => (
                                                     from command in CurrentSets.Command.AsTracking()
                                                     .Include(c => c.Army)
                                                     where command.FirstSeenAt > commandSeenThreshold
                                                     where command.Army != null
                                                     where commandSourceVillageIds.Contains(command.SourceVillageId)
                                                     select command
                                                     ).ToListAsync());

                bool updatedCommands = false;
                var result           = commandSourceVillageIds.ToDictionary(vid => vid, vid => new List <Scaffold.Command>());

                Profile("Update command returning and sort into dictionary", () =>
                {
                    foreach (var cmd in sentCommands)
                    {
                        if (cmd.LandsAt <= CurrentServerTime)
                        {
                            if (!cmd.IsReturning)
                            {
                                updatedCommands = true;
                                cmd.IsReturning = true;
                            }

                            result[cmd.SourceVillageId].Add(cmd);
                        }
                    }
                });

                if (updatedCommands)
                {
                    await Profile("Save commands now set to returning", () => context.SaveChangesAsync());
                }

                return(result);
            });

            var otherTargetedVillageIds = commandsReturningByVillageId.SelectMany(kvp => kvp.Value).Select(c => c.TargetVillageId).Distinct().Except(relevantVillages.Keys);
            var otherTargetVillages     = await Profile("Get other villages targeted by inc source villas", () =>
                                                        CurrentSets.Village.Where(v => otherTargetedVillageIds.Contains(v.VillageId)).ToListAsync()
                                                        );

            foreach (var id in otherTargetedVillageIds)
            {
                var village = otherTargetVillages.First(v => v.VillageId == id);
                relevantVillages.Add(id, new
                {
                    village.PlayerId, village.VillageId, village.VillageName, X = village.X.Value, Y = village.Y.Value
                });
            }

            var launchTimesByCommandId = commandsReturningByVillageId.SelectMany(kvp => kvp.Value).Where(cmd => !vaultOwnedVillages.Contains(cmd.SourceVillageId)).ToDictionary(
                cmd => cmd.CommandId,
                cmd => CommandLaunchedAt(cmd)
                );

            IEnumerable <Scaffold.Command> RelevantCommandsForIncoming(Scaffold.Command incoming)
            {
                if (!relevantVillages.ContainsKey(incoming.SourceVillageId))
                {
                    return(Enumerable.Empty <Scaffold.Command>());
                }

                var launchTime        = CommandLaunchedAt(incoming);
                var returningCommands = commandsReturningByVillageId.GetValueOrDefault(incoming.SourceVillageId);

                if (returningCommands == null)
                {
                    return(Enumerable.Empty <Scaffold.Command>());
                }

                return(returningCommands.Where(cmd => cmd.ReturnsAt > launchTime || (launchTimesByCommandId.ContainsKey(cmd.CommandId) && launchTimesByCommandId[cmd.CommandId] > launchTime)));
            }

            var duplicates = incomingData.GroupBy(i => i.Command.CommandId).Where(g => g.Count() > 1).ToDictionary(g => g.Key, g => g.ToList());

            Dictionary <long, JSON.IncomingTag> resultTags = new Dictionary <long, JSON.IncomingTag>();

            Profile("Make incomings tags", () =>
            {
                foreach (var data in incomingData)
                {
                    var incoming        = data.Command;
                    var sourceVillageId = incoming.SourceVillageId;
                    if (vaultOwnedVillages.Contains(sourceVillageId))
                    {
                        continue;
                    }

                    var sourceCurrentVillage = data.CurrentVillage;
                    var commandsReturning    = RelevantCommandsForIncoming(incoming);

                    var armyOwned     = sourceCurrentVillage?.ArmyOwned;
                    var armyTraveling = sourceCurrentVillage?.ArmyTraveling;
                    var armyStationed = sourceCurrentVillage?.ArmyStationed;

                    //  TODO - Make this a setting
                    var maxUpdateTime = TimeSpan.FromDays(4);

                    if (armyOwned?.LastUpdated != null && (CurrentServerTime - armyOwned.LastUpdated.Value > maxUpdateTime))
                    {
                        armyOwned = null;
                    }

                    if (armyTraveling?.LastUpdated != null && (CurrentServerTime - armyTraveling.LastUpdated.Value > maxUpdateTime))
                    {
                        armyTraveling = null;
                    }

                    if (armyStationed?.LastUpdated != null && (CurrentServerTime - armyStationed.LastUpdated.Value > maxUpdateTime))
                    {
                        armyStationed = null;
                    }

                    if (armyOwned != null && armyOwned.IsEmpty())
                    {
                        armyOwned = null;
                    }
                    if (armyTraveling != null && armyTraveling.IsEmpty())
                    {
                        armyTraveling = null;
                    }
                    if (armyStationed != null && armyStationed.IsEmpty())
                    {
                        armyStationed = null;
                    }

                    var troopsReturning = new JSON.Army();
                    if (commandsReturning != null)
                    {
                        foreach (var command in commandsReturning)
                        {
                            troopsReturning += ArmyConvert.ArmyToJson(command.Army);
                        }
                    }

                    Scaffold.CurrentArmy effectiveArmy = null;
                    bool isConfidentArmy = true;
                    if (armyOwned != null)
                    {
                        effectiveArmy = armyOwned;
                    }
                    else if (armyTraveling != null)
                    {
                        effectiveArmy = armyTraveling;
                    }
                    else if (armyStationed != null)
                    {
                        effectiveArmy   = armyStationed;
                        isConfidentArmy = false;
                    }

                    var tag            = new JSON.IncomingTag();
                    tag.CommandId      = incoming.CommandId;
                    tag.OriginalTag    = incoming.UserLabel;
                    tag.NumFromVillage = countsByVillage.GetValueOrDefault(sourceVillageId);
                    tag.TroopType      = TroopTypeConvert.StringToTroopType(incoming.TroopType);

                    var sourceVillage       = relevantVillages[incoming.SourceVillageId];
                    var targetVillage       = relevantVillages[incoming.TargetVillageId];
                    tag.SourceVillageCoords = $"{sourceVillage.X}|{sourceVillage.Y}";
                    tag.TargetVillageCoords = $"{targetVillage.X}|{targetVillage.Y}";
                    tag.SourcePlayerName    = sourcePlayerNames.GetValueOrDefault(incoming.SourcePlayerId, Translate("UNKNOWN")).UrlDecode();
                    tag.SourceVillageName   = sourceVillage.VillageName.UrlDecode();
                    tag.TargetVillageName   = targetVillage.VillageName.UrlDecode();
                    tag.Distance            = new Coordinate {
                        X = sourceVillage.X, Y = sourceVillage.Y
                    }.DistanceTo(targetVillage.X, targetVillage.Y);

                    if (effectiveArmy != null)
                    {
                        //  TODO - Make this a setting
                        bool isOffense = ArmyStats.IsOffensive(effectiveArmy);

                        tag.VillageType = isOffense ? Translate("OFFENSE") : Translate("DEFENSE");

                        if (!isOffense && isConfidentArmy && (effectiveArmy.Snob == null || effectiveArmy.Snob == 0) && incoming.TroopType != JSON.TroopType.Snob.ToTroopString())
                        {
                            tag.DefiniteFake = true;
                        }

                        var offensiveArmy = effectiveArmy.OfType(JSON.UnitBuild.Offensive);
                        var jsonArmy      = ArmyConvert.ArmyToJson(offensiveArmy);
                        var pop           = Native.ArmyStats.CalculateTotalPopulation(jsonArmy);

                        var returningOffensiveArmy = troopsReturning.OfType(JSON.UnitBuild.Offensive);
                        var returningPop           = Native.ArmyStats.CalculateTotalPopulation(returningOffensiveArmy);

                        tag.OffensivePopulation = pop - returningPop;
                        if (tag.OffensivePopulation < 0)
                        {
                            tag.OffensivePopulation = 0;
                        }

                        if ((tag.OffensivePopulation > 100 || returningPop > 5000) && tag.OffensivePopulation < 5000 && isConfidentArmy)
                        {
                            tag.DefiniteFake = true;
                        }

                        tag.ReturningPopulation = returningPop;

                        tag.NumCats = effectiveArmy.Catapult;
                    }

                    resultTags.Add(incoming.CommandId, tag);
                }
            });

            return(Ok(resultTags));
        }