Beispiel #1
0
        public QuestKnowledges(IStatePersist statePersist)
        {
            this.statePersist = statePersist;

            using var stream = Assembly.GetExecutingAssembly()
                               .GetManifestResourceStream(typeof(QuestCounterDescriptionJson), "default.json");
            targets = CreateTargetsAsync(stream).Result;
        }
Beispiel #2
0
        public NavalBase(GameProvider listener, ITimingService timingService, NotificationManager notification, IStatePersist statePersist, IQuestKnowledges questKnowledges)
        {
            Notification         = notification;
            StatePersist         = statePersist;
            this.questKnowledges = questKnowledges;

            MasterData      = new MasterDataRoot(listener);
            Battle          = new BattleManager(listener, this);
            Quests          = new QuestManager(listener, questKnowledges, statePersist);
            _allEquipment   = new IdTable <EquipmentId, HomeportEquipment, RawEquipment, NavalBase>(this);
            _buildingDocks  = new IdTable <BuildingDockId, BuildingDock, RawBuildingDock, NavalBase>(this);
            _repairingDocks = new IdTable <RepairingDockId, RepairingDock, RawRepairingDock, NavalBase>(this);
            _useItems       = new IdTable <UseItemId, UseItemCount, RawUseItemCount, NavalBase>(this);
            _allShips       = new IdTable <ShipId, HomeportShip, RawShip, NavalBase>(this);
            _fleets         = new IdTable <FleetId, HomeportFleet, RawFleet, NavalBase>(this);
            _maps           = new IdTable <MapId, Map, RawMap, NavalBase>(this);
            _airForce       = new IdTable <(MapAreaId MapArea, AirForceGroupId GroupId), AirForceGroup, RawAirForceGroup, NavalBase>(this);

            listener.AllEquipmentUpdated += (t, msg) => _allEquipment.BatchUpdate(msg, t);
            listener.BuildingDockUpdated += (t, msg) => _buildingDocks.BatchUpdate(msg, t);
            listener.UseItemUpdated      += (t, msg) => _useItems.BatchUpdate(msg, t);

            listener.AdmiralUpdated += (t, msg) =>
            {
                if (Admiral?.Id != msg.Id)
                {
                    var @new = new Admiral(msg, this, t);
                    AdmiralChanging?.Invoke(t, Admiral, @new);
                    Admiral = @new;
                    NotifyPropertyChanged(nameof(Admiral));
                    StatePersist?.Initialize(msg.Id);
                    this.questKnowledges.Load();
                }
                else
                {
                    Admiral.Update(msg, t);
                }
            };
            listener.MaterialsUpdated += (t, msg) =>
            {
                var oldMaterials = Materials;
                var materials    = oldMaterials;
                msg.Apply(ref materials);
                if (Materials != materials)
                {
                    Materials = materials;
                    MaterialsUpdating?.Invoke(t, oldMaterials, materials, msg.Reason);
                }
            };
            listener.HomeportReturned += (t, msg) =>
            {
                _allShips.BatchUpdate(msg.Ships, t);
                CombinedFleet = msg.CombinedFleetType;
                if (StatePersist != null)
                {
                    StatePersist.LastHomeportUpdate = t;
                    StatePersist.SaveChanges();
                }
                HomeportUpdated?.Invoke(t, this);
            };
            listener.CompositionChanged += (t, msg) =>
            {
                var fleet = Fleets[msg.FleetId];
                if (msg.ShipId is ShipId shipId)
                {
                    var ship = AllShips[shipId];
                    fleet.ChangeComposition(msg.Index, ship);
                }
                else
                {
                    fleet.ChangeComposition(msg.Index, null);
                }
            };
            listener.FleetsUpdated        += (t, msg) => _fleets.BatchUpdate(msg, t);
            listener.FleetPresetSelected  += (t, msg) => Fleets[msg.Id].Update(msg, t);
            listener.ShipExtraSlotOpened  += (t, msg) => AllShips[msg].OpenExtraSlot();
            listener.PartialFleetsUpdated += (t, msg) => _fleets.BatchUpdate(msg, t, removal: false);
            listener.PartialShipsUpdated  += (t, msg) => _allShips.BatchUpdate(msg, t, removal: false);
            listener.RepairingDockUpdated += (t, msg) => _repairingDocks.BatchUpdate(msg, t);
            listener.ShipSupplied         += (t, msg) =>
            {
                foreach (var raw in msg)
                {
                    var ship = AllShips[raw.ShipId];
                    ShipSupplying?.Invoke(t, ship, raw);
                    ship?.Supply(raw);
                }
                questKnowledges.OnSingletonEvent(SingletonEvent.ShipSupply);
            };

            listener.RepairStarted += (t, msg) =>
            {
                var ship = AllShips[msg.ShipId];
                if (ship == null)
                {
                    return;
                }

                ShipRepairing?.Invoke(t, ship, msg.InstantRepair);
                if (msg.InstantRepair)
                {
                    ship.SetRepaired();
                }

                var oldMaterials = Materials;
                var materials    = oldMaterials;
                materials.Fuel  -= ship.RepairingCost.Fuel;
                materials.Steel -= ship.RepairingCost.Steel;
                Materials        = materials;
                MaterialsUpdating?.Invoke(t, oldMaterials, materials, MaterialsChangeReason.ShipRepair);
                questKnowledges.OnSingletonEvent(SingletonEvent.ShipRepair);
            };
            listener.InstantRepaired += (t, msg) =>
            {
                var dock = RepairingDocks[msg];
                RepairingDockInstant?.Invoke(t, msg, dock.RepairingShip);
                dock.Instant();
            };
            listener.InstantBuilt += (t, msg) =>
            {
                var dock = BuildingDocks[msg];
                dock.Instant();

                var oldMaterials = Materials;
                var materials    = oldMaterials;
                materials.InstantBuild -= dock.IsLSC ? 10 : 1;
                Materials = materials;
                MaterialsUpdating?.Invoke(t, oldMaterials, materials, MaterialsChangeReason.InstantBuilt);
            };
            listener.ShipCreated += (t, msg)
                                    => questKnowledges.OnSingletonEvent(SingletonEvent.ShipConstruct);
            listener.ShipBuildCompleted += (t, msg) =>
            {
                _allEquipment.BatchUpdate(msg.Equipments, t, removal: false);
                _allShips.Add(msg.Ship, t);
            };
            listener.EquipmentCreated += (t, msg) =>
            {
                foreach (var e in msg.Equipment)
                {
                    if (e is object)
                    {
                        _allEquipment.Add(e, t);
                    }
                    questKnowledges.OnSingletonEvent(SingletonEvent.EquipmentCreate);
                }
            };
            listener.ShipDismantled += (t, msg) =>
            {
                var removed = RemoveShips(msg.ShipIds, msg.DismantleEquipments);
                questKnowledges.OnShipDismantle(removed);
                ShipDismantling?.Invoke(t, removed, msg.DismantleEquipments);
            };
            listener.EquipmentDismantled += (t, msg) =>
            {
                var removed = RemoveEquipment(msg);
                questKnowledges.OnEquipmentDismantle(removed);
                EquipmentDismantling?.Invoke(t, removed);
            };
            listener.EquipmentImproved += (t, msg) =>
            {
                var consumed = msg.ConsumedEquipmentIds != null?RemoveEquipment(msg.ConsumedEquipmentIds) : null;

                var original = AllEquipment[msg.EquipmentId];
                EquipmentImproving?.Invoke(t, original, msg.UpdatedTo, consumed, msg.IsSuccess);
                if (msg.IsSuccess)
                {
                    original.Update(msg.UpdatedTo, t);
                }
                questKnowledges?.OnSingletonEvent(SingletonEvent.EquipmentImprove);
            };
            listener.ShipPoweruped += (t, msg) =>
            {
                var consumed = RemoveShips(msg.ConsumedShipIds, msg.DismantleEquipments);
                var original = AllShips[msg.ShipId];
                questKnowledges.OnShipPowerup(original, consumed, msg.IsSuccess);
                ShipPoweruping?.Invoke(t, original, msg.UpdatedTo, consumed);
                original.Update(msg.UpdatedTo, t);
            };
            listener.ExpeditionCompleted += (t, msg) =>
            {
                var fleet = Fleets[msg.FleetId];
                questKnowledges?.OnExpeditionComplete(fleet, fleet.Expedition, msg.Result);
            };

            listener.MapsUpdated       += (t, msg) => _maps.BatchUpdate(msg, t);
            listener.AirForceUpdated   += (t, msg) => _airForce.BatchUpdate(msg, t);
            listener.AirForcePlaneSet  += (t, msg) => AirForce[(msg.MapAreaId, msg.GroupId)].SetPlane(t, msg);
Beispiel #3
0
 protected QuestTarget(IStatePersist statePersist)
 {
     StatePersist = statePersist;
 }
Beispiel #4
0
 public KnownQuestTarget(IStatePersist statePersist, IReadOnlyList <QuestCounter> counters)
     : base(statePersist)
 {
     Counters = counters;
 }
Beispiel #5
0
        public Logger(IDataService dataService, GameProvider provider, NavalBase navalBase, IStatePersist statePersist)
        {
            this.dataService  = dataService;
            this.navalBase    = navalBase;
            this.statePersist = statePersist;

            provider.EquipmentCreated += (t, m) =>
            {
                using var context = CreateContext();
                context.EquipmentCreationTable.AddRange(m.Equipment.Select((e, i) => new EquipmentCreationEntity
                {
                    TimeStamp        = t.AddMilliseconds(i),
                    Consumption      = m.Consumption,
                    EquipmentCreated = e?.EquipmentInfoId,
                    IsSuccess        = e is object,
                    AdmiralLevel     = this.navalBase.Admiral.Leveling.Level,
                    Secretary        = this.navalBase.Secretary.Info.Id,
                    SecretaryLevel   = this.navalBase.Secretary.Leveling.Level
                }));
                context.SaveChanges();
            };

            provider.ShipCreated += (t, m) =>
            {
                shipCreation = new ShipCreationEntity
                {
                    TimeStamp      = t,
                    Consumption    = m.Consumption,
                    IsLSC          = m.IsLSC,
                    AdmiralLevel   = this.navalBase.Admiral.Leveling.Level,
                    Secretary      = this.navalBase.Secretary.Info.Id,
                    SecretaryLevel = this.navalBase.Secretary.Leveling.Level
                };
                lastBuildingDock = m.BuildingDockId;
            };

            provider.BuildingDockUpdated += (t, m) =>
            {
                if (shipCreation != null)
                {
                    using var context           = CreateContext();
                    shipCreation.ShipBuilt      = m.Single(x => x.Id == lastBuildingDock).BuiltShipId.Value;
                    shipCreation.EmptyDockCount = this.navalBase.BuildingDocks.Count(x => x.State == BuildingDockState.Empty);
                    context.ShipCreationTable.Add(shipCreation);
                    shipCreation     = null;
                    lastBuildingDock = default;
                    context.SaveChanges();
                }
            };

            provider.ExpeditionCompleted += (t, m) =>
            {
                using var context = CreateContext();
                var fleet = this.navalBase.Fleets[m.FleetId];
                context.ExpeditionCompletionTable.Add(new ExpeditionCompletionEntity
                {
                    TimeStamp         = t,
                    ExpeditionId      = fleet.Expedition.Id,
                    ExpeditionName    = m.ExpeditionName,
                    Result            = m.Result,
                    MaterialsAcquired = m.MaterialsAcquired,
                    RewardItem1       = m.RewardItem1,
                    RewardItem2       = m.RewardItem2
                });
                context.SaveChanges();
                foreach (var ship in fleet.HomeportShips)
                {
                    this.statePersist.ClearLastSortie(ship.Id);
                }
                this.statePersist.SaveChanges();
            };

#if DEBUG
            InitializeAdmiral(null);
#endif

            navalBase.AdmiralChanging += (t, _, a) =>
            {
                if (a != null)
                {
                    lock (admiralLock)
                        InitializeAdmiral(a);
                }
            };

            navalBase.MaterialsUpdating += (t, old, @new, reason) =>
            {
                using var context = CreateContext();
                context.MaterialsChangeTable.Add(new MaterialsChangeEntity
                {
                    TimeStamp = t,
                    Materials = @new,
                    Reason    = reason
                });
                context.SaveChanges();
            };

            navalBase.HomeportUpdated += (t, n) =>
            {
                if (this.statePersist.LastSortieTime is DateTimeOffset last)
                {
                    currentBattleContext ??= CreateContext();

                    var consumption = this.navalBase.Fleets[this.statePersist.LastSortieFleets].Sum(f => f.RepairingCost + f.SupplyingCost);
                    var diff        = consumption - this.statePersist.ConsumptionBeforeSortie;
                    var entity      = currentBattleContext.BattleConsumptionTable.Find(last);
                    if (diff != default && entity != null)
                    {
                        entity.Consumption = diff;
                        currentBattleContext.BattleConsumptionTable.Update(entity);
                        currentBattleContext.SaveChanges();
                    }
                }

                currentBattle   = null;
                currentExercise = null;
                currentBattleContext?.Dispose();
                currentFleetInBattle  = null;
                currentFleet2InBattle = null;

                this.statePersist.LastSortieFleets = null;
                this.statePersist.LastSortieTime   = null;
                this.statePersist.SaveChanges();
            };

            navalBase.ShipSupplying += (t, s, raw) =>
            {
                if (this.statePersist.GetLastSortie(s.Id) is DateTimeOffset last)
                {
                    using var context = CreateContext();
                    var entity = context.BattleConsumptionTable.Find(last);
                    if (entity is null)
                    {
                        return;
                    }

                    int  fuel        = raw.CurrentFuel - s.Fuel.Current;
                    int  bullet      = raw.CurrentBullet - s.Bullet.Current;
                    bool isMarriaged = s.Leveling.Level >= 100;
                    entity.ActualConsumption += new Materials
                    {
                        Fuel    = isMarriaged ? (int)(fuel * 0.85) : fuel,
                        Bullet  = isMarriaged ? (int)(bullet * 0.85) : bullet,
                        Bauxite = (raw.SlotAircraft.Sum() - s.Slots.Sum(x => x.Aircraft.Current)) * 5
                    };
                    context.BattleConsumptionTable.Update(entity);
                    context.SaveChanges();
                }
            };

            navalBase.ShipRepairing += (t, s, i) =>
            {
                if (this.statePersist.GetLastSortie(s.Id) is DateTimeOffset last)
                {
                    using var context = CreateContext();
                    var entity = context.BattleConsumptionTable.Find(last);
                    if (entity is null)
                    {
                        return;
                    }

                    entity.ActualConsumption += s.RepairingCost;
                    if (i)
                    {
                        entity.ActualConsumption += new Materials
                        {
                            InstantRepair = 1
                        }
                    }
                    ;
                    context.BattleConsumptionTable.Update(entity);
                    context.SaveChanges();
                }
            };
            navalBase.RepairingDockInstant += (t, d, s) =>
            {
                if (this.statePersist.GetLastSortie(s.Id) is DateTimeOffset last)
                {
                    using var context = CreateContext();
                    var entity = context.BattleConsumptionTable.Find(last);
                    if (entity is null)
                    {
                        return;
                    }


                    entity.ActualConsumption += new Materials
                    {
                        InstantRepair = 1
                    };
                    context.BattleConsumptionTable.Update(entity);
                    context.SaveChanges();
                }
            };

            provider.SortieStarting += (t, m) =>
            {
                FleetId[] fleets;
                currentFleetInBattle = this.navalBase.Fleets[m.FleetId];
                currentCombinedFleet = this.navalBase.CombinedFleet;
                if (currentCombinedFleet != CombinedFleetType.None)
                {
                    currentFleet2InBattle = this.navalBase.Fleets[(FleetId)2];
                    fleets = new[] { (FleetId)1, (FleetId)2 };
                }
                else
                {
                    fleets = new[] { m.FleetId };
                }
                currentBattleContext = CreateContext();
                currentBattleContext.BattleConsumptionTable.Add(new BattleConsumptionEntity
                {
                    TimeStamp = t,
                    MapId     = m.MapId
                });
                currentBattleContext.SaveChanges();

                this.statePersist.ConsumptionBeforeSortie =
                    currentFleetInBattle.RepairingCost +
                    currentFleetInBattle.SupplyingCost +
                    (currentFleet2InBattle?.RepairingCost ?? default) +
                    (currentFleet2InBattle?.SupplyingCost ?? default);
                this.statePersist.LastSortieTime   = t;
                this.statePersist.LastSortieFleets = fleets;
                foreach (var ship in currentFleetInBattle.HomeportShips)
                {
                    this.statePersist.SetLastSortie(ship.Id, t);
                }
                if (currentFleet2InBattle != null)
                {
                    foreach (var ship in currentFleet2InBattle.HomeportShips)
                    {
                        this.statePersist.SetLastSortie(ship.Id, t);
                    }
                }
                this.statePersist.SaveChanges();
            };

            provider.MapRouting += (t, m) =>
            {
                var map = this.navalBase.Maps[m.MapId];
                currentBattle = new BattleEntity
                {
                    TimeStamp         = t,
                    CompletionTime    = t,
                    MapId             = m.MapId,
                    MapName           = map.Info.Name.Origin,
                    RouteId           = m.RouteId,
                    EventKind         = m.EventKind,
                    BattleKind        = m.BattleKind,
                    CombinedFleetType = this.navalBase.CombinedFleet,
                    MapRank           = map.Rank,
                    MapGaugeType      = map.GaugeType,
                    MapGaugeNumber    = map.GaugeIndex,
                    MapGaugeHP        = map.Gauge?.Current,
                    MapGaugeMaxHP     = map.Gauge?.Max
                };
                if (m.UnparsedLandBaseDefence != null)
                {
                    currentBattle.LandBaseDefence = m.UnparsedLandBaseDefence.ToString(Formatting.None);
                }

                Materials acquired = default;
                foreach (var r in m.ItemAcquired)
                {
                    switch ((KnownUseItem)r.ItemId)
                    {
                    case KnownUseItem.Fuel:
                        acquired.Fuel += r.Count;
                        break;

                    case KnownUseItem.Bullet:
                        acquired.Bullet += r.Count;
                        break;

                    case KnownUseItem.Steel:
                        acquired.Steel += r.Count;
                        break;

                    case KnownUseItem.Bauxite:
                        acquired.Bauxite += r.Count;
                        break;

                    case KnownUseItem.InstantBuild:
                        acquired.InstantBuild += r.Count;
                        break;

                    case KnownUseItem.InstantRepair:
                        acquired.InstantRepair += r.Count;
                        break;

                    case KnownUseItem.Development:
                        acquired.Development += r.Count;
                        break;

                    case KnownUseItem.Improvement:
                        acquired.Improvement += r.Count;
                        break;
                    }
                }
                if (acquired != default)
                {
                    var entity = currentBattleContext.BattleConsumptionTable.Find(this.statePersist.LastSortieTime);
                    if (entity != null)
                    {
                        entity.Acquired += acquired;
                        currentBattleContext.BattleConsumptionTable.Update(entity);
                    }
                }

                currentBattleContext.BattleTable.Add(currentBattle);
                currentBattleContext.SaveChanges();
            };

            provider.ExerciseCandidateSelected += (t, m) =>
            {
                currentExercise = new ExerciseEntity
                {
                    TimeStamp  = t,
                    EnemyId    = m.AdmiralId,
                    EnemyName  = m.Name,
                    EnemyLevel = m.Leveling.Level
                };
            };

            provider.ExerciseStarted += (t, m) =>
            {
                currentFleetInBattle = this.navalBase.Fleets[m];
                currentBattleContext = CreateContext();
            };

            provider.BattleStarted += (t, m) =>
            {
                if (currentBattle != null)
                {
                    currentBattle.CompletionTime    = t;
                    currentBattle.SortieFleetState  = currentFleetInBattle.Ships.Select(x => new ShipInBattleEntity(x)).ToArray();
                    currentBattle.SortieFleet2State = currentFleet2InBattle?.Ships.Select(x => new ShipInBattleEntity(x)).ToArray();
                    currentBattle.FirstBattleDetail = m.Unparsed.ToString(Formatting.None);
                    currentBattle.LbasState         = m.Parsed.LandBasePhases
                                                      .Select(x => new AirForceInBattle(this.navalBase.AirForce[(currentBattle.MapId.AreaId, x.GroupId)]))
                                                      .ToArray();
                }