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
| package com.meteor.activetop;
import com.meteor.activetop.storage.AbstractPlayerDataStorage; import com.meteor.activetop.util.Utils; import com.meteor.jellylib.mysql.FastMySQLStorage; import com.meteor.jellylib.mysql.column.Column; import com.meteor.jellylib.mysql.data.KeyValue; import org.bukkit.Bukkit;
import javax.security.sasl.SaslServer; import java.sql.*; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.ZoneId; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map;
public class RaceStore { private final String RACE_SCHEDULED_TABLE = "race_scheduled"; private final String RACE_DATA_TABLE = "race_data";
private ActiveTop plugin;
private Connection connection;
private long raceEndTime = 0L;
private RaceStore(ActiveTop plugin){ this.plugin = plugin; this.initDatabase(); this.initRace(); }
private void initDatabase(){ try { this.connection = DriverManager.getConnection(plugin.getConfig().getString("RaceStore.url")); String create_race_scheduled_tableQuery = SQL_MAPPER.CREATE_RACE_SCHEDULED_TABLE.getQuery(); create_race_scheduled_tableQuery = create_race_scheduled_tableQuery.replace("{table_name}",RACE_SCHEDULED_TABLE); executeSqlQuery(connection,create_race_scheduled_tableQuery); String create_race_dataQuery = SQL_MAPPER.CREATE_RACE_DATA_TABLE.getQuery(); create_race_dataQuery = create_race_dataQuery.replace("{table_name}",RACE_DATA_TABLE); executeSqlQuery(connection,create_race_dataQuery); } catch (SQLException e) { throw new RuntimeException(e); } plugin.getLogger().info("初始化竞赛数据库"); }
private long getLastEndTime(){ String select_current_race_endtimeQuery = SQL_MAPPER.SELECT_CURRENT_RACE_ENDTIME.getQuery(); select_current_race_endtimeQuery = select_current_race_endtimeQuery.replace("{table_name}",RACE_DATA_TABLE); try { PreparedStatement preparedStatement = this.connection.prepareStatement(select_current_race_endtimeQuery); ResultSet resultSet = preparedStatement.getResultSet(); while (resultSet.next()) Long.parseLong(resultSet.getString("value")); } catch (SQLException e) { throw new RuntimeException(e); } return -1; }
private boolean isExecuteClose(){ String is_execute_close_scheduledQuery = SQL_MAPPER.IS_EXECUTE_CLOSE_SCHEDULED.getQuery(); is_execute_close_scheduledQuery = is_execute_close_scheduledQuery.replace("{table_name}",RACE_SCHEDULED_TABLE); try { PreparedStatement preparedStatement = connection.prepareStatement(is_execute_close_scheduledQuery); preparedStatement.setLong(1,getRaceEndTime()); ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ int cnt = resultSet.getInt("cnt"); if(cnt>0) return true; return false; } } catch (SQLException e) { throw new RuntimeException(e); } return true; }
public static void executeSqlQuery(Connection connection,String sql,Object... args){ try (PreparedStatement preparedStatement = connection.prepareStatement(sql);){ for(int i=1;i<=args.length;i++) preparedStatement.setObject(i,args[i-1]); preparedStatement.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } }
public static ResultSet executeQuery(Connection connection,String sql,Object... args){ try (PreparedStatement preparedStatement = connection.prepareStatement(sql);){ for(int i=1;i<=args.length;i++) preparedStatement.setObject(i,args[i-1]); return preparedStatement.executeQuery(); } catch (SQLException e) { throw new RuntimeException(e); } }
private boolean closeReward(){ if(System.currentTimeMillis()>getRaceEndTime()){ if(!isExecuteClose()){ AbstractPlayerDataStorage.getInstance().closeReward(SQL_MAPPER.INSERT_EXECUTE_CLOSE_LOG .getQuery().replace("{table_name}",RACE_SCHEDULED_TABLE) .replace("{end_tme}",String.valueOf(getRaceEndTime())),getRaceEndTime()); } raceEndTime = Utils.countRaceEndTime(System.currentTimeMillis()); }
long delayTick = ((raceEndTime - System.currentTimeMillis())/1000)*20; plugin.getLogger().info("将在"+delayTick+"个tick后开始结算竞赛"); Bukkit.getScheduler().runTaskLaterAsynchronously(plugin,()->{ closeReward(); },delayTick+(60*20)); return true; }
private void initRace(){ if((this.raceEndTime = getLastEndTime())!=-1) closeReward(); else raceEndTime = Utils.countRaceEndTime(System.currentTimeMillis()); }
public void close(){ executeSqlQuery(connection,SQL_MAPPER.SAVE_ENDTIME.getQuery().replace("{table_name}",RACE_SCHEDULED_TABLE), raceEndTime,raceEndTime); try { connection.close(); } catch (SQLException e) { throw new RuntimeException(e); } }
public long getRaceEndTime() { return raceEndTime; } }
|