Example #1
0
        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();
            }
        }
Example #2
0
        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();
            }
        }