Example #1
0
        public async Task LeaveGame(GenericGameCmd cmd)
        {
            var gameEntity = await _repository.GetGame(cmd.GameId);

            var game   = gameEntity.ToModel();
            var player = game.Players.WithUserId(GetUserId());

            game.Leave(player);

            if (game.Players.Any())
            {
                await _repository.ReplaceGame(GameEntity.Create(game));

                var leftEvent = new PlayerLeftEvent()
                {
                    Player = PlayerDto.Create(player),
                    GameId = game.Id
                };
                await Clients.Group(LobbyGroup).PlayerLeft(leftEvent);
            }
            else
            {
                await _repository.DeleteGame(game.Id);

                var removedEvent = new GameDestroyedEvent()
                {
                    GameId = game.Id
                };
                await Clients.Group(LobbyGroup).GameDestroyed(removedEvent);
            }
        }
Example #2
0
        private PlayerDto GetGoalie(int skaterId)
        {
            var skater = db.Goalies.Select(a => PlayerDto.Create(a)).FirstOrDefault(a => a.Id == skaterId);

            if (skater == null)
            {
                throw new Exception("goalie not found");
            }
            return(skater);
        }
Example #3
0
        public async Task JoinGame(GenericGameCmd cmd)
        {
            var gameEntity = await _repository.GetGame(cmd.GameId);

            var game   = gameEntity.ToModel();
            var player = CreatePlayerForCurrentUser();

            game.Join(player);
            await _repository.ReplaceGame(GameEntity.Create(game));

            var joinedEvent = new PlayerJoinedEvent
            {
                Player = PlayerDto.Create(player),
                GameId = game.Id
            };
            await Clients.Group(LobbyGroup).PlayerJoined(joinedEvent);
        }
Example #4
0
        public SearchResults GetSearchResults(string rawSearchInput)
        {
            string        search  = rawSearchInput.ToLowerInvariant().Trim();
            SearchResults results = new SearchResults()
            {
                SearchInput = search, Results = null
            };

            using (db = new BojoBoxContext())
            {
                List <PlayerDto> playerDtos = new List <PlayerDto>();

                var skaterNulls = db.Skaters.Where(a => a.Name == null).ToList();
                db.Skaters.RemoveRange(skaterNulls);

                var skaters = db.Skaters
                              .Where(a => a.Name != null && a.Name.ToLowerInvariant().Contains(search))
                              .Select(a => PlayerDto.Create(a))
                              .ToArray();

                var goalies = db.Goalies
                              .Where(a => a.Name != null && a.Name.ToLowerInvariant().Contains(search))
                              .Select(a => PlayerDto.Create(a))
                              .ToArray();

                foreach (var skater in skaters)
                {
                    skater.Acronym = "skater";
                }

                foreach (var goalie in goalies)
                {
                    goalie.Acronym = "goalie";
                }

                playerDtos.AddRange(skaters);
                playerDtos.AddRange(goalies);

                results.Results = playerDtos.OrderBy(a => a.Name).ToArray();
            }

            return(results);
        }
Example #5
0
        public async Task <IEnumerable <PlayerResultDto> > HandleFinishedGame(Game game)
        {
            var results = game.Players.Select(p => new PlayerResultDto
            {
                Player = PlayerDto.Create(p),
                Result = p.CalculateVictoryPoints()
            }).ToList();

            var entity = GameEntity.Create(game);

            foreach (var player in entity.Players)
            {
                player.Result = results.First(r => r.Player.UserId == player.UserId).Result;
            }
            // TODO: use redis ttl
            // entity.ttl = 60 * 60 * 24 * 7;  // Delete finished in a week
            await _repository.ReplaceGame(entity);

            return(results);
        }
Example #6
0
        public override async Task OnConnectedAsync()
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, LobbyGroup);

            var games = await _repository.GetStartedGamesByPlayer(GetUserId());

            foreach (var game in games)
            {
                await Clients.Caller.GameStarted(new GameStartedEvent { Game = GameSummaryDto.Create(game.ToModel()) });

                if (game.Status == GameStatus.ENDED)
                {
                    var endedEvent = new GameEndedEvent
                    {
                        GameId  = game.Id,
                        Results = game.Players.Select(p => new PlayerResultDto
                        {
                            Player = PlayerDto.Create(p),
                            Result = p.Result ?? 0
                        })
                    };
                    await Clients.Caller.GameEnded(endedEvent);
                }
            }

            var lobbyGames = await _repository.GetLobbyGames();

            foreach (var lobbyGame in lobbyGames)
            {
                var gameCreatedEvent = new GameCreatedEvent
                {
                    GameId   = lobbyGame.Id,
                    GameName = lobbyGame.Name,
                    Players  = lobbyGame.Players.Select(PlayerDto.Create),
                };
                await Clients.Caller.GameCreated(gameCreatedEvent);
            }
        }
        public async Task <PlayerDto> CreatePlayer([FromBody] CreatePlayerRequest request, CancellationToken cancellationToken)
        {
            var player = await _playerService.CreatePlayerAsync(request.Username, cancellationToken).ConfigureAwait(false);

            return(PlayerDto.Create(player));
        }
