Beispiel #1
0
        public IEnumerable <ExtendedPlayer> ListPlayers(ListAllInput input)
        {
            var foostatsChannel = _channelFactory.CreateChannel();
            var async           = foostatsChannel.BeginListPlayers(input, null, null);

            return(foostatsChannel.EndListPlayers(async));
        }
Beispiel #2
0
        public IEnumerable <Match> ListMatches(ListAllInput input)
        {
            var foostatsChannel = _channelFactory.CreateChannel();
            var async           = foostatsChannel.BeginListMatches(input, null, null);

            return(foostatsChannel.EndListMatches(async));
        }
Beispiel #3
0
        public async Task <IEnumerable <ExtendedPlayer> > ListPlayersAsync(ListAllInput input)
        {
            var foostatsChannel = _channelFactory.CreateChannel();
            var task            = await Task <IEnumerable <ExtendedPlayer> > .Factory.FromAsync(
                foostatsChannel.BeginListPlayers,
                foostatsChannel.EndListPlayers,
                input,
                foostatsChannel);

            return(task);
        }
        public IEnumerable <ExtendedPlayer> ListAllOrderedBy(ListAllInput input)
        {
            using (var db = new FoostatsContext())
            {
                var players = db.Players;
                IEnumerable <ExtendedPlayer> extendedPlayers = null;

                List <Trueskill> trueskill = null;
                List <WinLoss>   winloss   = null;
                if (input.IncludeExtendedData)
                {
                    trueskill = db.Trueskill.Include(x => x.Player).ToList();
                    winloss   = db.WinLoss.Include(x => x.Player).ToList();
                }

                IQueryable <Player> queryAble = null;
                if (string.Compare(input.OrderBy, "DisplayName", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    queryAble = input.Desc ? players.OrderByDescending(x => x.MutableDisplayName) : players.OrderBy(x => x.MutableDisplayName);
                }
                else if (string.Compare(input.OrderBy, "Alias", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    queryAble = input.Desc ? players.OrderByDescending(x => x.DisplayName) : players.OrderBy(x => x.DisplayName);
                }
                else if (string.Compare(input.OrderBy, "Id", StringComparison.InvariantCultureIgnoreCase) == 0 ||
                         string.IsNullOrEmpty(input.OrderBy))
                {
                    queryAble = input.Desc ? players.OrderByDescending(x => x.Id) : players.OrderBy(x => x.Id);
                }
                else if (input.IncludeExtendedData && string.Compare(input.OrderBy, "Trueskill", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    // have to match first
                    extendedPlayers = players.ToList().Select(x => new ExtendedPlayer()
                    {
                        Id                 = x.Id,
                        DisplayName        = x.DisplayName,
                        MutableDisplayName = x.MutableDisplayName,
                        Password           = x.Password,
                        Salt               = x.Salt,
                        Trueskill          = trueskill.FirstOrDefault(y => y.Player.Id == x.Id),
                        WinLoss            = winloss.FirstOrDefault(z => z.Player.Id == x.Id)
                    });
                    extendedPlayers = input.Desc ? extendedPlayers.OrderByDescending(x => x.Trueskill == null ? 0 : x.Trueskill.ConservativeRating) :
                                      extendedPlayers.OrderBy(x => x => x.Trueskill == null ? 0 : x.Trueskill.ConservativeRating);
                }
                else if (input.IncludeExtendedData && string.Compare(input.OrderBy, "Trueskill", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    // have to match first
                    extendedPlayers = players.ToList().Select(x => new ExtendedPlayer()
                    {
                        Id                 = x.Id,
                        DisplayName        = x.DisplayName,
                        MutableDisplayName = x.MutableDisplayName,
                        Password           = x.Password,
                        Salt               = x.Salt,
                        Trueskill          = trueskill.FirstOrDefault(y => y.Player.Id == x.Id),
                        WinLoss            = winloss.FirstOrDefault(z => z.Player.Id == x.Id)
                    });
                    extendedPlayers = input.Desc ? extendedPlayers.OrderByDescending(x => (x.WinLoss.Win) / ((x.WinLoss.Win + x.WinLoss.Loss))) :
                                      extendedPlayers.OrderBy(x => (x.WinLoss.Win) / ((x.WinLoss.Win + x.WinLoss.Loss)));
                }
                else
                {
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        ReasonPhrase = string.Format("OrderBy value '{0}' is unknown.", input.OrderBy)
                    });
                }

                if (input.IncludeExtendedData && extendedPlayers == null)
                {
                    // we have a queryable to deal with then
                    extendedPlayers = queryAble.ToList().Select(x => new ExtendedPlayer()
                    {
                        Id                 = x.Id,
                        DisplayName        = x.DisplayName,
                        MutableDisplayName = x.MutableDisplayName,
                        Password           = x.Password,
                        Salt               = x.Salt,
                        Trueskill          = trueskill.FirstOrDefault(y => y.Player.Id == x.Id),
                        WinLoss            = winloss.FirstOrDefault(z => z.Player.Id == x.Id)
                    });
                }

                if (input.StartIndex != 0)
                {
                    if (extendedPlayers == null)
                    {
                        queryAble = queryAble.Skip(input.StartIndex - 1);
                    }
                    else
                    {
                        extendedPlayers = extendedPlayers.Skip(input.StartIndex - 1);
                    }
                }

                if (input.Limit != 0)
                {
                    if (extendedPlayers == null)
                    {
                        queryAble = queryAble.Take(input.Limit);
                    }
                    else
                    {
                        extendedPlayers = extendedPlayers.Take(input.Limit);
                    }
                }

                if (extendedPlayers == null)
                {
                    var playersResult = queryAble.ToList(); // resolve
                    extendedPlayers = playersResult.Select(x => new ExtendedPlayer()
                    {
                        Id                 = x.Id,
                        DisplayName        = x.DisplayName,
                        MutableDisplayName = x.MutableDisplayName,
                        Password           = x.Password,
                        Salt               = x.Salt
                    });
                }

                extendedPlayers = extendedPlayers.ToList(); // load

                // strip away player data
                foreach (var player in extendedPlayers)
                {
                    if (player.Trueskill != null)
                    {
                        player.Trueskill.Player = null;
                    }
                    if (player.WinLoss != null)
                    {
                        player.WinLoss.Player = null;
                    }
                }
                return(extendedPlayers);
            }
        }