Création de la librairie Java PandacubeUtil
Utilisé par PandacubeBungee et PandacubeSpigot
This commit is contained in:
57
src/fr/pandacube/java/util/db/DBConnection.java
Normal file
57
src/fr/pandacube/java/util/db/DBConnection.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class DBConnection {
|
||||
JavaPlugin plugin;
|
||||
Connection conn;
|
||||
String url;
|
||||
String login;
|
||||
String pass;
|
||||
|
||||
public DBConnection(String host, int port, String dbname, String l, String p) throws ClassNotFoundException, SQLException {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
url = "jdbc:mysql://"+host+":"+port+"/"+dbname;
|
||||
login = l;
|
||||
pass = p;
|
||||
conn = DriverManager.getConnection(url, login, pass);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void reconnectIfNecessary() throws SQLException
|
||||
{
|
||||
try
|
||||
{
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.close();
|
||||
}
|
||||
catch(SQLException e)
|
||||
{
|
||||
close();
|
||||
conn = DriverManager.getConnection(url, login, pass);
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException
|
||||
{
|
||||
if (!conn.isValid(1))
|
||||
reconnectIfNecessary();
|
||||
return conn;
|
||||
}
|
||||
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (Exception e) { }
|
||||
|
||||
}
|
||||
|
||||
}
|
114
src/fr/pandacube/java/util/db/LoginHistoryElement.java
Normal file
114
src/fr/pandacube/java/util/db/LoginHistoryElement.java
Normal file
@@ -0,0 +1,114 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.UUID;
|
||||
|
||||
public class LoginHistoryElement extends SQLElement {
|
||||
|
||||
|
||||
private long time;
|
||||
private String playerId;
|
||||
private String ip;
|
||||
private ActionType actionType;
|
||||
private int nbOnline;
|
||||
|
||||
|
||||
public LoginHistoryElement(long t, UUID pId, InetAddress IP, ActionType action, int nbO) {
|
||||
super("pandacube_login_history");
|
||||
setTime(t);
|
||||
setPlayerId(pId);
|
||||
setIp(IP);
|
||||
setActionType(action);
|
||||
setNbOnline(nbO);
|
||||
}
|
||||
|
||||
LoginHistoryElement(int id, long t, String pId, String IP, ActionType action, int nbO) {
|
||||
super("pandacube_login_history", id);
|
||||
if (IP == null || pId == null)
|
||||
throw new IllegalArgumentException("pId et IP ne peuvent être null");
|
||||
setTime(t);
|
||||
playerId = pId;
|
||||
ip = IP;
|
||||
setActionType(action);
|
||||
setNbOnline(nbO);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getValues() {
|
||||
return new String[] {
|
||||
Long.toString(time),
|
||||
playerId,
|
||||
ip,
|
||||
actionType.toString(),
|
||||
Integer.toString(nbOnline)
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getFieldsName() {
|
||||
return new String[] {
|
||||
"time",
|
||||
"playerId",
|
||||
"ip",
|
||||
"actionType",
|
||||
"nbOnline"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
|
||||
public UUID getPlayerId() {
|
||||
return UUID.fromString(playerId);
|
||||
}
|
||||
|
||||
public void setPlayerId(UUID pId) {
|
||||
if (pId == null)
|
||||
throw new IllegalArgumentException("pId ne peut être null");
|
||||
playerId = pId.toString();
|
||||
}
|
||||
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(InetAddress addr) {
|
||||
if (addr == null)
|
||||
throw new IllegalArgumentException("addr ne peut être null");
|
||||
ip = addr.getHostAddress();
|
||||
}
|
||||
|
||||
|
||||
public ActionType getActionType() {
|
||||
return actionType;
|
||||
}
|
||||
|
||||
public void setActionType(ActionType actionT) {
|
||||
if (actionT == null)
|
||||
throw new IllegalArgumentException("actionT ne peut être null");
|
||||
actionType = actionT;
|
||||
}
|
||||
|
||||
|
||||
public int getNbOnline() {
|
||||
return nbOnline;
|
||||
}
|
||||
|
||||
public void setNbOnline(int nbOnline) {
|
||||
this.nbOnline = nbOnline;
|
||||
}
|
||||
|
||||
|
||||
public enum ActionType {
|
||||
LOGIN, LOGOUT
|
||||
}
|
||||
|
||||
}
|
36
src/fr/pandacube/java/util/db/LoginHistoryTable.java
Normal file
36
src/fr/pandacube/java/util/db/LoginHistoryTable.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import fr.pandacube.java.util.db.LoginHistoryElement.ActionType;
|
||||
|
||||
public class LoginHistoryTable extends SQLTable<LoginHistoryElement> {
|
||||
|
||||
public LoginHistoryTable() throws SQLException {
|
||||
super("pandacube_login_history");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createTableParameters() {
|
||||
return "id INT AUTO_INCREMENT PRIMARY KEY,"
|
||||
+ "time BIGINT NOT NULL,"
|
||||
+ "playerId CHAR(36) NOT NULL,"
|
||||
+ "ip VARCHAR(128) NOT NULL,"
|
||||
+ "actionType ENUM('LOGIN', 'LOGOUT') NOT NULL,"
|
||||
+ "nbOnline INT NOT NULL";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LoginHistoryElement getElementInstance(ResultSet sqlResult) throws SQLException {
|
||||
LoginHistoryElement el = new LoginHistoryElement(
|
||||
sqlResult.getInt("id"),
|
||||
sqlResult.getLong("time"),
|
||||
sqlResult.getString("playerId"),
|
||||
sqlResult.getString("ip"),
|
||||
ActionType.valueOf(sqlResult.getString("actionType")),
|
||||
sqlResult.getInt("nbOnline"));
|
||||
return el;
|
||||
}
|
||||
|
||||
}
|
63
src/fr/pandacube/java/util/db/MPGroupElement.java
Normal file
63
src/fr/pandacube/java/util/db/MPGroupElement.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import fr.pandacube.java.util.PlayerFinder;
|
||||
|
||||
public class MPGroupElement extends SQLElement {
|
||||
|
||||
private String groupName;
|
||||
|
||||
|
||||
|
||||
public MPGroupElement(String name) {
|
||||
super("pandacube_mp_group");
|
||||
setGroupName(name);
|
||||
}
|
||||
|
||||
protected MPGroupElement(int id, String name) {
|
||||
super("pandacube_mp_group", id);
|
||||
setGroupName(name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] getValues() {
|
||||
return new String[] {
|
||||
groupName
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getFieldsName() {
|
||||
return new String[] {
|
||||
"groupName"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getGroupName() { return groupName; }
|
||||
|
||||
public void setGroupName(String name) {
|
||||
if (name == null)
|
||||
throw new NullPointerException();
|
||||
if (!PlayerFinder.isValidPlayerName(name))
|
||||
throw new IllegalArgumentException("Le nom d'un groupe doit respecter le pattern d'un pseudo valide");
|
||||
groupName = name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<MPGroupUserElement> getUsers() throws SQLException {
|
||||
return ORM.getTable(MPGroupUserTable.class)
|
||||
.getAll("groupId = "+getId(), "id ASC", null, null);
|
||||
}
|
||||
|
||||
}
|
26
src/fr/pandacube/java/util/db/MPGroupTable.java
Normal file
26
src/fr/pandacube/java/util/db/MPGroupTable.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class MPGroupTable extends SQLTable<MPGroupElement> {
|
||||
|
||||
public MPGroupTable() throws SQLException {
|
||||
super("pandacube_mp_group");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createTableParameters() {
|
||||
return "id INT AUTO_INCREMENT PRIMARY KEY,"
|
||||
+ "groupName VARCHAR(16) NOT NULL";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MPGroupElement getElementInstance(ResultSet sqlResult)
|
||||
throws SQLException {
|
||||
return new MPGroupElement(
|
||||
sqlResult.getInt("id"),
|
||||
sqlResult.getString("groupName"));
|
||||
}
|
||||
|
||||
}
|
71
src/fr/pandacube/java/util/db/MPGroupUserElement.java
Normal file
71
src/fr/pandacube/java/util/db/MPGroupUserElement.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MPGroupUserElement extends SQLElement {
|
||||
|
||||
private int groupId;
|
||||
private String playerId;
|
||||
|
||||
|
||||
public MPGroupUserElement(int gId, UUID pId) {
|
||||
super("pandacube_mp_group_user");
|
||||
setGroupId(gId);
|
||||
setPlayerId(pId);
|
||||
}
|
||||
|
||||
protected MPGroupUserElement(int id, int gId, String pId) {
|
||||
super("pandacube_mp_group_user", id);
|
||||
setGroupId(gId);
|
||||
setPlayerId(UUID.fromString(pId));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] getValues() {
|
||||
return new String[] {
|
||||
Integer.toString(groupId),
|
||||
playerId
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getFieldsName() {
|
||||
return new String[] {
|
||||
"groupId",
|
||||
"playerId"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public int getGroupId() { return groupId; }
|
||||
public UUID getPlayerId() { return UUID.fromString(playerId); }
|
||||
|
||||
public void setGroupId(int gId) { groupId = gId; }
|
||||
public void setPlayerId(UUID pId) {
|
||||
if (pId == null)
|
||||
throw new NullPointerException();
|
||||
this.playerId = pId.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public PlayerElement getPlayerElement() throws SQLException {
|
||||
return ORM.getTable(PlayerTable.class)
|
||||
.getFirst("playerId LIKE '"+getPlayerId()+"'", "id ASC");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
42
src/fr/pandacube/java/util/db/MPGroupUserTable.java
Normal file
42
src/fr/pandacube/java/util/db/MPGroupUserTable.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MPGroupUserTable extends SQLTable<MPGroupUserElement> {
|
||||
|
||||
public MPGroupUserTable() throws SQLException {
|
||||
super("pandacube_mp_group_user");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createTableParameters() {
|
||||
return "id INT AUTO_INCREMENT PRIMARY KEY,"
|
||||
+ "groupId INT NOT NULL,"
|
||||
+ "playerId VARCHAR(36) NOT NULL";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MPGroupUserElement getElementInstance(ResultSet sqlResult)
|
||||
throws SQLException {
|
||||
return new MPGroupUserElement(
|
||||
sqlResult.getInt("id"),
|
||||
sqlResult.getInt("groupId"),
|
||||
sqlResult.getString("playerId"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retourne l'instance de MPGroupUserElement correcpondant à la présence d'un joueur dans un groupe
|
||||
* @param group le groupe concerné, sous forme d'instance de MPGroupElement
|
||||
* @param player l'identifiant du joueur
|
||||
* @return null si la correspondance n'a pas été trouvée
|
||||
* @throws SQLException
|
||||
*/
|
||||
public MPGroupUserElement getPlayerInGroup(MPGroupElement group, UUID player) throws SQLException {
|
||||
if (player == null || group == null) return null;
|
||||
return getFirst("groupId = "+group.getId()+" AND playerId = '"+player+"'", "id");
|
||||
}
|
||||
|
||||
}
|
177
src/fr/pandacube/java/util/db/MPMessageElement.java
Normal file
177
src/fr/pandacube/java/util/db/MPMessageElement.java
Normal file
@@ -0,0 +1,177 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Représente un message dans la base de donnée<br/>
|
||||
* <br/>
|
||||
* Les propriétés suivantes doivent être complétés hors constructeur (par défaut <code>null</code>) :
|
||||
* <ul>
|
||||
* <li><code>destNick</code></li>
|
||||
* <li>ou <code>destGroup</code></li>
|
||||
* </ul>
|
||||
* La propriété <code>deleted</code> est défini par défaut à Faux.
|
||||
* @author Marc Baloup
|
||||
*
|
||||
*/
|
||||
public class MPMessageElement extends SQLElement {
|
||||
|
||||
private long time;
|
||||
private int securityKey; // permet de différencier deux message, dans le cas où 2 messages ont exactement la même valeur time
|
||||
private String viewerId;
|
||||
private String sourceId;
|
||||
private String destId = null;
|
||||
private Integer destGroup = null;
|
||||
private String message;
|
||||
private boolean wasRead;
|
||||
private boolean deleted = false;
|
||||
private boolean serverSync;
|
||||
|
||||
|
||||
|
||||
public MPMessageElement(long t, int secKey, UUID viewId, UUID srcId, String msg, boolean r, boolean sync) {
|
||||
super("pandacube_mp_message");
|
||||
setTime(t);
|
||||
setSecurityKey(secKey);
|
||||
setViewerId(viewId);
|
||||
setSourceId(srcId);
|
||||
setMessage(msg);
|
||||
setRead(r);
|
||||
setServerSync(sync);
|
||||
}
|
||||
|
||||
|
||||
protected MPMessageElement(int id, long t, int secKey, String viewNick, String srcNick, String msg, boolean r, boolean sync) {
|
||||
super("pandacube_mp_message", id);
|
||||
setTime(t);
|
||||
setSecurityKey(secKey);
|
||||
setViewerId(UUID.fromString(viewNick));
|
||||
setSourceId((srcNick == null) ? null : UUID.fromString(srcNick));
|
||||
setMessage(msg);
|
||||
setRead(r);
|
||||
setServerSync(sync);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] getValues() {
|
||||
return new String[] {
|
||||
Long.toString(time),
|
||||
Integer.toString(securityKey),
|
||||
viewerId,
|
||||
sourceId,
|
||||
destId,
|
||||
(destGroup==null)?null:destGroup.toString(),
|
||||
message,
|
||||
(wasRead)?"1":"0",
|
||||
(deleted)?"1":"0",
|
||||
(serverSync)?"1":"0"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] getFieldsName() {
|
||||
return new String[] {
|
||||
"time",
|
||||
"securityKey",
|
||||
"viewerId",
|
||||
"sourceId",
|
||||
"destId",
|
||||
"destGroup",
|
||||
"message",
|
||||
"wasRead",
|
||||
"deleted",
|
||||
"serverSync"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public long getTime() { return time; }
|
||||
public int getSecurityKey() { return securityKey; }
|
||||
public UUID getViewerId() { return UUID.fromString(viewerId); }
|
||||
public UUID getSourceId() {
|
||||
if (sourceId == null) return null;
|
||||
return UUID.fromString(sourceId);
|
||||
}
|
||||
public UUID getDestId() {
|
||||
if (destId == null) return null;
|
||||
return UUID.fromString(destId);
|
||||
}
|
||||
public Integer getDestGroup() { return destGroup; }
|
||||
public String getMessage() { return message; }
|
||||
public boolean isRead() { return wasRead; }
|
||||
public boolean isDeleted() { return deleted; }
|
||||
public boolean isServerSync() { return serverSync; }
|
||||
|
||||
|
||||
|
||||
|
||||
public void setTime(long t) { time = t; }
|
||||
public void setSecurityKey(int secKey) { securityKey = secKey; }
|
||||
|
||||
public void setViewerId(UUID viewId) {
|
||||
if (viewId == null)
|
||||
throw new NullPointerException();
|
||||
viewerId = viewId.toString();
|
||||
}
|
||||
|
||||
public void setSourceId(UUID srcId) {
|
||||
if (srcId == null) sourceId = null;
|
||||
else sourceId = srcId.toString();
|
||||
}
|
||||
|
||||
public void setDestId(UUID destId) {
|
||||
if (destId == null) this.destId = null;
|
||||
else {
|
||||
this.destId = destId.toString();
|
||||
destGroup = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setDestGroup(Integer destGroup) {
|
||||
this.destGroup = destGroup;
|
||||
if (destGroup != null)
|
||||
destId = null;
|
||||
}
|
||||
|
||||
public void setMessage(String msg) {
|
||||
if (msg == null)
|
||||
throw new NullPointerException();
|
||||
message = msg;
|
||||
}
|
||||
|
||||
public void setRead(boolean r) { wasRead = r; }
|
||||
public void setDeleted(boolean del) { deleted = del; }
|
||||
public void setServerSync(boolean sync) { serverSync = sync; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public MPGroupElement getDestGroupElement() throws SQLException {
|
||||
if (getDestGroup() == null) return null;
|
||||
|
||||
return ORM.getTable(MPGroupTable.class).get(getDestGroup());
|
||||
}
|
||||
|
||||
}
|
99
src/fr/pandacube/java/util/db/MPMessageTable.java
Normal file
99
src/fr/pandacube/java/util/db/MPMessageTable.java
Normal file
@@ -0,0 +1,99 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.pandacube.java.util.PlayerFinder;
|
||||
|
||||
public class MPMessageTable extends SQLTable<MPMessageElement> {
|
||||
|
||||
public MPMessageTable() throws SQLException {
|
||||
super("pandacube_mp_message");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createTableParameters() {
|
||||
return "id INT AUTO_INCREMENT PRIMARY KEY,"
|
||||
+ "time BIGINT NOT NULL,"
|
||||
+ "securityKey INT NOT NULL,"
|
||||
+ "viewerId VARCHAR(36) NOT NULL,"
|
||||
+ "sourceId VARCHAR(36) NULL," // Null si la source est la console ou une autre entité qu'un joueur
|
||||
+ "destId VARCHAR(36) NULL,"
|
||||
+ "destGroup INT NULL,"
|
||||
+ "message VARCHAR(512) NOT NULL,"
|
||||
+ "wasRead TINYINT NOT NULL,"
|
||||
+ "deleted TINYINT NOT NULL,"
|
||||
+ "serverSync TINYINT NOT NULL";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MPMessageElement getElementInstance(ResultSet sqlResult)
|
||||
throws SQLException {
|
||||
MPMessageElement el = new MPMessageElement(
|
||||
sqlResult.getInt("id"),
|
||||
sqlResult.getLong("time"),
|
||||
sqlResult.getInt("securityKey"),
|
||||
sqlResult.getString("viewerId"),
|
||||
sqlResult.getString("sourceId"),
|
||||
sqlResult.getString("message"),
|
||||
sqlResult.getBoolean("wasRead"),
|
||||
sqlResult.getBoolean("serverSync"));
|
||||
String destId = sqlResult.getString("destId");
|
||||
el.setDestId(destId==null ? null : UUID.fromString(destId));
|
||||
|
||||
int group = sqlResult.getInt("destGroup");
|
||||
el.setDestGroup(sqlResult.wasNull()?null:group);
|
||||
|
||||
el.setDeleted(sqlResult.getBoolean("deleted"));
|
||||
|
||||
return el;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public List<MPMessageElement> getAllUnsyncMessage() throws SQLException {
|
||||
return getAll("serverSync = 0", "time ASC", null, null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<MPMessageElement> getAllUnreadForPlayer(UUID player) throws SQLException {
|
||||
return getForPlayer(player, true, null, false);
|
||||
}
|
||||
|
||||
|
||||
public List<MPMessageElement> getOneDiscussionForPlayer(UUID player, Object discussion, Integer numberLast, boolean revert) throws SQLException {
|
||||
if (player == null) return null;
|
||||
if (discussion != null && !(discussion instanceof String) && !(discussion instanceof UUID)) return null;
|
||||
if (discussion != null && discussion instanceof String && !PlayerFinder.isValidPlayerName(discussion.toString())) return null;
|
||||
|
||||
String where = "viewerId = '"+player+"'";
|
||||
if (discussion == null)
|
||||
where += " AND sourceId IS NULL AND destGroup IS NULL";
|
||||
else if (discussion instanceof String)
|
||||
where += " AND destGroup IN (SELECT id FROM "+ORM.getTable(MPGroupTable.class).getTableName()+" WHERE groupName LIKE '"+discussion+"')";
|
||||
else if (discussion instanceof UUID && discussion.equals(player))
|
||||
where += " AND destId LIKE '"+discussion+"' AND sourceId LIKE '"+discussion+"' AND destGroup IS NULL";
|
||||
else // discussion instanceof UUID
|
||||
where += " AND (destId LIKE '"+discussion+"' OR sourceId LIKE '"+discussion+"') AND destGroup IS NULL";
|
||||
|
||||
return getAll(where, (revert)?"time DESC":"time ASC", numberLast, null);
|
||||
}
|
||||
|
||||
|
||||
public List<MPMessageElement> getForPlayer(UUID player, boolean onlyUnread, Integer numberLast, boolean revert) throws SQLException {
|
||||
if (player == null) return null;
|
||||
|
||||
String where = "viewerId = '"+player+"'";
|
||||
if (onlyUnread)
|
||||
where += " AND wasRead = 0";
|
||||
|
||||
return getAll(where, (revert)?"time DESC":"time ASC", numberLast, null);
|
||||
}
|
||||
|
||||
|
||||
}
|
141
src/fr/pandacube/java/util/db/ModoHistoryElement.java
Normal file
141
src/fr/pandacube/java/util/db/ModoHistoryElement.java
Normal file
@@ -0,0 +1,141 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class ModoHistoryElement extends SQLElement {
|
||||
|
||||
private String modoId = null;
|
||||
private ActionType actionType;
|
||||
private long time;
|
||||
private String playerId;
|
||||
private Long value = null;
|
||||
private String message;
|
||||
|
||||
|
||||
public ModoHistoryElement(UUID modo, ActionType type, UUID player, String message) {
|
||||
super("pandacube_modo_history");
|
||||
setModoId(modo);
|
||||
setActionType(type);
|
||||
setPlayerId(player);
|
||||
setMessage(message);
|
||||
time = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
ModoHistoryElement(int id, String modo, ActionType type, String player, String message) {
|
||||
super("pandacube_modo_history", id);
|
||||
setModoId((modo == null)?null:UUID.fromString(modo));
|
||||
setActionType(type);
|
||||
setPlayerId(UUID.fromString(player));
|
||||
setMessage(message);
|
||||
time = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getValues() {
|
||||
return new String[] {
|
||||
modoId,
|
||||
actionType.name(),
|
||||
String.valueOf(time),
|
||||
playerId,
|
||||
(value == null)?null:value.toString(),
|
||||
message
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getFieldsName() {
|
||||
return new String[] {
|
||||
"modoId",
|
||||
"actionType",
|
||||
"time",
|
||||
"playerId",
|
||||
"value",
|
||||
"message"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
public UUID getModoId() {
|
||||
return modoId == null ? null : UUID.fromString(modoId);
|
||||
}
|
||||
|
||||
public void setModoId(UUID modo) {
|
||||
this.modoId = modo == null ? null : modo.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ActionType getActionType() {
|
||||
return actionType;
|
||||
}
|
||||
|
||||
public void setActionType(ActionType actionType) {
|
||||
if (actionType == null) throw new IllegalArgumentException("le paramètre ne peut être null");
|
||||
this.actionType = actionType;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retourne la durée de la sanction appliquée (en secondes), ou la somme d'argent retirée du compte
|
||||
* @return
|
||||
*/
|
||||
public long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Value correspond soit à la durée de la sanction appliquée (en secondes), soit à la valeur de l'amende appliquée
|
||||
* @param value
|
||||
*/
|
||||
public void setValue(Long value) {
|
||||
if (value != null && value.longValue() < 0) value = null;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public UUID getPlayerId() {
|
||||
return UUID.fromString(playerId);
|
||||
}
|
||||
|
||||
public void setPlayerId(UUID player) {
|
||||
if (player == null) throw new IllegalArgumentException("le paramètre ne peut être null");
|
||||
this.playerId = player.toString();
|
||||
}
|
||||
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
if (message == null) throw new IllegalArgumentException("le paramètre ne peut être null");
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public enum ActionType{
|
||||
BAN, UNBAN, MUTE, UNMUTE, REPORT, KICK
|
||||
}
|
||||
|
||||
}
|
40
src/fr/pandacube/java/util/db/ModoHistoryTable.java
Normal file
40
src/fr/pandacube/java/util/db/ModoHistoryTable.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import fr.pandacube.java.util.db.ModoHistoryElement.ActionType;
|
||||
|
||||
public class ModoHistoryTable extends SQLTable<ModoHistoryElement> {
|
||||
|
||||
|
||||
|
||||
public ModoHistoryTable() throws SQLException {
|
||||
super("pandacube_modo_history");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createTableParameters() {
|
||||
return "id INT AUTO_INCREMENT PRIMARY KEY,"
|
||||
+ "modoId CHAR(36) NULL," // null si c'est la console
|
||||
+ "actionType ENUM('BAN', 'UNBAN', 'MUTE', 'UNMUTE', 'REPORT', 'KICK') NOT NULL,"
|
||||
+ "time BIGINT NOT NULL,"
|
||||
+ "playerId CHAR(36) NOT NULL,"
|
||||
+ "value BIGINT NULL,"
|
||||
+ "message VARCHAR(512) NOT NULL";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ModoHistoryElement getElementInstance(ResultSet sqlResult) throws SQLException {
|
||||
ModoHistoryElement el = new ModoHistoryElement(
|
||||
sqlResult.getInt("id"),
|
||||
sqlResult.getString("modoId"),
|
||||
ActionType.valueOf(sqlResult.getString("actionType")),
|
||||
sqlResult.getString("playerId"),
|
||||
sqlResult.getString("message"));
|
||||
el.setValue(sqlResult.getLong("value"));
|
||||
el.setTime(sqlResult.getLong("time"));
|
||||
return el;
|
||||
}
|
||||
|
||||
}
|
86
src/fr/pandacube/java/util/db/ORM.java
Normal file
86
src/fr/pandacube/java/util/db/ORM.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <b>ORM = Object-Relational Mapping</b><br/>
|
||||
* Liste des tables avec leur classes :
|
||||
* <ul>
|
||||
* <li><code>LoginHistoryTable</code></li>
|
||||
* <li><code>ModoHistoryTable</code></li>
|
||||
* <li><code>StaffTicketTable</code></li>
|
||||
* <li><code>MPMessageTable</code></li>
|
||||
* <li><code>MPGroupTable</code></li>
|
||||
* <li><code>MPGroupUserTable</code></li>
|
||||
* <li><code>MPWebSessionTable</code></li>
|
||||
* <li><code>PlayerIgnoreTable</code></li>
|
||||
* </ul>
|
||||
* @author Marc Baloup
|
||||
*
|
||||
*/
|
||||
public final class ORM {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static List<SQLTable> tables = new ArrayList<SQLTable>();
|
||||
|
||||
/* package */ static DBConnection connection;
|
||||
|
||||
|
||||
public synchronized static void init(DBConnection conn) {
|
||||
try {
|
||||
|
||||
connection = conn;
|
||||
/*
|
||||
* Les tables SQL sont à instancier ici !
|
||||
*/
|
||||
|
||||
tables.add(new LoginHistoryTable());
|
||||
|
||||
tables.add(new ModoHistoryTable());
|
||||
|
||||
tables.add(new StaffTicketTable());
|
||||
|
||||
tables.add(new MPMessageTable());
|
||||
tables.add(new MPGroupTable());
|
||||
tables.add(new MPGroupUserTable());
|
||||
tables.add(new PlayerTable());
|
||||
|
||||
tables.add(new PlayerIgnoreTable());
|
||||
|
||||
tables.add(new ShopStockTable());
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public synchronized static <T extends SQLTable> T getTable(Class<T> c) {
|
||||
if (c == null) return null;
|
||||
for (SQLTable table : tables) {
|
||||
|
||||
if (c.isAssignableFrom(table.getClass())) {
|
||||
return c.cast(table);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private ORM() { } // rend la classe non instanciable
|
||||
|
||||
|
||||
}
|
163
src/fr/pandacube/java/util/db/PlayerElement.java
Normal file
163
src/fr/pandacube/java/util/db/PlayerElement.java
Normal file
@@ -0,0 +1,163 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerElement extends SQLElement {
|
||||
|
||||
private String playerId;
|
||||
private String token = null;
|
||||
private String mailCheck = null;
|
||||
private String password = null;
|
||||
private String mail = null;
|
||||
private String playerDisplayName;
|
||||
private long firstTimeInGame;
|
||||
private long timeWebRegister = 0;
|
||||
private long lastTimeInGame = 0;
|
||||
private long lastWebActivity = 0;
|
||||
private String onlineInServer = null;
|
||||
private String skinURL = null;
|
||||
private boolean isVanish = false;
|
||||
private Date birthday = null;
|
||||
private int lastYearCelebratedBirthday = 0;
|
||||
private Long banTimeout = null;
|
||||
private Long muteTimeout = null;
|
||||
private boolean isWhitelisted = false;
|
||||
|
||||
public PlayerElement(UUID pId, String dispName, long firstTimeIG, long lastWebAct, String onlineInServer) {
|
||||
super("pandacube_player");
|
||||
setPlayerId(pId);
|
||||
setOnlineInServer(onlineInServer);
|
||||
setLastWebActivity(lastWebAct);
|
||||
setPlayerDisplayName(dispName);
|
||||
setFirstTimeInGame(firstTimeIG);
|
||||
}
|
||||
|
||||
protected PlayerElement(int id, String pId, String dispName, long firstTimeIG, long lastWebAct, String onlineInServer) {
|
||||
super("pandacube_player", id);
|
||||
setPlayerId(UUID.fromString(pId));
|
||||
setOnlineInServer(onlineInServer);
|
||||
setLastWebActivity(lastWebAct);
|
||||
setPlayerDisplayName(dispName);
|
||||
setFirstTimeInGame(firstTimeIG);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getValues() {
|
||||
return new String[] {
|
||||
playerId,
|
||||
token,
|
||||
mailCheck,
|
||||
password,
|
||||
mail,
|
||||
playerDisplayName,
|
||||
Long.toString(firstTimeInGame),
|
||||
Long.toString(timeWebRegister),
|
||||
Long.toString(lastTimeInGame),
|
||||
Long.toString(lastWebActivity),
|
||||
onlineInServer,
|
||||
skinURL,
|
||||
isVanish?"1":"0",
|
||||
(birthday!=null)?birthday.toString():null,
|
||||
Integer.toString(lastYearCelebratedBirthday),
|
||||
(banTimeout!=null)?banTimeout.toString():null,
|
||||
(muteTimeout!=null)?muteTimeout.toString():null,
|
||||
isWhitelisted?"1":"0"
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getFieldsName() {
|
||||
return new String[] {
|
||||
"playerId",
|
||||
"token",
|
||||
"mailCheck",
|
||||
"password",
|
||||
"mail",
|
||||
"playerDisplayName",
|
||||
"firstTimeInGame",
|
||||
"timeWebRegister",
|
||||
"lastTimeInGame",
|
||||
"lastWebActivity",
|
||||
"onlineInServer",
|
||||
"skinURL",
|
||||
"isVanish",
|
||||
"birthday",
|
||||
"lastYearCelebratedBirthday",
|
||||
"banTimeout",
|
||||
"muteTimeout",
|
||||
"isWhitelisted"
|
||||
};
|
||||
}
|
||||
|
||||
public UUID getPlayerId() { return UUID.fromString(playerId); }
|
||||
public UUID getToken() { return (token == null) ? null : UUID.fromString(token); }
|
||||
public String getMailCheck() { return mailCheck; }
|
||||
public String getPasswordHash() { return password; }
|
||||
public String getMail() { return mail; }
|
||||
public long getFirstTimeInGame() { return firstTimeInGame; }
|
||||
public long getTimeWebRegister() { return timeWebRegister; }
|
||||
public long getLastTimeInGame() { return lastTimeInGame; }
|
||||
public long getLastWebActivity() { return lastWebActivity; }
|
||||
public String getOnlineInServer() { return onlineInServer; }
|
||||
public String getPlayerDisplayName() { return playerDisplayName; }
|
||||
public String getSkinURL() { return skinURL; }
|
||||
public boolean isVanish() { return isVanish; }
|
||||
public Date getBirthday() { return birthday; }
|
||||
public int getLastYearCelebratedBirthday() { return lastYearCelebratedBirthday; }
|
||||
public Long getBanTimeout() { return banTimeout; }
|
||||
public Long getMuteTimeout() { return muteTimeout; }
|
||||
public boolean isWhitelisted() { return isWhitelisted; }
|
||||
|
||||
|
||||
|
||||
public void setPlayerId(UUID pName) {
|
||||
if (pName == null)
|
||||
throw new NullPointerException();
|
||||
playerId = pName.toString();
|
||||
}
|
||||
|
||||
public void setToken(UUID t) {
|
||||
if (t == null)
|
||||
token = null;
|
||||
else
|
||||
token = t.toString();
|
||||
}
|
||||
|
||||
public void setMailCheck(String mCheck) { mailCheck = mCheck; }
|
||||
|
||||
public void setPasswordHash(String pass) { password = pass; }
|
||||
|
||||
public void setMail(String m) { mail = m; }
|
||||
|
||||
public void setFirstTimeInGame(long time) { firstTimeInGame = time; }
|
||||
|
||||
public void setTimeWebRegister(long time) { timeWebRegister = time; }
|
||||
|
||||
public void setLastTimeInGame(long time) { lastTimeInGame = time; }
|
||||
|
||||
public void setLastWebActivity(long time) { lastWebActivity = time; }
|
||||
|
||||
public void setOnlineInServer(String onlineInServer) { this.onlineInServer = onlineInServer; }
|
||||
|
||||
public void setSkinURL(String skinURL) { this.skinURL = skinURL; }
|
||||
|
||||
public void setPlayerDisplayName(String dispName) {
|
||||
if (dispName == null)
|
||||
throw new NullPointerException();
|
||||
playerDisplayName = dispName;
|
||||
}
|
||||
|
||||
public void setVanish(boolean v) { isVanish = v; }
|
||||
|
||||
public void setBirthday(Date b) { birthday = b; }
|
||||
|
||||
public void setLastYearCelebratedBirthday(int y) { lastYearCelebratedBirthday = y; }
|
||||
|
||||
public void setBanTimeout(Long banT) { banTimeout = banT; }
|
||||
|
||||
public void setMuteTimeout(Long muteT) { muteTimeout = muteT; }
|
||||
|
||||
public void setWhitelisted(boolean w) { isWhitelisted = w; }
|
||||
|
||||
}
|
66
src/fr/pandacube/java/util/db/PlayerIgnoreElement.java
Normal file
66
src/fr/pandacube/java/util/db/PlayerIgnoreElement.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerIgnoreElement extends SQLElement {
|
||||
|
||||
private String ignore;
|
||||
private String ignored;
|
||||
|
||||
|
||||
public PlayerIgnoreElement(UUID ignore, UUID ignored) {
|
||||
super("pandacube_player_ignore");
|
||||
setIgnore(ignore);
|
||||
setIgnored(ignored);
|
||||
}
|
||||
|
||||
|
||||
protected PlayerIgnoreElement(int id, String ignore, String ignored) {
|
||||
super("pandacube_player_ignore", id);
|
||||
this.ignore = ignore;
|
||||
this.ignored = ignored;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] getValues() {
|
||||
return new String[] {
|
||||
ignore,
|
||||
ignored
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] getFieldsName() {
|
||||
return new String[] {
|
||||
"ignorer",
|
||||
"ignored"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public UUID getIgnore() {
|
||||
return UUID.fromString(ignore);
|
||||
}
|
||||
|
||||
|
||||
public void setIgnore(UUID i) {
|
||||
if (i == null)
|
||||
throw new IllegalArgumentException("i can't be null");
|
||||
ignore = i.toString();
|
||||
}
|
||||
|
||||
|
||||
public UUID getIgnored() {
|
||||
return UUID.fromString(ignored);
|
||||
}
|
||||
|
||||
|
||||
public void setIgnored(UUID i) {
|
||||
if (i == null)
|
||||
throw new IllegalArgumentException("i can't be null");
|
||||
ignored = i.toString();
|
||||
}
|
||||
|
||||
}
|
79
src/fr/pandacube/java/util/db/PlayerIgnoreTable.java
Normal file
79
src/fr/pandacube/java/util/db/PlayerIgnoreTable.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerIgnoreTable extends SQLTable<PlayerIgnoreElement> {
|
||||
|
||||
public PlayerIgnoreTable() throws SQLException {
|
||||
super("pandacube_player_ignore");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createTableParameters() {
|
||||
return "id INT AUTO_INCREMENT PRIMARY KEY,"
|
||||
+ "ignorer CHAR(36) NOT NULL,"
|
||||
+ "ignored CHAR(36) NOT NULL";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PlayerIgnoreElement getElementInstance(ResultSet sqlResult) throws SQLException {
|
||||
return new PlayerIgnoreElement(sqlResult.getInt("id"),
|
||||
sqlResult.getString("ignorer"),
|
||||
sqlResult.getString("ignored"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<UUID> getListIgnoredPlayer(UUID ignore) throws SQLException {
|
||||
if (ignore == null)
|
||||
throw new IllegalArgumentException("ignore can't be null");
|
||||
|
||||
List<PlayerIgnoreElement> dbIgnored = getAll("ignorer = '"+ignore+"'", "id", null, null);
|
||||
|
||||
List<UUID> ret = new ArrayList<UUID>();
|
||||
|
||||
for (PlayerIgnoreElement el : dbIgnored) {
|
||||
ret.add(el.getIgnored());
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public boolean isPlayerIgnoringPlayer(UUID ignore, UUID ignored) throws SQLException {
|
||||
if (ignore == null)
|
||||
throw new IllegalArgumentException("ignore can't be null");
|
||||
if (ignored == null)
|
||||
throw new IllegalArgumentException("ignored can't be null");
|
||||
|
||||
return getFirst("ignorer = '"+ignore+"' AND ignored = '"+ignored+"'", "id") != null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void setPlayerIgnorePlayer(UUID ignore, UUID ignored, boolean set) throws SQLException {
|
||||
if (ignore == null)
|
||||
throw new IllegalArgumentException("ignore can't be null");
|
||||
if (ignored == null)
|
||||
throw new IllegalArgumentException("ignored can't be null");
|
||||
if (ignore.equals(ignored)) // on ne peut pas s'auto ignorer
|
||||
return;
|
||||
|
||||
PlayerIgnoreElement el = getFirst("ignorer = '"+ignore+"' AND ignored = '"+ignored+"'", "id");
|
||||
|
||||
if (set && el == null) {
|
||||
el = new PlayerIgnoreElement(ignore, ignored);
|
||||
el.save();
|
||||
}
|
||||
else if (!set && el != null) {
|
||||
el.delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
70
src/fr/pandacube/java/util/db/PlayerTable.java
Normal file
70
src/fr/pandacube/java/util/db/PlayerTable.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerTable extends SQLTable<PlayerElement> {
|
||||
|
||||
public PlayerTable() throws SQLException {
|
||||
super("pandacube_player");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createTableParameters() {
|
||||
return "id INT AUTO_INCREMENT PRIMARY KEY,"
|
||||
+ "playerId CHAR(36) NOT NULL,"
|
||||
+ "token CHAR(36) NULL,"
|
||||
+ "mailCheck VARCHAR(255) NULL,"
|
||||
+ "password VARCHAR(255) NULL,"
|
||||
+ "mail VARCHAR(255) NULL,"
|
||||
+ "playerDisplayName VARCHAR(255) NOT NULL,"
|
||||
+ "firstTimeInGame BIGINT NOT NULL,"
|
||||
+ "timeWebRegister BIGINT NULL,"
|
||||
+ "lastTimeInGame BIGINT NULL,"
|
||||
+ "lastWebActivity BIGINT NOT NULL,"
|
||||
+ "onlineInServer VARCHAR(32) NULL,"
|
||||
+ "skinURL VARCHAR(255) NULL,"
|
||||
+ "isVanish TINYINT NULL,"
|
||||
+ "birthday DATE NULL,"
|
||||
+ "lastYearCelebratedBirthday INT NOT NULL DEFAULT 0,"
|
||||
+ "banTimeout BIGINT NULL,"
|
||||
+ "muteTimeout BIGINT NULL,"
|
||||
+ "isWhitelisted TINYINT NOT NULL DEFAULT 0";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PlayerElement getElementInstance(ResultSet sqlResult)
|
||||
throws SQLException {
|
||||
PlayerElement el = new PlayerElement(
|
||||
sqlResult.getInt("id"),
|
||||
sqlResult.getString("playerId"),
|
||||
sqlResult.getString("playerDisplayName"),
|
||||
sqlResult.getLong("firstTimeInGame"),
|
||||
sqlResult.getLong("lastWebActivity"),
|
||||
sqlResult.getString("onlineInServer"));
|
||||
String token = sqlResult.getString("token");
|
||||
el.setToken((token == null) ? null : UUID.fromString(token));
|
||||
el.setMailCheck(sqlResult.getString("mailCheck"));
|
||||
el.setPasswordHash(sqlResult.getString("password"));
|
||||
el.setMail(sqlResult.getString("mail"));
|
||||
el.setFirstTimeInGame(sqlResult.getLong("firstTimeInGame"));
|
||||
el.setTimeWebRegister(sqlResult.getLong("timeWebRegister"));
|
||||
el.setLastTimeInGame(sqlResult.getLong("lastTimeInGame"));
|
||||
el.setSkinURL(sqlResult.getString("skinURL"));
|
||||
el.setVanish(sqlResult.getBoolean("isVanish"));
|
||||
el.setBirthday(sqlResult.getDate("birthday"));
|
||||
el.setLastYearCelebratedBirthday(sqlResult.getInt("lastYearCelebratedBirthday"));
|
||||
|
||||
long longVal;
|
||||
|
||||
longVal = sqlResult.getLong("banTimeout");
|
||||
el.setBanTimeout(sqlResult.wasNull()?null:longVal);
|
||||
longVal = sqlResult.getLong("muteTimeout");
|
||||
el.setMuteTimeout(sqlResult.wasNull()?null:longVal);
|
||||
|
||||
el.setWhitelisted(sqlResult.getBoolean("isWhitelisted"));
|
||||
return el;
|
||||
}
|
||||
|
||||
}
|
160
src/fr/pandacube/java/util/db/SQLElement.java
Normal file
160
src/fr/pandacube/java/util/db/SQLElement.java
Normal file
@@ -0,0 +1,160 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
public abstract class SQLElement {
|
||||
|
||||
DBConnection db = ORM.connection;
|
||||
|
||||
|
||||
private boolean saved = false;
|
||||
|
||||
protected final String tableName;
|
||||
|
||||
// champ relatif aux données
|
||||
private int id = 0;
|
||||
|
||||
|
||||
|
||||
public SQLElement(String name) {
|
||||
tableName = name;
|
||||
saved = false;
|
||||
}
|
||||
protected SQLElement(String name, int id) {
|
||||
tableName = name;
|
||||
this.id = id;
|
||||
saved = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void save() {
|
||||
|
||||
try {
|
||||
Connection conn;
|
||||
conn = db.getConnection();
|
||||
|
||||
String[] fields = getFieldsName(), values = getValues();
|
||||
|
||||
|
||||
|
||||
if (saved)
|
||||
{ // mettre à jour les valeurs dans la base
|
||||
String sql = "";
|
||||
for (int i=0; i<fields.length && i<values.length; i++)
|
||||
{
|
||||
sql += fields[i]+" = ? ,";
|
||||
}
|
||||
|
||||
if (sql.length() > 0)
|
||||
sql = sql.substring(0, sql.length()-1);
|
||||
|
||||
PreparedStatement st = conn.prepareStatement("UPDATE "+tableName+" SET "+sql+" WHERE id="+id);
|
||||
try {
|
||||
for (int i=0; i<fields.length && i<values.length; i++)
|
||||
{
|
||||
st.setString(i+1, values[i]);
|
||||
}
|
||||
|
||||
st.executeUpdate();
|
||||
} finally {
|
||||
st.close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // ajouter dans la base
|
||||
String concat_vals = "";
|
||||
String concat_fields = StringUtils.join(fields, ',');
|
||||
for (int i=0; i<fields.length && i<values.length; i++)
|
||||
{
|
||||
if (i!=0) concat_vals += ",";
|
||||
concat_vals += " ? ";
|
||||
}
|
||||
|
||||
|
||||
PreparedStatement st = conn.prepareStatement("INSERT INTO "+tableName+" ("+concat_fields+") VALUES ("+concat_vals+")", Statement.RETURN_GENERATED_KEYS);
|
||||
try {
|
||||
for (int i=0; i<fields.length && i<values.length; i++)
|
||||
{
|
||||
st.setString(i+1, values[i]);
|
||||
}
|
||||
|
||||
st.executeUpdate();
|
||||
|
||||
ResultSet rs = st.getGeneratedKeys();
|
||||
try {
|
||||
if(rs.next())
|
||||
{
|
||||
id = rs.getInt(1);
|
||||
}
|
||||
|
||||
saved = true;
|
||||
} finally {
|
||||
rs.close();
|
||||
}
|
||||
} finally {
|
||||
st.close();
|
||||
}
|
||||
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void delete() {
|
||||
|
||||
try {
|
||||
if (saved)
|
||||
{ // supprimer la ligne de la base
|
||||
PreparedStatement st = db.getConnection().prepareStatement("DELETE FROM "+tableName+" WHERE id="+id);
|
||||
try {
|
||||
st.executeUpdate();
|
||||
saved = false;
|
||||
} finally {
|
||||
st.close();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public int getId() {
|
||||
if (!saved)
|
||||
throw new IllegalStateException("Ne peut pas fournir l'ID d'un élément non sauvegardé");
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Récupère la liste des valeurs des champs de la table correspondante, excepté
|
||||
* le champ <code>id</code>
|
||||
* @return les valeurs des champs sous la forme de chaine de caractères
|
||||
*/
|
||||
protected abstract String[] getValues();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Récupère la liste des noms des champs de la table correspondante, excepté
|
||||
* le champ <code>id</code>
|
||||
* @return les noms des champs sous la forme de chaine de caractères
|
||||
*/
|
||||
protected abstract String[] getFieldsName();
|
||||
}
|
140
src/fr/pandacube/java/util/db/SQLTable.java
Normal file
140
src/fr/pandacube/java/util/db/SQLTable.java
Normal file
@@ -0,0 +1,140 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class SQLTable<T extends SQLElement> {
|
||||
|
||||
DBConnection db = ORM.connection;
|
||||
|
||||
private final String tableName;
|
||||
|
||||
|
||||
public SQLTable(String name) throws SQLException {
|
||||
tableName = name;
|
||||
|
||||
if (!tableExist())
|
||||
createTable();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void createTable() throws SQLException {
|
||||
Statement stmt = db.getConnection().createStatement();
|
||||
String sql = "CREATE TABLE IF NOT EXISTS "+tableName+" " +
|
||||
"("+createTableParameters()+")";
|
||||
try {
|
||||
stmt.executeUpdate(sql);
|
||||
} finally {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean tableExist() throws SQLException {
|
||||
ResultSet set = null;
|
||||
boolean exist = false;
|
||||
try {
|
||||
set = db.getConnection().getMetaData().getTables(null, null, tableName, null);
|
||||
exist = set.next();
|
||||
} finally {
|
||||
if (set != null)
|
||||
set.close();
|
||||
}
|
||||
return exist;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retourne une chaine de caractère qui sera inclu dans la requête SQL de création de la table.
|
||||
* La requête est de la forme : <code>CRATE TABLE tableName ();</code>
|
||||
* La chaine retournée sera ajoutée entre les parenthèses.
|
||||
*/
|
||||
protected abstract String createTableParameters();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Crée une instance de l'élément courant se trouvant dans le resultSet passé en paramètre
|
||||
* @param sqlResult le set de résultat, déjà positionné sur un élément. Ne surtout pas appeler la méthode next() !
|
||||
* @return
|
||||
* @throws SQLException
|
||||
*/
|
||||
protected abstract T getElementInstance(ResultSet sqlResult) throws SQLException;
|
||||
|
||||
|
||||
|
||||
|
||||
public String getTableName() {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public T get(int id) throws SQLException {
|
||||
T elementInstance = null;
|
||||
Statement stmt = db.getConnection().createStatement();
|
||||
try {
|
||||
String sql = "SELECT * FROM "+tableName+" WHERE id = "+id+";";
|
||||
|
||||
ResultSet set = stmt.executeQuery(sql);
|
||||
try {
|
||||
if (set.next())
|
||||
elementInstance = getElementInstance(set);
|
||||
} finally {
|
||||
set.close();
|
||||
}
|
||||
} finally {
|
||||
stmt.close();
|
||||
}
|
||||
return elementInstance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<T> getAll() throws SQLException {
|
||||
return getAll(null, null, null, null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public T getFirst(String where, String orderBy) throws SQLException {
|
||||
List<T> elts = getAll(where, orderBy, 1, null);
|
||||
return (elts.size() == 0)? null : elts.get(0);
|
||||
}
|
||||
|
||||
|
||||
public List<T> getAll(String where, String orderBy, Integer limit, Integer offset) throws SQLException {
|
||||
Statement stmt = db.getConnection().createStatement();
|
||||
String sql = "SELECT * FROM "+tableName;
|
||||
|
||||
if (where != null)
|
||||
sql += " WHERE "+where;
|
||||
if (orderBy != null)
|
||||
sql += " ORDER BY "+orderBy;
|
||||
if (limit != null)
|
||||
sql += " LIMIT "+limit;
|
||||
if (offset != null)
|
||||
sql += " OFFSET "+offset;
|
||||
sql += ";";
|
||||
|
||||
List<T> elmts = new ArrayList<T>();
|
||||
try {
|
||||
ResultSet set = stmt.executeQuery(sql);
|
||||
try {
|
||||
while (set.next())
|
||||
elmts.add(getElementInstance(set));
|
||||
} finally {
|
||||
set.close();
|
||||
}
|
||||
} finally {
|
||||
stmt.close();
|
||||
}
|
||||
return elmts;
|
||||
}
|
||||
|
||||
}
|
74
src/fr/pandacube/java/util/db/ShopStockElement.java
Normal file
74
src/fr/pandacube/java/util/db/ShopStockElement.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
public class ShopStockElement extends SQLElement {
|
||||
|
||||
private String material;
|
||||
private short damage = 0;
|
||||
private double quantity;
|
||||
private String server;
|
||||
|
||||
|
||||
public ShopStockElement(String m, short d, double q, String s) {
|
||||
super("pandacube_shop_stock");
|
||||
setMaterial(m);
|
||||
setDamage(d);
|
||||
setQuantity(q);
|
||||
setServer(s);
|
||||
}
|
||||
|
||||
protected ShopStockElement(int id, String m, short d, double q, String s) {
|
||||
super("pandacube_shop_stock", id);
|
||||
setMaterial(m);
|
||||
setDamage(d);
|
||||
setQuantity(q);
|
||||
setServer(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getValues() {
|
||||
return new String[] {
|
||||
material,
|
||||
Short.toString(damage),
|
||||
Double.toString(quantity),
|
||||
server
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getFieldsName() {
|
||||
return new String[] {
|
||||
"material",
|
||||
"damage",
|
||||
"quantity",
|
||||
"server"
|
||||
};
|
||||
}
|
||||
|
||||
public String getMaterial() { return material; }
|
||||
|
||||
public void setMaterial(String m) {
|
||||
if (m == null) throw new IllegalArgumentException("Material can't be null");
|
||||
material = m;
|
||||
}
|
||||
|
||||
public short getDamage() { return damage; }
|
||||
|
||||
public void setDamage(short d) {
|
||||
damage = d;
|
||||
}
|
||||
|
||||
public double getQuantity() { return quantity; }
|
||||
|
||||
public void setQuantity(double q) {
|
||||
if (q < 0) q = 0;
|
||||
quantity = q;
|
||||
}
|
||||
|
||||
public String getServer() { return server; }
|
||||
|
||||
public void setServer(String s) {
|
||||
if (s == null) throw new IllegalArgumentException("Server can't be null");
|
||||
server = s;
|
||||
}
|
||||
|
||||
}
|
30
src/fr/pandacube/java/util/db/ShopStockTable.java
Normal file
30
src/fr/pandacube/java/util/db/ShopStockTable.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ShopStockTable extends SQLTable<ShopStockElement> {
|
||||
|
||||
public ShopStockTable() throws SQLException {
|
||||
super("pandacube_shop_stock");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createTableParameters() {
|
||||
return "id INT AUTO_INCREMENT PRIMARY KEY,"
|
||||
+ "material varchar(50) NOT NULL,"
|
||||
+ "damage int(11) NOT NULL DEFAULT '0',"
|
||||
+ "quantity double NOT NULL,"
|
||||
+ "server varchar(50) NOT NULL";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ShopStockElement getElementInstance(ResultSet sqlResult) throws SQLException {
|
||||
return new ShopStockElement(sqlResult.getInt("id"),
|
||||
sqlResult.getString("material"),
|
||||
sqlResult.getShort("damage"),
|
||||
sqlResult.getDouble("quantity"),
|
||||
sqlResult.getString("server"));
|
||||
}
|
||||
|
||||
}
|
77
src/fr/pandacube/java/util/db/StaffTicketElement.java
Normal file
77
src/fr/pandacube/java/util/db/StaffTicketElement.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class StaffTicketElement extends SQLElement {
|
||||
|
||||
|
||||
private String playerId;
|
||||
private String message;
|
||||
private long creationTime;
|
||||
private String staffPlayerId = null;
|
||||
|
||||
|
||||
public StaffTicketElement(UUID pId, String m, long creaTime) {
|
||||
super("pandacube_staff_ticket");
|
||||
setPlayerId(pId);
|
||||
setMessage(m);
|
||||
setCreationTime(creaTime);
|
||||
|
||||
}
|
||||
protected StaffTicketElement(int id, String pId, String m, long creaTime) {
|
||||
super("pandacube_staff_ticket", id);
|
||||
setPlayerId(UUID.fromString(pId));
|
||||
setMessage(m);
|
||||
setCreationTime(creaTime);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] getValues() {
|
||||
return new String[] {
|
||||
playerId,
|
||||
message,
|
||||
Long.toString(creationTime),
|
||||
staffPlayerId,
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getFieldsName() {
|
||||
return new String[] {
|
||||
"playerId",
|
||||
"message",
|
||||
"creationTime",
|
||||
"staffPlayerId"
|
||||
};
|
||||
}
|
||||
public UUID getPlayerId() {
|
||||
return UUID.fromString(playerId);
|
||||
}
|
||||
public void setPlayerId(UUID pId) {
|
||||
if (pId == null) throw new IllegalArgumentException("playerName can't be null");
|
||||
this.playerId = pId.toString();
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
public void setMessage(String message) {
|
||||
if (message == null) throw new IllegalArgumentException("message can't be null");
|
||||
this.message = message;
|
||||
}
|
||||
public long getCreationTime() {
|
||||
return creationTime;
|
||||
}
|
||||
public void setCreationTime(long creationTime) {
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
public UUID getStaffPlayer() {
|
||||
if (staffPlayerId == null) return null;
|
||||
return UUID.fromString(staffPlayerId);
|
||||
}
|
||||
public void setStaffPlayer(UUID staffId) {
|
||||
if (staffId == null) staffPlayerId = null;
|
||||
else staffPlayerId = staffId.toString();
|
||||
}
|
||||
|
||||
}
|
37
src/fr/pandacube/java/util/db/StaffTicketTable.java
Normal file
37
src/fr/pandacube/java/util/db/StaffTicketTable.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package fr.pandacube.java.util.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class StaffTicketTable extends SQLTable<StaffTicketElement> {
|
||||
|
||||
|
||||
public StaffTicketTable() throws SQLException {
|
||||
super("pandacube_staff_ticket");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createTableParameters() {
|
||||
return "id INT AUTO_INCREMENT PRIMARY KEY,"
|
||||
+ "playerId CHAR(36) NOT NULL,"
|
||||
+ "message VARCHAR(1024) NOT NULL,"
|
||||
+ "creationTime BIGINT NOT NULL,"
|
||||
+ "staffPlayerId CHAR(36) NULL";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StaffTicketElement getElementInstance(ResultSet sqlResult)
|
||||
throws SQLException {
|
||||
StaffTicketElement el = new StaffTicketElement(
|
||||
sqlResult.getInt("id"),
|
||||
sqlResult.getString("playerId"),
|
||||
sqlResult.getString("message"),
|
||||
sqlResult.getLong("creationTime"));
|
||||
String staffId = sqlResult.getString("staffPlayerId");
|
||||
el.setStaffPlayer((staffId == null) ? null : UUID.fromString(staffId));
|
||||
|
||||
return el;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user