· 7 years ago · Dec 12, 2018, 08:58 PM
1public class HubDatabase extends MySQL {
2
3 private final String permSuffix = "_perm_to_join";
4 private final String vanishedSuffix = "_vanished";
5
6 /**
7 * All methods used in this class are async and/or utilise asynchronous calls
8 */
9 public HubDatabase() {
10 super("localhost","development","root","password");
11 createLobbyTable();
12 createSelectorTable();
13 getLobbies().thenAcceptAsync(a -> Arrays.stream(a).forEach(this::createPermToJoinTable));
14 getLobbies().thenAcceptAsync(a -> Arrays.stream(a).forEach(this::createVanishedTable));
15 }
16
17 public void createLobbyTable() {
18 String sql = "CREATE TABLE IF NOT EXISTS lobby (" +
19 "lobby VARCHAR(36) NOT NULL," +
20 " PRIMARY KEY (lobby))";
21 operateAsync(PreparedStatement::execute, sql);
22 }
23
24 public CompletableFuture<Void> addLobby(String name) {
25 return isLobby(name).thenAcceptAsync(b -> {
26 if (b) return;
27 String sql = "INSERT INTO lobby (lobby) VALUES (?)";
28 operateAsync(PreparedStatement::execute, sql, name);
29 });
30 }
31
32 public CompletableFuture<Void> removeLobby(String name) {
33 return isLobby(name).thenAcceptAsync(b -> {
34 if (!b) return;
35 String sql = "DELETE FROM lobby WHERE lobby=" + name;
36 operateAsync(PreparedStatement::execute, sql);
37 });
38 }
39
40 public CompletableFuture<String[]> getLobbies() {
41 String sql = "SELECT * FROM lobby";
42 CompletableFuture<SQLResponse<ResultSet>> responseFuture = unclosedOperateAsync(PreparedStatement::executeQuery, sql);
43 List<String> list = new ArrayList<>();
44 CompletableFuture<Void> future = responseFuture.thenAcceptAsync(r -> {
45 if (!r.hasResponse()) return;
46 try {
47 while(r.getResponse().next()) {
48 list.add(r.getResponse().getString(1));
49 }
50 } catch (SQLException e) {
51 e.printStackTrace();
52 } finally {
53 close(r.getResponse());
54 }
55 });
56
57 return future.thenApplyAsync(v -> list.toArray(new String[list.size()]));
58 }
59
60 public CompletableFuture<Boolean> isLobby(String lobby) {
61 return getLobbies().thenApplyAsync(s -> Arrays.stream(s).anyMatch(l -> l.equalsIgnoreCase(lobby)));
62 }
63
64 public void createPermToJoinTable(String server) {
65 String sql = "CREATE TABLE IF NOT EXISTS " + server + permSuffix + " (" +
66 "slot INT NOT NULL," +
67 " title VARCHAR(36) DEFAULT NULL," +
68 " lore VARCHAR(36) DEFAULT NULL," +
69 " material VARCHAR(36) DEFAULT NULL," +
70 " perm VARCHAR(36) DEFAULT NULL," +
71 " allowed BOOLEAN NOT NULL DEFAULT 0," +
72 " PRIMARY KEY (slot))";
73 operateAsync(PreparedStatement::execute, sql);
74 }
75
76 public CompletableFuture<Void> addToPermToJoinTable(String server, int slot, String title, String lore, Material material, String permission, boolean allowed) {
77 String sql = "INSERT INTO " + server + permSuffix + " (slot, title, lore, material, perm, allowed) VALUES (?, ?, ?, ?, ?, ?)" +
78 " ON DUPLICATE KEY UPDATE " +
79 "slot=VALUES(slot)," +
80 " title=VALUES(title)," +
81 " lore=VALUES(lore)," +
82 " material=VALUES(material)," +
83 " perm=VALUES(perm)," +
84 " allowed=VALUES(allowed)";
85 return operateAsync(PreparedStatement::execute, sql, slot, title, lore, material.name(), permission, allowed).thenAcceptAsync(v -> {});
86 }
87
88 public CompletableFuture<Void> retrieveFromPermToJoinTable(String server, int slot) {
89 String sql = "SELECT slot FROM " + server + permSuffix + " WHERE slot=" + slot;
90 CompletableFuture<SQLResponse<ResultSet>> set = operateAsync(PreparedStatement::executeQuery, sql);
91 return isInPermTable(server, slot).thenAcceptAsync(b -> {
92 if (!b) return;
93 set.thenApply(s -> {
94 PermToJoinObject permToJoinObject = new PermToJoinObject();
95 try {
96 String title = operate(PreparedStatement::executeQuery, "SELECT title FROM " + server + permSuffix + " WHERE slot=" + slot)
97 .getResponse().getString(2);
98 String lore = operate(PreparedStatement::executeQuery, "SELECT lore FROM " + server + permSuffix + " WHERE slot=" + slot)
99 .getResponse().getString(3);
100 Material material = Material.matchMaterial(operate(PreparedStatement::executeQuery, "SELECT material FROM " + server + permSuffix + " WHERE slot=" + slot)
101 .getResponse().getString(4));
102 String perm = operate(PreparedStatement::executeQuery, "SELECT perm FROM " + server + permSuffix + " WHERE slot=" + slot)
103 .getResponse().getString(5);
104 boolean allowed = operate(PreparedStatement::executeQuery, "SELECT allowed FROM " + server + permSuffix + " WHERE slot=" + slot)
105 .getResponse().getBoolean(6);
106 permToJoinObject.setTitle(title);
107 permToJoinObject.setLore(lore);
108 permToJoinObject.setMaterial(material);
109 permToJoinObject.setPerm(perm);
110 permToJoinObject.setAllowed(allowed);
111
112 Optional<? extends PermToJoinObject> oPerm = DatabaseManager.getPerm().getRegistered().stream().filter(p -> p.getSlot() == slot).findFirst();
113 oPerm.ifPresent(DatabaseManager.getPerm()::unregister);
114 DatabaseManager.getPerm().register(permToJoinObject);
115
116 } catch (SQLException e) {
117 if (shouldPrintErrors()) e.printStackTrace();
118 }
119 return new SQLResponse<>(s);
120 });
121 });
122 }
123
124 public CompletableFuture<Boolean> isInPermTable(String server, int slot) {
125 String sql = "SELECT slot FROM " + server + permSuffix + " WHERE slot=" + slot;
126 CompletableFuture<SQLResponse<ResultSet>> set = operateAsync(PreparedStatement::executeQuery, sql);
127 return set.thenApplyAsync(s -> {
128 try {
129 return s.getResponse().isBeforeFirst();
130 } catch (SQLException e) {
131 if (shouldPrintErrors()) e.printStackTrace();
132 }
133 return false;
134 });
135 }
136
137 public void createVanishedTable(String server) {
138 String sql = "CREATE TABLE IF NOT EXISTS " + server + vanishedSuffix + " (" +
139 "uuid CHAR(36) NOT NULL," +
140 " PRIMARY KEY (uuid))";
141 operateAsync(PreparedStatement::execute, sql);
142 }
143
144 public CompletableFuture<String[]> getVanished(String server) {
145 String sql = "SELECT * FROM " + server + vanishedSuffix;
146 CompletableFuture<SQLResponse<ResultSet>> responseFuture = unclosedOperateAsync(PreparedStatement::executeQuery, sql);
147 List<String> list = new ArrayList<>();
148 CompletableFuture<Void> future = responseFuture.thenAcceptAsync(r -> {
149 if (!r.hasResponse()) return;
150 try {
151 while(r.getResponse().next()) {
152 list.add(r.getResponse().getString(1));
153 }
154 } catch (SQLException e) {
155 e.printStackTrace();
156 } finally {
157 close(r.getResponse());
158 }
159 });
160
161 return future.thenApplyAsync(v -> list.toArray(new String[list.size()]));
162 }
163
164 public CompletableFuture<Boolean> isVanished(OfflinePlayer offlinePlayer, String server) {
165 return getVanished(server).thenApplyAsync(s -> Arrays.stream(s).anyMatch(u -> u.equalsIgnoreCase(offlinePlayer.getUniqueId().toString())));
166 }
167
168 public CompletableFuture<Void> addToVanishedTable(OfflinePlayer offlinePlayer, String server) {
169 return isVanished(offlinePlayer, server).thenAcceptAsync(b -> {
170 if (b) return;
171 String sql = "INSERT INTO " + server + vanishedSuffix + " (uuid) VALUES (?)";
172 operateAsync(PreparedStatement::execute, sql, offlinePlayer.getUniqueId().toString());
173 });
174 }
175
176 public CompletableFuture<Void> removeFromVanishedTable(OfflinePlayer offlinePlayer, String server) {
177 return isVanished(offlinePlayer, server).thenAcceptAsync(b -> {
178 if (!b) return;
179 String sql = "DELETE FROM " + server + vanishedSuffix + " WHERE uuid=" + offlinePlayer.getUniqueId().toString();
180 operateAsync(PreparedStatement::execute, sql);
181 });
182 }
183
184 public void createSelectorTable() {
185 String sql = "CREATE TABLE IF NOT EXISTS selector (" +
186 "slot INT NOT NULL," +
187 " title VARCHAR(36) DEFAULT NULL," +
188 " lore VARCHAR(36) DEFAULT NULL," +
189 " material VARCHAR(36) DEFAULT NULL," +
190 " PRIMARY KEY (slot))";
191 operateAsync(PreparedStatement::execute, sql);
192 }
193
194 public void addToSelectorTable() {
195
196 }
197
198}