Beispiel #1
0
        private static Save LoadFromDb(string saveName)
        {
            using AppDbContext ctx = new AppDbContext();

            SaveObject save = ctx.Saves.FirstOrDefault(x => x.SaveName == saveName);

            if (save == null)
            {
                return(null);
            }

            var props     = ctx.Properties.First(x => x.BattlePropertiesObjectId == save.BattlePropertiesObjectId);
            var test      = ctx.PropertiesFlotillas.Where(x => x.BattleId == props.BattlePropertiesObjectId).ToList();
            var flotilla1 = ctx.Flotillas.First(x => x.BattleFlotillasObjectId == test[1].FlotillaId);
            var flotilla2 = ctx.Flotillas.First(x => x.BattleFlotillasObjectId == test[0].FlotillaId);
            var ships1    = ctx.Ships.Where(x => x.FlotillaId == flotilla1.BattleFlotillasObjectId).ToList();
            var ships2    = ctx.Ships.Where(x => x.FlotillaId == flotilla2.BattleFlotillasObjectId).ToList();

            Flotilla player1Flotilla = GetFlotilla(flotilla1);
            Flotilla player2Flotilla = GetFlotilla(flotilla2);


            FillFlotilla(player1Flotilla, ships1);
            FillFlotilla(player2Flotilla, ships2);

            GameProperties properties = GetGameProps(props, player1Flotilla, player2Flotilla);

            Save saveFromDb = new Save(save.SaveName, properties);

            return(saveFromDb);
        }
Beispiel #2
0
        private static int SaveToDb(Save save)
        {
            using AppDbContext ctx = new AppDbContext();

            GameProperties props = save.Properties;

            Flotilla flotilla1 = props.Player1Flotilla;
            BattleFlotillasObject flotillaInDb1 = GetBattleFlotilla(flotilla1);

            Flotilla flotilla2 = save.Properties.Player2Flotilla;
            BattleFlotillasObject flotillaInDb2 = GetBattleFlotilla(flotilla2);

            ctx.Flotillas.Add(flotillaInDb1);
            ctx.Flotillas.Add(flotillaInDb2);
            ctx.SaveChanges();

            foreach (Ship ship in flotilla1.Ships)
            {
                BattleShipsObject shipInDb = GetBattleShip(flotillaInDb1, ship);
                ctx.Add(shipInDb);
            }

            foreach (Ship ship in flotilla2.Ships)
            {
                BattleShipsObject shipInDb = GetBattleShip(flotillaInDb2, ship);
                ctx.Add(shipInDb);
            }
            ctx.SaveChanges();

            BattlePropertiesObject propertiesInDb = GetBattleProps(props);

            ctx.Add(propertiesInDb);
            ctx.SaveChanges();

            var propertiesFlotillasInDb1 = new PropertiesFlotillasObject {
                BattleId   = propertiesInDb.BattlePropertiesObjectId,
                FlotillaId = flotillaInDb1.BattleFlotillasObjectId
            };

            ctx.Add(propertiesFlotillasInDb1);

            var propertiesFlotillasInDb2 = new PropertiesFlotillasObject {
                BattleId   = propertiesInDb.BattlePropertiesObjectId,
                FlotillaId = flotillaInDb2.BattleFlotillasObjectId
            };

            ctx.Add(propertiesFlotillasInDb2);
            ctx.SaveChanges();

            var saveInDb = new SaveObject {
                SaveName = save.SaveName,
                BattlePropertiesObjectId = propertiesInDb.BattlePropertiesObjectId
            };

            ctx.Add(saveInDb);
            ctx.SaveChanges();

            return(saveInDb.SaveObjectId);
        }
Beispiel #3
0
        private static BattleFlotillasObject GetBattleFlotilla(Flotilla flotilla)
        {
            BattleFlotillasObject flotillaObject = new BattleFlotillasObject {
                Size           = flotilla.Size,
                FlotillaHealth = flotilla.FlotillaHealth,
                ShipCount      = flotilla.ShipCount
            };

            return(flotillaObject);
        }
