Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.43 KB | None | 0 0
  1. /*
  2.  * This program is free software: you can redistribute it and/or modify it under
  3.  * the terms of the GNU General Public License as published by the Free Software
  4.  * Foundation, either version 3 of the License, or (at your option) any later
  5.  * version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful, but WITHOUT
  8.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9.  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10.  * details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License along with
  13.  * this program. If not, see <http://www.gnu.org/licenses/>.
  14.  */
  15. package ru.catssoftware.gameserver.communitybbs;
  16.  
  17. import javolution.util.FastMap;
  18. import ru.catssoftware.Config;
  19. import ru.catssoftware.gameserver.cache.HtmCache;
  20. import ru.catssoftware.gameserver.communitybbs.handlers.*;
  21. import ru.catssoftware.gameserver.model.actor.instance.L2PcInstance;
  22. import ru.catssoftware.gameserver.network.L2GameClient;
  23. import ru.catssoftware.gameserver.network.SystemMessageId;
  24. import ru.catssoftware.gameserver.network.serverpackets.HideBoard;
  25. import ru.catssoftware.gameserver.network.serverpackets.ShowBoard;
  26. import ru.hwutils;
  27. import ru.masone.rutils;
  28.  
  29. import java.util.Map;
  30. import java.util.regex.Matcher;
  31. import java.util.regex.Pattern;
  32.  
  33.  
  34. public class CommunityBoard
  35. {
  36.     private static CommunityBoard   _instance;
  37.     private static Map<String, IBBSHandler > _handlers;
  38.     public static final String HTMBase = "data/html/CommunityBoard/";
  39.     public CommunityBoard()
  40.     {
  41.         _handlers = new FastMap<String, IBBSHandler>();
  42.         registerBBSHandler(new BBSFavorites());
  43.         registerBBSHandler(new GMShop());
  44.         registerBBSHandler(new ProfManager());
  45.         registerBBSHandler(new BBSPages());
  46.         registerBBSHandler(new BBSRegion());
  47.     }
  48.  
  49.     public static CommunityBoard getInstance()
  50.     {
  51.         if (_instance == null)
  52.             _instance = new CommunityBoard();
  53.         return _instance;
  54.     }
  55.  
  56.     /**
  57.      * Обработка команды _bbs<br>
  58.      * @param activeChar as L2PcInstance - персонаж, для которого открывается BBS<br>
  59.      * @param command as String - полная команда (с параметрами), передаваемая обработчику
  60.      */
  61.     public boolean handleCommand(L2PcInstance activeChar, String command) {
  62.         String cmd = command.substring(4); // Стрипаем _bbs
  63.         String params = "";
  64.         if(cmd.indexOf(" ")!=-1) {
  65.             params = cmd.substring(cmd.indexOf(" ")+1);
  66.             cmd = cmd.substring(0,cmd.indexOf(" "));
  67.         }
  68.         IBBSHandler handler = getHandler(cmd);
  69.         if (handler!=null) {
  70.             String result = handler.handleCommand(activeChar, cmd, params);
  71.             if(result==null) {
  72.                 activeChar.sendPacket(new HideBoard());
  73.                 return true;
  74.             }
  75.             if(result.endsWith(".htm")) {
  76.                 result = HtmCache.getInstance().getHtm(HTMBase+result, activeChar);
  77.                 if(result==null) {
  78.                     activeChar.sendPacket(new HideBoard());
  79.                     return true;
  80.                 }
  81.             }
  82.             Pattern p = Pattern.compile("bypass +-h");  // убираем -h
  83.             Matcher m = p.matcher(result);
  84.             if(m.find())
  85.                 result = m.replaceAll("bypass");
  86.             separateAndSend(result, activeChar);
  87.  
  88.             return true;
  89.         }
  90.         return false;
  91.     }
  92.     /**
  93.      * Преобразование "общих" (т.е. не написанных специально для BBS) html<br>
  94.      * При преобразовании:<br>
  95.      * - удаляется &lt;title&gt; (приводит к падению клиента)<br>
  96.      * - добавляются два &lt;br&gt; для опускания содержимого<br>
  97.      * - все bypass [-h] заменяются на bypass -h _bbs для корректной передачи в обработчик<br>
  98.      * @param result as String - имя htm-файла или его содержимое<br>
  99.      * @return as String - переработаный результат
  100.      */
  101.     public static String parseOldFormat(String result, L2PcInstance talker ) {
  102.         if(result == null)
  103.             return null;
  104.         if(result.endsWith(".htm")) {
  105.             result = HtmCache.getInstance().getHtm(HTMBase+result, talker);
  106.         }
  107.         if(result==null)
  108.             result = "<html><body><br><br><center>Команда не реализована.</center></body></html>";
  109.         Pattern p = Pattern.compile("<title>.*?</title>");  // убираем <tile> иначе клиент вылетает
  110.         Matcher m = p.matcher(result);
  111.         if(m.find())
  112.             result = m.replaceAll("");
  113.  
  114.         result = result.replace("<body>", "<body><br><br>"); // Опустить в центр доски
  115.         p = Pattern.compile("bypass +(-h)? ?");
  116.         m = p.matcher(result);
  117.         if(m.find())
  118.             result = m.replaceAll("bypass -h _bbs");
  119.         return result;
  120.     }
  121.     /**
  122.      * Получить обработчик по имени команды<br>
  123.      * @param command as String - имя команды<br>
  124.      * @return as IBBSHandler - обработчик или null
  125.      */
  126.     public IBBSHandler getHandler(String command) {
  127.         return _handlers.get(command);
  128.     }
  129.     /**
  130.      * Зарегистрировать обработчик<br>
  131.      * @param handler as IBBSHandler - регистрируемый обработчик
  132.      */
  133.     public void registerBBSHandler(IBBSHandler handler) {
  134.         for(String s: handler.getCommands())
  135.             _handlers.put(s, handler);
  136.     }
  137.  
  138.     public void handleCommands(L2GameClient client, String command)
  139.     {
  140.         L2PcInstance activeChar = client.getActiveChar();
  141.         if (activeChar == null)
  142.             return;
  143.         switch (Config.COMMUNITY_TYPE)
  144.         {
  145.             default:
  146.             case 0: //disabled
  147.                 activeChar.sendPacket(SystemMessageId.CB_OFFLINE);
  148.                 break;
  149.             case 1: // old
  150.             case 2: // new
  151.                 for(String s : Config.BBS_DISABLED_PAGES)
  152.                     if(s.length() > 0 && command.startsWith(s)) {
  153.                         separateAndSend(HtmCache.getInstance().getHtm(HTMBase+"disabled.htm", activeChar), activeChar);
  154.                         return;
  155.                     }
  156.                 if(!GMShop.checkMagicCondition(activeChar)) {
  157.                     separateAndSend(HtmCache.getInstance().getHtm(HTMBase+"disabled.htm", activeChar), activeChar);
  158.                     return;
  159.                 }
  160.  
  161.                 if (!handleCommand(activeChar, command) )  {
  162.                     separateAndSend(HtmCache.getInstance().getHtm(HTMBase+"disabled.htm", activeChar), activeChar);
  163.                 }
  164.         }
  165.     }
  166.  
  167.     public static void separateAndSend(String html, L2PcInstance acha)
  168.     {
  169.         if (html == null)
  170.             return;
  171.         acha.setLastPage(html);
  172.  
  173.         String ip = "N/A";
  174.         ip = acha.getClient().getHostAddress();
  175.  
  176.         html = html.replace("%username%", acha.getName());
  177.         html = html.replace("%nobles%", hwutils.getNoble(acha));
  178.         html = html.replace("%hero%", hwutils.getHero(acha));
  179.         html = html.replace("%premium%", hwutils.getPremium(acha));
  180.         html = html.replace("%premBuff%", hwutils.getPremBuff(acha));
  181.         html = html.replace("%accname%", acha.getAccountName());
  182.         html = html.replace("%ip%", ip);
  183.         html = html.replace("%class%", rutils.getClassName(acha.getClassId().getId()));
  184.         html = html.replace("%servername%",Config.SERVER_NAME);
  185.  
  186.         html = html.replace("%clanname%", acha.getClan() != null ? acha.getClan().getName() : "Вы не состоите в клане!");
  187.         /*
  188.         html = html.replace("%allyname%", acha.getClan().getAllyName() != null ? acha.getClan().getAllyName() : "Ваш клан не состоит в альянсе!");
  189.         html = html.replace("%clanleader%", acha.getClan().getLeaderName());
  190.         html = html.replace("%clanlvl%", String.valueOf(acha.getClan().getLevel()));
  191.         html = html.replace("%testcl%", Integer.toString(acha.getClan().getLevel()));
  192.         html = html.replace("%membercount%", String.valueOf(acha.getClan().getMembersCount()));
  193.         html = html.replace("%clanpenalty%", String.valueOf(acha.getClan().getAllyPenaltyExpiryTime()));
  194.         */
  195.  
  196.  
  197.         if (html.length() < 4090)
  198.         {
  199.             acha.sendPacket(new ShowBoard(html, "101"));
  200.             acha.sendPacket(new ShowBoard(null, "102"));
  201.             acha.sendPacket(new ShowBoard(null, "103"));
  202.         }
  203.         else if (html.length() < 8180)
  204.         {
  205.             acha.sendPacket(new ShowBoard(html.substring(0, 4090), "101"));
  206.             acha.sendPacket(new ShowBoard(html.substring(4090, html.length()), "102"));
  207.             acha.sendPacket(new ShowBoard(null, "103"));
  208.         }
  209.         else if (html.length() < 12270)
  210.         {
  211.             acha.sendPacket(new ShowBoard(html.substring(0, 4090), "101"));
  212.             acha.sendPacket(new ShowBoard(html.substring(4090, 8180), "102"));
  213.             acha.sendPacket(new ShowBoard(html.substring(8180, html.length()), "103"));
  214.         }
  215.     }
  216.  
  217.  
  218.  
  219.     public void write(L2GameClient client, String...args) {
  220.  
  221.     }
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement