Ejemplo n.º 1
0
 public ShipCreation(BuildingDockId buildingDockId, bool instantBuild, bool isLSC, Materials consumption)
 {
     BuildingDockId = buildingDockId;
     InstantBuild   = instantBuild;
     IsLSC          = isLSC;
     Consumption    = consumption;
 }
Ejemplo n.º 2
0
        public Logger(IDataService dataService, IGameProvider provider, NavalBase navalBase)
        {
            this.dataService = dataService;
            this.navalBase   = navalBase;

            provider.EquipmentCreated += (t, m) =>
            {
                using (var context = CreateContext())
                {
                    context.EquipmentCreationTable.Add(new EquipmentCreationEntity
                    {
                        TimeStamp        = t,
                        Consumption      = m.Consumption,
                        EquipmentCreated = m.SelectedEquipentInfoId,
                        IsSuccess        = m.IsSuccess,
                        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 = 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())
                {
                    context.ExpeditionCompletionTable.Add(new ExpeditionCompletionEntity
                    {
                        TimeStamp         = t,
                        ExpeditionId      = this.navalBase.Fleets[m.FleetId].Expedition.Id,
                        ExpeditionName    = m.ExpeditionName,
                        Result            = m.Result,
                        MaterialsAcquired = m.MaterialsAcquired,
                        RewardItem1       = m.RewardItem1,
                        RewardItem2       = m.RewardItem2
                    });
                    context.SaveChanges();
                }
            };

#if DEBUG
            using (var context = CreateContext())
                context.Database.Migrate();
#endif

            provider.AdmiralUpdated += (t, m) =>
            {
                if (PlayerLoaded)
                {
                    using (var context = CreateContext())
                        context.Database.Migrate();
                }
            };
        }
Ejemplo n.º 3
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();
                }