Esempio n. 1
0
 /// <summary>
 ///     check the rank of the specified user corresponds with their score.
 /// </summary>
 /// <param name="server">current server</param>
 /// <param name="user">user to modify</param>
 /// <param name="subject">user profile</param>
 /// <returns></returns>
 public async Task CheckRank(Servers.Server server, IUser user, Servers.Server.User subject)
 {
     foreach (var role in server.Ranks)
     {
         var u = user as IGuildUser;
         var r = Context.Guild.GetRole(role.RoleId);
         if (u != null && u.RoleIds.Contains(role.RoleId))
         {
             await u.RemoveRoleAsync(r);
         }
     }
     try
     {
         var toprole = server.Ranks.Where(x => x.Points <= subject.Points).Max(x => x.Points);
         var top     = server.Ranks.Where(x => x.Points == toprole);
         try
         {
             var first = top.FirstOrDefault();
             if (first != null)
             {
                 var newrole = Context.Guild.GetRole(first.RoleId);
                 await((IGuildUser)user).AddRoleAsync(newrole);
             }
         }
         catch
         {
             //role has been deleted
         }
     }
     catch
     {
         // No available roles
     }
 }
Esempio n. 2
0
        public async Task Initialise()
        {
            if (Servers.ServerList.All(x => x.ServerId != Context.Guild.Id))
            {
                var server = new Servers.Server
                {
                    ServerId = Context.Guild.Id,
                    UserList = new List <Servers.Server.User>()
                };

                Servers.ServerList.Add(server);
                await ReplyAsync("Server Initialised, users may now register");

                return;
            }

            await ReplyAsync("Server has already been initialised. Denied.");
        }
Esempio n. 3
0
        internal void Monitor()
        {
            _isMonitoring = true;
            _monitoringDownloader++;

            while (true)
            {
                BeatmapsetPackage p;
                lock (_pendingQueue)
                {
                    if (_pendingQueue.Count == 0)
                    {
                        _downloaderCount--;
                        GC.Collect();
                        break;
                    }
                    p = _pendingQueue.Dequeue();
                }

                while (true)
                {
                    if (p.Server == null)
                    {
                        try
                        {
                            p.Server = ChooseServer(p);
                        }
                        catch (NoServerToChoose e)
                        {
                            break;
                        }
                    }

                    byte[] data     = null;
                    string fileName = null;

                    try
                    {
                        Servers.Server server = GetServer((Server)p.Server);
                        if (!DownloadManager.IsServerVaild[(Server)p.Server])
                        {
                            throw new ServerNotAvailable();
                        }
                        data = server.Download(p, out fileName);
                        string s = _downloadMgr.FileWriter(data, fileName);
                        p.OnWriteFileComplete(new BeatmapsetPackage.WriteFileCompletedArg(s));
                        break;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        p.FailedServerList.Add((Server)p.Server);
                        p.Server = null;
                    }
                }
                GC.Collect();
            }

            _isMonitoring = false;
            _monitoringDownloader--;
        }
Esempio n. 4
0
        /// <summary>
        ///     For given server and users
        ///     if win = true
        ///     add one win and add given points
        ///     if win = false
        ///     add one loss and subtract given points
        /// </summary>
        /// <param name="server"> current server's object</param>
        /// <param name="users"> users to be modified.</param>
        /// <param name="win"> check if the provided users won or lost</param>
        /// <param name="points">default points for the user's to be modified.</param>
        /// <param name="gametext">Game text information</param>
        /// <returns></returns>
        public async Task WinLossPoints(Servers.Server server, List <IUser> users, bool win, int points,
                                        string gametext = null)
        {
            var embed = new EmbedBuilder();

            foreach (var user in users)
            {
                var usr = server.UserList.FirstOrDefault(x => x.UserId == user.Id);


                if (usr == null || user.Id != usr.UserId)
                {
                    continue;
                }
                {
                    //checks against possible ranks for each user.
                    //if the user has a rank that has a different point modifier to the server's one, then modify
                    //their points according to their rank
                    //if there is no role then ignore this.
                    points = win ? server.Winamount : server.Lossamount;
                    try
                    {
                        var toprole = server.Ranks.Where(x => x.Points <= usr.Points).OrderByDescending(x => x.Points);

                        try
                        {
                            var rank = toprole.First();
                            if (rank.WinModifier != 0 && win)
                            {
                                points = rank.WinModifier;
                            }
                            else if (rank.LossModifier != 0 && !win)
                            {
                                points = rank.LossModifier;
                            }
                        }
                        catch
                        {
                            //
                        }
                    }
                    catch
                    {
                        //
                    }


                    if (win)
                    {
                        usr.Points = usr.Points + points;
                        usr.Wins++;
                        embed.AddField($"{usr.Username} WON (+{points})", $"Points: **{usr.Points}**\n" +
                                       $"W/L: **[{usr.Wins}/{usr.Losses}]**");
                        embed.Color = Color.Green;
                    }
                    else
                    {
                        points     = Math.Abs(points);
                        usr.Points = usr.Points - points;
                        usr.Losses++;
                        if (usr.Points < 0)
                        {
                            usr.Points = 0;
                        }
                        embed.AddField($"{usr.Username} LOST (-{points})", $"Points: **{usr.Points}**\n" +
                                       $"W/L: **[{usr.Wins}/{usr.Losses}]**");
                        embed.Color = Color.Red;
                    }
                    try
                    {
                        await UserRename(server.UsernameSelection, user, usr.Username, usr.Points);
                    }
                    catch
                    {
                        //
                    }
                    await CheckRank(server, user, usr);
                }
            }

            if (gametext != null)
            {
                embed.Title = $"Game Decided: {gametext}";
            }

            await ReplyAsync("", false, embed.Build());
        }