Пример #1
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(string Player1Name, string Player2Name, int x, int y, int TouchingRule)
        {
            BattleShip = new BattleShip(x, y, Player1Name, Player2Name);
            InsertBoat(ECellState.Carrier, 1);
            InsertBoat(ECellState.Battleship, 1);
            InsertBoat(ECellState.Submarine, 1);
            InsertBoat(ECellState.Cruiser, 1);
            InsertBoat(ECellState.Patrol, 1);
            BattleShip.TouchingRule = ETouchingRule.CanTouch;

            Domain.GameState gameState = new Domain.GameState()
            {
                DT   = DateTime.Now,
                Json = BattleShip.GetSerializedGameState(),
                Game = new Game()
                {
                    CreationDT = DateTime.Now,
                    PlayerA    = BattleShip.Player1Name,
                    PlayerB    = BattleShip.Player2Name
                }
            }
            ;
            // createInitialGamestate
            _context.GameStates.Add(gameState);
            await _context.SaveChangesAsync();

            /*_context.Games.Add(Game);
             * await _context.SaveChangesAsync();*/
            return(RedirectToPage("../Setup/Index"));

            //return Page();
        }
Пример #2
0
        public ActionResult Main(bool isStatic,int? id)
        {
            short state;
            Game[] table;
            GameState currentGameState = new GameState();
            if (isStatic)
            {
                if (id == null) id = 0;
                table = Unit.PokerBetSrvc.GetTableById(id.Value);
                state = 3;
               // currentGameState = Unit.PokerBetSrvc.GetCurrentState();
            }
            else
            {
                table = Unit.PokerBetSrvc.GetTable(out state);
                currentState = state;
                if (state == 0 && !hasRiverFinderStarted)
                {
                    stakes = new Stakes();
                    hasRiverFinderStarted = true;
                }
                if (state == 3 && hasRiverFinderStarted)
                {
                    hasRiverFinderStarted = false;
                    riverNumber = Unit.PokerBetSrvc.GetBestPrizeNumber(table);
                }
                currentGameState = Unit.PokerBetSrvc.GetCurrentState();
            }

            string finalWinners="";

            JObject mainJson =
            new JObject(
                CreateGameJSON(table[2], state,2,isStatic,ref finalWinners),
                new JProperty("timestamp", currentGameState==null ? 0 : currentGameState.StartTime.Second),
                CreateGameJSON(table[0], state, 0, isStatic, ref finalWinners),
                CreateGameJSON(table[1], state, 1, isStatic,ref finalWinners),
                new JProperty("ts", (DateTime.Now - currentGameState.StartTime).TotalSeconds)
            );

            if (state == 0 && firstTimeHistoryAdd == true)
            {
                firstTimeHistoryAdd = false;
                finalWinners = "";
            }

            if (state == 3 && !String.IsNullOrEmpty(finalWinners) && firstTimeHistoryAdd==false)
            {
                firstTimeHistoryAdd = true;
            }

            return Content(mainJson.ToString(Newtonsoft.Json.Formatting.None));
        }
Пример #3
0
        public async Task <IActionResult> OnPostAsync(string Carrier_A, string Battleship_A, string Submarine_A, string Cruiser_A, string Patrol_A,
                                                      string Carrier_B, string Battleship_B, string Submarine_B, string Cruiser_B, string Patrol_B)
        {
            GameStates = await _context.GameStates.Include(g => g.Game).ToListAsync();

            var j = GameStates.Last().Json;
            var g = GameStates.Last().Game;

            gamee = g;
            BattleShip game = new BattleShip(5, 5, "unknown1", "unknown2");

            game.SetGameStateFromJsonString(j);
            ships11 = game.Ships1;
            ships22 = game.Ships2;
            x       = game._x;
            y       = game._y;
            b1      = game._board1;
            b2      = game._board2;
            TheGame = game;
            TheGame.ShipCountPerPlayer = 5;
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships1, b2, Carrier_A, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships1, b2, Battleship_A, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships1, b2, Submarine_A, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships1, b2, Cruiser_A, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships1, b2, Patrol_A, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships2, b1, Carrier_B, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships2, b1, Battleship_B, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships2, b1, Submarine_B, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships2, b1, Cruiser_B, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships2, b1, Patrol_B, false);
            string json = TheGame.GetSerializedGameState();

            Domain.GameState gs = new Domain.GameState()
            {
                DT   = DateTime.Now,
                Game = gamee,
                Json = json
            };
            _context.GameStates.Add(gs);
            await _context.SaveChangesAsync();

            int gsid = gs.Id;
            int gid  = gs.GameId;

            return(RedirectToPage("../Fight/Index", new { gameid = gid, gamestateid = gsid }));
        }
