コード例 #1
0
        /// <summary>
        /// Finds all games and returns ViewModel to share that data with users
        /// </summary>
        /// <returns>ViewModel used to display information to the user</returns>
        public IndexGamesViewModel GetAllGames()
        {
            ICollection <GameSingleViewModel> collection = new List <GameSingleViewModel>();

            var games = this.context.Games.Where(g => g.RemovedOn == null);

            foreach (var game in games)
            {
                GameSingleViewModel gameVM = new GameSingleViewModel()
                {
                    Id          = game.Id,
                    Name        = game.Name,
                    Description = game.Description,
                    RequiredAge = game.RequiredAge
                };
                int score = SumGameScore(game.Id);
                gameVM.Score = score;
                collection.Add(gameVM);
            }

            var model = new IndexGamesViewModel()
            {
                Games = collection
            };

            return(model);
        }
コード例 #2
0
        /// <summary>
        /// Finds information about game by given id and returns ViewModel to share that data
        /// </summary>
        /// <param name="gameId"></param>
        /// <returns></returns>
        public GameSingleExpandedViewModel GetGame(int gameId)
        {
            Game gameContext = context.Games.Find(gameId);

            if (gameContext == null)
            {
                throw new IndexOutOfRangeException("Game[index] - indexOutOfRange");
            }

            ICollection <PlayerSingleViewModel> players = new List <PlayerSingleViewModel>();

            List <Scoreboard> certainGameBestPlayers = context.Scoreboards.Where(x => x.GameId == gameId).OrderBy(x => x.Score).ToList();

            foreach (var playerGame in certainGameBestPlayers)
            {
                if (context.Players.Find(playerGame.PlayerId).BeingBannedOn != null ||
                    context.Players.Find(playerGame.PlayerId).PlayerOptedOutOn != null ||
                    context.Games.Find(playerGame.GameId).RemovedOn != null)
                {
                    continue;
                }
                players.Add(new PlayerSingleViewModel()
                {
                    UserName  = context.Players.Find(playerGame.PlayerId).UserName,
                    FirstName = context.Players.Find(playerGame.PlayerId).UserName,
                    LastName  = context.Players.Find(playerGame.PlayerId).UserName,
                    Score     = playerGame.Score,
                    Id        = playerGame.PlayerId
                });
            }

            var game = new GameSingleViewModel()
            {
                Id          = gameContext.Id,
                Name        = gameContext.Name,
                Description = gameContext.Description,
                RequiredAge = gameContext.RequiredAge,
                IsRemoved   = context.Games.Find(gameId).RemovedOn != null
            };


            game.Score = SumGameScore(gameId);


            int signedId   = CurrentSignedPlayer.PlayerId;
            var detailGame = new GameSingleExpandedViewModel()
            {
                Players                  = players,
                Game                     = game,
                SignedInPlayerScore      = (signedId != 0 && context.Scoreboards.Find(signedId, gameId) != null)? context.Scoreboards.Find(signedId, gameId).Score : 0,
                CanSignedInPlayerCompete = /*signedId != 0 ? (context.Players.Find(signedId).BeingBannedOn == null && context.Players.Find(signedId).PlayerOptedOutOn == null) :*/ true
            };

            if (context.Scoreboards.Find(CurrentSignedPlayer.PlayerId, gameId) == null)
            {
                detailGame.IsCurrentPlayerAlreadySignedIn = false;
            }
            else
            {
                detailGame.IsCurrentPlayerAlreadySignedIn = true;
            }

            detailGame.Game.LoggedInId = CurrentSignedPlayer.PlayerId;

            return(detailGame);
        }