Example #1
0
        private async Task <Data.Challenge> Sync(Data.Challenge entity, Task <GameState> task = null)
        {
            if (task is null)
            {
                task = Mojo.LoadGamespaceAsync(entity.Id);
            }

            try
            {
                var state = await task;

                Mapper.Map(state, entity);
            }
            catch (Exception ex)
            {
                entity.LastSyncTime = DateTimeOffset.Now;
                Logger.LogError(ex, "Sync error on {0} {1}", entity.Id, entity.Name);
            }

            await Store.Update(entity);

            return(entity);
        }
Example #2
0
        public async Task <Player> Start(SessionStartRequest model, bool sudo)
        {
            var team = await Store.ListTeamByPlayer(model.Id);

            var player = team.First();

            var game = await Store.DbContext.Games.FindAsync(player.GameId);

            if (!sudo && game.SessionLimit > 0)
            {
                var ts = DateTimeOffset.UtcNow;

                int sessionCount = await Store.DbSet
                                   .CountAsync(p =>
                                               p.GameId == game.Id &&
                                               ts < p.SessionEnd
                                               )
                ;

                if (sessionCount >= game.SessionLimit)
                {
                    throw new SessionLimitReached();
                }
            }

            if (!sudo && game.IsLive.Equals(false))
            {
                throw new GameNotActive();
            }

            if (
                !sudo &&
                game.RequireTeam &&
                team.Length < game.MinTeamSize
                )
            {
                throw new InvalidTeamSize();
            }

            var st = DateTimeOffset.UtcNow;
            var et = st.AddMinutes(game.SessionMinutes);

            foreach (var p in team)
            {
                p.SessionMinutes = game.SessionMinutes;
                p.SessionBegin   = st;
                p.SessionEnd     = et;
            }

            await Store.Update(team);

            if (player.Score > 0)
            {
                // insert _initialscore_ "challenge"
                var challenge = new Data.Challenge
                {
                    Id       = Guid.NewGuid().ToString("n"),
                    PlayerId = player.Id,
                    TeamId   = player.TeamId,
                    GameId   = player.GameId,
                    SpecId   = "_initialscore_",
                    Name     = "_initialscore_",
                    Points   = player.Score,
                    Score    = player.Score,
                };

                Store.DbContext.Add(challenge);

                await Store.DbContext.SaveChangesAsync();
            }

            return(Mapper.Map <Player>(
                       team.First(p => p.Id == model.Id)
                       ));
        }