Advertisement
Guest User

MathQuiz

a guest
Apr 4th, 2023
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.79 KB | Gaming | 0 0
  1. diff --git a/aCis_datapack/data/xml/mathquiz.xml b/aCis_datapack/data/xml/mathquiz.xml
  2. new file mode 100644
  3. index 0000000..ad88472
  4. --- /dev/null
  5. +++ b/aCis_datapack/data/xml/mathquiz.xml
  6. @@ -0,0 +1,18 @@
  7. +<?xml version='1.0' encoding='utf-8'?>
  8. +<list>
  9. +   <MathQuiz QuizTaskTimeManager ="15" KillsTask="20" QuestionTime="120" Attempts="4" Punishment="2"/>
  10. +
  11. +    <!-- QuizTaskTime - The amount of time in seconds for which the task will run if the player reaches the KillsTask.
  12. +         Once the player reaches the number of kills specified by KillsTask, the quiz will appear after the next time interval specified by this value. -->
  13. +
  14. +    <!-- KillsTask - The minimum number of monsters the player must kill to start the task.
  15. +         If set to 0, the task will never start. -->
  16. +
  17. +    <!-- QuestionTime - The time limit in seconds for the player to answer the question.
  18. +         If the player fails to answer within the specified time, they will be disconnected from the game. -->
  19. +
  20. +    <!-- Attempts - The number of attempts allowed for the player to provide the correct answer. -->
  21. +
  22. +    <!-- Punishment - Selects the punishment for the player in case of a wrong answer in the quiz.
  23. +         If value is 1 will logout the player in 5 seconds. If value is 2 will teleport the player to town in 5 seconds. -->
  24. +</list>
  25. diff --git a/aCis_gameserver/java/net/sf/l2j/gameserver/GameServer.java b/aCis_gameserver/java/net/sf/l2j/gameserver/GameServer.java
  26. index e682cc6..d0e351e 100644
  27. --- a/aCis_gameserver/java/net/sf/l2j/gameserver/GameServer.java
  28. +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/GameServer.java
  29. @@ -59,6 +59,7 @@
  30.  import net.sf.l2j.gameserver.data.xml.InstantTeleportData;
  31.  import net.sf.l2j.gameserver.data.xml.ItemData;
  32.  import net.sf.l2j.gameserver.data.xml.MapRegionData;
  33. +import net.sf.l2j.gameserver.data.xml.MathQuizData;
  34.  import net.sf.l2j.gameserver.data.xml.MultisellData;
  35.  import net.sf.l2j.gameserver.data.xml.NewbieBuffData;
  36.  import net.sf.l2j.gameserver.data.xml.NpcData;
  37. @@ -273,6 +274,9 @@
  38.         LOGGER.info("Loaded {} target handlers.", TargetHandler.getInstance().size());
  39.         LOGGER.info("Loaded {} user command handlers.", UserCommandHandler.getInstance().size());
  40.        
  41. +       StringUtil.printSection("MathQuiz");
  42. +       MathQuizData.getInstance();
  43. +      
  44.         StringUtil.printSection("System");
  45.         Runtime.getRuntime().addShutdownHook(Shutdown.getInstance());
  46.        
  47. diff --git a/aCis_gameserver/java/net/sf/l2j/gameserver/data/xml/MathQuizData.java b/aCis_gameserver/java/net/sf/l2j/gameserver/data/xml/MathQuizData.java
  48. new file mode 100644
  49. index 0000000..bd57ac6
  50. --- /dev/null
  51. +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/data/xml/MathQuizData.java
  52. @@ -0,0 +1,56 @@
  53. +package net.sf.l2j.gameserver.data.xml;
  54. +
  55. +import java.nio.file.Path;
  56. +import java.util.ArrayList;
  57. +import java.util.List;
  58. +
  59. +import net.sf.l2j.commons.data.xml.IXmlReader;
  60. +
  61. +import net.sf.l2j.gameserver.model.actor.MathQuiz;
  62. +
  63. +import org.w3c.dom.Document;
  64. +
  65. +/**
  66. + * @author Baggos
  67. + */
  68. +public class MathQuizData implements IXmlReader
  69. +{
  70. +   private final List<MathQuiz> _config = new ArrayList<>();
  71. +  
  72. +   protected MathQuizData()
  73. +   {
  74. +       load();
  75. +   }
  76. +  
  77. +   @Override
  78. +   public void load()
  79. +   {
  80. +       parseFile("./data/xml/mathquiz.xml");
  81. +       LOGGER.info("Quiz XML File Loaded!");
  82. +   }
  83. +  
  84. +   @Override
  85. +   public void parseDocument(Document doc, Path path)
  86. +   {
  87. +       forEach(doc, "list", listNode -> forEach(listNode, "MathQuiz", iconNode ->
  88. +       {
  89. +           var set = parseAttributes(iconNode);
  90. +           _config.add(new MathQuiz(set));
  91. +       }));
  92. +   }
  93. +  
  94. +   public void reload()
  95. +   {
  96. +       load();
  97. +   }
  98. +  
  99. +   public static MathQuizData getInstance()
  100. +   {
  101. +       return SingletonHolder.INSTANCE;
  102. +   }
  103. +  
  104. +   private static class SingletonHolder
  105. +   {
  106. +       protected static final MathQuizData INSTANCE = new MathQuizData();
  107. +   }
  108. +}
  109. diff --git a/aCis_gameserver/java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminReload.java b/aCis_gameserver/java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminReload.java
  110. index 5f11f94..eb1e5ee 100644
  111. --- a/aCis_gameserver/java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminReload.java
  112. +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminReload.java
  113. @@ -14,6 +14,7 @@
  114.  import net.sf.l2j.gameserver.data.xml.DoorData;
  115.  import net.sf.l2j.gameserver.data.xml.InstantTeleportData;
  116.  import net.sf.l2j.gameserver.data.xml.ItemData;
  117. +import net.sf.l2j.gameserver.data.xml.MathQuizData;
  118.  import net.sf.l2j.gameserver.data.xml.MultisellData;
  119.  import net.sf.l2j.gameserver.data.xml.NpcData;
  120.  import net.sf.l2j.gameserver.data.xml.ScriptData;
  121. @@ -122,6 +123,11 @@
  122.                     ZoneManager.getInstance().reload();
  123.                     player.sendMessage("Zones have been reloaded.");
  124.                 }
  125. +               else if (type.startsWith("quiz"))
  126. +               {
  127. +                   MathQuizData.getInstance().reload();
  128. +                   player.sendMessage("MathQuiz have been reloaded.");
  129. +               }
  130.                 else
  131.                     sendUsage(player);
  132.             }
  133. @@ -137,7 +143,7 @@
  134.     {
  135.         player.sendMessage("Usage : //reload <admin|announcement|buylist|config>");
  136.         player.sendMessage("Usage : //reload <crest|cw|door|htm|item|multisell|npc>");
  137. -       player.sendMessage("Usage : //reload <npcwalker|script|skill|teleport|zone>");
  138. +       player.sendMessage("Usage : //reload <npcwalker|script|skill|teleport|zone|quiz>");
  139.     }
  140.    
  141.     @Override
  142. diff --git a/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Creature.java b/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Creature.java
  143. index 9009a45..968f0a3 100644
  144. --- a/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Creature.java
  145. +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Creature.java
  146. @@ -75,6 +75,7 @@
  147.  import net.sf.l2j.gameserver.skills.funcs.FuncRegenHpMul;
  148.  import net.sf.l2j.gameserver.skills.funcs.FuncRegenMpMul;
  149.  import net.sf.l2j.gameserver.taskmanager.AttackStanceTaskManager;
  150. +import net.sf.l2j.gameserver.taskmanager.MathQuizTaskManager;
  151.  
  152.  /**
  153.   * An instance type extending {@link WorldObject} which represents the mother class of all character objects of the world such as players, NPCs and monsters.
  154. @@ -487,6 +488,13 @@
  155.        
  156.         calculateRewards(killer);
  157.        
  158. +       if (MathQuiz.killsTask > 0)
  159. +       {
  160. +           MathQuizTaskManager.countKills();
  161. +           if (MathQuizTaskManager._killCount == MathQuiz.killsTask)
  162. +               MathQuizTaskManager.getInstance().add((Player) killer);
  163. +       }
  164. +      
  165.         // Send the Server->Client packet StatusUpdate with current HP and MP to all other Player to inform
  166.         getStatus().broadcastStatusUpdate();
  167.        
  168. diff --git a/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/MathQuiz.java b/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/MathQuiz.java
  169. new file mode 100644
  170. index 0000000..d625d3a
  171. --- /dev/null
  172. +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/MathQuiz.java
  173. @@ -0,0 +1,147 @@
  174. +package net.sf.l2j.gameserver.model.actor;
  175. +
  176. +import java.util.HashMap;
  177. +import java.util.Map;
  178. +import java.util.Random;
  179. +import java.util.concurrent.ScheduledFuture;
  180. +
  181. +import net.sf.l2j.commons.data.StatSet;
  182. +import net.sf.l2j.commons.pool.ThreadPool;
  183. +
  184. +import net.sf.l2j.gameserver.data.xml.MapRegionData.TeleportType;
  185. +import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  186. +import net.sf.l2j.gameserver.taskmanager.MathQuizTaskManager;
  187. +
  188. +/**
  189. + * @author Baggos
  190. + */
  191. +public class MathQuiz
  192. +{
  193. +   private static final Random randomGen = new Random();
  194. +   private static final Map<Player, Integer> attemptsMap = new HashMap<>();
  195. +   private static ScheduledFuture<?> inTime;
  196. +   private static boolean _showQuestionOpened = false;
  197. +   public static int QuizTaskTime;
  198. +   public static int killsTask;
  199. +   private static int QuestionTimeLimit; // seconds
  200. +   private static int maxAttempts;
  201. +   private static int _punishment;
  202. +  
  203. +   public MathQuiz(StatSet set)
  204. +   {
  205. +       QuizTaskTime = set.getInteger("QuizTaskTimeManager");
  206. +       killsTask = set.getInteger("KillsTask");
  207. +       QuestionTimeLimit = set.getInteger("QuestionTime");
  208. +       maxAttempts = set.getInteger("Attempts");
  209. +       _punishment = set.getInteger("Punishment");
  210. +   }
  211. +  
  212. +   public static void startQuestionWindow(Player player)
  213. +   {
  214. +       if (killsTask == 0 || _showQuestionOpened == true)
  215. +           return;
  216. +      
  217. +       showQuestion(player);
  218. +       inTime = ThreadPool.schedule(() ->
  219. +       {
  220. +           var html = new NpcHtmlMessage(0);
  221. +           String htmlContent2 = "<html><head><title>Error Message</title></head>" + "<body><center><font color=\"FF0000\">You didn't answer the math question in time.</font></center><br><br>" + "<center>" + (_punishment == 1 ? "Your character will be disconnected in 5 seconds." : "You will be teleported in Town in 5 seconds.") + "</center></body></html>";
  222. +           html.setHtml(htmlContent2);
  223. +           player.sendPacket(html);
  224. +           ThreadPool.schedule(() -> handlePunishment(player), 5000);
  225. +       }, QuestionTimeLimit * 1000);
  226. +   }
  227. +  
  228. +   public static void showQuestion(Player player)
  229. +   {
  230. +       var html = new NpcHtmlMessage(0);
  231. +       var attempts = attemptsMap.getOrDefault(player, 0);
  232. +       if (attempts > maxAttempts)
  233. +       {
  234. +           String htmlContent = "<html><head><title>Error Message</title></head>" + "<body><center><font color=\"FF0000\">Incorrect Answer</font></center><br><br>" + "<center>You have exceeded the maximum number of attempts.</center><br>" + "<center>" + (_punishment == 1 ? "Your character will be disconnected in 5 seconds." : "You will be teleported in Town in 5 seconds.") + "</center></body></html>";
  235. +           html.setHtml(htmlContent);
  236. +           player.sendPacket(html);
  237. +           ThreadPool.schedule(() -> handlePunishment(player), 5000);
  238. +           return;
  239. +       }
  240. +      
  241. +       int evenNumber = getRandomEvenNumber(), oddNumber = getRandomOddNumber();
  242. +       boolean firstNumberIsEven = randomGen.nextBoolean();
  243. +       int a = firstNumberIsEven ? evenNumber : oddNumber;
  244. +       int b = firstNumberIsEven ? oddNumber : evenNumber;
  245. +       var numbers = "<center>Example of Even Numbers:<font color=\"LEVEL\"> 2, 4, 6, 8, 10</font><br>" + "Example of Odd Numbers:<font color=\"LEVEL\"> 1, 3, 5, 7, 9, 11</font></center>";
  246. +       var question = String.format("Which of the following two numbers is even:<font color=\"LEVEL\"> %d or %d?</font>", a, b);
  247. +       var htmlContent = "<html><head><title>Locate the even number.</title></head>" + "<body><center><font color=\"LEVEL\">Math Question</font></center><br><br>" + "<center>You have " + maxAttempts + " chances to choose the even number!</center><br>" + "<center>Don't worry, take your time. <font color=\"LEVEL\">You have " + QuestionTimeLimit + " second(s).</font></center><br>" + "<center>" + question + "</center><br><br>" + "<center><edit var=\"answer\" width=50></center><br><br>" + "<center><button value=\"Submit\" action=\"bypass -h question " + evenNumber + " $answer " + (evenNumber % 2 == 0 ? "1" : "0") + "\" width=80 height=20 back=\"sek.cbui94\" fore=\"sek.cbui92\"></center><br><br>" + "<center>" + numbers + "</center></body></html>";
  248. +       html.setHtml(htmlContent);
  249. +       player.sendPacket(html);
  250. +       player.sendMessage(attempts < 1 ? "You have " + QuestionTimeLimit + " seconds to answer!" : "Wrong answer, try again!");
  251. +       attemptsMap.put(player, attempts + 1);
  252. +       setShowQuestionOpened(true);
  253. +   }
  254. +  
  255. +   public static void showSecondQuestion(Player player)
  256. +   {
  257. +       var attempts = attemptsMap.getOrDefault(player, 0);
  258. +       if (attempts > maxAttempts)
  259. +       {
  260. +           var html = new NpcHtmlMessage(0);
  261. +           var htmlContent = "<html><head><title>Error Message</title></head>" + "<body><center><font color=\"FF0000\">Incorrect Answer</font></center><br><br>" + "<center>You have exceeded the maximum number of attempts.</center><br>" + "<center>" + (_punishment == 1 ? "Your character will be disconnected in 5 seconds." : "You will be teleported in Town in 5 seconds.") + "</center></body></html>";
  262. +           html.setHtml(htmlContent);
  263. +           player.sendPacket(html);
  264. +           ThreadPool.schedule(() -> handlePunishment(player), 5000);
  265. +           return;
  266. +       }
  267. +      
  268. +       int min = 1, max = 10, randomNum1 = (int) (Math.random() * (max - min + 1)) + min, randomNum2 = (int) (Math.random() * (max - min + 1)) + min, sum = randomNum1 + randomNum2;
  269. +       var question = String.format("What is the sum of <font color=\"LEVEL\">%d + %d?</font>", randomNum1, randomNum2);
  270. +       var html = new NpcHtmlMessage(0);
  271. +       var buttonAction = String.format("bypass -h second_question %d $answer", sum);
  272. +       var htmlContent = "<html><head><title>Second Math Question</title></head>" + "<body><center><font color=\"LEVEL\">Second Math Question</font></center><br><br><center>You have reached the final step to unlock your character.</center><br>" + "<center>" + question + "</center><br><br>" + "<center><edit var=\"answer\" width=50></center><br><br>" + "<center><button value=\"Submit\" action=\"" + buttonAction + "\" width=80 height=20 back=\"sek.cbui94\" fore=\"sek.cbui92\"></center></body></html>";
  273. +       html.setHtml(htmlContent);
  274. +       player.sendPacket(html);
  275. +       attemptsMap.put(player, attempts + 1);
  276. +   }
  277. +  
  278. +   private static void handlePunishment(Player player)
  279. +   {
  280. +       switch (_punishment)
  281. +       {
  282. +           case 1:
  283. +               player.logout(true);
  284. +               break;
  285. +           case 2:
  286. +               player.teleportTo(TeleportType.TOWN);
  287. +               break;
  288. +       }
  289. +       stopTask(player);
  290. +   }
  291. +  
  292. +   public static void stopTask(Player player)
  293. +   {
  294. +       inTime.cancel(true);
  295. +       setShowQuestionOpened(false);
  296. +       attemptsMap.remove(player);
  297. +       MathQuizTaskManager.getInstance().remove(player);
  298. +   }
  299. +  
  300. +   public static boolean setShowQuestionOpened(boolean showQuestionOpened)
  301. +   {
  302. +       _showQuestionOpened = showQuestionOpened;
  303. +       return _showQuestionOpened;
  304. +   }
  305. +  
  306. +   public static int removeAttempts(Player player)
  307. +   {
  308. +       return attemptsMap.remove(player);
  309. +   }
  310. +  
  311. +   private static int getRandomEvenNumber()
  312. +   {
  313. +       return randomGen.nextInt(9) * 2 + 2;
  314. +   }
  315. +  
  316. +   private static int getRandomOddNumber()
  317. +   {
  318. +       return randomGen.nextInt(9) * 2 + 1;
  319. +   }
  320. +}
  321. \ No newline at end of file
  322. diff --git a/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Player.java b/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Player.java
  323. index 023516a..b093da3 100644
  324. --- a/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Player.java
  325. +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Player.java
  326. @@ -219,6 +219,7 @@
  327.  import net.sf.l2j.gameserver.skills.funcs.FuncRegenCpMul;
  328.  import net.sf.l2j.gameserver.taskmanager.AttackStanceTaskManager;
  329.  import net.sf.l2j.gameserver.taskmanager.GameTimeTaskManager;
  330. +import net.sf.l2j.gameserver.taskmanager.MathQuizTaskManager;
  331.  import net.sf.l2j.gameserver.taskmanager.PvpFlagTaskManager;
  332.  import net.sf.l2j.gameserver.taskmanager.ShadowItemTaskManager;
  333.  import net.sf.l2j.gameserver.taskmanager.WaterTaskManager;
  334. @@ -6500,6 +6501,7 @@
  335.             PvpFlagTaskManager.getInstance().remove(this, false);
  336.             GameTimeTaskManager.getInstance().remove(this);
  337.             ShadowItemTaskManager.getInstance().remove(this);
  338. +           MathQuizTaskManager.getInstance().remove(this);
  339.            
  340.             // Cancel the cast of eventual fusion skill users on this target.
  341.             for (final Creature creature : getKnownType(Creature.class))
  342. diff --git a/aCis_gameserver/java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java b/aCis_gameserver/java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java
  343. index d8adb02..52d0581 100644
  344. --- a/aCis_gameserver/java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java
  345. +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java
  346. @@ -12,6 +12,7 @@
  347.  import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
  348.  import net.sf.l2j.gameserver.model.World;
  349.  import net.sf.l2j.gameserver.model.WorldObject;
  350. +import net.sf.l2j.gameserver.model.actor.MathQuiz;
  351.  import net.sf.l2j.gameserver.model.actor.Npc;
  352.  import net.sf.l2j.gameserver.model.actor.Player;
  353.  import net.sf.l2j.gameserver.model.actor.instance.OlympiadManagerNpc;
  354. @@ -165,6 +166,46 @@
  355.             if (heroid > 0)
  356.                 HeroManager.getInstance().showHeroDiary(player, heroclass, heroid, heropage);
  357.         }
  358. +       else if (_command.startsWith("question"))
  359. +       {
  360. +           String[] parts = _command.split(" ");
  361. +           if (parts.length < 4)
  362. +           {
  363. +               MathQuiz.showQuestion(player);
  364. +               return;
  365. +           }
  366. +           int evenNumber = Integer.parseInt(parts[1]);
  367. +           String answer = parts[2];
  368. +           int expectedAnswer = Integer.parseInt(parts[3]);
  369. +           boolean isAnswerCorrect = answer.equals(String.valueOf(evenNumber));
  370. +           if (isAnswerCorrect && expectedAnswer == 1)
  371. +           {
  372. +               MathQuiz.showSecondQuestion(player);
  373. +               MathQuiz.removeAttempts(player);
  374. +               return;
  375. +           }
  376. +           MathQuiz.showQuestion(player);
  377. +       }
  378. +       else if (_command.startsWith("second_question"))
  379. +       {
  380. +           String[] parts = _command.split(" ");
  381. +           if (parts.length < 3)
  382. +           {
  383. +               MathQuiz.showSecondQuestion(player);
  384. +               return;
  385. +           }
  386. +           int expectedAnswer = Integer.parseInt(parts[1]);
  387. +           int givenAnswer = Integer.parseInt(parts[2]);
  388. +           if (givenAnswer == expectedAnswer)
  389. +           {
  390. +               var html = new NpcHtmlMessage(0);
  391. +               html.setHtml("<html><head><title>Congrats!</title></head><body><center><font color=\"00FF00\">Correct answer! Quiz Passed!</font><br>Your answer was correct! You can continue playing!<br>Have fun!</center></body></html>");
  392. +               player.sendPacket(html);
  393. +               MathQuiz.stopTask(player);
  394. +               return;
  395. +           }
  396. +           MathQuiz.showSecondQuestion(player);
  397. +       }
  398.         else if (_command.startsWith("arenachange")) // change
  399.         {
  400.             final boolean isManager = player.getCurrentFolk() instanceof OlympiadManagerNpc;
  401. diff --git a/aCis_gameserver/java/net/sf/l2j/gameserver/taskmanager/MathQuizTaskManager.java b/aCis_gameserver/java/net/sf/l2j/gameserver/taskmanager/MathQuizTaskManager.java
  402. new file mode 100644
  403. index 0000000..dce50d0
  404. --- /dev/null
  405. +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/taskmanager/MathQuizTaskManager.java
  406. @@ -0,0 +1,96 @@
  407. +package net.sf.l2j.gameserver.taskmanager;
  408. +
  409. +import java.util.Map;
  410. +import java.util.concurrent.ConcurrentHashMap;
  411. +import java.util.concurrent.TimeUnit;
  412. +
  413. +import net.sf.l2j.commons.pool.ThreadPool;
  414. +
  415. +import net.sf.l2j.gameserver.enums.ZoneId;
  416. +import net.sf.l2j.gameserver.model.actor.MathQuiz;
  417. +import net.sf.l2j.gameserver.model.actor.Player;
  418. +
  419. +/**
  420. + * @author Baggos
  421. + */
  422. +public final class MathQuizTaskManager implements Runnable
  423. +{
  424. +   private static final long QUIZ_TASK = TimeUnit.MINUTES.toMillis(MathQuiz.QuizTaskTime);
  425. +  
  426. +   private final Map<Player, Long> _players = new ConcurrentHashMap<>();
  427. +   public static int _killCount;
  428. +  
  429. +   public static final MathQuizTaskManager getInstance()
  430. +   {
  431. +       return SingletonHolder._instance;
  432. +   }
  433. +  
  434. +   protected MathQuizTaskManager()
  435. +   {
  436. +       // Run task each second.
  437. +       ThreadPool.scheduleAtFixedRate(this, 1000, 1000);
  438. +   }
  439. +  
  440. +   /**
  441. +    * Adds the player to the Task.
  442. +    * @param player
  443. +    */
  444. +   public final void add(Player player)
  445. +   {
  446. +       _players.put(player, System.currentTimeMillis() + QUIZ_TASK);
  447. +   }
  448. +  
  449. +   /**
  450. +    * Removes the player from the Task.
  451. +    * @param player
  452. +    */
  453. +   public final void remove(Player player)
  454. +   {
  455. +       _killCount = 0;
  456. +       _players.remove(player);
  457. +   }
  458. +  
  459. +   public static void countKills()
  460. +   {
  461. +       _killCount++;
  462. +   }
  463. +  
  464. +   @Override
  465. +   public final void run()
  466. +   {
  467. +       // List is empty, skip.
  468. +       if (_players.isEmpty())
  469. +           return;
  470. +      
  471. +       // Get current time.
  472. +       final long time = System.currentTimeMillis();
  473. +      
  474. +       // Loop all characters.
  475. +       for (Map.Entry<Player, Long> entry : _players.entrySet())
  476. +       {
  477. +           // Get time left and check.
  478. +           final Player player = entry.getKey();
  479. +           final long timeLeft = entry.getValue();
  480. +          
  481. +           // Time hasn't passed yet, skip.
  482. +           if (time < timeLeft)
  483. +               continue;
  484. +          
  485. +           // Time passed, start the Quiz or remove him if is in Town.
  486. +           if (_killCount >= MathQuiz.killsTask)
  487. +           {
  488. +               if (player.isInsideZone(ZoneId.PEACE))
  489. +               {
  490. +                   remove(player);
  491. +                   return;
  492. +               }
  493. +               MathQuiz.startQuestionWindow(player);
  494. +           }
  495. +       }
  496. +   }
  497. +  
  498. +   private static class SingletonHolder
  499. +   {
  500. +       protected static final MathQuizTaskManager _instance = new MathQuizTaskManager();
  501. +   }
  502. +}
  503. \ No newline at end of file
  504.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement