Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. package server.util;
  2.  
  3. import java.sql.*;
  4. import java.security.MessageDigest;
  5. import server.model.players.Client;
  6. import java.util.ArrayList;
  7.  
  8. public class BanHandler
  9. {
  10. //public ArrayList<BanInstance> bans = new ArrayList<BanInstance>();
  11. public BanHandler()
  12. {
  13. //nah
  14. }
  15. public static int getReturnCode(String user) //returns returncode (if any) for client
  16. {
  17. ArrayList<Integer> idL = new ArrayList<Integer>();
  18. PreparedStatement ps = null;
  19. Connection con = null;
  20. int fid = 0;
  21. int id = 0;
  22. long time = 0;
  23. try{
  24. Class.forName("com.mysql.jdbc.Driver").newInstance();
  25. con = DriverManager.getConnection("jdbc:mysql://localhost/ac","root","");
  26. ps = con.prepareStatement("SELECT id,time FROM bans WHERE name = ?");
  27. ps.setString(1, user);
  28. ResultSet results = ps.executeQuery();
  29. if(results.next()) {
  30.  
  31. id = results.getInt("id");
  32. time = results.getLong("time");
  33. if (System.currentTimeMillis() < time || time == -1)
  34. idL.add(id);
  35.  
  36. } else {
  37. return -1;
  38. }
  39. }
  40. catch (Exception e) {
  41. e.printStackTrace();
  42. return -1;
  43. } finally {
  44. try {
  45. ps.close();
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. return -1;
  49. }
  50. }
  51. for (int i : idL)
  52. {
  53. if (i > 3 && i < 10)
  54. fid = i;
  55. }
  56. long timeLeft = time - System.currentTimeMillis();
  57. if (timeLeft < 0)
  58. return -1;
  59. int daysLeft = 1;
  60.  
  61. switch (fid)
  62. {
  63. case 4: return 4;
  64. case 5:
  65. case 6:
  66. case 7:
  67. case 8:
  68. case 9:
  69. while (timeLeft > 0)
  70. {
  71. timeLeft -= 86400000;
  72. daysLeft++;
  73. }
  74. return daysLeft+21; //base RC = 1 day left
  75. }
  76.  
  77. return -1;//default, not banned or muted
  78. }
  79. public static int muted(String user) //returns mute level. called on client init done
  80. {
  81. ArrayList<Integer> idL = new ArrayList<Integer>();
  82. PreparedStatement ps = null;
  83. Connection con = null;
  84. int fid = 0;
  85. long time = 0;
  86. try{
  87. Class.forName("com.mysql.jdbc.Driver").newInstance();
  88. con = DriverManager.getConnection("jdbc:mysql://localhost/ac","root","");
  89. ps = con.prepareStatement("SELECT id,time FROM bans WHERE name = ?");
  90. ps.setString(1, user);
  91. ResultSet results = ps.executeQuery();
  92. if(results.next()) {
  93. int id = results.getInt("id");
  94. time = results.getLong("time");
  95. if (System.currentTimeMillis() < time || time == -1)
  96. fid = id;
  97.  
  98. } else {
  99. return -1;
  100. }
  101. }
  102. catch (Exception e) {
  103. e.printStackTrace();
  104. return 0;
  105. } finally {
  106. try {
  107. ps.close();
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. return 0;
  111. }
  112. }
  113.  
  114. switch (fid)
  115. {
  116. case 1:
  117. case 7:
  118. case 8:
  119. return 2; //perm mute
  120. case 2:
  121. case 3:
  122. case 9:
  123. return 1;//temp mute ( ゚ д ゚ )彡
  124. }
  125. return 0;
  126. }
  127. public static boolean ban(Client c, String user, int rqid)
  128. {
  129. ArrayList<Integer> idL = new ArrayList<Integer>();
  130. PreparedStatement ps = null;
  131. Connection con = null;
  132. int id = 0;
  133. long time = 0;
  134. try{
  135. Class.forName("com.mysql.jdbc.Driver").newInstance();
  136. con = DriverManager.getConnection("jdbc:mysql://localhost/ac","root","");
  137. ps = con.prepareStatement("SELECT id,time FROM bans WHERE name = ?");
  138. ps.setString(1, user);
  139. ResultSet results = ps.executeQuery();
  140. if(results.next()) {
  141.  
  142. id = results.getInt("id");
  143. time = results.getLong("time");
  144. if (System.currentTimeMillis() < time || time == -1)
  145. idL.add(id);
  146.  
  147. } else {
  148.  
  149. }
  150. }
  151. catch (Exception e) {
  152. e.printStackTrace();
  153. return false;
  154. } finally {
  155. try {
  156. ps.close();
  157. } catch (SQLException e) {
  158. e.printStackTrace();
  159. return false;
  160. }
  161. }
  162. if (rqid > 4 && rqid < 7)
  163. {
  164. for (int ids : idL)
  165. {
  166. if (ids > 4 && ids < 7)
  167. {
  168. c.sendMessage("This user already has an active ban");
  169. return false;
  170. }
  171. }
  172. }
  173. if (rqid > 0 && rqid < 4)
  174. {
  175. for (int ids : idL)
  176. {
  177. if (ids > 0 && ids < 4)
  178. {
  179. c.sendMessage("This user already has an active mute");
  180. return false;
  181. }
  182. }
  183. }
  184.  
  185. try{
  186. Class.forName("com.mysql.jdbc.Driver").newInstance();
  187. con = DriverManager.getConnection("jdbc:mysql://localhost/ac","root","");
  188. ps = con.prepareStatement("INSERT INTO bans (name, id, time) VALUES (?,?,?)");
  189. ps.setString(1, user);
  190. ps.setInt(2, rqid);
  191. ps.setLong(3, getTime(rqid));
  192. ps.executeUpdate();
  193.  
  194. }
  195. catch (Exception e) {
  196. e.printStackTrace();
  197. return false;
  198. } finally {
  199. try {
  200. ps.close();
  201. } catch (SQLException e) {
  202. e.printStackTrace();
  203. return false;
  204. }
  205. }
  206. return true;
  207.  
  208. }
  209. public static long getTime(int x)
  210. {
  211. long tta = -1;
  212. switch(x)
  213. {
  214. case 2:
  215. case 5:
  216. tta = System.currentTimeMillis()+86400000*3;
  217. break;
  218. case 3:
  219. case 6:
  220. tta = System.currentTimeMillis()+86400000*6;
  221. break;
  222.  
  223. }
  224. return tta;
  225. }
  226. /*
  227. 0 = unbanned
  228. 1 = perm mute
  229. 2 = temp mute (3 days)
  230. 3 = temp mute (7 days)
  231. 4 = perm ban
  232. 5 = temp ban (3 days)
  233. 6 = temp ban (7 days)
  234. */
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement