AutobanManager.java
Upload User: gwt600
Upload Date: 2021-06-03
Package Size: 704k
Code Size: 5k
Category:

Games

Development Platform:

Java

  1. /*
  2. This file is part of the OdinMS Maple Story Server
  3. Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc> 
  4. Matthias Butz <matze@odinms.de>
  5. Jan Christian Meyer <vimes@odinms.de>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License version 3
  8. as published by the Free Software Foundation. You may not use, modify
  9. or distribute this program under any other version of the
  10. GNU Affero General Public License.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU Affero General Public License for more details.
  15. You should have received a copy of the GNU Affero General Public License
  16. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18. /*
  19.  * To change this template, choose Tools | Templates
  20.  * and open the template in the editor.
  21.  */
  22. package net.sf.odinms.server;
  23. import java.rmi.RemoteException;
  24. import java.util.HashMap;
  25. import java.util.LinkedList;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import java.util.TreeSet;
  30. import net.sf.odinms.client.MapleClient;
  31. import net.sf.odinms.tools.MaplePacketCreator;
  32. //import org.slf4j.Logger;
  33. //import org.slf4j.LoggerFactory;
  34. /**
  35.  *
  36.  * @author Matze
  37.  */
  38. public class AutobanManager implements Runnable {
  39.     private static class ExpirationEntry implements Comparable<ExpirationEntry> {
  40.         public long time;
  41.         public int acc;
  42.         public int points;
  43.         public ExpirationEntry(long time, int acc, int points) {
  44.             this.time = time;
  45.             this.acc = acc;
  46.             this.points = points;
  47.         }
  48.         public int compareTo(AutobanManager.ExpirationEntry o) {
  49.             return (int) (time - o.time);
  50.         }
  51.     }
  52.     private Map<Integer, Integer> points = new HashMap<Integer, Integer>();
  53.     private Map<Integer, List<String>> reasons = new HashMap<Integer, List<String>>();
  54.     private Set<ExpirationEntry> expirations = new TreeSet<ExpirationEntry>();
  55.     private static final int AUTOBAN_POINTS = 1000;
  56.     private static AutobanManager instance = null;
  57. // private static Logger log = LoggerFactory.getLogger(AutobanManager.class);
  58.     public static AutobanManager getInstance() {
  59.         if (instance == null) {
  60.             instance = new AutobanManager();
  61.         }
  62.         return instance;
  63.     }
  64.     public void autoban(MapleClient c, String reason) {
  65.         if (c.getPlayer().isGM()) {
  66.             return;
  67.         }
  68.         addPoints(c, AUTOBAN_POINTS, 0, reason);
  69.     }
  70.     public synchronized void addPoints(MapleClient c, int points, long expiration, String reason) {
  71.         // if (c.getPlayer().isGM()) return;
  72.         int acc = c.getPlayer().getAccountID();
  73.         List<String> reasonList;
  74.         if (this.points.containsKey(acc)) {
  75.             if (this.points.get(acc) >= AUTOBAN_POINTS) {
  76.                 return;
  77.             }
  78.             this.points.put(acc, this.points.get(acc) + points);
  79.             reasonList = this.reasons.get(acc);
  80.             reasonList.add(reason);
  81.         } else {
  82.             this.points.put(acc, points);
  83.             reasonList = new LinkedList<String>();
  84.             reasonList.add(reason);
  85.             this.reasons.put(acc, reasonList);
  86.         }
  87.         if (this.points.get(acc) >= AUTOBAN_POINTS) {
  88.             String name = c.getPlayer().getName();
  89.             StringBuilder banReason = new StringBuilder("Autoban for Character ");
  90.             banReason.append(name);
  91.             banReason.append(" (IP ");
  92.             banReason.append(c.getSession().getRemoteAddress().toString());
  93.             banReason.append("): ");
  94.             for (String s : reasons.get(acc)) {
  95.                 banReason.append(s);
  96.                 banReason.append(", ");
  97.             }
  98.             if (!c.getPlayer().isGM()) {
  99.                 c.getPlayer().ban(banReason.toString(), false);
  100.                 try {
  101.                     c.getChannelServer().getWorldInterface().broadcastGMMessage(null, MaplePacketCreator.serverNotice(0, "" + name + " has been banned by the system. (Last reason: " + reason + ")").getBytes());
  102.                 } catch (RemoteException e) {
  103.                     c.getChannelServer().reconnectWorld();
  104.                 }
  105.             }
  106.             return;
  107.         }
  108.         if (expiration > 0) {
  109.             expirations.add(new ExpirationEntry(System.currentTimeMillis() + expiration, acc, points));
  110.         }
  111.     }
  112.     public void run() {
  113.         long now = System.currentTimeMillis();
  114.         for (ExpirationEntry e : expirations) {
  115.             if (e.time <= now) {
  116.                 this.points.put(e.acc, this.points.get(e.acc) - e.points);
  117.             } else {
  118.                 return;
  119.             }
  120.         }
  121.     }
  122. }