예제 #1
0
        public async Task AddGame(GameResultModel gameModel)
        {
            using (var context = this.dbContext())
            {
                var game = new GameResult() { };
                game.InjectFrom(gameModel);

                if (gameModel.Hero != null)
                {
                    game.Hero = context.Heroes.Find(gameModel.Hero.Id);
                }

                if (gameModel.OpponentHero != null)
                {
                    game.OpponentHero = context.Heroes.Find(gameModel.OpponentHero.Id);
                }

                ArenaSessionModel arenaModel = null;
                ArenaSession arena = null;
                if (gameModel.ArenaSession != null)
                {
                    gameModel.Deck = null;
                    game.DeckKey = null;
                    game.Deck = null;

                    arenaModel = gameModel.ArenaSession;
                    arena = context.ArenaSessions.Query().FirstOrDefault(x => x.Id == arenaModel.Id);
                    if (arena == null)
                    {
                        throw new InvalidOperationException("Add arena using gameManager first!");
                    }
                    // context.Entry(arena).CurrentValues.SetValues(arenaModel);

                    AddGameToArena(game, arena);
                    SetEndDateIfNeeded(arena);
                    arena.Modified = DateTime.Now;
                }

                if (gameModel.Deck != null)
                {
                    game.Deck = context.Decks.Find(gameModel.Deck.Id);
                }

                context.Games.Add(game);

                await context.SaveChangesAsync();

                gameModel.InjectFrom(game);
                this.events.PublishOnBackgroundThread(new GameResultAdded(this, gameModel));
                if (arenaModel != null)
                {
                    arenaModel.InjectFrom(arena);
                    gameModel.ArenaSession = arenaModel;
                    arenaModel.Games.Add(gameModel);
                    var latestId = context.ArenaSessions.OrderByDescending(x => x.StartDate).Select(x => x.Id).FirstOrDefault();
                    this.events.PublishOnBackgroundThread(new ArenaSessionUpdated(arenaModel.Id, latestId == arenaModel.Id));
                }
            }
        }
 public static GameResultModel ToModel(this GameResult game, int arenaSessionRecurse = 0, ArenaSessionModel arenaSessionModel = null)
 {
     if (game == null)
     {
         return null;
     }
     GameResultModel model = null;
     model = new GameResultModel();
     model.InjectFrom(game);
     if (arenaSessionRecurse <= 1)
     {
         arenaSessionRecurse++;
         model.ArenaSession = arenaSessionModel ?? game.ArenaSession.ToModel(arenaSessionRecurse);
     }
     return model;
 }
        public static GameResultModel ToModel(this GameResult game, int arenaSessionRecurse = 0, ArenaSessionModel arenaSessionModel = null)
        {
            if (game == null)
            {
                return(null);
            }
            GameResultModel model = null;

            model = new GameResultModel();
            model.InjectFrom(game);
            if (arenaSessionRecurse <= 1)
            {
                arenaSessionRecurse++;
                model.ArenaSession = arenaSessionModel ?? game.ArenaSession.ToModel(arenaSessionRecurse);
            }
            return(model);
        }
예제 #4
0
        public async Task MoveGameToArena(GameResultModel gameModel, ArenaSessionModel arenaModel)
        {
            using (var context = this.dbContext())
            {
                var sourceArena = context.ArenaSessions.Query().First(x => x.Id == gameModel.ArenaSessionId);
                var targetarena = context.ArenaSessions.Query().First(x => x.Id == arenaModel.Id);
                var game = context.Games.Query().First(x => x.Id == gameModel.Id);

                if (sourceArena.Games.Contains(game))
                {
                    sourceArena.Games.Remove(game);
                    if (game.Victory)
                    {
                        sourceArena.Wins--;
                    }
                    else
                    {
                        sourceArena.Losses--;
                    }
                }
                if (!(Equals(targetarena.Hero, game.Hero)))
                {
                    game.Hero = targetarena.Hero;
                }
                AddGameToArena(game, targetarena);
                SetEndDateIfNeeded(targetarena);

                context.SaveChanges();
                var latestId = context.ArenaSessions.OrderByDescending(x => x.StartDate).Select(x => x.Id).FirstOrDefault();
                this.events.PublishOnBackgroundThread(new ArenaSessionUpdated(sourceArena.Id, latestId == sourceArena.Id));
                this.events.PublishOnBackgroundThread(new ArenaSessionUpdated(targetarena.Id, latestId == targetarena.Id));
                this.events.PublishOnBackgroundThread(new GameResultUpdated(gameModel.Id, game.ArenaSessionId));
            }
        }
