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()); } }
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)); }
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()); }
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")); } }
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 })); }
public IActionResult StartNewGame([FromBody] StartGameModel startGameModel) { return(new JsonResult(ChessService.StartNewGame(GetTicket(), startGameModel))); }