Esempio n. 1
0
        public async Task AddGame(GameResultModel gameModel)
        {
            using (var context = 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);
                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();
                    events.PublishOnBackgroundThread(new ArenaSessionUpdated(arenaModel.Id, latestId == arenaModel.Id));
                }
            }
        }
Esempio n. 2
0
        public async Task Retire(ArenaSessionModel arenaModel)
        {
            using (var context = dbContext())
            {
                var arena = context.ArenaSessions.Query().First(x => x.Id == arenaModel.Id);
                arena.Retired = true;
                if (arena.EndDate == null)
                {
                    arena.EndDate = DateTime.Now;
                }
                arena.Modified = DateTime.Now;
                await context.SaveChangesAsync();

                arenaModel.InjectFrom(arena);
            }
        }
Esempio n. 3
0
        public async Task UpdateArenaSession(ArenaSessionModel arenaModel)
        {
            using (var context = dbContext())
            {
                var arena = context.ArenaSessions.Query().FirstOrDefault(x => x.Id == arenaModel.Id);
                if (arena == null)
                {
                    throw new ArgumentException("arena does not exist", "arenaModel");
                }
                if (!Equals(arenaModel.Hero, arena.Hero))
                {
                    arena.Hero = context.Heroes.Find(arenaModel.Hero.Id);
                }
                if (arenaModel.Image1 != null)
                {
                    var img = context.ArenaDeckImages.Find(arenaModel.Image1.Id);
                    if (img == null)
                    {
                        img = arenaModel.Image1;
                        context.ArenaDeckImages.Add(img);
                    }
                    context.Entry(img).CurrentValues.SetValues(arenaModel.Image1);
                    arena.Image1 = img;
                }
                if (arenaModel.Image2 != null)
                {
                    var img = context.ArenaDeckImages.Find(arenaModel.Image2.Id);
                    if (img == null)
                    {
                        img = arenaModel.Image2;
                        context.ArenaDeckImages.Add(img);
                    }
                    context.Entry(img).CurrentValues.SetValues(arenaModel.Image2);
                    arena.Image2 = img;
                }

                context.Entry(arena).CurrentValues.SetValues(arenaModel);
                SetEndDateIfNeeded(arena);
                arena.Modified = DateTime.Now;
                await context.SaveChangesAsync();

                arena = context.ArenaSessions.Query().First(x => x.Id == arena.Id);
                var latestId = context.ArenaSessions.OrderByDescending(x => x.StartDate).Select(x => x.Id).FirstOrDefault();
                arenaModel.InjectFrom(arena);
                events.PublishOnBackgroundThread(new ArenaSessionUpdated(arenaModel.Id, latestId == arena.Id));
            }
        }
Esempio n. 4
0
        public async Task <ArenaSessionModel> AddArenaSession(ArenaSessionModel arenaModel)
        {
            using (var context = dbContext())
            {
                var arena = new ArenaSession();
                arena.InjectFrom(arenaModel);
                if (arenaModel.Hero != null)
                {
                    arena.Hero = context.Heroes.Find(arenaModel.Hero.Id);
                }
                if (arenaModel.Image1 != null)
                {
                    var img = context.ArenaDeckImages.Find(arenaModel.Image1.Id);
                    if (img == null)
                    {
                        img = arenaModel.Image1;
                        context.ArenaDeckImages.Add(img);
                    }
                    context.Entry(img).CurrentValues.SetValues(arenaModel.Image1);
                    arena.Image1 = img;
                }
                if (arenaModel.Image2 != null)
                {
                    var img = context.ArenaDeckImages.Find(arenaModel.Image2.Id);
                    if (img == null)
                    {
                        img = arenaModel.Image2;
                        context.ArenaDeckImages.Add(img);
                    }
                    context.Entry(img).CurrentValues.SetValues(arenaModel.Image2);
                    arena.Image2 = img;
                }
                context.ArenaSessions.Add(arena);
                await context.SaveChangesAsync();

                arenaModel.InjectFrom(arena);
            }
            events.PublishOnBackgroundThread(new ArenaSessionAdded(arenaModel));
            return(arenaModel);
        }
Esempio n. 5
0
        public async Task AssignGameToArena(GameResultModel gameModel, ArenaSessionModel arenaModel)
        {
            using (var context = 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);
            }
        }