ActionsTable, moved some column declarations

This commit is contained in:
Rsl1122 2017-08-22 15:28:05 +03:00
parent 91415d9125
commit fe9c32c324
17 changed files with 279 additions and 69 deletions

View File

@ -0,0 +1,60 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.data;
import main.java.com.djrapitops.plan.database.tables.Actions;
import main.java.com.djrapitops.plan.ui.html.Html;
import main.java.com.djrapitops.plan.utilities.FormatUtils;
/**
* Class that represents an action made by a player.
*
* @author Rsl1122
*/
public class Action {
private final long date;
private final Actions doneAction;
private final String additionalInfo;
private int serverID;
public Action(long date, Actions doneAction, String additionalInfo) {
this.date = date;
this.doneAction = doneAction;
this.additionalInfo = additionalInfo;
}
public Action(long date, Actions doneAction, String additionalInfo, int serverID) {
this.date = date;
this.doneAction = doneAction;
this.additionalInfo = additionalInfo;
this.serverID = serverID;
}
public long getDate() {
return date;
}
public Actions getDoneAction() {
return doneAction;
}
public String getAdditionalInfo() {
return additionalInfo;
}
/**
* Can only be used on Action classes returned by the ActionsTable.
*
* @return ID of the server the action occurred on.
*/
public int getServerID() {
return serverID;
}
@Override
public String toString() {
return Html.TABLELINE_3.parse(FormatUtils.formatTimeStampYear(date), doneAction.toString(), additionalInfo);
}
}

View File

