예제 #1
0
 public Player(IUser user, string name, Action action)
 {
     this.user     = user;
     this.name     = name;
     currentAction = action;
     inventory     = new PlayerInventory(this);
 }
예제 #2
0
        public static async Task <Player> GetOrFetchPlayer(ulong id, DiscordSocketClient client)
        {
            if (PlayerHandler.HasPlayer(client.GetUser(id)))
            {
                return(PlayerHandler.GetPlayer(client.GetUser(id)));
            }

            MySqlConnection connection;

            MySqlCommand getUserFromId;

            connection = new MySqlConnection(connString);
            if (!await CanFetchPlayer(id))
            {
                return(null);
            }
            MySqlDataReader reader;

            using (connection)
            {
                getUserFromId = new MySqlCommand("SELECT * FROM users WHERE id = @id", connection);
                await connection.OpenAsync();

                getUserFromId.Parameters.AddWithValue("@id", id);
                getUserFromId.Prepare();
                reader = getUserFromId.ExecuteReader();
                using (reader)
                {
                    if (!reader.Read())
                    {
                        return(null);
                    }
                    IUser user = client.GetUser(id);
                    if (PlayerHandler.HasPlayer(user))
                    {
                        return(PlayerHandler.GetPlayer(user));
                    }
                    Player p = PlayerHandler.CreatePlayer(user, reader.GetString("name"));

                    p.SetAction(Action.GetActionFromName(reader.GetString("currentActionName"), p), false, true);

                    if (p.currentAction is ActionIdle == false)
                    {
                        p.currentAction.SetFinishTime(reader.GetDateTime("currentActionFinishTime"));
                    }

                    p.SetPreference("pm", reader.GetBoolean("preference_pm"));
                    p.SetPreference("mention", reader.GetBoolean("preference_mention"));

                    //await p.Tick();

                    return(p);
                }
            }
        }
예제 #3
0
 /// <summary>
 /// Sets the action that the player is currently doing
 /// </summary>
 /// <param name="action">The action to start</param>
 /// <param name="announce">Whether it should be announced to the player that they started this action</param>
 /// <param name="force">Whether the action only should be started if the player is idle (It will always be started if true)</param>
 /// <returns>True if the action of the player was changed, false otherwise</returns>
 public bool SetAction(Action action, bool announce, bool force)
 {
     if (force)
     {
         currentAction = action;
         return(true);
     }
     else
     {
         if (currentAction is ActionIdle)
         {
             currentAction = action;
             return(true);
         }
     }
     return(false);
 }
예제 #4
0
        public async Task Chop()
        {
            if (!await PlayerHandler.AttemptLogin(Context.User))
            {
                await ReplyAsync(Context.User.Mention + ": " + Modules.NOT_REGISTERED_MSG);

                return;
            }

            Player p = PlayerHandler.GetPlayer(Context.User);

            if (!p.IsIdle)
            {
                await ReplyAsync(Context.User.Mention + ": " + String.Format(Modules.ALREADY_ACTIVE_TIME_LEFT_FORMAT, p.currentAction.finishTime - DateTime.Now));

                return;
            }

            p.SetAction(new ActionChopWood(p), p.GetPreference <bool>("pm"), false);
            Action a = p.currentAction;

            await ReplyAsync(Context.User.Mention + ": " + a.GetStartedFormattingSecondPerson());
        }
예제 #5
0
 public Player(ulong id, DiscordSocketClient client, string name, Action action) : this(client.GetUser(id), name, action)
 {
 }