Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package server.util;
- import java.sql.*;
- import java.security.MessageDigest;
- import server.model.players.Client;
- import java.util.ArrayList;
- public class BanHandler
- {
- //public ArrayList<BanInstance> bans = new ArrayList<BanInstance>();
- public BanHandler()
- {
- //nah
- }
- public static int getReturnCode(String user) //returns returncode (if any) for client
- {
- ArrayList<Integer> idL = new ArrayList<Integer>();
- PreparedStatement ps = null;
- Connection con = null;
- int fid = 0;
- int id = 0;
- long time = 0;
- try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = DriverManager.getConnection("jdbc:mysql://localhost/ac","root","");
- ps = con.prepareStatement("SELECT id,time FROM bans WHERE name = ?");
- ps.setString(1, user);
- ResultSet results = ps.executeQuery();
- if(results.next()) {
- id = results.getInt("id");
- time = results.getLong("time");
- if (System.currentTimeMillis() < time || time == -1)
- idL.add(id);
- } else {
- return -1;
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- return -1;
- } finally {
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- return -1;
- }
- }
- for (int i : idL)
- {
- if (i > 3 && i < 10)
- fid = i;
- }
- long timeLeft = time - System.currentTimeMillis();
- if (timeLeft < 0)
- return -1;
- int daysLeft = 1;
- switch (fid)
- {
- case 4: return 4;
- case 5:
- case 6:
- case 7:
- case 8:
- case 9:
- while (timeLeft > 0)
- {
- timeLeft -= 86400000;
- daysLeft++;
- }
- return daysLeft+21; //base RC = 1 day left
- }
- return -1;//default, not banned or muted
- }
- public static int muted(String user) //returns mute level. called on client init done
- {
- ArrayList<Integer> idL = new ArrayList<Integer>();
- PreparedStatement ps = null;
- Connection con = null;
- int fid = 0;
- long time = 0;
- try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = DriverManager.getConnection("jdbc:mysql://localhost/ac","root","");
- ps = con.prepareStatement("SELECT id,time FROM bans WHERE name = ?");
- ps.setString(1, user);
- ResultSet results = ps.executeQuery();
- if(results.next()) {
- int id = results.getInt("id");
- time = results.getLong("time");
- if (System.currentTimeMillis() < time || time == -1)
- fid = id;
- } else {
- return -1;
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- return 0;
- } finally {
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- return 0;
- }
- }
- switch (fid)
- {
- case 1:
- case 7:
- case 8:
- return 2; //perm mute
- case 2:
- case 3:
- case 9:
- return 1;//temp mute ( ゚ д ゚ )彡
- }
- return 0;
- }
- public static boolean ban(Client c, String user, int rqid)
- {
- ArrayList<Integer> idL = new ArrayList<Integer>();
- PreparedStatement ps = null;
- Connection con = null;
- int id = 0;
- long time = 0;
- try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = DriverManager.getConnection("jdbc:mysql://localhost/ac","root","");
- ps = con.prepareStatement("SELECT id,time FROM bans WHERE name = ?");
- ps.setString(1, user);
- ResultSet results = ps.executeQuery();
- if(results.next()) {
- id = results.getInt("id");
- time = results.getLong("time");
- if (System.currentTimeMillis() < time || time == -1)
- idL.add(id);
- } else {
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- return false;
- } finally {
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- }
- if (rqid > 4 && rqid < 7)
- {
- for (int ids : idL)
- {
- if (ids > 4 && ids < 7)
- {
- c.sendMessage("This user already has an active ban");
- return false;
- }
- }
- }
- if (rqid > 0 && rqid < 4)
- {
- for (int ids : idL)
- {
- if (ids > 0 && ids < 4)
- {
- c.sendMessage("This user already has an active mute");
- return false;
- }
- }
- }
- try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = DriverManager.getConnection("jdbc:mysql://localhost/ac","root","");
- ps = con.prepareStatement("INSERT INTO bans (name, id, time) VALUES (?,?,?)");
- ps.setString(1, user);
- ps.setInt(2, rqid);
- ps.setLong(3, getTime(rqid));
- ps.executeUpdate();
- }
- catch (Exception e) {
- e.printStackTrace();
- return false;
- } finally {
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- }
- return true;
- }
- public static long getTime(int x)
- {
- long tta = -1;
- switch(x)
- {
- case 2:
- case 5:
- tta = System.currentTimeMillis()+86400000*3;
- break;
- case 3:
- case 6:
- tta = System.currentTimeMillis()+86400000*6;
- break;
- }
- return tta;
- }
- /*
- 0 = unbanned
- 1 = perm mute
- 2 = temp mute (3 days)
- 3 = temp mute (7 days)
- 4 = perm ban
- 5 = temp ban (3 days)
- 6 = temp ban (7 days)
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement