Exemple #1
0
        public void Callback(object custom)
        {
            foreach (IStronghold stronghold in strongholdManager.Where(s => s.StrongholdState == StrongholdState.Inactive && strongholdActivationCondition.ShouldActivate(s)))
            {
                locker.Lock(stronghold).Do(() => strongholdManager.Activate(stronghold));
            }

            // Activate stronghold with highest score if there are no neutral strongholds
            var lastNeutralActivationTimeVar = systemVariableManager["Stronghold.neutral_check"];
            var lastNeutralActivationTime    = (DateTime)lastNeutralActivationTimeVar.Value;

            if (SystemClock.Now.Subtract(lastNeutralActivationTime).TotalHours >= 8)
            {
                if (strongholdManager.All(s => s.StrongholdState != StrongholdState.Neutral))
                {
                    var mapCenter = new Position(Config.map_width / 2, Config.map_height / 2);

                    var stronghold = strongholdManager.Where(s => s.StrongholdState == StrongholdState.Inactive && s.NearbyCitiesCount > 0)
                                     .OrderByDescending(s => strongholdActivationCondition.Score(s))
                                     .ThenBy(s => tileLocator.TileDistance(s.PrimaryPosition, 1, mapCenter, 1))
                                     .FirstOrDefault();

                    // Do the same check for a SH but w/o nearby cities restriction
                    if (stronghold == null)
                    {
                        stronghold = strongholdManager.Where(s => s.StrongholdState == StrongholdState.Inactive)
                                     .OrderByDescending(s => strongholdActivationCondition.Score(s))
                                     .ThenBy(s => tileLocator.TileDistance(s.PrimaryPosition, 1, mapCenter, 1))
                                     .FirstOrDefault();
                    }

                    if (stronghold != null)
                    {
                        locker.Lock(stronghold).Do(() => strongholdManager.Activate(stronghold));
                    }
                }

                using (dbManager.GetThreadTransaction())
                {
                    lastNeutralActivationTimeVar.Value = SystemClock.Now;
                    dbManager.Save(lastNeutralActivationTimeVar);
                }
            }

            Time = DateTime.UtcNow.Add(TimeSpan);
            scheduler.Put(this);
        }
Exemple #2
0
        private void SerializeStrongholds(JsonWriter jw)
        {
            jw.WritePropertyName("strongholds");
            jw.WriteStartArray();
            foreach (var stronghold in strongholdManager.Where(s => s.StrongholdState != StrongholdState.Inactive))
            {
                jw.WriteStartObject();

                jw.WritePropertyName("id");
                jw.WriteValue(stronghold.ObjectId);

                jw.WritePropertyName("name");
                jw.WriteValue(stronghold.Name);

                jw.WritePropertyName("x");
                jw.WriteValue(stronghold.PrimaryPosition.X);

                jw.WritePropertyName("y");
                jw.WriteValue(stronghold.PrimaryPosition.Y);

                jw.WritePropertyName("gate_open_to_tribe_id");
                var gateOpenTo = stronghold.GateOpenTo;
                jw.WriteValue(gateOpenTo == null ? null : (uint?)gateOpenTo.Id);

                jw.WritePropertyName("owner_tribe_id");
                var owner = stronghold.Tribe;
                jw.WriteValue(owner == null ? null : (uint?)owner.Id);

                jw.WritePropertyName("level");
                jw.WriteValue(stronghold.Lvl);

                jw.WritePropertyName("state");
                jw.WriteValue(stronghold.StrongholdState.ToString());

                jw.WritePropertyName("victory_point_rate");
                jw.WriteValue(stronghold.VictoryPointRate);

                jw.WritePropertyName("gate_max");
                jw.WriteValue(stronghold.GateMax);

                jw.WritePropertyName("theme");
                jw.WriteValue(stronghold.Theme);

                jw.WritePropertyName("date_occupied");
                jw.WriteValue(stronghold.DateOccupied);

                jw.WriteEndObject();
            }
            jw.WriteEndArray();
        }
Exemple #3
0
        private string CmdStrongholdFindNearbyCities(Session session, string[] parms)
        {
            var list = new List <Position>(mapFactory.Locations.Take(mapFactory.Index));

            foreach (var stronghold in strongholdManager.Where(s => s.StrongholdState == StrongholdState.Inactive))
            {
                locker.Lock(stronghold).Do(() =>
                {
                    stronghold.BeginUpdate();
                    int count = list.Count(pt =>
                                           tileLocator.TileDistance(stronghold.PrimaryPosition, 3, pt, 1) <= Config.stronghold_radius_base + Config.stronghold_radius_per_level * stronghold.Lvl);
                    stronghold.NearbyCitiesCount = (ushort)count;
                    stronghold.EndUpdate();
                });
            }

            return("OK");
        }