Guest User

Untitled

a guest
Oct 29th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.97 KB | None | 0 0
  1. package com.rs.worldserver.model.player;
  2.  
  3. import com.rs.worldserver.Server;
  4. import com.rs.worldserver.util.Misc;
  5. import com.rs.worldserver.Config;
  6. import com.rs.worldserver.world.PlayerManager;
  7. import com.rs.worldserver.*;
  8. import com.rs.worldserver.world.*;
  9. import java.io.*;
  10. import java.lang.Object;
  11. import java.sql.*;
  12.  
  13. /**
  14.  * Handles the friends/ignores/pm system
  15.  *
  16.  * @author Graham
  17.  *
  18.  */
  19. public class FriendsAssistant {
  20.  
  21.     private Client client;
  22.  
  23.     private static int lastChatId = 0;
  24.     public static Connection con = null;
  25.     public static Statement statement;
  26.  
  27.     public static void createConnection() {
  28.         try {
  29.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  30.             con = DriverManager.getConnection("jdbc:mysql://"+Config.SQLHost+"/donationsystem","root","NUIDSfnu7387ca");
  31.             statement = con.createStatement();
  32.         } catch (Exception e) {
  33.             e.printStackTrace();
  34.         }
  35.     }
  36.     public FriendsAssistant(Client client) {
  37.         this.client = client;
  38.         //test++;
  39.     }
  40.  
  41.     public boolean containsFriend(long l) {
  42.         for (int i = 0; i < client.getFriends().length; i++) {
  43.             if (client.getFriends()[i] == l) {
  44.                 return true;
  45.             }
  46.         }
  47.         return false;
  48.     }
  49.  
  50.     public boolean containsIgnore(long l) {
  51.         for (int i = 0; i < client.getIgnores().length; i++) {
  52.             if (client.getIgnores()[i] == l) {
  53.                 return true;
  54.             }
  55.         }
  56.         return false;
  57.     }
  58.    
  59.         public void updateFlist() {
  60.             if (!Server.WorldFriendsReady) return;
  61.             for(int i = 0; i < client.getFriends().length; i++) {
  62.                 if (client.getFriends()[i]==0) //if there is no friend at this location, skip to next friend (i)
  63.                     continue;
  64.                 if (Server.playerSQLnames.containsKey(Misc.longToPlayerName(client.getFriends()[i]).replace('_',' '))) {
  65.                     client.getFriendsAssistant().sendFriend(client.getFriends()[i],4);
  66.                     continue;
  67.                 }
  68.                 Player p = Server.getPlayerManager().getPlayerByName(Misc.longToPlayerName(client.getFriends()[i]).replace('_',' '));
  69.                 if (p != null) {
  70.                     if (client.getFriends()[i] == Misc.playerNameToInt64(p.playerName) && p.isActive) {
  71.                         continue;
  72.                     }
  73.                 }
  74.                 //System.out.println("Removed: " + Misc.longToPlayerName(client.getFriends()[i]));
  75.                 client.getFriendsAssistant().sendFriend(client.getFriends()[i],0);
  76.             }
  77.         }
  78.    
  79.     /*  public void updateFlistOld() {
  80.             boolean friendfound;
  81.             if (!Server.WorldFriendsReady) return;
  82.             for(int i = 0; i < client.getFriends().length; i++) {
  83.                 friendfound = false;
  84.                 if (client.getFriends()[i]==0) //if there is no friend at this location, skip to next friend (i)
  85.                     continue;
  86.                 for(int i2 = 0; i2 < Server.pcount; i2++) {
  87.                     if (Server.playernames[i2] == client.getFriends()[i]) {
  88.                         client.getFriendsAssistant().sendFriend(client.getFriends()[i],1);
  89.                         friendfound = true;
  90.                         break;
  91.                     }
  92.                     if (i2 < Constants.MAXIMUM_PLAYERS) {
  93.                         Player p = Server.getPlayerManager().getPlayers()[i2];  //lookup could be a lot faster using hashtables
  94.                         if (p != null && p.isActive) {                          //hashtables would eliminate the i2 loop
  95.                             if (client.getFriends()[i] == Misc.playerNameToInt64(p.playerName)) {
  96.                                 friendfound = true;
  97.                                 break;
  98.                             }
  99.                         }
  100.                     }
  101.                 }
  102.                 if (!friendfound) {
  103.                     client.getFriendsAssistant().sendFriend(client.getFriends()[i],0);
  104.                 }
  105.             }
  106.         } */
  107.  
  108.     public void initialize() {
  109.     if (Server.Friend) {
  110.     //if ((System.currentTimeMillis() - Server.FDelay >  2000) || (System.currentTimeMillis()- Server.Loadtime < 1200000)) {
  111.     client.checkFriends = 1;
  112.     Server.FDelay = System.currentTimeMillis();
  113.         sendStatus(2);
  114.         //refreshLists();
  115.         for(int i1 = 0; i1 < Constants.MAXIMUM_PLAYERS; i1++) {
  116.             Player p = Server.getPlayerManager().getPlayers()[i1];
  117.             if(p != null && p.isActive) {
  118.                 Client o = (Client)p;
  119.                 if(o != null) {
  120.                     o.getFriendsAssistant().updatePM(client.playerId, 1);
  121.                 }
  122.             }
  123.         }
  124.         boolean pmLoaded = false;
  125.         for(int i = 0; i < client.getFriends().length; i++) {
  126.             if(client.getFriends()[i] != 0)  {
  127.                 Player p = Server.getPlayerManager().getPlayerByName(Misc.longToPlayerName(client.getFriends()[i]).replace('_',' '));
  128.                 if (p != null && p.isActive && Misc.playerNameToInt64(p.playerName) == client.getFriends()[i]) {
  129.                     Client o = (Client)p;
  130.                     if(o != null) {
  131.                         if (p.pmstatus == 0 || (p.pmstatus == 1 && o.getFriendsAssistant().containsFriend(Misc.playerNameToInt64(client.playerName)))) {
  132.                             sendFriend(client.getFriends()[i], 1);
  133.                             pmLoaded = true;
  134.                         }
  135.                     }
  136.                 }
  137.                 if(!pmLoaded) {
  138.                     sendFriend(client.getFriends()[i], 0);
  139.                 }
  140.                 pmLoaded = false;
  141.             }
  142.         }
  143.     }  
  144.     //}
  145.     }
  146.  
  147.  
  148.  
  149. /*      if (client.getFriends() == null) {
  150.             client.setFriends(new long[200]);
  151.         }
  152.         if (client.getIgnores() == null) {
  153.             client.setIgnores(new long[100]);
  154.         }
  155.         refreshLists();
  156.         long l = Misc.playerNameToInt64(client.getPlayerName());
  157.         int world = getWorld(l);
  158.         for (Player p : Server.getPlayerManager().getPlayers()) {
  159.             if (p == null)
  160.                 continue;
  161.             if (!p.isActive || p.disconnected)
  162.                 continue;
  163.             Client c = (Client) p;
  164.             if (c.getFriendsAssistant().containsFriend(l)) {
  165.                 if (c.pmstatus == 0 || (c.pmstatus == 1)) {
  166.                     c.getFriendsAssistant().sendFriend(l, world);
  167.                 }
  168.             }
  169.         }
  170.     }*/
  171.  
  172.     public void destruct() {
  173.         long l = Misc.playerNameToInt64(client.getPlayerName());
  174.         for (Player p : Server.getPlayerManager().getPlayers()) {
  175.             if (p == null)
  176.                 continue;
  177.             if (!p.isActive || p.disconnected)
  178.                 continue;
  179.             Client c = (Client) p;
  180.             if (c.getFriendsAssistant().containsFriend(l)) {
  181.                 c.getFriendsAssistant().sendFriend(l, 0);
  182.             }
  183.         }
  184.     }
  185.  
  186.     public void refreshLists() {
  187.         for (int i = 0; i < Constants.MAXIMUM_PLAYERS; i++) {
  188.             long l = client.getFriends()[i];
  189.             if (l == 0)
  190.                 continue;
  191.             sendFriend(l, getWorld(l));
  192.         }
  193.         sendStatus(2);
  194.     }
  195.  
  196.     public void sendFriend(long l, int world) {
  197.         if(world != 0) {
  198.             world += 9;
  199.         } else if(!Config.WORLD_LIST_FIX) {
  200.             world += 1;
  201.         }  
  202.         client.getOutStream().createFrame(50);
  203.         client.getOutStream().writeQWord(l);
  204.         client.getOutStream().writeByte(world);
  205.         client.flushOutStream();
  206.     }
  207.  
  208.     public void sendStatus(int status) {
  209.         client.getOutStream().createFrame(221);
  210.         client.getOutStream().writeByte(status);
  211.         client.flushOutStream();
  212.     }
  213.  
  214.     public int getWorld(long who) {
  215.         Player p = Server.getPlayerManager().getPlayerByNameLong(who);
  216.         int world = 0;
  217.         if (p != null) {
  218.             world = 1;
  219.         }
  220.         return world;
  221.     }
  222.  
  223.     public void addFriend(long l) {
  224.         if (getAmountOf(client.getFriends()) > client.getFriendsSize()) {
  225.             // TODO is there a message? and is this correct?
  226.             client.getActionAssistant().sendMessage(
  227.                     "Your friends list is full.");
  228.             return;
  229.         } else {
  230.             if (containsFriend(l)) {
  231.                 client.getActionAssistant().sendMessage(
  232.                         Misc.longToPlayerName(l)
  233.                                 + " is already in your friendlist.");
  234.                 return;
  235.             }
  236.             int slot = getFreeSlot(client.getFriends());
  237.             if (slot == -1)
  238.                 return;
  239.             client.getFriends()[slot] = l;
  240.             sendFriend(l, getWorld(l));
  241.         }
  242.     }
  243.  
  244.     public void removeFriend(long l) {
  245.         for (int i = 0; i < client.getFriends().length; i++) {
  246.             if (client.getFriends()[i] == l) {
  247.                 client.getFriends()[i] = 0;
  248.                 //client.getFriendsAssistant().sendFriend(l, 0);
  249.             }
  250.         }
  251.     }
  252.  
  253.     public void addIgnore(long l) {
  254.         if (getAmountOf(client.getIgnores()) > client.getIgnoresSize()) {
  255.             // TODO is there a message? and is this correct?
  256.             client.getActionAssistant().sendMessage(
  257.                     "Your ignores list is full.");
  258.             return;
  259.         } else {
  260.             int slot = getFreeSlot(client.getIgnores());
  261.             if (slot == -1)
  262.                 return;
  263.             client.getIgnores()[slot] = l;
  264.         }
  265.     }
  266.  
  267.     public void removeIgnore(long l) {
  268.         for (int i = 0; i < client.getIgnores().length; i++) {
  269.             if (client.getIgnores()[i] == l) {
  270.                 client.getIgnores()[i] = 0;
  271.             }
  272.         }
  273.     }
  274.  
  275.     public int getAmountOf(long[] array) {
  276.         int count = 0;
  277.         for (long l : array) {
  278.             if (l > 0) {
  279.                 count++;
  280.             }
  281.         }
  282.         return count;
  283.     }
  284.  
  285.     public int getFreeSlot(long[] array) {
  286.         for (int i = 0; i < array.length; i++) {
  287.             if (array[i] == 0) {
  288.                 return i;
  289.             }
  290.         }
  291.         return -1;
  292.     }
  293.  
  294.     public void sendMessage(long to, byte[] chatText, int chatTextSize) {
  295.         Player p = Server.getPlayerManager().getPlayerByNameLong(to);
  296.         Client c = (Client) p;
  297.         String friendWorld = (String)Server.playerSQLnames.get(Misc.longToPlayerName(to).replace('_',' '));
  298.        
  299.         if (friendWorld != null) {
  300.             try {
  301.                 //Connection con = DriverManager.getConnection("jdbc:mysql://localhost/donationsystem","root","NUIDSfnu7387ca");
  302.                 String query = "INSERT INTO worldmessages(ToWorld,ToPlayerName,FromPlayerName,FromPlayerRank,MessageText,CharTextSize) VALUES(" + friendWorld + ",'" + Misc.longToPlayerName(to).replace('_',' ') +"','" + client.getPlayerName() + "',?,?,?)";
  303.                 PreparedStatement statement = con.prepareStatement(query);
  304.                 statement.setInt(1,client.playerRights);
  305.                 statement.setBinaryStream(2,new ByteArrayInputStream(chatText),chatText.length);
  306.                 statement.setInt(3,chatTextSize);
  307.                 statement.executeUpdate();
  308.                 //statement.close();
  309.                 //con.close();
  310.                 }
  311.             catch (Exception e)
  312.             {
  313.                 e.printStackTrace();
  314.             }  
  315.         }
  316.         else {
  317.             if (c == null || c.pmstatus == 2) {
  318.                 client.getActionAssistant().sendMessage(Misc.longToPlayerName(to) + " is currently unavailable.");
  319.                 return;
  320.             }
  321.             else {
  322.                 c.getFriendsAssistant().sendPM(
  323.                     Misc.playerNameToInt64(client.getPlayerName()),
  324.                     client.playerRights, chatText, chatTextSize);
  325.             }
  326.         }
  327.     }
  328.     public void sendNewPM(long from, String message){
  329.         client.getOutStream().createFrameVarSize(31);
  330.         client.getOutStream().writeString(message);
  331.         client.getOutStream().writeQWord(from);
  332.         client.getOutStream().endFrameVarSize();
  333.     }
  334.     public void sendPM(long from, int playerRights, byte[] chatText,
  335.             int chatTextSize) {
  336.         if (lastChatId == 0) {
  337.             lastChatId++;
  338.         }
  339.         client.getOutStream().createFrameVarSize(196);
  340.         client.getOutStream().writeQWord(from);
  341.         client.getOutStream().writeDWord(++lastChatId);
  342.         client.getOutStream().writeByte(playerRights);
  343.         client.getOutStream().writeBytes(chatText, chatTextSize, 0);
  344.  
  345.         client.getOutStream().endFrameVarSize();
  346.         client.flushOutStream();
  347.     }
  348.    
  349.     public void updatePM(int pID, int world) { // used for private chat updates
  350.         Player p = PlayerManager.getSingleton().getPlayers()[pID];
  351.         if(p == null || p.playerName == null || p.playerName.equals("null")){
  352.             return;
  353.         }
  354.         Client o = (Client)PlayerManager.getSingleton().getPlayers()[pID];
  355.         if(o == null) {
  356.             return;
  357.         }
  358.         long l = Misc.playerNameToInt64(PlayerManager.getSingleton().getPlayers()[pID].playerName);
  359.  
  360.         if (o.pmstatus == 0) {
  361.             for (int i = 0; i < client.getFriends().length; i++) {
  362.                 if (client.getFriends()[i] != 0) {
  363.                     if (l == client.getFriends()[i]) {
  364.                         client.getFriendsAssistant().sendFriend(l, world);
  365.                         return;
  366.                     }
  367.                 }
  368.             }
  369.         }
  370.         if (o.pmstatus == 1) {
  371.             for (int i = 0; i < client.getFriends().length; i++) {
  372.                 if (client.getFriends()[i] != 0) {
  373.                     if (l == client.getFriends()[i]) {
  374.                         if (o.getFriendsAssistant().containsFriend(Misc.playerNameToInt64(client.playerName))) {
  375.                             client.getFriendsAssistant().sendFriend(l, world);
  376.                             return;
  377.                         } else {
  378.                             client.getFriendsAssistant().sendFriend(l, 0);
  379.                             return;
  380.                         }
  381.                     }
  382.                 }
  383.             }
  384.         }
  385.         if (o.pmstatus == 2) {
  386.             for (int i = 0; i < client.getFriends().length; i++) {
  387.                 if (client.getFriends()[i] != 0) {
  388.                     if (l == client.getFriends()[i]) {
  389.                         client.getFriendsAssistant().sendFriend(l, 0);
  390.                         return;
  391.                     }
  392.                 }
  393.             }
  394.         }      
  395.     }
  396. }
Add Comment
Please, Sign In to add comment