private void DoDoubling() { Game.GoldMultiplier *= 2; Game.BlackPlayer.Gold -= Game.Stake / 2; Game.WhitePlayer.Gold -= Game.Stake / 2; if (Game.WhitePlayer.Gold < 0 || Game.BlackPlayer.Gold < 0) { throw new ApplicationException("Player out of gold. Should not be allowd."); } using (var db = new Db.BgDbContext()) { var black = db.Users.Single(u => Game.BlackPlayer.Id == u.Id); var white = db.Users.Single(u => Game.WhitePlayer.Id == u.Id); if (!IsAi(black.Id)) // gold for ai remains in the db { black.Gold = Game.BlackPlayer.Gold; // non gold games guarded earlier in block. } if (!IsAi(white.Id)) { white.Gold = Game.WhitePlayer.Gold; } db.SaveChanges(); } Game.Stake += Game.Stake; Game.LastDoubler = Game.CurrentPlayer; }
private static bool Maintenance() { using (var db = new Db.BgDbContext()) { var m = db.Maintenance.OrderByDescending(m => m.Time).FirstOrDefault(); return(m != null && m.On); } }
private void CreateDbGame() { using (var db = new Db.BgDbContext()) { var blackUser = db.Users.Single(u => u.Id == Game.BlackPlayer.Id); if (blackUser.Gold < firstBet) { throw new ApplicationException("Black player dont have enough gold"); // Should be guarder earlier } if (Game.IsGoldGame && !IsAi(blackUser.Id)) { blackUser.Gold -= firstBet; } var black = new Db.Player { Id = Guid.NewGuid(), // A player is not the same as a user. Color = Db.Color.Black, User = blackUser, }; blackUser.Players.Add(black); var whiteUser = db.Users.Single(u => u.Id == Game.WhitePlayer.Id); if (Game.IsGoldGame && whiteUser.Gold < firstBet) { throw new ApplicationException("White player dont have enough gold"); // Should be guarder earlier } if (Game.IsGoldGame && !IsAi(whiteUser.Id)) { whiteUser.Gold -= firstBet; } var white = new Db.Player { Id = Guid.NewGuid(), Color = Db.Color.White, User = whiteUser }; whiteUser.Players.Add(white); var game = new Db.Game { Id = Game.Id, Started = DateTime.Now, }; black.Game = game; white.Game = game; game.Players.Add(black); game.Players.Add(white); db.Games.Add(game); db.SaveChanges(); } }
private (NewScoreDto black, NewScoreDto white)? SaveWinner(PlayerColor color) { if (Game.BlackPlayer.IsGuest() || Game.WhitePlayer.IsGuest()) { return(null); } if (!Game.BlackPlayer.FirstMoveMade || !Game.WhitePlayer.FirstMoveMade) { return(null); } using (var db = new Db.BgDbContext()) { var dbGame = db.Games.Single(g => g.Id == this.Game.Id); if (dbGame.Winner.HasValue) // extra safety { return(null, null); } var black = db.Users.Single(u => u.Id == Game.BlackPlayer.Id); var white = db.Users.Single(u => u.Id == Game.WhitePlayer.Id); var computed = Score.NewScore(black.Elo, white.Elo, black.GameCount, white.GameCount, color == PlayerColor.black); var blackInc = computed.black - black.Elo; var whiteInc = computed.white - white.Elo; black.GameCount++; white.GameCount++; black.Elo = computed.black; white.Elo = computed.white; dbGame.Winner = color; db.SaveChanges(); return( new NewScoreDto { score = black.Elo, increase = blackInc }, new NewScoreDto { score = white.Elo, increase = whiteInc }); } }
private void CreateDbGame() { using (var db = new Db.BgDbContext()) { var blackUser = db.Users.Single(u => u.Id == Game.BlackPlayer.Id); var black = new Db.Player { Id = Guid.NewGuid(), // A player is not the same as a user. Color = Db.Color.Black, User = blackUser }; blackUser.Players.Add(black); var whiteUser = db.Users.Single(u => u.Id == Game.WhitePlayer.Id); var white = new Db.Player { Id = Guid.NewGuid(), Color = Db.Color.White, User = whiteUser }; whiteUser.Players.Add(white); var game = new Db.Game { Id = Game.Id, Started = DateTime.Now, }; black.Game = game; white.Game = game; game.Players.Add(black); game.Players.Add(white); db.Games.Add(game); db.SaveChanges(); } }
private (NewScoreDto black, NewScoreDto white)? SaveWinner(PlayerColor color) { if (Game.BlackPlayer.IsGuest() || Game.WhitePlayer.IsGuest()) { return(null); } if (!Game.BlackPlayer.FirstMoveMade || !Game.WhitePlayer.FirstMoveMade) { using (var db = new Db.BgDbContext()) { var black = db.Users.Single(u => u.Id == Game.BlackPlayer.Id); var white = db.Users.Single(u => u.Id == Game.WhitePlayer.Id); if (!IsAi(black.Id)) { black.Gold += Game.Stake / 2; } if (!IsAi(white.Id)) { white.Gold += Game.Stake / 2; } db.SaveChanges(); } return(null); // todo: return stakes } using (var db = new Db.BgDbContext()) { var dbGame = db.Games.Single(g => g.Id == this.Game.Id); if (dbGame.Winner.HasValue) // extra safety { return(null, null); } var black = db.Users.Single(u => u.Id == Game.BlackPlayer.Id); var white = db.Users.Single(u => u.Id == Game.WhitePlayer.Id); var computed = Score.NewScore(black.Elo, white.Elo, black.GameCount, white.GameCount, color == PlayerColor.black); var blackInc = computed.black - black.Elo; var whiteInc = computed.white - white.Elo; black.GameCount++; white.GameCount++; black.Elo = computed.black; white.Elo = computed.white; if (Game.IsGoldGame) { lock (StakeLock) // Preventing other thread to do the same transaction. { Logger.LogInformation("Locked " + Thread.CurrentThread.ManagedThreadId); var stake = Game.Stake; Game.Stake = 0; Logger.LogInformation("Stake" + stake); Logger.LogInformation($"Initial gold: {black.Gold} {Game.BlackPlayer.Gold} {white.Gold} {Game.WhitePlayer.Gold}"); if (color == PlayerColor.black) { if (!IsAi(black.Id)) { black.Gold += stake; } Game.BlackPlayer.Gold += stake; } else { if (!IsAi(white.Id)) { white.Gold += stake; } Game.WhitePlayer.Gold += stake; } Logger.LogInformation($"After transfer: {black.Gold} {Game.BlackPlayer.Gold} {white.Gold} {Game.WhitePlayer.Gold}"); Logger.LogInformation("Release Thread " + Thread.CurrentThread.ManagedThreadId); } } dbGame.Winner = color; db.SaveChanges(); return( new NewScoreDto { score = black.Elo, increase = blackInc }, new NewScoreDto { score = white.Elo, increase = whiteInc }); } }