Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/aCis_datapack/data/xml/mathquiz.xml b/aCis_datapack/data/xml/mathquiz.xml
- new file mode 100644
- index 0000000..ad88472
- --- /dev/null
- +++ b/aCis_datapack/data/xml/mathquiz.xml
- @@ -0,0 +1,18 @@
- +<?xml version='1.0' encoding='utf-8'?>
- +<list>
- + <MathQuiz QuizTaskTimeManager ="15" KillsTask="20" QuestionTime="120" Attempts="4" Punishment="2"/>
- +
- + <!-- QuizTaskTime - The amount of time in seconds for which the task will run if the player reaches the KillsTask.
- + Once the player reaches the number of kills specified by KillsTask, the quiz will appear after the next time interval specified by this value. -->
- +
- + <!-- KillsTask - The minimum number of monsters the player must kill to start the task.
- + If set to 0, the task will never start. -->
- +
- + <!-- QuestionTime - The time limit in seconds for the player to answer the question.
- + If the player fails to answer within the specified time, they will be disconnected from the game. -->
- +
- + <!-- Attempts - The number of attempts allowed for the player to provide the correct answer. -->
- +
- + <!-- Punishment - Selects the punishment for the player in case of a wrong answer in the quiz.
- + If value is 1 will logout the player in 5 seconds. If value is 2 will teleport the player to town in 5 seconds. -->
- +</list>
- diff --git a/aCis_gameserver/java/net/sf/l2j/gameserver/GameServer.java b/aCis_gameserver/java/net/sf/l2j/gameserver/GameServer.java
- index e682cc6..d0e351e 100644
- --- a/aCis_gameserver/java/net/sf/l2j/gameserver/GameServer.java
- +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/GameServer.java
- @@ -59,6 +59,7 @@
- import net.sf.l2j.gameserver.data.xml.InstantTeleportData;
- import net.sf.l2j.gameserver.data.xml.ItemData;
- import net.sf.l2j.gameserver.data.xml.MapRegionData;
- +import net.sf.l2j.gameserver.data.xml.MathQuizData;
- import net.sf.l2j.gameserver.data.xml.MultisellData;
- import net.sf.l2j.gameserver.data.xml.NewbieBuffData;
- import net.sf.l2j.gameserver.data.xml.NpcData;
- @@ -273,6 +274,9 @@
- LOGGER.info("Loaded {} target handlers.", TargetHandler.getInstance().size());
- LOGGER.info("Loaded {} user command handlers.", UserCommandHandler.getInstance().size());
- + StringUtil.printSection("MathQuiz");
- + MathQuizData.getInstance();
- +
- StringUtil.printSection("System");
- Runtime.getRuntime().addShutdownHook(Shutdown.getInstance());
- 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
- new file mode 100644
- index 0000000..bd57ac6
- --- /dev/null
- +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/data/xml/MathQuizData.java
- @@ -0,0 +1,56 @@
- +package net.sf.l2j.gameserver.data.xml;
- +
- +import java.nio.file.Path;
- +import java.util.ArrayList;
- +import java.util.List;
- +
- +import net.sf.l2j.commons.data.xml.IXmlReader;
- +
- +import net.sf.l2j.gameserver.model.actor.MathQuiz;
- +
- +import org.w3c.dom.Document;
- +
- +/**
- + * @author Baggos
- + */
- +public class MathQuizData implements IXmlReader
- +{
- + private final List<MathQuiz> _config = new ArrayList<>();
- +
- + protected MathQuizData()
- + {
- + load();
- + }
- +
- + @Override
- + public void load()
- + {
- + parseFile("./data/xml/mathquiz.xml");
- + LOGGER.info("Quiz XML File Loaded!");
- + }
- +
- + @Override
- + public void parseDocument(Document doc, Path path)
- + {
- + forEach(doc, "list", listNode -> forEach(listNode, "MathQuiz", iconNode ->
- + {
- + var set = parseAttributes(iconNode);
- + _config.add(new MathQuiz(set));
- + }));
- + }
- +
- + public void reload()
- + {
- + load();
- + }
- +
- + public static MathQuizData getInstance()
- + {
- + return SingletonHolder.INSTANCE;
- + }
- +
- + private static class SingletonHolder
- + {
- + protected static final MathQuizData INSTANCE = new MathQuizData();
- + }
- +}
- 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
- index 5f11f94..eb1e5ee 100644
- --- a/aCis_gameserver/java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminReload.java
- +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminReload.java
- @@ -14,6 +14,7 @@
- import net.sf.l2j.gameserver.data.xml.DoorData;
- import net.sf.l2j.gameserver.data.xml.InstantTeleportData;
- import net.sf.l2j.gameserver.data.xml.ItemData;
- +import net.sf.l2j.gameserver.data.xml.MathQuizData;
- import net.sf.l2j.gameserver.data.xml.MultisellData;
- import net.sf.l2j.gameserver.data.xml.NpcData;
- import net.sf.l2j.gameserver.data.xml.ScriptData;
- @@ -122,6 +123,11 @@
- ZoneManager.getInstance().reload();
- player.sendMessage("Zones have been reloaded.");
- }
- + else if (type.startsWith("quiz"))
- + {
- + MathQuizData.getInstance().reload();
- + player.sendMessage("MathQuiz have been reloaded.");
- + }
- else
- sendUsage(player);
- }
- @@ -137,7 +143,7 @@
- {
- player.sendMessage("Usage : //reload <admin|announcement|buylist|config>");
- player.sendMessage("Usage : //reload <crest|cw|door|htm|item|multisell|npc>");
- - player.sendMessage("Usage : //reload <npcwalker|script|skill|teleport|zone>");
- + player.sendMessage("Usage : //reload <npcwalker|script|skill|teleport|zone|quiz>");
- }
- @Override
- 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
- index 9009a45..968f0a3 100644
- --- a/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Creature.java
- +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Creature.java
- @@ -75,6 +75,7 @@
- import net.sf.l2j.gameserver.skills.funcs.FuncRegenHpMul;
- import net.sf.l2j.gameserver.skills.funcs.FuncRegenMpMul;
- import net.sf.l2j.gameserver.taskmanager.AttackStanceTaskManager;
- +import net.sf.l2j.gameserver.taskmanager.MathQuizTaskManager;
- /**
- * An instance type extending {@link WorldObject} which represents the mother class of all character objects of the world such as players, NPCs and monsters.
- @@ -487,6 +488,13 @@
- calculateRewards(killer);
- + if (MathQuiz.killsTask > 0)
- + {
- + MathQuizTaskManager.countKills();
- + if (MathQuizTaskManager._killCount == MathQuiz.killsTask)
- + MathQuizTaskManager.getInstance().add((Player) killer);
- + }
- +
- // Send the Server->Client packet StatusUpdate with current HP and MP to all other Player to inform
- getStatus().broadcastStatusUpdate();
- 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
- new file mode 100644
- index 0000000..d625d3a
- --- /dev/null
- +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/MathQuiz.java
- @@ -0,0 +1,147 @@
- +package net.sf.l2j.gameserver.model.actor;
- +
- +import java.util.HashMap;
- +import java.util.Map;
- +import java.util.Random;
- +import java.util.concurrent.ScheduledFuture;
- +
- +import net.sf.l2j.commons.data.StatSet;
- +import net.sf.l2j.commons.pool.ThreadPool;
- +
- +import net.sf.l2j.gameserver.data.xml.MapRegionData.TeleportType;
- +import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
- +import net.sf.l2j.gameserver.taskmanager.MathQuizTaskManager;
- +
- +/**
- + * @author Baggos
- + */
- +public class MathQuiz
- +{
- + private static final Random randomGen = new Random();
- + private static final Map<Player, Integer> attemptsMap = new HashMap<>();
- + private static ScheduledFuture<?> inTime;
- + private static boolean _showQuestionOpened = false;
- + public static int QuizTaskTime;
- + public static int killsTask;
- + private static int QuestionTimeLimit; // seconds
- + private static int maxAttempts;
- + private static int _punishment;
- +
- + public MathQuiz(StatSet set)
- + {
- + QuizTaskTime = set.getInteger("QuizTaskTimeManager");
- + killsTask = set.getInteger("KillsTask");
- + QuestionTimeLimit = set.getInteger("QuestionTime");
- + maxAttempts = set.getInteger("Attempts");
- + _punishment = set.getInteger("Punishment");
- + }
- +
- + public static void startQuestionWindow(Player player)
- + {
- + if (killsTask == 0 || _showQuestionOpened == true)
- + return;
- +
- + showQuestion(player);
- + inTime = ThreadPool.schedule(() ->
- + {
- + var html = new NpcHtmlMessage(0);
- + 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>";
- + html.setHtml(htmlContent2);
- + player.sendPacket(html);
- + ThreadPool.schedule(() -> handlePunishment(player), 5000);
- + }, QuestionTimeLimit * 1000);
- + }
- +
- + public static void showQuestion(Player player)
- + {
- + var html = new NpcHtmlMessage(0);
- + var attempts = attemptsMap.getOrDefault(player, 0);
- + if (attempts > maxAttempts)
- + {
- + 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>";
- + html.setHtml(htmlContent);
- + player.sendPacket(html);
- + ThreadPool.schedule(() -> handlePunishment(player), 5000);
- + return;
- + }
- +
- + int evenNumber = getRandomEvenNumber(), oddNumber = getRandomOddNumber();
- + boolean firstNumberIsEven = randomGen.nextBoolean();
- + int a = firstNumberIsEven ? evenNumber : oddNumber;
- + int b = firstNumberIsEven ? oddNumber : evenNumber;
- + 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>";
- + var question = String.format("Which of the following two numbers is even:<font color=\"LEVEL\"> %d or %d?</font>", a, b);
- + 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>";
- + html.setHtml(htmlContent);
- + player.sendPacket(html);
- + player.sendMessage(attempts < 1 ? "You have " + QuestionTimeLimit + " seconds to answer!" : "Wrong answer, try again!");
- + attemptsMap.put(player, attempts + 1);
- + setShowQuestionOpened(true);
- + }
- +
- + public static void showSecondQuestion(Player player)
- + {
- + var attempts = attemptsMap.getOrDefault(player, 0);
- + if (attempts > maxAttempts)
- + {
- + var html = new NpcHtmlMessage(0);
- + 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>";
- + html.setHtml(htmlContent);
- + player.sendPacket(html);
- + ThreadPool.schedule(() -> handlePunishment(player), 5000);
- + return;
- + }
- +
- + int min = 1, max = 10, randomNum1 = (int) (Math.random() * (max - min + 1)) + min, randomNum2 = (int) (Math.random() * (max - min + 1)) + min, sum = randomNum1 + randomNum2;
- + var question = String.format("What is the sum of <font color=\"LEVEL\">%d + %d?</font>", randomNum1, randomNum2);
- + var html = new NpcHtmlMessage(0);
- + var buttonAction = String.format("bypass -h second_question %d $answer", sum);
- + 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>";
- + html.setHtml(htmlContent);
- + player.sendPacket(html);
- + attemptsMap.put(player, attempts + 1);
- + }
- +
- + private static void handlePunishment(Player player)
- + {
- + switch (_punishment)
- + {
- + case 1:
- + player.logout(true);
- + break;
- + case 2:
- + player.teleportTo(TeleportType.TOWN);
- + break;
- + }
- + stopTask(player);
- + }
- +
- + public static void stopTask(Player player)
- + {
- + inTime.cancel(true);
- + setShowQuestionOpened(false);
- + attemptsMap.remove(player);
- + MathQuizTaskManager.getInstance().remove(player);
- + }
- +
- + public static boolean setShowQuestionOpened(boolean showQuestionOpened)
- + {
- + _showQuestionOpened = showQuestionOpened;
- + return _showQuestionOpened;
- + }
- +
- + public static int removeAttempts(Player player)
- + {
- + return attemptsMap.remove(player);
- + }
- +
- + private static int getRandomEvenNumber()
- + {
- + return randomGen.nextInt(9) * 2 + 2;
- + }
- +
- + private static int getRandomOddNumber()
- + {
- + return randomGen.nextInt(9) * 2 + 1;
- + }
- +}
- \ No newline at end of file
- 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
- index 023516a..b093da3 100644
- --- a/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Player.java
- +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/model/actor/Player.java
- @@ -219,6 +219,7 @@
- import net.sf.l2j.gameserver.skills.funcs.FuncRegenCpMul;
- import net.sf.l2j.gameserver.taskmanager.AttackStanceTaskManager;
- import net.sf.l2j.gameserver.taskmanager.GameTimeTaskManager;
- +import net.sf.l2j.gameserver.taskmanager.MathQuizTaskManager;
- import net.sf.l2j.gameserver.taskmanager.PvpFlagTaskManager;
- import net.sf.l2j.gameserver.taskmanager.ShadowItemTaskManager;
- import net.sf.l2j.gameserver.taskmanager.WaterTaskManager;
- @@ -6500,6 +6501,7 @@
- PvpFlagTaskManager.getInstance().remove(this, false);
- GameTimeTaskManager.getInstance().remove(this);
- ShadowItemTaskManager.getInstance().remove(this);
- + MathQuizTaskManager.getInstance().remove(this);
- // Cancel the cast of eventual fusion skill users on this target.
- for (final Creature creature : getKnownType(Creature.class))
- 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
- index d8adb02..52d0581 100644
- --- a/aCis_gameserver/java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java
- +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java
- @@ -12,6 +12,7 @@
- import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
- import net.sf.l2j.gameserver.model.World;
- import net.sf.l2j.gameserver.model.WorldObject;
- +import net.sf.l2j.gameserver.model.actor.MathQuiz;
- import net.sf.l2j.gameserver.model.actor.Npc;
- import net.sf.l2j.gameserver.model.actor.Player;
- import net.sf.l2j.gameserver.model.actor.instance.OlympiadManagerNpc;
- @@ -165,6 +166,46 @@
- if (heroid > 0)
- HeroManager.getInstance().showHeroDiary(player, heroclass, heroid, heropage);
- }
- + else if (_command.startsWith("question"))
- + {
- + String[] parts = _command.split(" ");
- + if (parts.length < 4)
- + {
- + MathQuiz.showQuestion(player);
- + return;
- + }
- + int evenNumber = Integer.parseInt(parts[1]);
- + String answer = parts[2];
- + int expectedAnswer = Integer.parseInt(parts[3]);
- + boolean isAnswerCorrect = answer.equals(String.valueOf(evenNumber));
- + if (isAnswerCorrect && expectedAnswer == 1)
- + {
- + MathQuiz.showSecondQuestion(player);
- + MathQuiz.removeAttempts(player);
- + return;
- + }
- + MathQuiz.showQuestion(player);
- + }
- + else if (_command.startsWith("second_question"))
- + {
- + String[] parts = _command.split(" ");
- + if (parts.length < 3)
- + {
- + MathQuiz.showSecondQuestion(player);
- + return;
- + }
- + int expectedAnswer = Integer.parseInt(parts[1]);
- + int givenAnswer = Integer.parseInt(parts[2]);
- + if (givenAnswer == expectedAnswer)
- + {
- + var html = new NpcHtmlMessage(0);
- + 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>");
- + player.sendPacket(html);
- + MathQuiz.stopTask(player);
- + return;
- + }
- + MathQuiz.showSecondQuestion(player);
- + }
- else if (_command.startsWith("arenachange")) // change
- {
- final boolean isManager = player.getCurrentFolk() instanceof OlympiadManagerNpc;
- diff --git a/aCis_gameserver/java/net/sf/l2j/gameserver/taskmanager/MathQuizTaskManager.java b/aCis_gameserver/java/net/sf/l2j/gameserver/taskmanager/MathQuizTaskManager.java
- new file mode 100644
- index 0000000..dce50d0
- --- /dev/null
- +++ b/aCis_gameserver/java/net/sf/l2j/gameserver/taskmanager/MathQuizTaskManager.java
- @@ -0,0 +1,96 @@
- +package net.sf.l2j.gameserver.taskmanager;
- +
- +import java.util.Map;
- +import java.util.concurrent.ConcurrentHashMap;
- +import java.util.concurrent.TimeUnit;
- +
- +import net.sf.l2j.commons.pool.ThreadPool;
- +
- +import net.sf.l2j.gameserver.enums.ZoneId;
- +import net.sf.l2j.gameserver.model.actor.MathQuiz;
- +import net.sf.l2j.gameserver.model.actor.Player;
- +
- +/**
- + * @author Baggos
- + */
- +public final class MathQuizTaskManager implements Runnable
- +{
- + private static final long QUIZ_TASK = TimeUnit.MINUTES.toMillis(MathQuiz.QuizTaskTime);
- +
- + private final Map<Player, Long> _players = new ConcurrentHashMap<>();
- + public static int _killCount;
- +
- + public static final MathQuizTaskManager getInstance()
- + {
- + return SingletonHolder._instance;
- + }
- +
- + protected MathQuizTaskManager()
- + {
- + // Run task each second.
- + ThreadPool.scheduleAtFixedRate(this, 1000, 1000);
- + }
- +
- + /**
- + * Adds the player to the Task.
- + * @param player
- + */
- + public final void add(Player player)
- + {
- + _players.put(player, System.currentTimeMillis() + QUIZ_TASK);
- + }
- +
- + /**
- + * Removes the player from the Task.
- + * @param player
- + */
- + public final void remove(Player player)
- + {
- + _killCount = 0;
- + _players.remove(player);
- + }
- +
- + public static void countKills()
- + {
- + _killCount++;
- + }
- +
- + @Override
- + public final void run()
- + {
- + // List is empty, skip.
- + if (_players.isEmpty())
- + return;
- +
- + // Get current time.
- + final long time = System.currentTimeMillis();
- +
- + // Loop all characters.
- + for (Map.Entry<Player, Long> entry : _players.entrySet())
- + {
- + // Get time left and check.
- + final Player player = entry.getKey();
- + final long timeLeft = entry.getValue();
- +
- + // Time hasn't passed yet, skip.
- + if (time < timeLeft)
- + continue;
- +
- + // Time passed, start the Quiz or remove him if is in Town.
- + if (_killCount >= MathQuiz.killsTask)
- + {
- + if (player.isInsideZone(ZoneId.PEACE))
- + {
- + remove(player);
- + return;
- + }
- + MathQuiz.startQuestionWindow(player);
- + }
- + }
- + }
- +
- + private static class SingletonHolder
- + {
- + protected static final MathQuizTaskManager _instance = new MathQuizTaskManager();
- + }
- +}
- \ No newline at end of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement