コード例 #1
0
 private static Game CreateGameModel(BattleBitsGame game)
 {
     var model = new Game {
         StartTime = game.StartTime,
         EndTime = game.EndTime
     };
     foreach (var score in game.Scores.Values) {
         model.Scores.Add(new Score {
             Value = score.Value,
             Time = score.Time,
             UserId = score.Player.UserId
         });
     }
     return model;
 }
コード例 #2
0
        public BattleBitsGame CreateGame(Action<BattleBitsGame> onGameStart, Action<BattleBitsGame, IList<BattleBitsScore>> onGameEnd)
        {
            if (CurrentGame != null || NextGame != null) {
                throw new Exception("Please wait for the current game to end.");
            }

            var date = DateTime.UtcNow;
            var game = new BattleBitsGame(CompetitionMeta.NumberCount) {
                StartTime = date + GameDelay,
                EndTime = date + GameDelay + CompetitionMeta.Duration
            };

            gameStartTimer = new Timer(state => {
                CurrentGame = game;
                NextGame = null;
                onGameStart(CurrentGame);
                gameStartTimer.Dispose();
                gameStartTimer = null;
            }, null, game.StartTime - DateTime.UtcNow, Timeout.InfiniteTimeSpan);

            gameEndTimer = new Timer(state => {
                using (var context = new CompetitionContext()) {
                    var competition = context.Competitions.FirstOrDefault(x => x.Id == CompetitionMeta.Id);
                    if (competition != null) {
                        competition.Games.Add(CreateGameModel(game));
                        context.SaveChanges();
                    }
                }
                onGameEnd(game, UpdateHighScores(game.Scores.Values));
                gameEndTimer.Dispose();
                gameEndTimer = null;
                PreviousGameScores = game.Scores.Values
                    .OrderByDescending(s => s.Value)
                    .ThenBy(s => s.Time).ToList();
                CurrentGame = null;
            }, null, game.EndTime - DateTime.UtcNow, Timeout.InfiniteTimeSpan);

            NextGame = game;
            return game;
        }