Skip to content

Commit b0369b8

Browse files
committed
[Fix] Fix a bug of get target player's ranking
1 parent 597cc46 commit b0369b8

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

src/main/java/jp/azisaba/lgw/kdstatus/KDStatusReloaded.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package jp.azisaba.lgw.kdstatus;
22

33
import java.io.File;
4-
import java.sql.Connection;
5-
import java.sql.PreparedStatement;
6-
import java.sql.SQLException;
7-
import java.util.logging.Level;
84

95
import jp.azisaba.lgw.kdstatus.sql.*;
106
import jp.azisaba.lgw.kdstatus.task.DBConnectionCheckTask;
@@ -58,35 +54,14 @@ public void onEnable() {
5854
sqlHandler = new SQLHandler(new File(getDataFolder(), "playerData.db"));
5955
kdDataContainer = new KillDeathDataContainer(new PlayerDataSQLController(sqlHandler).init());
6056

61-
this.kdData = new PlayerDataMySQLController(this);
62-
6357
DBAuthConfig.loadAuthConfig();
6458
sql = DBAuthConfig.getDatabase(getLogger(), 10);
6559

6660
sql.connect();
6761

68-
if(sql.isConnected()){
69-
getLogger().info("SQL Testing...");
70-
try(Connection conn = sql.getConnection();
71-
PreparedStatement pstmt = conn.prepareStatement("SELECT 1")) {
72-
if(pstmt.executeQuery().next()) {
73-
getLogger().info("SQL Test was success!");
74-
} else {
75-
getLogger().warning("Failed to test SQL Connection");
76-
}
77-
} catch (SQLException e) {
78-
getLogger().log(Level.SEVERE, "Error on SQL Testing", e);
79-
}
80-
getLogger().info("SQL Test is finished!");
81-
82-
getLogger().info("Connected SQLDatabase!");
83-
84-
//ここでテーブル作るぞ
85-
this.kdData.createTable();
86-
87-
getLogger().info("Table was created!");
62+
this.kdData = new PlayerDataMySQLController(sql, getLogger());
8863

89-
}
64+
this.kdData.init();
9065

9166
saveTask = new SavePlayerDataTask(this);
9267
saveTask.runTaskTimerAsynchronously(this, 20 * 60 * 3, 20 * 60 * 3);

src/main/java/jp/azisaba/lgw/kdstatus/sql/PlayerDataMySQLController.java

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,51 @@
33
import jp.azisaba.lgw.kdstatus.KDStatusReloaded;
44
import jp.azisaba.lgw.kdstatus.utils.TimeUnit;
55
import lombok.NonNull;
6+
import lombok.RequiredArgsConstructor;
7+
import org.slf4j.LoggerFactory;
68

79
import java.math.BigInteger;
810
import java.sql.*;
911
import java.util.ArrayList;
1012
import java.util.List;
1113
import java.util.UUID;
14+
import java.util.logging.Level;
15+
import java.util.logging.Logger;
1216

17+
@RequiredArgsConstructor
1318
public class PlayerDataMySQLController {
19+
private static final org.slf4j.Logger log = LoggerFactory.getLogger(PlayerDataMySQLController.class);
20+
private final HikariMySQLDatabase sql;
21+
private final Logger logger;
22+
23+
public void init() {
24+
if(sql.isConnected()){
25+
logger.info("SQL Testing...");
26+
try(Connection conn = sql.getConnection();
27+
PreparedStatement pstmt = conn.prepareStatement("SELECT 1")) {
28+
if(pstmt.executeQuery().next()) {
29+
logger.info("SQL Test was success!");
30+
} else {
31+
logger.warning("Failed to test SQL Connection");
32+
}
33+
} catch (SQLException e) {
34+
logger.log(Level.SEVERE, "Error on SQL Testing", e);
35+
}
36+
logger.info("SQL Test is finished!");
37+
38+
logger.info("Connected SQLDatabase!");
39+
40+
//ここでテーブル作るぞ
41+
createTable();
1442

15-
KDStatusReloaded plugin;
43+
logger.info("Table was created!");
1644

17-
public PlayerDataMySQLController(KDStatusReloaded plugin){ this.plugin = plugin; }
45+
}
46+
}
1847

1948
public void createTable(){
2049

21-
try(Connection conn = plugin.sql.getConnection();
50+
try(Connection conn = sql.getConnection();
2251
PreparedStatement ps = conn.prepareStatement("CREATE TABLE IF NOT EXISTS kill_death_data "
2352
+ "(uuid VARCHAR(64) NOT NULL ,name VARCHAR(36) NOT NULL," +
2453
"kills INT DEFAULT 0, " +
@@ -27,19 +56,19 @@ public void createTable(){
2756
"monthly_kills INT DEFAULT 0," +
2857
"yearly_kills INT DEFAULT 0," +
2958
"last_updated BIGINT DEFAULT -1 )")) {
30-
plugin.getLogger().info("Creating database table...");
59+
logger.info("Creating database table...");
3160

3261
ps.executeUpdate();
3362
ps.close();
34-
plugin.getLogger().info("Successfully to create database table!");
63+
logger.info("Successfully to create database table!");
3564

3665
}catch (SQLException e){e.printStackTrace();}
3766

3867
}
3968

4069
public boolean exist(UUID uuid){
4170

42-
try(Connection conn = plugin.sql.getConnection();
71+
try(Connection conn = sql.getConnection();
4372
PreparedStatement ps = conn.prepareStatement("SELECT * FROM kill_death_data WHERE name=?")) {
4473
ps.setString(1,uuid.toString());
4574

@@ -62,7 +91,7 @@ public void create(KDUserData data){
6291
if(exist(data.getUuid()))
6392
return;
6493

65-
try(Connection conn = plugin.sql.getConnection();
94+
try(Connection conn = sql.getConnection();
6695
PreparedStatement ps = conn.prepareStatement("INSERT INTO kill_death_data (uuid,name,kills,deaths,daily_kills,monthly_kills,yearly_kills,last_updated) VALUES (?,?,?,?,?,?,?,?)");) {
6796
ps.setString(1,data.getUuid().toString());
6897
ps.setString(2,data.getName());
@@ -82,7 +111,7 @@ public void create(KDUserData data){
82111

83112
public boolean update(KDUserData data){
84113

85-
try(Connection conn = plugin.sql.getConnection();
114+
try(Connection conn = sql.getConnection();
86115
PreparedStatement ps = conn.prepareStatement("UPDATE kill_death_data SET name=? ,kills=? ,deaths=? ,daily_kills=? ,monthly_kills=? ,yearly_kills=? ,last_updated=? WHERE uuid=?")) {
87116
ps.setString(8,data.getUuid().toString());
88117
ps.setString(1,data.getName());
@@ -105,7 +134,7 @@ public boolean update(KDUserData data){
105134

106135
public BigInteger getKills(@NonNull UUID uuid, @NonNull TimeUnit unit) {
107136

108-
try(Connection conn = plugin.sql.getConnection();
137+
try(Connection conn = sql.getConnection();
109138
PreparedStatement ps = conn.prepareStatement("SELECT "+ unit.getSqlColumnName() + " FROM kill_death_data WHERE uuid=?")) {
110139
ps.setString(1, uuid.toString());
111140

@@ -132,7 +161,7 @@ public BigInteger getDeaths(@NonNull UUID uuid) {
132161

133162
try {
134163

135-
PreparedStatement ps = plugin.sql.getConnection().prepareStatement("SELECT death FROM kill_death_data WHERE uuid=?");
164+
PreparedStatement ps = sql.getConnection().prepareStatement("SELECT death FROM kill_death_data WHERE uuid=?");
136165
ps.setString(1, uuid.toString());
137166

138167
ResultSet result = ps.executeQuery();
@@ -155,7 +184,7 @@ public BigInteger getDeaths(@NonNull UUID uuid) {
155184

156185
public String getName(@NonNull UUID uuid) {
157186

158-
try(Connection conn = plugin.sql.getConnection();
187+
try(Connection conn = sql.getConnection();
159188
PreparedStatement ps = conn.prepareStatement("SELECT name FROM kill_death_data WHERE uuid=?")) {
160189
ps.setString(1, uuid.toString());
161190

@@ -182,7 +211,7 @@ public long getLastUpdated(@NonNull UUID uuid) {
182211

183212
try {
184213

185-
PreparedStatement ps = plugin.sql.getConnection().prepareStatement("SELECT last_updated FROM kill_death_data WHERE uuid=?");
214+
PreparedStatement ps = sql.getConnection().prepareStatement("SELECT last_updated FROM kill_death_data WHERE uuid=?");
186215
ps.setString(1, uuid.toString());
187216

188217
ResultSet result = ps.executeQuery();
@@ -204,7 +233,7 @@ public long getLastUpdated(@NonNull UUID uuid) {
204233
}
205234

206235
public ResultSet getRawData(@NonNull UUID uuid) {
207-
try(Connection conn = plugin.sql.getConnection();
236+
try(Connection conn = sql.getConnection();
208237
PreparedStatement ps = conn.prepareStatement("SELECT * FROM kill_death_data WHERE uuid=?")) {
209238
ps.setString(1, uuid.toString());
210239
return ps.executeQuery();
@@ -220,7 +249,7 @@ public ResultSet getRawData(@NonNull UUID uuid) {
220249
* @return returns userdata. If failed, returns null.
221250
*/
222251
public KDUserData getUserData(@NonNull UUID uuid, @NonNull String name) {
223-
try(Connection conn = plugin.sql.getConnection();
252+
try(Connection conn = sql.getConnection();
224253
PreparedStatement ps = conn.prepareStatement("SELECT * FROM kill_death_data WHERE uuid=?")) {
225254
ps.setString(1, uuid.toString());
226255
ResultSet rs = ps.executeQuery();
@@ -246,15 +275,14 @@ public KDUserData getUserData(@NonNull UUID uuid, @NonNull String name) {
246275
return null;
247276
}
248277

249-
public static final String RANK_QUERY = "SELECT * FROM (SELECT uuid, ?, last_updated, RANK() over (ORDER BY ? DESC) as 'rank' FROM kill_death_data WHERE last_updated > ?) s WHERE s.uuid=?";
278+
public static final String RANK_QUERY = "SELECT * FROM (SELECT uuid, ${COLUMN_NAME}, last_updated, RANK() over (ORDER BY ${COLUMN_NAME} DESC) as 'rank' FROM kill_death_data WHERE last_updated > ?) s WHERE s.uuid=?";
250279

251280
public int getRank(UUID uuid,TimeUnit unit){
252-
try(Connection conn = plugin.sql.getConnection();
253-
PreparedStatement p = conn.prepareStatement(RANK_QUERY)) {
254-
p.setString(1, unit.getSqlColumnName());
255-
p.setString(2, unit.getSqlColumnName());
256-
p.setLong(3, TimeUnit.getFirstMilliSecond(unit));
257-
p.setString(4, uuid.toString());
281+
try(Connection conn = sql.getConnection();
282+
PreparedStatement p = conn.prepareStatement(RANK_QUERY.replace("${COLUMN_NAME}", unit.getSqlColumnName()))) {
283+
p.setLong(1, TimeUnit.getFirstMilliSecond(unit));
284+
p.setString(2, uuid.toString());
285+
logger.info("Executed query: " + p);
258286
ResultSet result = p.executeQuery();
259287
if(result.next()) {
260288
return result.getInt("rank");
@@ -270,7 +298,7 @@ public int getRank(UUID uuid,TimeUnit unit){
270298

271299
public List<KillRankingData> getTopKillRankingData(TimeUnit unit, int count){
272300

273-
try(Connection conn = plugin.sql.getConnection();
301+
try(Connection conn = sql.getConnection();
274302
PreparedStatement ps = conn.prepareStatement("select uuid, name, " + unit.getSqlColumnName()
275303
+ " from kill_death_data"
276304
+ " where last_updated >= ?"

0 commit comments

Comments
 (0)