Example #8
0
        public StatTableDto GetCareerGoalieTable(StatParametersDto paramDto)
        {
            StatTableDto      statTableDto    = new StatTableDto();
            StatParametersDto cleanParameters = CleanCareerParameters(paramDto);

            cleanParameters.Col = cleanParameters.Col ?? 1;

            using (db = new BojoBoxContext())
            {
                LeagueDto  leagueDto = GetLeague(cleanParameters.League.Value);
                TeamDto    teamDto   = GetTeam(cleanParameters.Team.Value);
                List <int> teamIds   = null;
                if (teamDto != null)
                {
                    teamIds = GetFranchiseTeamIds(teamDto.Id);
                }

                statTableDto.Teams = GetTeamParameters(leagueDto.Id);

                statTableDto.HeaderText = leagueDto.Acronym + " - Career Stats";

                var goalieCareerQuery = db.GoalieSeasons.Where(a => a.LeagueId == leagueDto.Id);

                if (teamDto != null)
                {
                    goalieCareerQuery = goalieCareerQuery.Where(a => a.TeamId.HasValue && teamIds.Contains(a.TeamId.Value));
                }
                else
                {
                    goalieCareerQuery = goalieCareerQuery.Where(a => a.TeamId.HasValue);
                }

                bool isPlayoffs = CheckIfPlayoffs(cleanParameters.SeasonType.Value);
                goalieCareerQuery = goalieCareerQuery.Where(a => a.isPlayoffs == isPlayoffs);

                var idStatPairs = GetIdStatPairsCareer(cleanParameters.Col, goalieCareerQuery);
                idStatPairs = idStatPairs.GroupBy(a => a.Id, b => b.Stat, (x, y) => new IdStatPair(x, y.Sum()));

                idStatPairs = idStatPairs.OrderByDescending(a => a.Stat);
                // TODO: Check if to sort ascending

                int pageSize = 20;
                statTableDto.PageCount = GetPageCount(cleanParameters, idStatPairs);
                var selectedGoalieIds = idStatPairs.Select(b => b.Id).Skip(cleanParameters.Page.Value * pageSize).Take(pageSize).ToArray();

                var goalieRows = db.GoalieSeasons
                                 .Include(a => a.Goalie)
                                 .Where(a => selectedGoalieIds.Contains(a.GoalieId))
                                 .Where(a => a.TeamId.HasValue)
                                 .Where(a => a.LeagueId == leagueDto.Id)
                                 .Where(a => a.isPlayoffs == isPlayoffs);

                if (teamDto != null)
                {
                    goalieRows = goalieRows.Where(a => a.TeamId.HasValue && teamIds.Contains(a.TeamId.Value));
                }

                statTableDto.PlayerRows = goalieRows.GroupBy(a => a.Goalie, b => b, (skater, rows) => new PlayerTableRow()
                {
                    Player      = PlayerDto.Create(skater),
                    Season      = null,
                    Team        = null,
                    SeasonCount = rows.Select(a => a.Season).Distinct().Count(),
                    TeamCount   = rows.Where(x => x.GoalieId == skater.Id).Select(x => x.TeamId).Distinct().Count(),
                    Stats       = new int[]
                    {
                        rows.Sum(x => x.GamesPlayed),
                        rows.Sum(x => x.Wins),
                        rows.Sum(x => x.Losses),
                        rows.Sum(x => x.OvertimeLosses),
                        rows.Sum(x => x.Minutes),
                        rows.Sum(x => x.PenaltyMinutes),
                        rows.Sum(x => x.Shutouts),
                        rows.Sum(x => x.GoalsAgainst),
                        rows.Sum(x => x.ShotsAgainst),
                        rows.Sum(x => x.Assists),
                        rows.Sum(x => x.EmptyGoalAgainst),
                        rows.Sum(x => x.PenaltyShotSaves),
                        rows.Sum(x => x.PenaltyShotAttempts),
                        rows.Sum(x => x.Starts),
                        rows.Sum(x => x.Backups),
                    }
                })
                                          .ToList();

                statTableDto.PlayerRows = statTableDto.PlayerRows.OrderByDescending(a => a.Stats.ElementAt(cleanParameters.Col.Value)).ThenByDescending(a => a.Stats.ElementAt(4));
                AddRanks(statTableDto.PlayerRows, 1 + (cleanParameters.Page.Value * pageSize));
            }

            statTableDto.DisplayType    = "career";
            statTableDto.PlayerType     = "goalie";
            statTableDto.StatParameters = cleanParameters;
            return(statTableDto);
        }
