Jump to content

Recommended Posts

Posted

Hello everyone. An event i created some time ago(1-2 months).

 

Every x minutes a question is poped and players have to answer it in trade(+) chat. First who answers correctly wins.

 

/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
* 
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* 
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.l2j.gameserver.model.entity.events;

import java.util.Map;

import javolution.util.FastList;
import javolution.util.FastMap;

import net.sf.l2j.Config;
import net.sf.l2j.gameserver.ThreadPoolManager;
import net.sf.l2j.gameserver.datatables.ItemTable;
import net.sf.l2j.gameserver.model.L2World;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.network.clientpackets.Say2;
import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
import net.sf.l2j.util.Rnd;
/**
* @author Anarchy
*
*/
public class Quiz
{
private static FastList<String> availableQuestions = new FastList<String>();
private static String currentQuestion = null,
	lastQuestion = null;
public static String currentAnswer = null;
private static int currentRewardId = 0,
currentRewardCount = 0;
public static boolean generatedQuestion = false;

public static void getInstance()
{
	int time = getRandomTime(Config.QUIZ_EVENT_TIME[0], Config.QUIZ_EVENT_TIME[1]);
	ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable()
	{
		@Override
		public void run()
		{
			if (!generatedQuestion)
			{
				generateQuestion();
			}
		}
	}, time*1000*60/2, time*1000*60);
}

public static void announceWinner(L2PcInstance p)
{
	p.addItem("Quiz event winner.", currentRewardId, currentRewardCount, p, true);
	announce("Winner: "+p.getName()+" Answer: "+currentAnswer);
	lastQuestion = currentQuestion;
	currentQuestion = null;
	currentAnswer = null;
	currentRewardId = 0;
	currentRewardCount = 0;
	generatedQuestion = false;
	availableQuestions.clear();
}

private static void generateQuestion()
{
	currentQuestion = getRandomQuestion();
	setReward();
	announce(currentQuestion);
	announce("Reward: "+currentRewardCount+" "+ItemTable.getInstance().getTemplate(currentRewardId).getName());
	announce("You have to answer in trade(+) chat.");
	announce("You have 5 minutes to answer.");
	generatedQuestion = true;

	ThreadPoolManager.getInstance().scheduleGeneral(new NoAnswerTask(), 5*1000*60);
}

private static class NoAnswerTask implements Runnable
{
	@Override
	public void run()
	{
		if (!generatedQuestion)
		{
			return;
		}

		generatedQuestion = false;
		lastQuestion = currentQuestion;
		currentQuestion = null;
		currentAnswer = null;
		currentRewardId = 0;
		currentRewardCount = 0;
		availableQuestions.clear();
		announce("There was no correct answer, so the question has been canceled.");
	}
}

private static void setReward()
{
	Map<String, String> questionsKey = new FastMap<String, String>();
	questionsKey.put(currentQuestion, currentAnswer);
	Map<Integer, Integer> questionsValue = Config.QUIZ_EVENT_QUESTIONS.get(questionsKey);
	for (int i : questionsValue.keySet())
	{
		currentRewardId = i;
		currentRewardCount = questionsValue.get(i);
	}
}

private static String getRandomQuestion()
{
	for (String s : Config.QUIZ_EVENT_QUESTIONS_KEYSET.keySet())
	{
		if (s.equals(lastQuestion))
		{
			continue;
		}

		availableQuestions.add(s);
	}

	int randomQuestionId = Rnd.get(availableQuestions.size());
	String question = availableQuestions.get(randomQuestionId);
	currentAnswer = Config.QUIZ_EVENT_QUESTIONS_KEYSET.get(question);

	return question;
}

private static void announce(String msg)
{
	CreatureSay cs = new CreatureSay(0, Say2.TRADE, "Quiz Event", msg);
	for (L2PcInstance p : L2World.getInstance().getAllPlayers().values())
	{
		p.sendPacket(cs);
	}
}

private static int getRandomTime(int min, int max)
{
	int time = Rnd.get(min, max);
	return time;
}
}

 

Configs:

# Quiz event.
AllowQuizEvent = True
# Quiz event time(in minutes) between questions.
# Must be set like: min/max.
# Time will be randomly chosen, for example if you put 5,15 it may be 7 or 9 or 13.
QuizEventTime = 10,20
# Quiz event questions.
# Must be set like: question,answer,rewardid,rewardcount;question,answer,rewardid,rewardcount;
QuizEventQuestions = Who created this event?,Anarchy,3470,5;What is the name of this server?,L2Server,57,100000000;

    public static boolean ALLOW_QUIZ_EVENT;
    public static int[] QUIZ_EVENT_TIME = new int[2];
    public static Map<Map<String, String>, Map<Integer, Integer>> QUIZ_EVENT_QUESTIONS = new FastMap<Map<String, String>, Map<Integer, Integer>>();
    public static Map<String, String> QUIZ_EVENT_QUESTIONS_KEYSET = new FastMap<String, String>(); // Hidden variable.

                ALLOW_QUIZ_EVENT = Boolean.parseBoolean(elcardia.getProperty("AllowQuizEvent", "false"));
                String quiz_event_time = elcardia.getProperty("QuizEventTime", "10,20");
                QUIZ_EVENT_TIME[0] = Integer.parseInt(quiz_event_time.split(",")[0]);
                QUIZ_EVENT_TIME[1] = Integer.parseInt(quiz_event_time.split(",")[1]);
                String quiz_event_questions = elcardia.getProperty("QuizEventQuestions"," Who created this event?,Anarchy,3470,5;What is the name of this server?,L2Server,57,100000000;");
                String[] quiz_event_questions_splitted_1 = quiz_event_questions.split(";");
                for (String s : quiz_event_questions_splitted_1)
                {
                	String[] quiz_event_questions_splitted_2 = s.split(",");
                	Map<String, String> string_map = new FastMap<String, String>();
                	string_map.put(quiz_event_questions_splitted_2[0], quiz_event_questions_splitted_2[1]);
                	Map<Integer, Integer> int_map = new FastMap<Integer, Integer>();
                	int_map.put(Integer.parseInt(quiz_event_questions_splitted_2[2]), Integer.parseInt(quiz_event_questions_splitted_2[3]));
                	QUIZ_EVENT_QUESTIONS.put(string_map, int_map);
                	QUIZ_EVENT_QUESTIONS_KEYSET.put(quiz_event_questions_splitted_2[0], quiz_event_questions_splitted_2[1]);
                }

 

GameServer.java

	if (Config.ALLOW_QUIZ_EVENT)
	{
		Quiz.getInstance();
	}

 

I know it's created before and shared too, but this one is coded in another way(configs) and created from scratch by me.

 

Have fun.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...