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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* This file is part of Kenshins Hide and Seek
*
* Copyright (c) 2021-2022 Tyler Murphy.
*
* Kenshins Hide and Seek free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* he Free Software Foundation version 3.
*
* Kenshins Hide and Seek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package net.tylermurphy.hideAndSeek.database;
import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.database.util.PlayerInfo;
import net.tylermurphy.hideAndSeek.game.Board;
import net.tylermurphy.hideAndSeek.game.util.WinType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.*;
import java.util.*;
public class GameDataTable {
private final Map<UUID, PlayerInfo> CACHE = new HashMap<>();
private final Database database;
protected GameDataTable(Database database) {
String sql = "CREATE TABLE IF NOT EXISTS hs_data (\n"
+ " uuid BINARY(16) PRIMARY KEY,\n"
+ " hider_wins int NOT NULL,\n"
+ " seeker_wins int NOT NULL,\n"
+ " hider_games int NOT NULL,\n"
+ " seeker_games int NOT NULL,\n"
+ " hider_kills int NOT NULL,\n"
+ " seeker_kills int NOT NULL,\n"
+ " hider_deaths int NOT NULL,\n"
+ " seeker_deaths int NOT NULL\n"
+ ");";
try(Connection connection = database.connect(); Statement statement = connection.createStatement()) {
statement.executeUpdate(sql);
} catch (SQLException e) {
Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage());
e.printStackTrace();
}
this.database = database;
}
@Nullable
public PlayerInfo getInfo(@Nullable UUID uuid) {
if (uuid == null) return null;
if(CACHE.containsKey(uuid)) return CACHE.get(uuid);
String sql = "SELECT * FROM hs_data WHERE uuid = ?;";
try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setBytes(1, database.encodeUUID(uuid));
ResultSet rs = statement.executeQuery();
if (rs.next()) {
PlayerInfo info = new PlayerInfo(
uuid,
rs.getInt("hider_wins"),
rs.getInt("seeker_wins"),
rs.getInt("hider_games"),
rs.getInt("seeker_games"),
rs.getInt("hider_kills"),
rs.getInt("seeker_kills"),
rs.getInt("hider_deaths"),
rs.getInt("seeker_deaths")
);
rs.close();
CACHE.put(uuid, info);
return info;
}
rs.close();
} catch (SQLException e) {
Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage());
e.printStackTrace();
}
return null;
}
@Nullable
public PlayerInfo getInfoRanking(@NotNull String order, int place) {
String sql = "SELECT * FROM hs_data ORDER BY "+order+" DESC LIMIT 1 OFFSET ?;";
try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setInt(1, place-1);
ResultSet rs = statement.executeQuery();
if (rs.next()) {
UUID uuid = database.decodeUUID(rs.getBytes("uuid"));
PlayerInfo info = new PlayerInfo(
uuid,
rs.getInt("hider_wins"),
rs.getInt("seeker_wins"),
rs.getInt("hider_games"),
rs.getInt("seeker_games"),
rs.getInt("hider_kills"),
rs.getInt("seeker_kills"),
rs.getInt("hider_deaths"),
rs.getInt("seeker_deaths")
);
rs.close();
CACHE.put(uuid, info);
return info;
}
rs.close();
} catch (SQLException e) {
Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage());
e.printStackTrace();
}
return null;
}
@Nullable
public List<PlayerInfo> getInfoPage(int page) {
String sql = "SELECT * FROM hs_data ORDER BY (hider_wins + seeker_wins) DESC LIMIT 10 OFFSET ?;";
try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setInt(1, (page-1)*10);
ResultSet rs = statement.executeQuery();
List<PlayerInfo> infoList = new ArrayList<>();
while(rs.next()) {
PlayerInfo info = new PlayerInfo(
database.decodeUUID(rs.getBytes("uuid")),
rs.getInt("hider_wins"),
rs.getInt("seeker_wins"),
rs.getInt("hider_games"),
rs.getInt("seeker_games"),
rs.getInt("hider_kills"),
rs.getInt("seeker_kills"),
rs.getInt("hider_deaths"),
rs.getInt("seeker_deaths")
);
infoList.add(info);
}
rs.close();
return infoList;
} catch (SQLException e) {
Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage());
e.printStackTrace();
}
return null;
}
@Nullable
public Integer getRanking(@NotNull String order, @NotNull UUID uuid) {
String sql = "SELECT count(*) AS total FROM hs_data WHERE "+order+" >= (SELECT "+order+" FROM hs_data WHERE uuid = ?) AND "+order+" > 0;";
try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setBytes(1, database.encodeUUID(uuid));
ResultSet rs = statement.executeQuery();
if (rs.next()) {
return rs.getInt("total");
}
rs.close();
} catch (SQLException e) {
Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage());
e.printStackTrace();
}
return null;
}
public void addWins(
@NotNull Board board,
@NotNull List<UUID> uuids,
@NotNull List<UUID> winners,
@NotNull Map<UUID,Integer> hider_kills,
@NotNull Map<UUID,Integer> hider_deaths,
@NotNull Map<UUID,Integer> seeker_kills,
@NotNull Map<UUID,Integer> seeker_deaths,
@NotNull WinType type
) {
for(UUID uuid : uuids) {
PlayerInfo info = getInfo(uuid);
if(info == null){
info = new PlayerInfo(uuid, 0, 0, 0, 0, 0, 0, 0, 0);
}
updateInfo(
database.encodeUUID(info.getUniqueId()),
info.getHiderWins() + (winners.contains(uuid) && type == WinType.HIDER_WIN ? 1 : 0),
info.getSeekerWins() + (winners.contains(uuid) && type == WinType.SEEKER_WIN ? 1 : 0),
info.getHiderGames() + (board.isHider(uuid) || (board.isSeeker(uuid) && winners.contains(uuid)) ? 1 : 0),
info.getSeekerGames() + (board.isSeeker(uuid) && winners.contains(uuid) ? 1 : 0),
info.getHiderKills() + hider_kills.getOrDefault(uuid, 0),
info.getSeekerKills() + seeker_kills.getOrDefault(uuid, 0),
info.getHiderDeaths() + hider_deaths.getOrDefault(uuid, 0),
info.getSeekerDeaths() + seeker_deaths.getOrDefault(uuid, 0)
);
}
}
protected void updateInfo(byte[] uuid, int hider_wins, int seeker_wins, int hider_games, int seeker_games, int hider_kills, int seeker_kills, int hider_deaths, int seeker_deaths){
String sql = "REPLACE INTO hs_data (uuid, hider_wins, seeker_wins, hider_games, seeker_games, hider_kills, seeker_kills, hider_deaths, seeker_deaths) VALUES (?,?,?,?,?,?,?,?,?)";
try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setBytes(1, uuid);
statement.setInt(2, hider_wins);
statement.setInt(3, seeker_wins);
statement.setInt(4, hider_games);
statement.setInt(5, seeker_games);
statement.setInt(6, hider_kills);
statement.setInt(7, seeker_kills);
statement.setInt(8, hider_deaths);
statement.setInt(9, seeker_deaths);
statement.execute();
} catch (SQLException e) {
Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage());
e.printStackTrace();
} finally {
CACHE.remove(database.decodeUUID(uuid));
}
}
}
|