Example #9
0
        public StatTableDto GetCareerSkaterTable(StatParametersDto paramDto)
        {
            StatTableDto      statTableDto    = new StatTableDto();
            StatParametersDto cleanParameters = CleanCareerParameters(paramDto);

            cleanParameters.Col = cleanParameters.Col ?? 3;

            using (db = new BojoBoxContext())
            {
                LeagueDto  leagueDto = GetLeague(cleanParameters.League.Value);
                TeamDto    teamDto   = GetTeam(cleanParameters.Team.Value);
                List <int> teamIds   = null;
                if (teamDto != null)
                {
                    teamIds = GetFranchiseTeamIds(teamDto.Id);
                }

                statTableDto.Teams = GetTeamParameters(leagueDto.Id);

                statTableDto.HeaderText = leagueDto.Acronym + " - Career Stats";

                var skaterCareerQuery = db.SkaterSeasons.Where(a => a.LeagueId == leagueDto.Id);

                if (teamDto != null)
                {
                    skaterCareerQuery = skaterCareerQuery.Where(a => a.TeamId.HasValue && teamIds.Contains(a.TeamId.Value));
                }
                else
                {
                    skaterCareerQuery = skaterCareerQuery.Where(a => a.TeamId.HasValue);
                }

                bool isPlayoffs = CheckIfPlayoffs(cleanParameters.SeasonType.Value);
                skaterCareerQuery = skaterCareerQuery.Where(a => a.isPlayoffs == isPlayoffs);

                var idStatPairs = GetIdStatPairsCareer(cleanParameters.Col, skaterCareerQuery);

                idStatPairs = idStatPairs.GroupBy(a => a.Id, b => b.Stat, (x, y) => new IdStatPair(x, y.Sum()));

                idStatPairs = idStatPairs.OrderByDescending(a => a.Stat);
                // TODO: Check if to sort ascending

                int pageSize = 20;
                statTableDto.PageCount = GetPageCount(cleanParameters, idStatPairs);
                var selectedSkaterIds = idStatPairs.Select(b => b.Id).Skip(cleanParameters.Page.Value * pageSize).Take(pageSize).ToArray();

                var skaterRows = db.SkaterSeasons
                                 .Include(a => a.Skater)
                                 .Where(a => selectedSkaterIds.Contains(a.SkaterId))
                                 .Where(a => a.TeamId.HasValue)
                                 .Where(a => a.LeagueId == leagueDto.Id)
                                 .Where(a => a.isPlayoffs == isPlayoffs);

                if (teamDto != null)
                {
                    skaterRows = skaterRows.Where(a => a.TeamId.HasValue && teamIds.Contains(a.TeamId.Value));
                }

                statTableDto.PlayerRows = skaterRows.GroupBy(a => a.Skater, b => b, (skater, rows) => new PlayerTableRow()
                {
                    Player      = PlayerDto.Create(skater),
                    Season      = null,
                    Team        = null,
                    SeasonCount = rows.Select(a => a.Season).Distinct().Count(),
                    TeamCount   = rows.Where(x => x.SkaterId == skater.Id).Select(x => x.TeamId).Distinct().Count(),
                    Stats       = new int[]
                    {
                        rows.Sum(x => x.GamesPlayed),
                        rows.Sum(x => x.Goals),
                        rows.Sum(x => x.Assists),
                        rows.Sum(x => x.Points),
                        rows.Sum(x => x.PlusMinus),
                        rows.Sum(x => x.PenaltyMinutes),
                        rows.Sum(x => x.PenaltyMajors),
                        rows.Sum(x => x.Hits),
                        rows.Sum(x => x.HitsTaken),
                        rows.Sum(x => x.Shots),
                        rows.Sum(x => x.OwnShotsBlocked),
                        rows.Sum(x => x.OwnShotsMissed),
                        rows.Sum(x => x.ShotsBlocked),
                        rows.Sum(x => x.MinutesPlayed),
                        rows.Sum(x => x.PPGoals),
                        rows.Sum(x => x.PPAssists),
                        rows.Sum(x => x.PPPoints),
                        rows.Sum(x => x.PPShots),
                        rows.Sum(x => x.PPMinutes),
                        rows.Sum(x => x.PKGoals),
                        rows.Sum(x => x.PKAssists),
                        rows.Sum(x => x.PKPoints),
                        rows.Sum(x => x.PKShots),
                        rows.Sum(x => x.PKMinutes),
                        rows.Sum(x => x.GameWinningGoals),
                        rows.Sum(x => x.GameTyingGoals),
                        rows.Sum(x => x.FaceoffWins),
                        rows.Sum(x => x.FaceoffsTotal),
                        rows.Sum(x => x.EmptyNetGoals),
                        rows.Sum(x => x.HatTricks),
                        rows.Sum(x => x.PenaltyShotGoals),
                        rows.Sum(x => x.PenaltyShotAttempts),
                        rows.Sum(x => x.FightsWon),
                        rows.Sum(x => x.FightsLost),
                        rows.Sum(x => x.FightsDraw)
                    }
                })
                                          .ToList();

                statTableDto.PlayerRows = statTableDto.PlayerRows.OrderByDescending(a => a.Stats.ElementAt(cleanParameters.Col.Value)).ThenByDescending(a => a.Stats.ElementAt(13));
                AddRanks(statTableDto.PlayerRows, 1 + (cleanParameters.Page.Value * pageSize));
            }

            statTableDto.DisplayType    = "career";
            statTableDto.PlayerType     = "skater";
            statTableDto.StatParameters = cleanParameters;
            return(statTableDto);
        }