예제 #1
0
        public async Task <Team[]> FindAllTeamsWhereTheUserHasRights(ClaimsPrincipal cp, LiteDbUser user)
        {
            var steamIdentities = await _selectedSteamIdentityService.FindAllFrom(user.Id);

            var allTeams = steamIdentities.Where(x => x.Team != null).Select(x => x.Team).Distinct().ToArray();

            foreach (var team in allTeams)
            {
                team.TeamSelectedSteamIdentities = (await _selectedSteamIdentityService.FindAllFrom(team.Id)).ToList();
            }
            if (cp.IsInRole("Admin") || cp.IsInRole("Mod") || cp.IsInRole("Captain"))
            {
                var tmpTeams = (await FindAll()).ToArray();
                foreach (var tmpTeam in tmpTeams)
                {
                    tmpTeam.TeamRole = "Admin";
                }
                return(tmpTeams.ToArray());
            }
            //Team Admins and Mods get added as mods to the server. So they don't need to get handled here.
            var tmpServer = new List <Team>();

            var ownSteamIdentityTeamSelectedSteamIdentity = steamIdentities.FirstOrDefault(x => x.SteamIdentity.LiteDbUser.Id == user.Id);

            if (ownSteamIdentityTeamSelectedSteamIdentity == null)
            {
                return(tmpServer.ToArray());
            }

            var ownSteamIdentity = ownSteamIdentityTeamSelectedSteamIdentity.SteamIdentity;

            foreach (var singleTeam in allTeams)
            {
                singleTeam.TeamRole = singleTeam.TeamSelectedSteamIdentities?
                                      .FirstOrDefault(x => x.SteamIdentity != null && x.SteamIdentity.Id == ownSteamIdentity.Id)?.RoleOverwrite;
            }

            return(allTeams.Where(x => x.TeamRole == "Admin" || x.TeamRole == "Captain" || x.TeamRole == "Mod").ToArray());
        }
        public async Task <bool> SaveMatchToService(MatchViewModel match, Match realmatch)
        {
            realmatch.Name        = match.Name;
            realmatch.MapId       = match.MapId;
            realmatch.GameMode    = match.GameMode;
            realmatch.TimeLimit   = match.TimeLimit;
            realmatch.PlayerSlots = match.PlayerSlots;
            realmatch.ScoreToEnd  = match.ScoreToEnd;

            var gotAnswer = GameModes.HasTeams.TryGetValue(realmatch.GameMode, out var hasTeams);

            if (gotAnswer)
            {
                if (hasTeams)
                {
                    realmatch.Team0 = await _teamService.FindOne((int)match.Team0Id);

                    realmatch.Team0.TeamSelectedSteamIdentities =
                        (await _teamSelectedSteamIdentityService.FindAllFrom(realmatch.Team0.Id)).ToList();
                    realmatch.Team1 = await _teamService.FindOne((int)match.Team1Id);

                    realmatch.Team1.TeamSelectedSteamIdentities =
                        (await _teamSelectedSteamIdentityService.FindAllFrom(realmatch.Team1.Id)).ToList();

                    // Check all steam identities
                    foreach (var team0SelectedSteamIdentity in match.MatchTeam0SelectedSteamIdentitiesStrings)
                    {
                        var tmp = realmatch.Team0.TeamSelectedSteamIdentities.FirstOrDefault(x =>
                                                                                             x.SteamIdentity.Id.ToString() == team0SelectedSteamIdentity);
                        if (tmp != null)
                        {
                            realmatch.MatchTeam0SelectedSteamIdentities.Add(new MatchTeamSelectedSteamIdentity
                            {
                                matchId         = realmatch.Id,
                                SteamIdentityId = team0SelectedSteamIdentity,
                                TeamId          = 0,
                                OverWriteRole   = tmp.RoleOverwrite
                            });
                        }
                        else
                        {
                            return(true);
                        }
                    }

                    foreach (var team1SelectedSteamIdentity in match.MatchTeam1SelectedSteamIdentitiesStrings)
                    {
                        var tmp = realmatch.Team1.TeamSelectedSteamIdentities.FirstOrDefault(x =>
                                                                                             x.SteamIdentity.Id.ToString() == team1SelectedSteamIdentity);
                        if (tmp != null)
                        {
                            realmatch.MatchTeam1SelectedSteamIdentities.Add(new MatchTeamSelectedSteamIdentity
                            {
                                matchId         = realmatch.Id,
                                SteamIdentityId = team1SelectedSteamIdentity,
                                TeamId          = 1,
                                OverWriteRole   = tmp.RoleOverwrite
                            });
                        }
                        else
                        {
                            return(true);
                        }
                    }
                }
                else
                {
                    foreach (var SelectedSteamIdentity in match.MatchSelectedSteamIdentitiesStrings)
                    {
                        var tmp = await _steamIdentityService.FindOne(SelectedSteamIdentity);

                        if (tmp != null)
                        {
                            realmatch.MatchSelectedSteamIdentities.Add(new MatchSelectedSteamIdentity
                            {
                                matchId         = realmatch.Id,
                                SteamIdentityId = SelectedSteamIdentity
                            });
                        }
                        else
                        {
                            return(true);
                        }
                    }
                }

                //When not a server is set!!!! or server already is running a match
                if (match.PavlovServerId <= 0)
                {
                    return(true);
                }
                realmatch.PavlovServer = await _pavlovServerService.FindOne(match.PavlovServerId);

                if (realmatch.PavlovServer == null)
                {
                    return(true);
                }
                realmatch.Status = match.Status;
            }
            else
            {
                return(true);
            }


            //Problem if i save here the MatchID upper is not set  until its an update:(

            var bla = await Upsert(realmatch);

            if (bla)
            {
                if (realmatch.MatchSelectedSteamIdentities.Count > 0)
                {
                    foreach (var matchSelectedSteamIdentity in realmatch.MatchSelectedSteamIdentities)
                    {
                        matchSelectedSteamIdentity.matchId = realmatch.Id;
                    }
                    // First remove Old TeamSelected and Match selected stuff
                    // Then write the new ones
                    await _matchSelectedSteamIdentitiesService.RemoveFromMatch(realmatch.Id);

                    await _matchSelectedSteamIdentitiesService.Upsert(realmatch.MatchSelectedSteamIdentities, match.Id);
                }

                if (realmatch.MatchTeam0SelectedSteamIdentities.Count > 0 || realmatch.MatchTeam1SelectedSteamIdentities.Count > 0)
                {
                    await _matchSelectedTeamSteamIdentitiesService.RemoveFromMatch(realmatch.Id);

                    foreach (var matchTeam0SelectedSteamIdentity in realmatch.MatchTeam0SelectedSteamIdentities)
                    {
                        matchTeam0SelectedSteamIdentity.matchId = realmatch.Id;
                    }
                    foreach (var matchTeam1SelectedSteamIdentity in realmatch.MatchTeam1SelectedSteamIdentities)
                    {
                        matchTeam1SelectedSteamIdentity.matchId = realmatch.Id;
                    }
                    if (realmatch.MatchTeam0SelectedSteamIdentities.Any())
                    {
                        await _matchSelectedTeamSteamIdentitiesService.Upsert(realmatch.MatchTeam0SelectedSteamIdentities, match.Id, 0);
                    }
                    if (realmatch.MatchTeam1SelectedSteamIdentities.Any())
                    {
                        await _matchSelectedTeamSteamIdentitiesService.Upsert(realmatch.MatchTeam1SelectedSteamIdentities, match.Id, 1);
                    }
                }

                return(true);
            }

            return(false);
        }