diff options
| author | davidovski <david@davidovski.xyz> | 2025-10-11 04:55:51 +0100 |
|---|---|---|
| committer | davidovski <david@davidovski.xyz> | 2025-10-11 04:55:51 +0100 |
| commit | 10b327e9f6441a8863227ce3e075a5a587654065 (patch) | |
| tree | 5e94ecc91290febacf5181d3596563a7a24bd010 /src/main/java/net/uomc/mineshaft/resources/Resource.java | |
v0.3.1
Diffstat (limited to 'src/main/java/net/uomc/mineshaft/resources/Resource.java')
| -rw-r--r-- | src/main/java/net/uomc/mineshaft/resources/Resource.java | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/main/java/net/uomc/mineshaft/resources/Resource.java b/src/main/java/net/uomc/mineshaft/resources/Resource.java new file mode 100644 index 0000000..1cd508e --- /dev/null +++ b/src/main/java/net/uomc/mineshaft/resources/Resource.java @@ -0,0 +1,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(); + } +} |
