add more for real time quiz.

This commit is contained in:
SilicaAndPina 2021-04-16 01:48:24 +12:00
parent 753abb5231
commit 4e8ad17d1c
8 changed files with 227 additions and 17 deletions
Horse Isle Server/HorseIsleServer/Game

View file

@ -3,6 +3,7 @@ using HISP.Server;
using HISP.Game.Items;
using System;
using System.Collections.Generic;
using HISP.Game.Events;
namespace HISP.Game.Chat
{
@ -371,12 +372,22 @@ namespace HISP.Game.Chat
public static bool Quiz(string message, string[] args, User user)
{
bool quizActive = false;
// TODO: Check quiz event is running.
bool quizActive = (GameServer.QuizEvent != null);
if (quizActive)
{
string formattedmessage = Messages.FormatPlayerCommandCompleteMessage(message.Substring(1));
RealTimeQuiz.Participent participent = GameServer.QuizEvent.JoinEvent(user);
if(participent.Quit)
{
byte[] quizQuit = PacketBuilder.CreateChat(Messages.EventQuitRealTimeQuiz, PacketBuilder.CHAT_BOTTOM_RIGHT);
user.LoggedinClient.SendPacket(quizQuit);
return false;
}
participent.UpdateParticipent();
byte[] enteredRealTimeQuiz = PacketBuilder.CreateChat(Messages.EventEnteredRealTimeQuiz, PacketBuilder.CHAT_BOTTOM_RIGHT);
user.LoggedinClient.SendPacket(enteredRealTimeQuiz);

View file

@ -1,12 +1,11 @@
using System;
using HISP.Player;
using HISP.Server;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace HISP.Game.Events
{
class RealTimeQuiz
public class RealTimeQuiz
{
public class QuizQuestion
{
@ -24,7 +23,185 @@ namespace HISP.Game.Events
public QuizQuestion[] Questions;
}
public static QuizCategory[] Categories;
public class Participent
{
public Participent(User user)
{
UserInstance = user;
Won = false;
Quit = false;
CorrectAnswers = 0;
MistakenAnswers = 0;
}
public void NextQuestion()
{
QuizCategory chosenCategory = Categories[GameServer.RandomNumberGenerator.Next(0, Categories.Length)];
OnQuestion = chosenCategory.Questions[GameServer.RandomNumberGenerator.Next(0, chosenCategory.Questions.Length)];
}
public void UpdateParticipent()
{
if (this.Quit)
return;
byte[] realTimeQuizQuestion = PacketBuilder.CreateMetaPacket(Meta.BuildRealTimeQuiz(this));
this.UserInstance.LoggedinClient.SendPacket(realTimeQuizQuestion);
}
public User UserInstance;
public int CorrectAnswers;
public int MistakenAnswers;
public bool Quit;
public bool Won;
public QuizQuestion OnQuestion;
}
public static QuizCategory[] Categories;
public bool Active;
public const int QUIZ_TIMEOUT = 5;
private Timer quizTimer;
public Participent[] Participents
{
get
{
return participents.ToArray();
}
}
private List<Participent> participents;
public RealTimeQuiz()
{
participents = new List<Participent>();
Active = false;
}
private Participent getParticipent(int id)
{
foreach (Participent participent in Participents)
{
if (participent.UserInstance.Id == id)
{
return participent;
}
}
throw new KeyNotFoundException("No participent found.");
}
public Participent JoinEvent(User user)
{
try
{
return getParticipent(user.Id);
}
catch (KeyNotFoundException) { };
Participent newParticipent = new Participent(user);
user.InRealTimeQuiz = true;
participents.Add(newParticipent);
return newParticipent;
}
public void LeaveEvent(User user, bool sendData=true)
{
try
{
Participent partcipent = getParticipent(user.Id);
partcipent.Quit = true;
user.InRealTimeQuiz = false;
if(sendData)
if (user.LoggedinClient != null)
if(GameServer.IsUserOnline(user.Id))
GameServer.UpdateArea(user.LoggedinClient);
}
catch (KeyNotFoundException) { };
}
public void StartEvent()
{
quizTimer = new Timer(new TimerCallback(quizTimesUp), null, QUIZ_TIMEOUT * 60 * 1000, QUIZ_TIMEOUT * 60 * 1000);
byte[] quizStartMessage = PacketBuilder.CreateChat(Messages.EventStartRealTimeQuiz, PacketBuilder.CHAT_BOTTOM_RIGHT);
foreach (GameClient client in GameServer.ConnectedClients)
if (client.LoggedIn)
client.SendPacket(quizStartMessage);
Active = true;
GameServer.QuizEvent = this;
}
public void EndEvent()
{
byte[] eventEndMessage = PacketBuilder.CreateChat(Messages.EventEndRealTimeQuiz, PacketBuilder.CHAT_BOTTOM_RIGHT);
foreach (GameClient client in GameServer.ConnectedClients)
if(client.LoggedIn)
client.SendPacket(eventEndMessage);
stopEvent();
}
private void stopEvent()
{
foreach(Participent participent in Participents)
{
if (participent.UserInstance.LoggedinClient != null)
{
if (GameServer.IsUserOnline(participent.UserInstance.Id))
{
if (participent.Quit)
continue;
if (participent.UserInstance.InRealTimeQuiz)
GameServer.UpdateArea(participent.UserInstance.LoggedinClient);
participent.UserInstance.InRealTimeQuiz = false;
int money = 0;
if (participent.Won)
money += 2500;
else
money += 250;
money += (participent.CorrectAnswers * 500);
money -= (participent.MistakenAnswers * 100);
if (money < 250)
money = 250;
if (participent.Won)
{
byte[] wonBonusMessage = PacketBuilder.CreateChat(Messages.FormatEventRealTimeQuizWinBonus(money), PacketBuilder.CHAT_BOTTOM_RIGHT);
participent.UserInstance.LoggedinClient.SendPacket(wonBonusMessage);
}
else
{
byte[] bonusMessage = PacketBuilder.CreateChat(Messages.FormatEventRealTimeQuizBonus(money), PacketBuilder.CHAT_BOTTOM_RIGHT);
participent.UserInstance.LoggedinClient.SendPacket(bonusMessage);
}
participent.UserInstance.Money += money;
}
}
}
participents.Clear();
participents = null;
quizTimer.Dispose();
Active = false;
GameServer.TackShopGiveawayEvent = null;
}
private void quizTimesUp(object state)
{
EndEvent();
}
}
}

View file

@ -74,6 +74,7 @@ namespace HISP.Game
public static string EventWinRealTimeQuizFormat;
public static string EventUnavailableRealTimeQuiz;
public static string EventEnteredRealTimeQuiz;
public static string EventQuitRealTimeQuiz;
// Events : Real Time Riddles
public static string EventStartRealTimeRiddleFormat;

View file

@ -9,6 +9,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using HISP.Game.Chat;
using HISP.Game.Events;
namespace HISP.Game
{
@ -733,6 +734,10 @@ namespace HISP.Game
message += Messages.MetaTerminator;
return message;
}
public static string BuildRealTimeQuiz(RealTimeQuiz.Participent participent)
{
return Messages.FormatEventRealTimeQuizMeta(participent.CorrectAnswers, participent.MistakenAnswers, participent.OnQuestion.BaseCategory.Name, participent.OnQuestion.Question);
}
public static string BuildCompanionLibary()
{
string message = "";