Пример #4
0
        public ActionResult CurrentRids()
        {
            short state;
            Game[] table;
            GameState currentGameState = new GameState();

             table = Unit.PokerBetSrvc.GetTable(out state);
             currentGameState = Unit.PokerBetSrvc.GetCurrentState();
             var ridJson = CreateRidJson(table, state);

             string json = "[";

             foreach (var rid in ridJson)
             {
                 json += rid.ToString()+",";
             }

             json += "]";

             return Json(json, JsonRequestBehavior.AllowGet);
        }
Пример #5
0
        public async Task OnGetAsync(int gameid, int gamestateid, int y = 999, int x = 999)
        {
            gid  = gameid;
            gsid = gamestateid;
            //GameStates = await _context.GameStates.Include(g=> g.Game).ToListAsync();
            GameState = _context.GameStates.Find(gsid);

            if (GameState == null)
            {
                GameState = _context.GameStates.Find(gsid - 1);
            }
            var        j    = GameState.Json;
            var        g    = _context.Games.Find(gameid);
            BattleShip game = new BattleShip(5, 5, "unknown1", "unknown2");

            game.SetGameStateFromJsonString(j);


            if (x != 999 && y != 999)
            {
                game.MakeAMove(x, y);
                game._nextMoveByPlayer2 = game._nextMoveByPlayer2;
                string           json = game.GetSerializedGameState();
                Domain.GameState gs   = new Domain.GameState()
                {
                    DT   = DateTime.Now,
                    Game = g,
                    Json = json
                };
                // createInitialGamestate
                _context.GameStates.Add(gs);
                await _context.SaveChangesAsync();

                /*int gsid = gs.Id;
                 * int gid = gs.GameId;*/
            }

            width             = game._x;
            height            = game._y;
            player1name       = game.Player1Name;
            player2name       = game.Player2Name;
            nextmovebyplayer2 = game._nextMoveByPlayer2;
            if (game._nextMoveByPlayer2)
            {
                b1 = game._board1;
                b2 = game._board2;
            }
            else
            {
                b1 = game._board2;
                b2 = game._board1;
            }

            if (game.GameOver)
            {
                GameOver = true;
            }


            //return RedirectToPage("/Index");
        }
Пример #6
0
        private GameState SetState(GameState state)
        {
            ctx.GameStates.Add(state);
            try
            {
                ctx.SaveChanges();
                return state;
            }
            catch { }

            return null;
        }
Пример #7
0
        // Save when starting game.
        public static void InitialSave(GameState gameState)
        {
            Console.Clear();
            Console.Write("Saving...");
            using var dbCtx = GetConnection();
            dbCtx.Database.Migrate();

            var playerOne = gameState.PlayerAState;
            var playerTwo = gameState.PlayerBState;

            var playerA = new Domain.Player
            {
                Name       = playerOne.GetName(),
                PlayerType = playerOne.GetPlayerType(),
                GameShips  = new List <GameShip>(),
            };
            var playerB = new Domain.Player
            {
                Name       = playerTwo.GetName(),
                PlayerType = playerTwo.GetPlayerType(),
                GameShips  = new List <GameShip>(),
            };

            var playerAShips = playerOne.GetShips()
                               .Select(ship => new GameShip()
            {
                ECellState = ship.CellState,
                Hits       = ship.Hits,
                IsSunk     = ship.IsSunk,
                Width      = ship.Width,
                Name       = ship.Name,
                Player     = playerA
            })
                               .ToList();

            var playerBShips = playerTwo.GetShips()
                               .Select(ship => new GameShip()
            {
                ECellState = ship.CellState,
                Hits       = ship.Hits,
                IsSunk     = ship.IsSunk,
                Width      = ship.Width,
                Name       = ship.Name,
                Player     = playerB
            })
                               .ToList();

            playerA.GameShips = playerAShips;
            playerB.GameShips = playerBShips;

            var gameOptions = new GameOption
            {
                Name             = DateTime.Now.ToString(CultureInfo.CurrentCulture),
                BoardWidth       = gameState.BoardWidthState,
                BoardHeight      = gameState.BoardHeightState,
                EShipsCanTouch   = gameState.ShipsCanTouchState,
                NextMoveAfterHit = gameState.GameOptions.GetNextMoveAfterHit(),
                NumberOfShips    = playerAShips.Count
            };

            var newGame = new Domain.Game
            {
                Description = $"{playerA.Name}&{playerB.Name}@{DateTime.Now}".Replace(" ", "_"),
                GameOption  = gameOptions,
                PlayerA     = playerA,
                PlayerB     = playerB,
                GameStates  = new List <Domain.GameState>()
            };

            var state = new Domain.GameState
            {
                PlayerABoardState = playerOne.GetSerializedGameBoardState(),
                PlayerBBoardState = playerTwo.GetSerializedGameBoardState(),
                NextMoveByPlayerA = true
            };

            newGame.GameStates.Add(state);
            dbCtx.Games.Add(newGame);
            dbCtx.SaveChanges();
        }