Beispiel #4
0
 public void SetPlayerFlotilla(Flotilla flotilla)
 {
     if (_props.Player1Flotilla == null)
     {
         _props.Player1Flotilla = flotilla;
     }
     else
     {
         _props.Player2Flotilla = flotilla;
     }
 }
Beispiel #5
0
 public static void FillFlotilla(Flotilla flotilla, IEnumerable <BattleShipsObject> ships)
 {
     foreach (BattleShipsObject ship in ships)
     {
         flotilla.Ships.Add(new Ship {
             Health         = ship.Health,
             Name           = ship.Name,
             ShipCellsArray = JsonSerializer.Deserialize <int[]>(ship.ShipCellsArray),
             Size           = ship.Size
         });
     }
 }
Beispiel #6
0
        public static Flotilla GetFlotilla(BattleFlotillasObject flotillaInDb)
        {
            Flotilla flotilla = new Flotilla {
                Destroyed      = false,
                FlotillaHealth = flotillaInDb.FlotillaHealth,
                ShipCount      = flotillaInDb.ShipCount,
                Ships          = new List <Ship>(),
                Size           = flotillaInDb.Size
            };

            return(flotilla);
        }
Beispiel #7
0
        private void UpdateShips(Flotilla flotilla, IReadOnlyList <BattleShipsObject> ships)
        {
            int index = 0;

            while (index < flotilla.ShipCount)
            {
                BattleShipsObject shipFromDb = ships[index];
                Ship shipFromGame            = flotilla.Ships[index++];
                shipFromDb.Name           = shipFromGame.Name;
                shipFromDb.Size           = shipFromGame.Size;
                shipFromDb.Health         = shipFromGame.Health;
                shipFromDb.ShipCellsArray = JsonSerializer.Serialize(shipFromGame.ShipCellsArray);
            }
        }