@ -1,5 +1,7 @@
package main.java.com.djrapitops.plan.data;
import main.java.com.djrapitops.plan.database.tables.Actions;
import java.util.Objects;
import java.util.UUID;
@ -12,20 +14,20 @@ import java.util.UUID;
public class KillData {
private final UUID victim;
private final long date;
private final long time;
private final String weapon;
/**
* Creates a KillData object with given parameters.
*
* @param victim UUID of the victim.
* @param weapon Weapon used.
* @param date Epoch millisecond at which the kill occurred.
* @param victim UUID of the victim.
* @param weapon Weapon used.
* @param time Epoch millisecond at which the kill occurred.
*/
public KillData(UUID victim, String weapon, long date) {
public KillData(UUID victim, String weapon, long time) {
this.victim = victim;
this.weapon = weapon;
this.date = date;
this.time = time;
}
/**
@ -42,8 +44,8 @@ public class KillData {
*
* @return long in ms.
*/
public long getDate() {
return date;
public long getTime() {
return time;
}
/**
@ -58,7 +60,7 @@ public class KillData {
@Override
public String toString() {
return "{victim:" + victim + "|date:" + date + "|weapon:" + weapon + '}';
return "{victim:" + victim + "|time:" + time + "|weapon:" + weapon + '}';
}
@Override
@ -73,7 +75,7 @@ public class KillData {
return false;
}
final KillData other = (KillData) obj;
return this.date == other.date
return this.time == other.time
&& Objects.equals(this.weapon, other.weapon)
&& Objects.equals(this.victim, other.victim);
}
@ -82,8 +84,12 @@ public class KillData {
public int hashCode() {
int hash = 3;
hash = 89 * hash + Objects.hashCode(this.victim);
hash = 89 * hash + (int) (this.date ^ (this.date >>> 32));
hash = 89 * hash + (int) (this.time ^ (this.time >>> 32));
hash = 89 * hash + Objects.hashCode(this.weapon);
return hash;
}
public Action convertToAction() {
return new Action(time, Actions.KILLED, "name with " + weapon); // TODO Name Cache.
}
}

View File

@ -0,0 +1,45 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.database.tables;
import org.apache.commons.lang3.text.WordUtils;
/**
* IDs of various actions
*
* @author Rsl1122
*/
public enum Actions {
UNKNOWN(-1),
REGISTERED(1),
FIRST_LOGOUT(2),
CHANGED_NAME(3),
KILLED(-2), // Not stored in ActionsTable.
;
private final int id;
Actions(int id) {
this.id = id;
}
public int getId() {
return id;
}
public static Actions getById(int id) {
for (Actions a : values()) {
if (a.getId() == id) {
return a;
}
}
return UNKNOWN;
}
@Override
public String toString() {
return WordUtils.capitalizeFully(name(), '_').replace('_', ' ');
}
}

View File

@ -4,12 +4,120 @@
*/
package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.Action;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* //TODO Class Javadoc Comment
* Table that is in charge of storing actions.
* <p>
* plan_actions contains columns:
* <ul>
* <li>user_id (plan_users: id)</li>
* <li>server_id (plan_servers: id)</li>
* <li>action_id</li>
* <li>date</li>
* <li>additional_info</li>
* </ul>
*
* @author Rsl1122
*/
public class ActionsTable {
// TODO Actions table
// time, action, additional info
public class ActionsTable extends UserIDTable {
private final String columnServerID = "server_id";
private final String columnDate = "date";
private final String columnActionID = "action_id";
private final String columnAdditionalInfo = "additional_info";
public ActionsTable(String name, SQLDB db, boolean usingMySQL) {
super(name, db, usingMySQL);
}
@Override
public boolean createTable() {
ServerTable serverTable = db.getServerTable();
try {
execute(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull()
.column(columnServerID, Sql.INT).notNull()
.column(columnDate, Sql.LONG).notNull()
.column(columnActionID, Sql.INT).notNull()
.column(columnAdditionalInfo, Sql.varchar(100))
.foreignKey(columnUserID, usersTable.toString(), usersTable.getColumnID())
.foreignKey(columnServerID, serverTable.toString(), serverTable.getColumnID())
.toString());
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
return false;
}
}
public void insertAction(UUID uuid, Action action) throws SQLException {
PreparedStatement statement = null;
try {
ServerTable serverTable = db.getServerTable();
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnUserID + ", "
+ columnServerID + ", "
+ columnActionID + ", "
+ columnDate + ", "
+ columnAdditionalInfo
+ ") VALUES ("
+ usersTable.statementSelectID + ", "
+ serverTable.statementSelectServerID + ", "
+ "?, ?, ?)"
);
statement.setString(1, uuid.toString());
statement.setString(2, Plan.getInstance().getServerInfoManager().getServerUUID().toString());
statement.setInt(3, action.getDoneAction().getId());
statement.setLong(4, action.getDate());
statement.setString(5, action.getAdditionalInfo());
statement.execute();
} finally {
close(statement);
}
}
/**
* Used to get all Actions done by a user on this server.
*
* @param uuid
* @return
* @throws SQLException
*/
public List<Action> getActions(UUID uuid) throws SQLException {
List<Action> actions = new ArrayList<>();
PreparedStatement statement = null;
ResultSet set = null;
try {
ServerTable serverTable = db.getServerTable();
statement = prepareStatement(Select.from(tableName, "*")
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString());
set = statement.executeQuery();
while (set.next()) {
int serverID = set.getInt(columnServerID);
long date = set.getLong(columnDate);
Actions doneAction = Actions.getById(set.getInt(columnActionID));
String additionalInfo = set.getString(columnAdditionalInfo);
actions.add(new Action(date, doneAction, additionalInfo, serverID));
}
return actions;
} finally {
close(set, statement);
}
}
}

View File

@ -19,8 +19,8 @@ import java.util.*;
*/
public class IPsTable extends UserIDTable {
private final String columnIP;
private final String columnGeolocation; // TODO
private final String columnIP = "ip";
private final String columnGeolocation = "geolocation"; // TODO
/**
* @param db The database
@ -28,9 +28,6 @@ public class IPsTable extends UserIDTable {
*/
public IPsTable(SQLDB db, boolean usingMySQL) {
super("plan_ips", db, usingMySQL);
columnUserID = "user_id";
columnIP = "ip";
columnGeolocation = "geolocation";
}
/**

View File

@ -136,7 +136,7 @@ public class KillsTable extends Table {
statement.setInt(1, userId);
statement.setInt(2, -1); // TODO Victim ID Retrieval
statement.setString(3, kill.getWeapon());
statement.setLong(4, kill.getDate());
statement.setLong(4, kill.getTime());
statement.addBatch();
commitRequired = true;
}
@ -222,7 +222,7 @@ public class KillsTable extends Table {
statement.setInt(1, id);
statement.setInt(2, -1); // TODO Victim ID Retrieval
statement.setString(3, kill.getWeapon());
statement.setLong(4, kill.getDate());
statement.setLong(4, kill.getTime());
statement.addBatch();
commitRequired = true;
}

View File

@ -17,9 +17,9 @@ import java.util.*;
*/
public class NicknamesTable extends UserIDTable {
private final String columnNick;
private final String columnCurrent;
private final String columnServerID; //TODO
private final String columnNick = "nickname";
private final String columnCurrent = "current_nick";
private final String columnServerID = "server_id"; //TODO
/**
* @param db The database
@ -27,10 +27,6 @@ public class NicknamesTable extends UserIDTable {
*/
public NicknamesTable(SQLDB db, boolean usingMySQL) {
super("plan_nicknames", db, usingMySQL);
columnUserID = "user_id";
columnNick = "nickname";
columnCurrent = "current_nick";
columnServerID = "server_id";
}
/**

View File

@ -27,19 +27,17 @@ import java.util.UUID;
*/
public class ServerTable extends Table {
private final String columnServerID;
private final String columnServerUUID;
private final String columnServerName;
private final String columnWebserverAddress;
private final String columnInstalled;
private final String columnServerID = "id";
private final String columnServerUUID = "uuid";
private final String columnServerName = "name";
private final String columnWebserverAddress = "web_address";
private final String columnInstalled = "is_installed";
public final String statementSelectServerID;
public ServerTable(SQLDB db, boolean usingMySQL) {
super("plan_servers", db, usingMySQL);
columnServerID = "id";
columnServerUUID = "uuid";
columnServerName = "name";
columnWebserverAddress = "web_address";
columnInstalled = "is_installed";
statementSelectServerID = "(" + Select.from(tableName, tableName + "." + columnServerID).where(columnServerUUID + "=?").toString() + ")";
}
@Override
@ -226,4 +224,8 @@ public class ServerTable extends Table {
close(set, statement);
}
}
public String getColumnID() {
return columnServerID;
}
}

View File

@ -18,12 +18,12 @@ import java.util.*;
*/
public class SessionsTable extends UserIDTable {
private final String columnSessionID; //TODO
private final String columnSessionStart;
private final String columnSessionEnd;
private final String columnServerID; //TODO
private final String columnMobKills; //TODO
private final String columnDeaths; //TODO
private final String columnSessionID = "id"; //TODO
private final String columnSessionStart = "session_start";
private final String columnSessionEnd = "session_end";
private final String columnServerID = "server_id"; //TODO
private final String columnMobKills = "mob_kills"; //TODO
private final String columnDeaths = "deaths"; //TODO
/**
* @param db
@ -31,13 +31,6 @@ public class SessionsTable extends UserIDTable {
*/
public SessionsTable(SQLDB db, boolean usingMySQL) {
super("plan_sessions", db, usingMySQL);
columnUserID = "user_id";
columnSessionStart = "session_start";
columnSessionEnd = "session_end";
columnServerID = "server_id";
columnSessionID = "id";
columnMobKills = "mob_kills";
columnDeaths = "deaths";
}
/**

View File

@ -14,10 +14,12 @@ import java.sql.SQLException;
*/
public abstract class UserIDTable extends Table {
protected String columnUserID;
protected final String columnUserID = "user_id";
protected final UsersTable usersTable;
public UserIDTable(String name, SQLDB db, boolean usingMySQL) {
super(name, db, usingMySQL);
usersTable = db.getUsersTable();
}
protected boolean removeDataOf(int userID) {

View File

@ -3,6 +3,7 @@ package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.utilities.Benchmark;
@ -17,8 +18,8 @@ import java.util.*;
*/
public class UsersTable extends Table {
private final String columnID;
private final String columnUUID;
private final String columnID = "id";
private final String columnUUID = "uuid";
@Deprecated
private final String columnGeolocation;
@Deprecated
@ -50,14 +51,16 @@ public class UsersTable extends Table {
@Deprecated
private final String columnLastWorld;
public final String statementSelectID;
/**
* @param db
* @param usingMySQL
*/
public UsersTable(SQLDB db, boolean usingMySQL) {
super("plan_users", db, usingMySQL);
columnID = "id";
columnUUID = "uuid";
statementSelectID = "(" + Select.from(tableName, tableName + "." + columnID).where(columnUUID + "=?").toString() + ")";
columnGeolocation = "geolocation";
columnLastGM = "last_gamemode";
columnLastGMSwapTime = "last_gamemode_swap";

View File

@ -21,13 +21,14 @@ import java.util.Map;
*/
public class WorldTimesTable extends UserIDTable {
private final String columnServerID; //TODO
private final String columnServerID = "server_id"; //TODO
private final WorldTable worldTable;
private final String worldIDColumn;
private final String worldNameColumn;
private final String columnWorldId;
@Deprecated private final String columnPlaytime;
private final String columnWorldId = "world_id";
@Deprecated
private final String columnPlaytime = "playtime";
//TODO GM Times to World table
private final String selectWorldIDsql;
@ -43,10 +44,6 @@ public class WorldTimesTable extends UserIDTable {
worldTable = db.getWorldTable();
worldIDColumn = worldTable + "." + worldTable.getColumnID();
worldNameColumn = worldTable.getColumnWorldName();
columnWorldId = "world_id";
columnUserID = "user_id";
columnPlaytime = "playtime";
columnServerID = "server_id";
selectWorldIDsql = "(SELECT " + worldIDColumn + " FROM " + worldTable + " WHERE (" + worldNameColumn + "=?))";
}

View File

@ -139,7 +139,8 @@ public enum Msg {
HTML_OFFLINE("Html - Offline"),
HTML_ACTIVE("Html - Active"),
HTML_INACTIVE("Html - Inactive"),
HTML_TABLE_NO_KILLS("Html - Table No Kills"),;
HTML_TABLE_NO_KILLS("Html - Table No Kills"),
;
private final String identifier;

View File

@ -44,7 +44,7 @@ public class KillsTableCreator {
break;
}
long date = kill.getDate();
long date = kill.getTime();
IOfflinePlayer victim = Fetch.getIOfflinePlayer(kill.getVictim());
String name = victim.getName();

View File

@ -34,7 +34,7 @@ public class PlaceholderUtils {
replaceMap.put("tabContentPlugins", data.replacePluginsTabLayout());
// TODO Refresh time for Network pages
// replaceMap.put("refresh", FormatUtils.formatTimeAmountDifference(data.getRefreshDate(), MiscUtils.getTime()));
// replaceMap.put("refresh", FormatUtils.formatTimeAmountDifference(data.getRefreshDate(), MiscUtils.getDate()));
// replaceMap.put("refreshlong", String.valueOf(data.getRefreshDate()));
replaceMap.put("serverName", Settings.SERVER_NAME.toString());

View File

@ -11,7 +11,7 @@ public class KillDataComparator implements Comparator<KillData> {
@Override
public int compare(KillData o1, KillData o2) {
return Long.compare(o1.getDate(), o2.getDate());
return Long.compare(o1.getTime(), o2.getTime());
}
}

View File

@ -46,7 +46,7 @@ public class KillDataTest {
*/
@Test
public void testGetDate() {
assertEquals(test.getDate(), 100L);
assertEquals(test.getTime(), 100L);
}
/**