summaryrefslogtreecommitdiff
path: root/src/main/java/net/uomc/mineshaft/resources/Resource.java
blob: 1cd508ec60b5c8e0b5a70e06da1dd9ab9068bb63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package net.uomc.mineshaft.resources;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONObject;

import com.mouldycheerio.dbot.util.DatabaseUtils;
import com.mouldycheerio.dbot.util.PeelingUtils;

import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;

public class Resource extends JSONObject {

    private static final String SYMBOL = "symbol";
    private static final String NAME = "name";
    private static final String VALUE = "value";
    private ResourceManager resourceManager;
    private String tableName;

    public Resource(String name, String symbol, long value, ResourceManager resourceManager) {
        super();
        setName(name);
        setSymbol(symbol);
        setValue(value);
        this.resourceManager = resourceManager;
        initDB();

    }

    public Resource(JSONObject jsonObject, ResourceManager resourceManager) {
        super();
        this.resourceManager = resourceManager;
        jsonObject.keySet().forEach(k -> put(k, jsonObject.get(k)));
        initDB();
    }

    public void initDB() {
        tableName = getName().split(" ")[0];
        try {
            DatabaseUtils.createSimpleKVtable(resourceManager.getDatabasepath(), tableName);
        } catch (SQLException e) {
            // e.printStackTrace();
        }
    }

    public void set(Member member, long value) {
        try {
            DatabaseUtils.putInKVtable(resourceManager.getDatabasepath(), tableName, member.getId() + ":" + member.getGuild().getId(), value);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public long get(Member member) {
        try {
            return DatabaseUtils.getInKVtable(resourceManager.getDatabasepath(), tableName, member.getId() + ":" + member.getGuild().getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0l;
    }

    public long getValue(String memberid, String guildId) {
        try {
            return DatabaseUtils.getInKVtable(resourceManager.getDatabasepath(), tableName, memberid + ":" + guildId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0l;
    }

    public long total() {
        try {
            return DatabaseUtils.getTotal(resourceManager.getDatabasepath(), tableName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public List<String> getMembersGuilds() {
        try {
            return DatabaseUtils.listKeys(resourceManager.getDatabasepath(), tableName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    public List<String> getMembers(Guild guild) {
        return getMembersGuilds().stream().filter(m -> guild.getId().equals(m.split(":")[1])).map(m -> m.split(":")[0]).collect(Collectors.toList());
    }

    public void increment(Member member, long amount) {
        set(member, get(member) + amount);
    }

    public String getName() {
        return getString(NAME);
    }

    public String getPrettyName() {
        return Arrays.stream(getString(NAME).replaceAll("_", " ").split("\\s+"))
          .map(word -> word.substring(0, 1).toUpperCase() + word.substring(1))
          .collect(Collectors.joining(" "));
    }

    public void setName(String name) {
        put(NAME, name);
    }

    public String getSymbol() {
        return getString(SYMBOL);
    }

    public void setSymbol(String symbol) {
        put(SYMBOL, symbol);
    }

    public long getValue() {
        if (total() <= 0) {
            return -1;
        } else {
            return resourceManager.getPrimaryResource().total() / total();
        }
    }

    public void setValue(long value) {
        put(VALUE, value);
    }

    public String prettyValue(long value) {
        return PeelingUtils.amountToString(value) + getSymbol();
    }

    @Override
    public String toString() {
        return getName();
    }
}