Beispiel #8
0
        public async Task <IActionResult> OnGetAsync(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Flotilla = await _context.Flotilla
                       .Include(f => f.Company).FirstOrDefaultAsync(m => m.id == id);

            if (Flotilla == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Beispiel #9
0
        public async Task <IActionResult> OnPostAsync(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Flotilla = await _context.Flotilla.FindAsync(id);

            if (Flotilla != null)
            {
                _context.Flotilla.Remove(Flotilla);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Beispiel #10
0
        public async Task <IActionResult> OnGetAsync(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Flotilla = await _context.Flotilla
                       .Include(f => f.Company).FirstOrDefaultAsync(m => m.id == id);

            if (Flotilla == null)
            {
                return(NotFound());
            }
            ViewData["CompanyID"] = new SelectList(_context.Company, "id", "id");
            return(Page());
        }
Beispiel #11
0
        public static GameProperties GetGameProps(BattlePropertiesObject propsInDb, Flotilla flotilla1, Flotilla flotilla2)
        {
            GameProperties properties = new GameProperties {
                Id                 = propsInDb.GameId,
                GameMode           = propsInDb.GameMode,
                Player1Name        = propsInDb.Player1Name,
                Player2Name        = propsInDb.Player2Name,
                Player1Flotilla    = flotilla1,
                Player2Flotilla    = flotilla2,
                FieldSize          = JsonSerializer.Deserialize <int[]>(propsInDb.FieldSize),
                Player1FieldArray  = JsonSerializer.Deserialize <string[]>(propsInDb.Player1FieldArray),
                Player2FieldArray  = JsonSerializer.Deserialize <string[]>(propsInDb.Player2FieldArray),
                CurrentPlayer      = propsInDb.CurrentPlayer,
                Round              = propsInDb.Round,
                SelectableRowCount = propsInDb.SelectableRowCount,
                BattleHistory      = JsonSerializer.Deserialize <List <string> >(propsInDb.BattleHistory),
                MenuOptions        = JsonSerializer.Deserialize <List <string> >(propsInDb.MenuOptions)
            };

            return(properties);
        }
Beispiel #12
0
        public async Task <ActionResult> OnGetAsync(int?gameId, int?row, int?col)
        {
            if (gameId == null)
            {
                return(RedirectToPage("/"));
            }
            GameId = gameId.Value;

            var save = await _ctx.Saves.FirstAsync(x => x.SaveObjectId == gameId.Value);

            var propsDb   = _ctx.Properties.First(x => x.BattlePropertiesObjectId == save.BattlePropertiesObjectId);
            var test      = _ctx.PropertiesFlotillas.Where(x => x.BattleId == propsDb.BattlePropertiesObjectId).ToList();
            var flotilla1 = _ctx.Flotillas.First(x => x.BattleFlotillasObjectId == test[1].FlotillaId);
            var flotilla2 = _ctx.Flotillas.First(x => x.BattleFlotillasObjectId == test[0].FlotillaId);
            var ships1    = _ctx.Ships.Where(x => x.FlotillaId == flotilla1.BattleFlotillasObjectId).ToList();
            var ships2    = _ctx.Ships.Where(x => x.FlotillaId == flotilla2.BattleFlotillasObjectId).ToList();

            Flotilla player1Flotilla = SaveManager.GetFlotilla(flotilla1);
            Flotilla player2Flotilla = SaveManager.GetFlotilla(flotilla2);

            if (player1Flotilla.Destroyed || player2Flotilla.Destroyed)
            {
                return(Page());
            }

            SaveManager.FillFlotilla(player1Flotilla, ships1);
            SaveManager.FillFlotilla(player2Flotilla, ships2);

            Props = SaveManager.GetGameProps(propsDb, player1Flotilla, player2Flotilla);
            Props.LoadPlayer1FieldFromArray();
            Props.LoadPlayer2FieldFromArray();
            Props.Player1Flotilla.LoadShipFromArrays();
            Props.Player2Flotilla.LoadShipFromArrays();

            if (row != null && col != null)
            {
                BattleManager manager = new BattleManager();
                Props.Manager = manager;
                if (manager.CheckMove(Props.Player2Field, row.Value, col.Value))
                {
                    manager.MakeMove(Props, row.Value, col.Value);
                    Props.LoadPlayer1FieldToArray();
                    Props.LoadPlayer2FieldToArray();
                    manager.TimeManager.SaveRoundHistory(Props.Id);
                }
                if (Props.Winner != null)
                {
                    return(Page());
                }
            }

            // REWRITE BASE INFO

            flotilla1.Size           = Props.Player1Flotilla.Size;
            flotilla1.FlotillaHealth = Props.Player1Flotilla.FlotillaHealth;
            flotilla1.ShipCount      = Props.Player1Flotilla.ShipCount;

            flotilla2.Size           = Props.Player2Flotilla.Size;
            flotilla2.FlotillaHealth = Props.Player2Flotilla.FlotillaHealth;
            flotilla2.ShipCount      = Props.Player2Flotilla.ShipCount;

            UpdateShips(Props.Player1Flotilla, ships1);
            UpdateShips(Props.Player2Flotilla, ships2);

            propsDb.Player1FieldArray = JsonSerializer.Serialize(Props.Player1FieldArray);
            propsDb.Player2FieldArray = JsonSerializer.Serialize(Props.Player2FieldArray);
            propsDb.CurrentPlayer     = Props.CurrentPlayer;
            propsDb.Round             = Props.Round;
            propsDb.BattleHistory     = JsonSerializer.Serialize(Props.BattleHistory);
            propsDb.MenuOptions       = JsonSerializer.Serialize(Props.MenuOptions);

            await _ctx.SaveChangesAsync();

            return(Page());
        }