Advertisement
Guest User

Untitled

a guest
May 24th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.38 KB | None | 0 0
  1. package com.rs.worldserver.model.player;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.PrintWriter;
  8. import java.util.Scanner;
  9. import java.util.Collections;
  10. import java.util.Comparator;
  11. import com.rs.worldserver.events.Event;
  12. import com.rs.worldserver.events.EventContainer;
  13. import com.rs.worldserver.events.EventManager;
  14. import com.rs.worldserver.model.player.*;
  15. import com.rs.worldserver.util.Misc;
  16. import com.rs.worldserver.world.PlayerManager;
  17. import java.sql.Connection;
  18. import java.sql.DriverManager;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.sql.Statement;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import com.rs.worldserver.Config;
  25. import java.util.List;
  26. import java.util.ArrayList;
  27.  
  28. public class BloodLust {
  29. /** Connection **/
  30. public Connection con = null;
  31. /** Statement **/
  32. public Statement statement;
  33.  
  34. /** Create Connection **/
  35. public void createConnection() {
  36. try {
  37. Class.forName("com.mysql.jdbc.Driver").newInstance();
  38. con = DriverManager.getConnection("jdbc:mysql://" + Config.SQLHost
  39. + "/vanquish", "root", "testing");
  40. statement = con.createStatement();
  41. System.out.println("[BloodLust] Database pooled");
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46.  
  47. /** Database methods **/
  48.  
  49. /**
  50. * isATeam Checks database to see if team exists
  51. *
  52. * @param name
  53. * - Team Name
  54. * @return True or False
  55. */
  56. public int isATeam(String name) {
  57. try {
  58. String query = "SELECT * FROM blteams";
  59. ResultSet results = query(query);
  60. while (results.next()) {
  61. if (results.getString("tname").equalsIgnoreCase(name))
  62. return results.getInt("tid");
  63. }
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. return -1;
  68. }
  69.  
  70.  
  71.  
  72. /**
  73. * isATeamPlayer Checks database to see if player is on a team
  74. *
  75. * @param name
  76. * - player Name
  77. * @return -1 if not found
  78. * @return tid if found
  79. */
  80. public int isATeamPlayer(String name) {
  81. try {
  82. String query = "SELECT * FROM blplayer";
  83. ResultSet results = query(query);
  84. while (results.next()) {
  85. if (results.getString("pname").equalsIgnoreCase(name))
  86. return results.getInt("tid");
  87. }
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. }
  91. return -1;
  92. }
  93.  
  94. public int getPlayerId(String name) {
  95. try {
  96. String query = "SELECT * FROM blplayer";
  97. ResultSet results = query(query);
  98. while (results.next()) {
  99. if (results.getString("pname").equalsIgnoreCase(name))
  100. return results.getInt("pid");
  101. }
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. return -1;
  106. }
  107. public int geTRating(int tid){
  108. try {
  109. String query = "SELECT * FROM blteams where tid="+tid;
  110. ResultSet results = query(query);
  111. while (results.next()) {
  112. return results.getInt("TRating");
  113. }
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. }
  117. return -1;
  118. }
  119.  
  120. public String getTeamFromPlayer(String name) {
  121. try {
  122. String query = "SELECT t.tname FROM blteams t, blplayer p WHERE p.pname = '"+name+"' AND t.tid = p.tid";
  123. ResultSet results = query(query);
  124. while (results.next()) {
  125. return results.getString("tname");
  126. }
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. }
  130. return "";
  131. }
  132.  
  133. /**
  134. * getTeamInfo Retrieve our team data from database
  135. *
  136. * @param teamid
  137. * @return team object
  138. */
  139. public Team getTeamInfo(int id) {
  140. try {
  141. String query = "SELECT * FROM blteams where tid='" + id + "'";
  142. ResultSet results = query(query);
  143. while (results.next()) {
  144. return new Team(results.getInt("tid"),
  145. results.getString("tcapt"), results.getString("tname"),
  146. results.getInt("tkills"), results.getInt("tdeaths"),
  147. results.getInt("TRating"),results.getString("tratio"));
  148. }
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. }
  152. return null;
  153. }
  154.  
  155. public void updateKills(int p1, int o1){
  156. String query = "Update blplayer set pdeaths = pdeaths + 1 where pid="+p1;
  157. try {
  158. query(query);
  159. } catch (SQLException e) {
  160.  
  161. }
  162. String query2 = "Update blplayer set pkills=pkills + 1 where pid="+o1;
  163. try {
  164. query(query2);
  165. } catch (SQLException e) {
  166.  
  167. }
  168. }
  169. /**
  170. * createTeam Create a Team
  171. *
  172. * @param teamname
  173. * @param c
  174. * @return
  175. */
  176.  
  177. public boolean createTeam(String teamname, String pass, String playername) {
  178. try {
  179. String query = "INSERT INTO blteams(tid,tname,tcapt,tkills,tdeaths,TRating,tpass,tratio) VALUES('null', '"
  180. + teamname
  181. + "','"
  182. + playername
  183. + "','0','0','1500','"
  184. + pass + "','0')";
  185. query(query);
  186. // add capt
  187. String query2 = "INSERT INTO blplayer (pid,pname,pkills,pdeaths,pratio,tid) VALUES('null','"
  188. + playername + "','0','0','0','" + isATeam(teamname) + "')";
  189. query(query2);
  190. return true;
  191. } catch (Exception e) {
  192. e.printStackTrace();
  193. }
  194. return false;
  195. }
  196.  
  197. public boolean createPlayer(String playername, int id) {
  198. String query2 = "INSERT INTO blplayer (pid,pname,pkills,pdeaths,pratio,tid) VALUES('null','"
  199. + playername + "','0','0','0','" + id + "')";
  200. try {
  201. query(query2);
  202. } catch (SQLException e) {
  203. return false;
  204.  
  205. }
  206. return true;
  207. }
  208.  
  209. public void recordKill(int kid,int oid,int pkp){
  210. Team t1 = getTeamInfo(kid);
  211. Team t2 = getTeamInfo(oid);
  212. int t1kills = t1.getTkills() + 1;
  213. int t1deaths = t1.getTdeaths();
  214. int t1rating = calculateRating(kid, oid, pkp, 1);
  215. int t2kills = t2.getTkills();
  216. int t2deaths = t2.getTdeaths() + 1;
  217. int t2rating = calculateRating(oid, kid, pkp, -1);
  218. String query = "Update blteams set tkills="+t1kills+",tdeaths="+t1deaths+",TRating="+t1rating+" where tid="+t1.gettId();
  219. try {
  220. query(query);
  221. } catch (SQLException e) {
  222.  
  223. }
  224. String query2 = "Update blteams set tkills="+t2kills+",tdeaths="+t2deaths+",TRating="+t2rating+" where tid="+t2.gettId();
  225. try {
  226. query(query2);
  227. } catch (SQLException e) {
  228.  
  229. }
  230.  
  231. }
  232.  
  233. public ResultSet query(String s) throws SQLException {
  234.  
  235. try {
  236. statement = con.createStatement();
  237. if (s.toLowerCase().startsWith("select")) {
  238. ResultSet rs = statement.executeQuery(s);
  239. return rs;
  240. } else {
  241. statement.executeUpdate(s);
  242. }
  243. return null;
  244. } catch (Exception e) {
  245. System.out.println("MySQL Error:"+s);
  246. e.printStackTrace();
  247. }
  248. return null;
  249. }
  250.  
  251.  
  252. public List<Team> getTeam(int ladder){
  253. List<Team> teams = new ArrayList<Team>();
  254. switch(ladder){
  255. //
  256. case 0: // best overal
  257. try {
  258. String query = "SELECT * FROM blteams ORDER BY TRating DESC";
  259. ResultSet results = query(query);
  260. while (results.next()) {
  261. teams.add(new Team(results.getInt("tid"),
  262. results.getString("tcapt"), results.getString("tname"),
  263. results.getInt("tkills"), results.getInt("tdeaths"),
  264. results.getInt("TRating"),results.getString("tratio")));
  265. }
  266. } catch (Exception e) {
  267. e.printStackTrace();
  268. }
  269. return teams;
  270. case 1: // best oplayer overall
  271. try {
  272. String query = "SELECT * FROM blplayer ORDER BY pkills DESC";
  273. ResultSet results = query(query);
  274. while (results.next()) {
  275. teams.add(new Team(results.getInt("pid"),
  276. "", results.getString("pname"),
  277. results.getInt("pkills"), results.getInt("pdeaths"),
  278. 0,""));
  279. }
  280. } catch (Exception e) {
  281. e.printStackTrace();
  282. }
  283. return teams;
  284. case 2: // most kills
  285. try {
  286. String query = "SELECT * FROM blteams ORDER BY tkills DESC";
  287. ResultSet results = query(query);
  288. while (results.next()) {
  289. teams.add(new Team(results.getInt("tid"),
  290. results.getString("tcapt"), results.getString("tname"),
  291. results.getInt("tkills"), results.getInt("tdeaths"),
  292. results.getInt("TRating"),results.getString("tratio")));
  293. }
  294. } catch (Exception e) {
  295. e.printStackTrace();
  296. }
  297. return teams;
  298. case 3: // ratio
  299. try {
  300. String query = "SELECT * FROM blteams ORDER BY tratio DESC";
  301. ResultSet results = query(query);
  302. while (results.next()) {
  303. teams.add(new Team(results.getInt("tid"),
  304. results.getString("tcapt"), results.getString("tname"),
  305. results.getInt("tkills"), results.getInt("tdeaths"),
  306. results.getInt("TRating"),results.getString("tratio")));
  307. }
  308. } catch (Exception e) {
  309. e.printStackTrace();
  310. }
  311. return teams;
  312. }
  313. return teams;
  314. }
  315.  
  316. /** Bloodlust **/
  317. public BloodLust() {
  318. createConnection();
  319. }
  320.  
  321. /** Show rankings interface **/
  322. public void showRankings(Client client, int ladder) {
  323. List<Team> team = getTeam(ladder);
  324. switch (ladder) {
  325. case 0:
  326. for(int k = 0; k < 100; k++){
  327. client.getActionAssistant().sendFrame126("0", 46713+k); // rating
  328. client.getActionAssistant().sendFrame126("", 46814+k); // name
  329. }
  330. for(int k = 0; k < team.size() ; k++){
  331. client.getActionAssistant().sendFrame126(""+team.get(k).getTRatings(), 46713+k); // rating
  332. client.getActionAssistant().sendFrame126(team.get(k).getTName(), 46814+k); // name
  333. }
  334. break;
  335. case 1:
  336. for(int k = 0; k < 100; k++){
  337. client.getActionAssistant().sendFrame126("0", 46713+k); // rating
  338. client.getActionAssistant().sendFrame126("", 46814+k); // name
  339. }
  340. for(int k = 0; k < team.size() ; k++){
  341. client.getActionAssistant().sendFrame126(""+team.get(k).getTkills(), 46713+k); // rating
  342. client.getActionAssistant().sendFrame126(team.get(k).getTName(), 46814+k); // name
  343. }
  344. break;
  345. case 2:
  346. for(int k = 0; k < 100; k++){
  347. client.getActionAssistant().sendFrame126("0", 46713+k); // rating
  348. client.getActionAssistant().sendFrame126("", 46814+k); // name
  349. }
  350. for(int k = 0; k < team.size() ; k++){
  351. client.getActionAssistant().sendFrame126(""+team.get(k).getTkills(), 46713+k); // rating
  352. client.getActionAssistant().sendFrame126(team.get(k).getTName(), 46814+k); // name
  353. }
  354. break;
  355. case 3:
  356. for(int k = 0; k < 100; k++){
  357. client.getActionAssistant().sendFrame126("0", 46713+k); // rating
  358. client.getActionAssistant().sendFrame126("", 46814+k); // name
  359. }
  360. for(int k = 0; k < team.size() ; k++){
  361. client.getActionAssistant().sendFrame126(""+team.get(k).getTRatio(), 46713+k); // rating
  362. client.getActionAssistant().sendFrame126(team.get(k).getTName(), 46814+k); // name
  363. }
  364. break;
  365. }
  366. client.getActionAssistant().showInterface(46000);
  367. }
  368.  
  369. /** Elo Ranking **/
  370. public int calculateRating(int kid, int oid, int pkp, int win) {
  371. Team t1 = getTeamInfo(kid);
  372. Team t2 = getTeamInfo(oid);
  373. int c = pkp >> 1;
  374. int s = win;
  375. int rating = 0;
  376. if (t1 == null || t2 == null)
  377. return -1;
  378. rating = (int) (((s) * (c * expectancy(
  379. t1.getTRatings(), t2.getTRatings()))));
  380. if(rating == 0)
  381. rating = 3;
  382. else if (rating > 60)
  383. rating = 60;
  384.  
  385. rating += t1.getTRatings();
  386.  
  387. return rating;
  388. }
  389. public int calcEstRating(int o1rate,int p1rate,int pkp,int win){
  390. Team t1 = getTeamInfo(o1rate);
  391. Team t2 = getTeamInfo(p1rate);
  392. int c = pkp >> 1;
  393. int s = win;
  394. int rating = 0;
  395. if (t1 == null || t2 == null)
  396. return -1;
  397. rating = (int) (((s) * (c * expectancy(
  398. t1.getTRatings(), t2.getTRatings()))));
  399. if(rating == 0)
  400. rating = 3;
  401. else if (rating > 60)
  402. rating = 60;
  403.  
  404. return rating;
  405.  
  406. }
  407. /** Expected ratio of winning **/
  408. private double expectancy(int rating1, int rating2) {
  409. int diff = rating1 - rating2;
  410. return 1.0 / (1.0 + Math.pow(10.0, diff / 400.0));
  411. }
  412. }
  413.  
  414. class Team {
  415. private int tId;
  416. private String tchat;
  417. private String tname;
  418. private String tratio;
  419. private int tkills;
  420. private int tdeaths;
  421. private int tRatings;
  422.  
  423. public Team(int tId, String tchat, String tname, int tkills, int tdeaths, int TRatings,String ratio) {
  424. super();
  425. this.tId = tId;
  426. this.tchat = tchat;
  427. this.tname = tname;
  428. this.tkills = tkills;
  429. this.tdeaths = tdeaths;
  430. this.tRatings = TRatings;
  431. this.tratio = ratio;
  432. }
  433.  
  434. public String getTName(){
  435. return tname;
  436. }
  437. public int gettId() {
  438. return tId;
  439. }
  440.  
  441. public String getTchat() {
  442. return tchat;
  443. }
  444.  
  445. public int getTkills() {
  446. return tkills;
  447. }
  448.  
  449. public int getTdeaths() {
  450. return tdeaths;
  451. }
  452.  
  453. public int getTRatings() {
  454. return tRatings;
  455. }
  456. public String getTRatio(){
  457. return tratio;
  458. }
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement