package com.rs.worldserver.model.player;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import com.rs.worldserver.Server;
@SuppressWarnings("all")
public class BloodlustDatabase {
private static BloodlustDatabase singleton;
private static final String SQL_HOST = "----------------";
private static final String SQL_USER = "patronep_bloodlu";
private static final String SQL_PASS = "-----------------";
private static final String SQL_DB = "patronep_Bloodlust";
private Connection getConnection() throws SQLException {
Properties connectionProperties = new Properties();
connectionProperties.put("user", SQL_USER);
connectionProperties.put("password", SQL_PASS);
return DriverManager.getConnection("jdbc:mysql://" + SQL_HOST + "/",
connectionProperties);
}
public void join(Client me, int teamId) {
System.err.println("Called");
Connection connection = null;
PreparedStatement statement = null;
try {
connection = getConnection();
statement = connection
.prepareStatement("INSERT INTO `patronep_Bloodlust`.`blplayer` (`pname`, `pkills`, `pdeaths`, `pratio`, `tid`) VALUES (?, ?, ?, ?, ?);");
statement.setString(1, me.getPlayerName());
statement.setInt(2, 0);
statement.setInt(3, 0);
statement.setDouble(4, 1.00);
statement.setInt(5, teamId);
if (statement.executeUpdate() > 0) {
me.getActionAssistant()
.sendMessage("You have joined the team.");
me.blTeamId = teamId;
me.title += "[ " + BLTeam.getBy(teamId).getName() + " ] ";
} else {
me.getActionAssistant()
.sendMessage(
"There was a problem joining the team, please try again.");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public HashMap<Integer, BLTeam> getTeams() {
Connection connection = null;
Statement statement = null;
ResultSet results;
HashMap<Integer, BLTeam> teams = new HashMap<Integer, BLTeam>();
try {
connection = getConnection();
statement = connection.createStatement();
results = statement.executeQuery("SELECT * FROM `" + SQL_DB
+ "`.`blteams`");
// String owner, String name, int teamId, int kills, int deaths, int
// rating
while (results.next()) {
teams.put(
results.getInt("tid"),
new BLTeam(results.getString("tcapt"), results
.getString("tname"), results.getInt("tid"),
results.getInt("tkills"), results
.getInt("tdeaths"), results
.getInt("TRating")));
}
return teams;
} catch (SQLException e) {
e.printStackTrace();
return teams;
} finally {
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<String> getTeamates(int teamId) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet results;
try {
connection = getConnection();
statement = connection.prepareStatement("SELECT * FROM `" + SQL_DB
+ "`.blplayer` WHERE `tid`=?");
statement.setInt(1, teamId);
results = statement.executeQuery();
List<String> clients = new ArrayList<String>();
while (results.next()) {
clients.add(results.getString("pname"));
}
return clients;
} catch (SQLException e) {
e.printStackTrace();
return new ArrayList<String>();
} finally {
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void invite(Client player, String target) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet results = null;
try {
connection = getConnection();
statement = connection.prepareStatement("SELECT * FROM `" + SQL_DB
+ "`.`blteams` WHERE `tcapt`=?");
statement.setString(1, player.getPlayerName());
results = statement.executeQuery();
// If the team exists.
while (results.next()) {
for (Player p : Server.getPlayerManager().getPlayers()) {
if (p == null)
continue;
if (p.getPlayerName().equalsIgnoreCase(target)) {
((Client) p).setBLInvite(new BLInvite(player,
(Client) p, results.getInt("tid")));
((Client) p).getBLInvite().sendInvite(
results.getString("tname"));
// Connection is closed in finally, eclipse bug.
return;
}
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
statement.close();
connection.close();
results.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void kickMember(Client me, String member) {
if (me.getBLTeam() == null)
return;
if (!(me.getBLTeam().getOwner().equalsIgnoreCase(me.g etPlayerName())))
return;
Connection connection = null;
PreparedStatement statement = null;
ResultSet results;
try {
connection = getConnection();
statement = connection.prepareStatement("SELECT * FROM `" + SQL_DB
+ "`.`blteams` WHERE `tcapt`=?");
statement.setString(1, me.getPlayerName());
results = statement.executeQuery();
while (results.next()) {
statement = connection.prepareStatement("SELECT * FROM `"
+ SQL_DB + "`.`blplayer` WHERE `tid`=?");
statement.setInt(1, results.getInt("tid"));
ResultSet r = statement.executeQuery();
while (r.next()) {
statement = connection.prepareStatement("DELETE FROM `"
+ SQL_DB + "`.`blplayer` WHERE `pname`=?");
statement.setString(1, member);
if (statement.executeUpdate() > 0) {
me.getActionAssistant().sendMessage(
"You have successfully kicked " + member
+ " from your BL team");
for (Client c : (Client[]) Server.getPlayerManager()
.getPlayers()) {
if (c == null)
continue;
if (c.getPlayerName().equalsIgnoreCase(member)) {
c.setBLTeam(null);
c.blTeamId = 0;
c.getActionAssistant()
.sendMessage(
"You have been removed from your bloodlust team.");
return;
}
}
} else {
me.getActionAssistant().sendMessage(
member + " is not in your team.");
}
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void updateKills(String killer, int killerTeam, String victim,
int victimTeam) {
Connection connection = null;
PreparedStatement killerStatement = null;
PreparedStatement victimStatement = null;
PreparedStatement statement = null;
ResultSet results;
try {
connection = getConnection();
killerStatement = connection.prepareStatement("SELECT * FROM `"
+ SQL_DB + "`.`blplayer` WHERE `pname`=?");
killerStatement.setString(1, killer);
results = killerStatement.executeQuery();
String teamA = "";
String teamB = "";
while (results.next()) {
killerStatement = connection
.prepareStatement("UPDATE `"
+ SQL_DB
+ "`.`blplayer` SET `pkills` = ?, `pratio` = ? WHERE `blplayer`.`pname` =?;");
killerStatement.setInt(1, results.getInt("pkills") + 1);
if (results.getInt("pdeaths") == 0) {
killerStatement.setDouble(2, results.getInt("pkills"));
} else if (results.getInt("pkills") == 0) {
killerStatement.setDouble(2, 0);
} else {
killerStatement.setDouble(2,
(float) (results.getInt("pkills") + 1)
/ (float) results.getInt("pdeaths"));
}
killerStatement.setString(3, killer);
killerStatement.executeUpdate();
}
victimStatement = connection.prepareStatement("SELECT * FROM `"
+ SQL_DB + "`.`blplayer` WHERE `pname`=?");
victimStatement.setString(1, victim);
results = victimStatement.executeQuery();
while (results.next()) {
victimStatement = connection
.prepareStatement("UPDATE `"
+ SQL_DB
+ "`.`blplayer` SET `pdeaths` = ?, `pratio` = ? WHERE `blplayer`.`pname` =?;");
victimStatement.setInt(1, results.getInt("pdeaths") + 1);
if (results.getInt("pdeaths") == 0) {
victimStatement.setDouble(2, results.getInt("pkills"));
} else if (results.getInt("pkills") == 0) {
victimStatement.setDouble(2, 0);
} else {
victimStatement.setDouble(2,
(float) (results.getInt("pkills"))
/ (float) (results.getInt("pdeaths") + 1));
}
victimStatement.setString(3, victim);
victimStatement.executeUpdate();
}
statement = connection.prepareStatement("SELECT * FROM `" + SQL_DB
+ "`.`blplayer` WHERE `tid`=?");
statement.setInt(1, killerTeam);
results = statement.executeQuery();
List<Integer> ratios = new ArrayList<Integer>();
List<Integer> kills = new ArrayList<Integer>();
List<Integer> deaths = new ArrayList<Integer>();
while (results.next()) {
ratios.add(results.getInt("pratio"));
kills.add(results.getInt("pkills"));
deaths.add(results.getInt("pdeaths"));
}
float ratio = 0;
for (float f : ratios) {
ratio += f;
}
ratio = ratio / ratios.size();
int kill = 0;
for (int i : kills) {
kill += i;
}
kill = (int) Math.floor((double) (kill / kills.size()));
int death = 0;
for (int i : deaths) {
death += i;
}
death = (int) Math.floor((double) (death / deaths.size()));
statement = connection.prepareStatement("SELECT * FROM `" + SQL_DB
+ "`.`blteams` WHERE `tid`=?");
statement.setInt(1, killerTeam);
results = statement.executeQuery();
int rating = 0;
while (results.next()) {
rating = results.getInt("TRating");
}
// Statement is closed in finally, eclipse bug.
statement = connection
.prepareStatement("UPDATE `"
+ SQL_DB
+ "`.`blteams` SET `tkills`=?, `tdeaths`=?, `TRating`=?, `tratio`=? WHERE `tid`=?");
statement.setInt(1, kill);
statement.setInt(2, death);
statement.setInt(3, rating + 12);
statement.setDouble(4, ratio);
statement.setInt(5, killerTeam);
statement.executeUpdate();
kills.clear();
deaths.clear();
ratios.clear();
kill = 0;
death = 0;
ratio = 0;
// ----------------------------
// Statement is closed in finally, eclipse bug.
statement = connection.prepareStatement("SELECT * FROM `" + SQL_DB
+ "`.`blplayer` WHERE `tid`=?");
statement.setInt(1, victimTeam);
results = statement.executeQuery();
while (results.next()) {
ratios.add(results.getInt("pratio"));
kills.add(results.getInt("pkills"));
deaths.add(results.getInt("pdeaths"));
}
for (float f : ratios) {
ratio += f;
}
ratio = ratio / ratios.size();
for (int i : kills) {
kill += i;
}
kill = (int) Math.floor((double) (kill / kills.size()));
for (int i : deaths) {
death += i;
}
death = (int) Math.floor((double) (death / deaths.size()));
statement = connection.prepareStatement("SELECT * FROM `" + SQL_DB
+ "`.`blteams` WHERE `tid`=?");
statement.setInt(1, victimTeam);
results = statement.executeQuery();
rating = 0;
while (results.next()) {
rating = results.getInt("TRating");
}
// Statement is closed in finally, eclipse bug.
statement = connection
.prepareStatement("UPDATE `"
+ SQL_DB
+ "`.`blteams` SET `tkills`=?, `tdeaths`=?, `TRating`=?, `tratio`=? WHERE `tid`=?");
statement.setInt(1, kill);
statement.setInt(2, death);
statement.setInt(3, rating - 12);
statement.setDouble(4, ratio);
statement.setInt(5, victimTeam);
statement.executeUpdate();
Client cKiller = null;
Client cVictim = null;
for (Player client : Server.getPlayerManager().getPlayers()) {
if (client == null)
continue;
Client c = (Client) client;
if (c.getPlayerName().equalsIgnoreCase(victim)) {
cVictim = c;
teamA = c.getBLTeam().getName();
} else if (c.getPlayerName().equalsIgnoreCase(killer)) {
cKiller = c;
teamB = c.getBLTeam().getName();
}
}
if (cKiller != null)
cKiller.getActionAssistant().sendMessage(
"@red@You have killed " + victim + " of team [" + teamA
+ "] and gain a +12 rating.");
if (cVictim != null)
cVictim.getActionAssistant().sendMessage(
"@red@You have been defeated by " + killer
+ " of team [" + teamB
+ "] and lost -12 rating.");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
statement.close();
killerStatement.close();
victimStatement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public boolean createTeam(Client player, String name) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet results;
try {
connection = getConnection();
statement = connection
.prepareStatement("INSERT INTO `patronep_Bloodlust`.`blteams` (`tname`, `tcapt`, `tkills`, `tdeaths`, `TRating`, `tratio`) VALUES (?, ?, ?, ?, ?, ?)");
statement.setString(1, name);
statement.setString(2, player.getPlayerName());
statement.setInt(3, 0);
statement.setInt(4, 0);
statement.setInt(5, 1200);
statement.setDouble(6, 1.00);
if (statement.executeUpdate() > 0) {
player.getActionAssistant().sendMessage(
"Your team as been successfully created");
// Statement is closed in finally, eclipse bug.
statement = connection.prepareStatement("SELECT * FROM `"
+ SQL_DB + "`.`blteams` WHERE `tname`=?");
statement.setString(1, name);
results = statement.executeQuery();
while (results.next()) {
BLTeam.getTeams().put(
results.getInt("tid"),
new BLTeam(player.getPlayerName(), name, results
.getInt("tid"), 0, 0, 1200));
player.blTeamId = results.getInt("tid");
join(player, results.getInt("tid"));
return true;
}
return false;
} else {
player.getActionAssistant().sendMessage(
"There was an error creating your team.");
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static BloodlustDatabase getSingleton() {
if (singleton == null)
singleton = new BloodlustDatabase();
return singleton;
}
}