Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.47 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.      * isATeamPlayer Checks database to see if player is on a team
  72.      *
  73.      * @param name
  74.      *            - player Name
  75.      * @return -1 if not found
  76.      * @return tid if found
  77.      */
  78.     public int isATeamPlayer(String name) {
  79.         try {
  80.             String query = "SELECT * FROM blplayer";
  81.             ResultSet results = query(query);
  82.             while (results.next()) {
  83.                 if (results.getString("pname").equalsIgnoreCase(name))
  84.                     return results.getInt("tid");
  85.             }
  86.         } catch (Exception e) {
  87.             e.printStackTrace();
  88.         }
  89.         return -1;
  90.     }
  91.  
  92.     public int getPlayerId(String name) {
  93.         try {
  94.             String query = "SELECT * FROM blplayer";
  95.             ResultSet results = query(query);
  96.             while (results.next()) {
  97.                 if (results.getString("pname").equalsIgnoreCase(name))
  98.                     return results.getInt("pid");
  99.             }
  100.         } catch (Exception e) {
  101.             e.printStackTrace();
  102.         }
  103.         return -1;
  104.     }
  105.  
  106.     public int geTRating(int tid) {
  107.         try {
  108.             String query = "SELECT * FROM blteams where tid=" + tid;
  109.             ResultSet results = query(query);
  110.             while (results.next()) {
  111.                 return results.getInt("TRating");
  112.             }
  113.         } catch (Exception e) {
  114.             e.printStackTrace();
  115.         }
  116.         return -1;
  117.     }
  118.  
  119.     public String getTeamFromPlayer(String name) {
  120.         try {
  121.             String query = "SELECT t.tname FROM blteams t, blplayer p WHERE p.pname = '"
  122.                     + 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="
  157.                 + p1;
  158.         try {
  159.             query(query);
  160.         } catch (SQLException e) {
  161.  
  162.         }
  163.         String query2 = "Update blplayer set pkills=pkills + 1 where pid=" + o1;
  164.         try {
  165.             query(query2);
  166.         } catch (SQLException e) {
  167.  
  168.         }
  169.     }
  170.  
  171.     /**
  172.      * createTeam Create a Team
  173.      *
  174.      * @param teamname
  175.      * @param c
  176.      * @return
  177.      */
  178.  
  179.     public boolean createTeam(String teamname, String pass, String playername) {
  180.         try {
  181.             String query = "INSERT INTO blteams(tid,tname,tcapt,tkills,tdeaths,TRating,tpass,tratio) VALUES('null', '"
  182.                     + teamname
  183.                     + "','"
  184.                     + playername
  185.                     + "','0','0','1500','"
  186.                     + pass + "','0')";
  187.             query(query);
  188.             // add capt
  189.             String query2 = "INSERT INTO blplayer (pid,pname,pkills,pdeaths,pratio,tid) VALUES('null','"
  190.                     + playername + "','0','0','0','" + isATeam(teamname) + "')";
  191.             query(query2);
  192.             return true;
  193.         } catch (Exception e) {
  194.             e.printStackTrace();
  195.         }
  196.         return false;
  197.     }
  198.  
  199.     public boolean createPlayer(String playername, int id) {
  200.         String query2 = "INSERT INTO blplayer (pid,pname,pkills,pdeaths,pratio,tid) VALUES('null','"
  201.                 + playername + "','0','0','0','" + id + "')";
  202.         try {
  203.             query(query2);
  204.         } catch (SQLException e) {
  205.             return false;
  206.  
  207.         }
  208.         return true;
  209.     }
  210.  
  211.     public void recordKill(int kid, int oid, int pkp) {
  212.         Team t1 = getTeamInfo(kid);
  213.         Team t2 = getTeamInfo(oid);
  214.         int t1kills = t1.getTkills() + 1;
  215.         int t1deaths = t1.getTdeaths();
  216.         int t1rating = calculateRating(kid, oid, pkp, 1);
  217.         int t2kills = t2.getTkills();
  218.         int t2deaths = t2.getTdeaths() + 1;
  219.         int t2rating = calculateRating(oid, kid, pkp, -1);
  220.         String query = "Update blteams set tkills=" + t1kills + ",tdeaths="
  221.                 + t1deaths + ",TRating=" + t1rating + " where tid="
  222.                 + t1.gettId();
  223.         try {
  224.             query(query);
  225.         } catch (SQLException e) {
  226.  
  227.         }
  228.         String query2 = "Update blteams set tkills=" + t2kills + ",tdeaths="
  229.                 + t2deaths + ",TRating=" + t2rating + " where tid="
  230.                 + t2.gettId();
  231.         try {
  232.             query(query2);
  233.         } catch (SQLException e) {
  234.  
  235.         }
  236.  
  237.     }
  238.  
  239.     public ResultSet query(String s) throws SQLException {
  240.  
  241.         try {
  242.             statement = con.createStatement();
  243.             if (s.toLowerCase().startsWith("select")) {
  244.                 ResultSet rs = statement.executeQuery(s);
  245.                 return rs;
  246.             } else {
  247.                 statement.executeUpdate(s);
  248.             }
  249.             return null;
  250.         } catch (Exception e) {
  251.             System.out.println("MySQL Error:" + s);
  252.             e.printStackTrace();
  253.         }
  254.         return null;
  255.     }
  256.  
  257.     public List<Team> getTeam(int ladder) {
  258.         List<Team> teams = new ArrayList<Team>();
  259.         switch (ladder) {
  260.         //
  261.         case 0: // best overal
  262.             try {
  263.                 String query = "SELECT * FROM blteams ORDER BY TRating DESC";
  264.                 ResultSet results = query(query);
  265.                 while (results.next()) {
  266.                     teams.add(new Team(results.getInt("tid"), results
  267.                             .getString("tcapt"), results.getString("tname"),
  268.                             results.getInt("tkills"),
  269.                             results.getInt("tdeaths"), results
  270.                                     .getInt("TRating"), results
  271.                                     .getString("tratio")));
  272.                 }
  273.             } catch (Exception e) {
  274.                 e.printStackTrace();
  275.             }
  276.             return teams;
  277.         case 1: // best oplayer overall
  278.             try {
  279.                 String query = "SELECT * FROM blplayer ORDER BY pkills DESC";
  280.                 ResultSet results = query(query);
  281.                 while (results.next()) {
  282.                     teams.add(new Team(results.getInt("pid"), "", results
  283.                             .getString("pname"), results.getInt("pkills"),
  284.                             results.getInt("pdeaths"), 0, ""));
  285.                 }
  286.             } catch (Exception e) {
  287.                 e.printStackTrace();
  288.             }
  289.             return teams;
  290.         case 2: // most kills
  291.             try {
  292.                 String query = "SELECT * FROM blteams ORDER BY tkills DESC";
  293.                 ResultSet results = query(query);
  294.                 while (results.next()) {
  295.                     teams.add(new Team(results.getInt("tid"), results
  296.                             .getString("tcapt"), results.getString("tname"),
  297.                             results.getInt("tkills"),
  298.                             results.getInt("tdeaths"), results
  299.                                     .getInt("TRating"), results
  300.                                     .getString("tratio")));
  301.                 }
  302.             } catch (Exception e) {
  303.                 e.printStackTrace();
  304.             }
  305.             return teams;
  306.         case 3: // ratio
  307.             try {
  308.                 String query = "SELECT * FROM blteams ORDER BY tratio DESC";
  309.                 ResultSet results = query(query);
  310.                 while (results.next()) {
  311.                     teams.add(new Team(results.getInt("tid"), results
  312.                             .getString("tcapt"), results.getString("tname"),
  313.                             results.getInt("tkills"),
  314.                             results.getInt("tdeaths"), results
  315.                                     .getInt("TRating"), results
  316.                                     .getString("tratio")));
  317.                 }
  318.             } catch (Exception e) {
  319.                 e.printStackTrace();
  320.             }
  321.             return teams;
  322.         }
  323.         return teams;
  324.     }
  325.  
  326.     /** Bloodlust **/
  327.     public BloodLust() {
  328.         createConnection();
  329.     }
  330.  
  331.     /** Show rankings interface **/
  332.     public void showRankings(Client client, int ladder) {
  333.         List<Team> team = getTeam(ladder);
  334.         switch (ladder) {
  335.         case 0:
  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(
  342.                         "" + team.get(k).getTRatings(), 46713 + k); // rating
  343.                 client.getActionAssistant().sendFrame126(
  344.                         team.get(k).getTName(), 46814 + k); // name
  345.             }
  346.             break;
  347.         case 1:
  348.             for (int k = 0; k < 100; k++) {
  349.                 client.getActionAssistant().sendFrame126("0", 46713 + k); // rating
  350.                 client.getActionAssistant().sendFrame126("", 46814 + k); // name
  351.             }
  352.             for (int k = 0; k < team.size(); k++) {
  353.                 client.getActionAssistant().sendFrame126(
  354.                         "" + team.get(k).getTkills(), 46713 + k); // rating
  355.                 client.getActionAssistant().sendFrame126(
  356.                         team.get(k).getTName(), 46814 + k); // name
  357.             }
  358.             break;
  359.         case 2:
  360.             for (int k = 0; k < 100; k++) {
  361.                 client.getActionAssistant().sendFrame126("0", 46713 + k); // rating
  362.                 client.getActionAssistant().sendFrame126("", 46814 + k); // name
  363.             }
  364.             for (int k = 0; k < team.size(); k++) {
  365.                 client.getActionAssistant().sendFrame126(
  366.                         "" + team.get(k).getTkills(), 46713 + k); // rating
  367.                 client.getActionAssistant().sendFrame126(
  368.                         team.get(k).getTName(), 46814 + k); // name
  369.             }
  370.             break;
  371.         case 3:
  372.             for (int k = 0; k < 100; k++) {
  373.                 client.getActionAssistant().sendFrame126("0", 46713 + k); // rating
  374.                 client.getActionAssistant().sendFrame126("", 46814 + k); // name
  375.             }
  376.             for (int k = 0; k < team.size(); k++) {
  377.                 client.getActionAssistant().sendFrame126(
  378.                         "" + team.get(k).getTRatio(), 46713 + k); // rating
  379.                 client.getActionAssistant().sendFrame126(
  380.                         team.get(k).getTName(), 46814 + k); // name
  381.             }
  382.             break;
  383.         }
  384.         client.getActionAssistant().showInterface(46000);
  385.     }
  386.  
  387.     /** Elo Ranking **/
  388.     public int calculateRating(int kid, int oid, int pkp, int win) {
  389.         Team t1 = getTeamInfo(kid);
  390.         Team t2 = getTeamInfo(oid);
  391.         int c = pkp >> 1;
  392.         int s = win;
  393.         int rating = 0;
  394.         if (t1 == null || t2 == null)
  395.             return -1;
  396.         rating = (int) (((s) * (c * expectancy(t1.getTRatings(),
  397.                 t2.getTRatings()))));
  398.         if (rating == 0)
  399.             rating = 3;
  400.         else if (rating > 60)
  401.             rating = 60;
  402.  
  403.         rating += t1.getTRatings();
  404.  
  405.         return rating;
  406.     }
  407.  
  408.     public int calcEstRating(int o1rate, int p1rate, int pkp, int win) {
  409.         Team t1 = getTeamInfo(o1rate);
  410.         Team t2 = getTeamInfo(p1rate);
  411.         int c = pkp >> 1;
  412.         int s = win;
  413.         int rating = 0;
  414.         if (t1 == null || t2 == null)
  415.             return -1;
  416.         rating = (int) (((s) * (c * expectancy(t1.getTRatings(),
  417.                 t2.getTRatings()))));
  418.         if (rating == 0)
  419.             rating = 3;
  420.         else if (rating > 60)
  421.             rating = 60;
  422.  
  423.         return rating;
  424.  
  425.     }
  426.  
  427.     /** Expected ratio of winning **/
  428.     private double expectancy(int rating1, int rating2) {
  429.         int diff = rating1 - rating2;
  430.         return 1.0 / (1.0 + Math.pow(10.0, diff / 400.0));
  431.     }
  432. }
  433.  
  434. class Team {
  435.     private int tId;
  436.     private String tchat;
  437.     private String tname;
  438.     private String tratio;
  439.     private int tkills;
  440.     private int tdeaths;
  441.     private int tRatings;
  442.  
  443.     public Team(int tId, String tchat, String tname, int tkills, int tdeaths,
  444.             int TRatings, String ratio) {
  445.         super();
  446.         this.tId = tId;
  447.         this.tchat = tchat;
  448.         this.tname = tname;
  449.         this.tkills = tkills;
  450.         this.tdeaths = tdeaths;
  451.         this.tRatings = TRatings;
  452.         this.tratio = ratio;
  453.     }
  454.  
  455.     public String getTName() {
  456.         return tname;
  457.     }
  458.  
  459.     public int gettId() {
  460.         return tId;
  461.     }
  462.  
  463.     public String getTchat() {
  464.         return tchat;
  465.     }
  466.  
  467.     public int getTkills() {
  468.         return tkills;
  469.     }
  470.  
  471.     public int getTdeaths() {
  472.         return tdeaths;
  473.     }
  474.  
  475.     public int getTRatings() {
  476.         return tRatings;
  477.     }
  478.  
  479.     public String getTRatio() {
  480.         return tratio;
  481.     }
  482. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement