217 lines
5.6 KiB
Java
217 lines
5.6 KiB
Java
package eu.univento.core.api.player;
|
|
|
|
import eu.univento.core.Core;
|
|
import eu.univento.core.api.database.MySQL;
|
|
import eu.univento.core.api.server.Game;
|
|
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
|
|
/**
|
|
* statistics management
|
|
* @author joethei
|
|
* @version 1.0
|
|
*/
|
|
public class Statistics {
|
|
|
|
CustomPlayer player;
|
|
Game game;
|
|
|
|
/**
|
|
* @param player CustomPlayer
|
|
* @param game Game
|
|
*/
|
|
public Statistics(CustomPlayer player, Game game) {
|
|
this.player = player;
|
|
this.game = game;
|
|
}
|
|
|
|
|
|
/**
|
|
* gets deaths of player for game
|
|
* @return Integer
|
|
*/
|
|
public int getDeaths() {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("SELECT Deaths FROM " + game.toString() + "Stats WHERE ID='" + player.getID() + "');");
|
|
ResultSet rs = st.executeQuery();
|
|
int deaths = rs.getInt("Deaths");
|
|
sql.closeConnection();
|
|
return deaths;
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* sets deaths of player for game
|
|
* @param deaths Integer
|
|
*/
|
|
public void setDeaths(int deaths) {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("UPDATE " + game.toString() + "Stats WHERE ID='" + player.getID() + "' SET Deaths='" + deaths +"');");
|
|
st.execute();
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void addDeath() {
|
|
setDeaths(getDeaths() + 1);
|
|
}
|
|
|
|
/**
|
|
* gets kills of player for game
|
|
* @return Integer
|
|
*/
|
|
public int getKills() {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("SELECT Kills FROM " + game.toString() + "Stats WHERE ID='" + player.getID() + "');");
|
|
ResultSet rs = st.executeQuery();
|
|
int kills = rs.getInt("Kills");
|
|
sql.closeConnection();
|
|
return kills;
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* sets kills of player for game
|
|
* @param kills Integer
|
|
*/
|
|
public void setKills(int kills) {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("UPDATE " + game.toString() + "Stats WHERE ID='" + player.getID() + "' SET Kills='" + kills +"');");
|
|
st.execute();
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void addKill() {
|
|
setKills(getKills() + 1);
|
|
}
|
|
|
|
/**
|
|
* gets played rounds of player for game
|
|
* @return Integer
|
|
*/
|
|
public int getPlayedRounds() {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("SELECT PlayedRounds FROM " + game.toString() + "Stats WHERE ID='" + player.getID() + "');");
|
|
ResultSet rs = st.executeQuery();
|
|
int rounds = rs.getInt("PlayedRounds");
|
|
sql.closeConnection();
|
|
return rounds;
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* sets played rounds of player for game
|
|
* @param rounds Integer
|
|
*/
|
|
public void setPlayedRounds(int rounds) {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("UPDATE " + game.toString() + "Stats WHERE ID='" + player.getID() + "' SET PlayedRounds='" + rounds +"');");
|
|
st.execute();
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void addPlayedRound() {
|
|
setPlayedRounds(getPlayedRounds() + 1);
|
|
}
|
|
|
|
/**
|
|
* gets playtime of player for game
|
|
* @return Integer
|
|
*/
|
|
public int getPlaytime() {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("SELECT Playtime FROM " + game.toString() + "Stats WHERE ID='" + player.getID() + "');");
|
|
ResultSet rs = st.executeQuery();
|
|
int time = rs.getInt("Playtime");
|
|
sql.closeConnection();
|
|
return time;
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* sets playtime of player for game
|
|
* @param time Integer
|
|
*/
|
|
public void setPlaytime(int time) {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("UPDATE " + game.toString() + "Stats WHERE ID='" + player.getID() + "' SET Playtime='" + time +"');");
|
|
st.execute();
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* gets wins of player for game
|
|
* @return Integer
|
|
*/
|
|
public int getWins() {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("SELECT Wins FROM " + game.toString() + "Stats WHERE ID='" + player.getID() + "');");
|
|
ResultSet rs = st.executeQuery();
|
|
int wins = rs.getInt("Wins");
|
|
sql.closeConnection();
|
|
return wins;
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* sets wins of player for game
|
|
* @param wins Integer
|
|
*/
|
|
public void setWins(int wins) {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("UPDATE " + game.toString() + "Stats WHERE ID='" + player.getID() + "' SET Wins='" + wins +"');");
|
|
st.execute();
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void addWin() {
|
|
setWins(getWins() + 1);
|
|
}
|
|
|
|
} |