예제 #5
0
        public async Task AssignGameToArena(GameResultModel gameModel, ArenaSessionModel arenaModel)
        {
            using (var context = this.dbContext())
            {
                // var lastGame = context.Games.Where(x => x.ArenaSessionId == arenaModel.Id).OrderByDescending(x => x.ArenaGameNo).FirstOrDefault();
                var arena = context.ArenaSessions.Query().First(x => x.Id == arenaModel.Id);
                var game = context.Games.Query().First(x => x.Id == gameModel.Id);
                // game.ArenaGameNo = lastGame == null ? 1 : lastGame.ArenaGameNo + 1;

                AddGameToArena(game, arena);
                SetEndDateIfNeeded(arena);
                arena.Modified = DateTime.Now;
                game.Modified = DateTime.Now;
                await context.SaveChangesAsync();

                gameModel.InjectFrom(game);
                arenaModel.InjectFrom(arena);

                gameModel.ArenaSession = arenaModel;
                arenaModel.Games.Add(gameModel);
            }
        }
예제 #6
0
        public async Task UpdateGame(GameResultModel gameModel)
        {
            using (var context = this.dbContext())
            {
                var game = context.Games.FirstOrDefault(x => x.Id == gameModel.Id);
                if (game == null)
                {
                    throw new ArgumentException("game does not exist", "gameModel");
                }
                var oldVictory = game.Victory;
                if (gameModel.ArenaSession != null)
                {
                    gameModel.Deck = null;
                }

                context.Entry(game).CurrentValues.SetValues(gameModel);
                if (!Equals(gameModel.Hero, game.Hero) && gameModel.Hero != null)
                {
                    game.Hero = context.Heroes.Find(gameModel.Hero.Id);
                }
                if (!Equals(gameModel.OpponentHero, game.OpponentHero) && gameModel.OpponentHero != null)
                {
                    game.OpponentHero = context.Heroes.Find(gameModel.OpponentHero.Id);
                }
                if (!Equals(gameModel.Deck, game.Deck) && gameModel.Deck != null)
                {
                    game.Deck = context.Decks.Find(gameModel.Deck.Id);
                }

                game.Modified = DateTime.Now;
                await context.SaveChangesAsync();

                ArenaSession arena = null;
                if (gameModel.ArenaSession != null)
                {
                    arena = context.ArenaSessions.Query().First(x => x.Id == game.ArenaSessionId);

                    if (game.Victory && !oldVictory)
                    {
                        arena.Wins++;
                        arena.Losses--;
                    }
                    else if (!game.Victory && oldVictory)
                    {
                        arena.Wins--;
                        arena.Losses++;
                    }

                    SetEndDateIfNeeded(arena);

                    gameModel.ArenaSession.InjectFrom(arena);
                    arena.Modified = DateTime.Now;
                    await context.SaveChangesAsync();
                }

                gameModel.InjectFrom(game);
                this.events.PublishOnBackgroundThread(new GameResultUpdated(gameModel.Id, game.ArenaSessionId));
                if (gameModel.ArenaSession != null)
                {
                    this.events.PublishOnBackgroundThread(new ArenaSessionUpdated(gameModel.ArenaSession.Id));
                }
            }
        }
 public void SetGameResult(GameResultModel gameResult)
 {
     if (gameResult == null)
     {
         throw new ArgumentNullException("gameResult");
     }
     this.gameResult = gameResult;
     this.Hero = gameResult.Hero;
     this.OpponentHero = gameResult.OpponentHero;
     this.Victory = gameResult.Victory;
     this.GameMode = gameResult.GameMode;
     this.Turns = gameResult.Turns;
 }
예제 #8
0
 protected bool Equals(GameResultModel other)
 {
     return(this.Id.Equals(other.Id));
 }
 public static void MapFrom(this GameResultModel target, GameResultModel source)
 {
     target.InjectFrom(source);
 }
 public static void MapFrom(this GameResultModel target, GameResult source)
 {
     target.InjectFrom(source);
     target.ArenaSession = source.ArenaSession.ToModel();
 }
 public static void MapFrom(this GameResultModel target, GameResultModel source)
 {
     target.InjectFrom(source);
 }
 public SelectedGameChanged(object source, GameResultModel game)
 {
     this.Source = source;
     this.Game = game;
 }
예제 #13
0
 public GameResultAdded(object source, GameResultModel gameResult)
 {
     this.Source = source;
     this.GameResult = gameResult;
 }
예제 #14
0
 protected bool Equals(GameResultModel other)
 {
     return this.Id.Equals(other.Id);
 }
 public GameResultAdded(object source, GameResultModel gameResult)
 {
     Source = source;
     GameResult = gameResult;
 }