예제 #1
0
 public long StartNewGame(string ticket, StartGameModel startGameModel)
 {
     lock (mutex)
     {
         var ctx = GetContext(ticket);
         if (ctx != null && chessboard == null && (IsComputerGame() || userTickets.Count == 2))
         {
             ChessEngineOption engineOpt = null;
             if (IsComputerGame())
             {
                 engineOpt = GetOptions().ChessEngines.SingleOrDefault((opt) => opt.Name == startGameModel.ChessEngineName);
                 if (engineOpt == null)
                 {
                     throw new ArgumentException($"Invalid chess engine '{startGameModel.ChessEngineName}'.");
                 }
                 if (startGameModel.Level < 1 || startGameModel.Level > 9)
                 {
                     throw new ArgumentException($"Invalid level ${startGameModel.Level}.");
                 }
             }
             var    users       = userTickets.Values.OrderBy(ctx => ctx.Created).ToList();
             string whitePlayer = users[0].Name;
             string blackPlayer = !computerGame ? users[1].Name : engineOpt.Name;
             if (startGameModel.MyColor == "W" && ctx.Name != whitePlayer ||
                 startGameModel.MyColor == "B" && ctx.Name != blackPlayer)
             {
                 (whitePlayer, blackPlayer) = (blackPlayer, whitePlayer);
             }
             GameOption gameOption = startGameModel.GameOption switch
             {
                 "fastchess" => GameOption.FastChess,
                 "chess15" => GameOption.Chess15,
                 "chess30" => GameOption.Chess30,
                 "chess60" => GameOption.Chess60,
                 _ => GameOption.FastChess
             };
             if (IsComputerGame() && gameOption == GameOption.FastChess)
             {
                 gameOption = GameOption.Chess15;
             }
             chessboard             = new Chessboard(whitePlayer, blackPlayer, gameOption);
             ctx.StartGameConfirmed = true;
             if (IsComputerGame())
             {
                 StartChessEngine(engineOpt);
                 chessengine.Level      = startGameModel.Level;
                 chessboard.GameStarted = true;
                 PlayChessEngineNewGame(ctx, chessboard);
             }
             stateChanged = DateTime.UtcNow;
         }
         return(GetStateChanged());
     }
 }
예제 #2
0
        public IActionResult Index()
        {
            var model = new StartGameModel();

            model.Games   = _context.Games.ToList();
            model.Players = _context.Players.OrderByDescending(player => player.LastPlayed).ToList();

            var asd = model.Games.ToList().GetDropdownItems <Game>("Name");

            Console.WriteLine(JsonConvert.SerializeObject(asd));

            return(View("StartGame", model));
        }
예제 #3
0
        public ActionResult StartGame(StartGameModel startGame)
        {
            ViewBag.Player         = startGame.PlayerName;
            ViewBag.RoomID         = startGame.RoomID;
            ViewBag.QuestionNumber = 1;
            ViewBag.Question       = startGame.Question;
            GameManagerModel gameModel = GameManagerModel.GetInstance();

            try
            {
                ViewBag.Players = gameModel.GetGame(startGame.RoomID).PlayerNickNames.Values;
            }
            catch (Exception) { }
            return(View());
        }
예제 #4
0
 public IActionResult Start(StartGameModel model)
 {
     try
     {
         var             generatorParams = new FieldGeneratorParams(SeedGenerator.GenerateSeed(), model.MinesCount);
         IFieldGenerator fieldGenerator  = FieldGeneratorFactory.Create(generatorParams);
         var             game            = new Game(fieldGenerator, model.Width, model.Height);
         Guid            gameKey         = Guid.NewGuid();
         GameStorage.AddGame(gameKey, game);
         return(Ok(new GameModelBase {
             GameKey = gameKey
         }));
     }
     catch (Exception ex)
     {
         Log.Error(ex, "Couldn't create the game with params {@Model}", model);
         return(Error("An error occured while creating the game"));
     }
 }
예제 #5
0
        public ActionResult CreateGame(StartGameModel model)
        {
            var newGamePlayed = new GamePlayed();
            var players       = new List <GamePlayers>();

            players.AddRange(_context.Players.Where(pl => model.SelectedPlayers.Contains(pl.Id)).Select(x => new GamePlayers()
            {
                Player = x
            }));
            var game = _context.Games.First(ga => ga.Id == model.SelectedGame);

            newGamePlayed.Players = players;
            newGamePlayed.Game    = game;
            if (model.SelectedGameStake != null)
            {
                newGamePlayed.Stake = model.SelectedGameStake.Value;
            }
            else
            {
                newGamePlayed.Stake = game.DefaultStake;
            }
            _context.GamesPlayed.Add(newGamePlayed);
            _context.SaveChanges();
            //Add the first hand too.
            var newhand = new Hand();

            newhand.Game = newGamePlayed;
            var scores = new List <PlayerHand>();

            foreach (var player in players)
            {
                var plhand = new PlayerHand();
                plhand.Player = player.Player;
                plhand.Hand   = newhand;
                plhand.Done   = false;
                scores.Add(plhand);
            }
            newhand.Scores      = scores;
            newGamePlayed.Hands = new Hand[] { newhand };
            _context.SaveChanges();
            return(RedirectToAction("Manage", new { gameId = newGamePlayed.Id }));
        }
예제 #6
0
 public IActionResult StartNewGame([FromBody] StartGameModel startGameModel)
 {
     return(new JsonResult(ChessService.StartNewGame(GetTicket(), startGameModel)));
 }