From 4e8ad17d1c8ce66b6dcf2bfe5984b650486886db Mon Sep 17 00:00:00 2001 From: SilicaAndPina Date: Fri, 16 Apr 2021 01:48:24 +1200 Subject: [PATCH] add more for real time quiz. --- DataCollection/gamedata.json | 10 +- .../HorseIsleServer/Game/Chat/Command.cs | 15 +- .../Game/Events/RealTimeQuiz.cs | 189 +++++++++++++++++- .../HorseIsleServer/Game/Messages.cs | 1 + .../HorseIsleServer/Game/Meta.cs | 5 + .../HorseIsleServer/Player/User.cs | 3 +- .../HorseIsleServer/Server/GameDataJson.cs | 1 + .../HorseIsleServer/Server/GameServer.cs | 20 +- 8 files changed, 227 insertions(+), 17 deletions(-) diff --git a/DataCollection/gamedata.json b/DataCollection/gamedata.json index a472bca..d7c358c 100755 --- a/DataCollection/gamedata.json +++ b/DataCollection/gamedata.json @@ -62,7 +62,12 @@ "starved_horse":"You have been sent to Prison Isle for starving one of your horses! They have been fed for you.", "message_queue":"OFFLINE MESSAGE:", "timed_messages":{ - {"hours":2, "messages":["You have been playing for 2 hours. Aren't you sleepy? It's okay to go to bed. We'll all be here tomorrow."]}, + "rng_message":["Don't forget to pet your kitty/dog/fish/stuffed animal.", + "Don't forget to stretch once in a while.", "Are you running late for school/work?!?!", + "Aren't you sleepy? It's okay to go to bed. We'll all be here tomorrow.", + "Please take a break and check on your family/pets/world. :)", + "Please take breaks once in a while."] + "playtime_message": "You have been playing for %TOTALHOURS% hours. " }, "chat_errors":{ "cant_find_player":"Could not find player to Private chat!", @@ -94,7 +99,8 @@ "event_win_bonus":"QUIZ WINNER BONUS: $%MONEY% Earned. ($2500 win + $500/correct - $100/mistake)", "event_win":"QUIZ COMPLETED: %USERNAME% answered all questions first!", "event_unavailable":"Realtime quiz is currently unavailable", - "event_entered":"You have entered the Realtime Quiz. Good luck!" + "event_entered":"You have entered the Realtime Quiz. Good luck!", + "event_quit":"Realtime quiz was quit already. Only one try per Quiz.", }, }, "horse_leaser":{ diff --git a/Horse Isle Server/HorseIsleServer/Game/Chat/Command.cs b/Horse Isle Server/HorseIsleServer/Game/Chat/Command.cs index 4788705..4406d4c 100755 --- a/Horse Isle Server/HorseIsleServer/Game/Chat/Command.cs +++ b/Horse Isle Server/HorseIsleServer/Game/Chat/Command.cs @@ -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); diff --git a/Horse Isle Server/HorseIsleServer/Game/Events/RealTimeQuiz.cs b/Horse Isle Server/HorseIsleServer/Game/Events/RealTimeQuiz.cs index 3c79ef9..dd36734 100644 --- a/Horse Isle Server/HorseIsleServer/Game/Events/RealTimeQuiz.cs +++ b/Horse Isle Server/HorseIsleServer/Game/Events/RealTimeQuiz.cs @@ -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 participents; + + public RealTimeQuiz() + { + participents = new List(); + 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(); + } } } diff --git a/Horse Isle Server/HorseIsleServer/Game/Messages.cs b/Horse Isle Server/HorseIsleServer/Game/Messages.cs index e42f99e..a5ddec8 100755 --- a/Horse Isle Server/HorseIsleServer/Game/Messages.cs +++ b/Horse Isle Server/HorseIsleServer/Game/Messages.cs @@ -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; diff --git a/Horse Isle Server/HorseIsleServer/Game/Meta.cs b/Horse Isle Server/HorseIsleServer/Game/Meta.cs index 6562452..dfff4f1 100755 --- a/Horse Isle Server/HorseIsleServer/Game/Meta.cs +++ b/Horse Isle Server/HorseIsleServer/Game/Meta.cs @@ -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 = ""; diff --git a/Horse Isle Server/HorseIsleServer/Player/User.cs b/Horse Isle Server/HorseIsleServer/Player/User.cs index 7a6d988..32e80a2 100755 --- a/Horse Isle Server/HorseIsleServer/Player/User.cs +++ b/Horse Isle Server/HorseIsleServer/Player/User.cs @@ -77,7 +77,7 @@ namespace HISP.Player public HorseInfo.Breed PawneerOrderBreed = null; public string PawneerOrderColor = ""; public string PawneerOrderGender = ""; - + public bool InRealTimeQuiz = false; public int PendingTradeTo; public Mailbox MailBox; public Friends Friends; @@ -439,7 +439,6 @@ namespace HISP.Player public int SecCodeInc = 0; public int SecCodeCount = 0; - public int GetPlayerListIcon() { int icon = -1; diff --git a/Horse Isle Server/HorseIsleServer/Server/GameDataJson.cs b/Horse Isle Server/HorseIsleServer/Server/GameDataJson.cs index 8e0d52d..c757397 100755 --- a/Horse Isle Server/HorseIsleServer/Server/GameDataJson.cs +++ b/Horse Isle Server/HorseIsleServer/Server/GameDataJson.cs @@ -962,6 +962,7 @@ namespace HISP.Server Messages.EventWinRealTimeQuizFormat = gameData.messages.events.real_time_quiz.event_win; Messages.EventUnavailableRealTimeQuiz = gameData.messages.events.real_time_quiz.event_unavailable; Messages.EventEnteredRealTimeQuiz = gameData.messages.events.real_time_quiz.event_entered; + Messages.EventQuitRealTimeQuiz = gameData.messages.events.real_time_quiz.event_quit; // Events : Real Time Riddle diff --git a/Horse Isle Server/HorseIsleServer/Server/GameServer.cs b/Horse Isle Server/HorseIsleServer/Server/GameServer.cs index b8553dc..7661785 100755 --- a/Horse Isle Server/HorseIsleServer/Server/GameServer.cs +++ b/Horse Isle Server/HorseIsleServer/Server/GameServer.cs @@ -40,7 +40,8 @@ namespace HISP.Server // Events public static RealTimeRiddle RiddleEvent = RealTimeRiddle.GetRandomRiddle(); - public static TackShopGiveaway TackShopGiveawayEvent; + public static TackShopGiveaway TackShopGiveawayEvent = null; + public static RealTimeQuiz QuizEvent = null; /* * Private stuff @@ -164,7 +165,8 @@ namespace HISP.Server // Real Time Quiz if(totalMinutesElapsed % 75 == 0) { - + QuizEvent = new RealTimeQuiz(); + QuizEvent.StartEvent(); } if (totalMinutesElapsed % 60 == 0) @@ -6951,7 +6953,6 @@ namespace HISP.Server if (sender.LoggedIn) { Database.SetPlayerLastLogin(Convert.ToInt32(new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()), sender.LoggedinUser.Id); // Set last login date - Database.RemoveOnlineUser(sender.LoggedinUser.Id); // Leave multirooms @@ -6961,9 +6962,13 @@ namespace HISP.Server // Remove Trade Reference sender.LoggedinUser.TradingWith = null; sender.LoggedinUser.PendingTradeTo = 0; + + // Quit open quiz. + if (QuizEvent != null) + QuizEvent.LeaveEvent(sender.LoggedinUser, false); // Delete Arena Entries - if(Arena.UserHasEnteredHorseInAnyArena(sender.LoggedinUser)) + if (Arena.UserHasEnteredHorseInAnyArena(sender.LoggedinUser)) { Arena arena = Arena.GetArenaUserEnteredIn(sender.LoggedinUser); arena.DeleteEntry(sender.LoggedinUser); @@ -7371,7 +7376,7 @@ namespace HISP.Server forClient.LoggedinUser.ListingAuction = false; string LocationStr = ""; - if (!World.InSpecialTile(forClient.LoggedinUser.X, forClient.LoggedinUser.Y)) + if (!World.InSpecialTile(forClient.LoggedinUser.X, forClient.LoggedinUser.Y) && !forClient.LoggedinUser.InRealTimeQuiz) { LocationStr = Meta.BuildMetaInfo(forClient.LoggedinUser, forClient.LoggedinUser.X, forClient.LoggedinUser.Y); } @@ -7383,12 +7388,17 @@ namespace HISP.Server byte[] swfModulePacket = PacketBuilder.CreateSwfModulePacket(specialTile.AutoplaySwf,PacketBuilder.PACKET_SWF_MODULE_GENTLE); forClient.SendPacket(swfModulePacket); } + + if (forClient.LoggedinUser.InRealTimeQuiz) + return; + if (specialTile.Code != null) if (!ProcessMapCodeWithArg(forClient, specialTile)) return; LocationStr = Meta.BuildSpecialTileInfo(forClient.LoggedinUser, specialTile); } + byte[] AreaMessage = PacketBuilder.CreateMetaPacket(LocationStr); forClient.SendPacket(AreaMessage);