Add Real Time Riddles

This commit is contained in:
SilicaAndPina 2021-03-26 10:00:30 +13:00
parent 5308b53c89
commit 679b6efac8
7 changed files with 188 additions and 23 deletions

View file

@ -65,7 +65,8 @@
"real_time_riddle":{
"event_start":"<B>CHAT RIDDLE:</B> %RIDDLETEXT% <I>(answer first via any chat method)</I>",
"event_end":"<B>TIME IS UP:</B> No one answered the Chat Riddle in time.",
"event_won":"<B>RIDDLE SOLVED!</B> By player %PLAYERNAME%!",
"event_won_others":"<B>RIDDLE SOLVED!</B> By player %PLAYERNAME%!",
"event_won_you":"<B>YOU SOLVED THE RIDDLE!</B> You earned: $%PRIZE%!"
}
},
"horse_leaser":{

View file

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace HISP.Game.Chat
{

View file

@ -1,8 +1,10 @@
using HISP.Server;
using HISP.Player;
using HISP.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace HISP.Game.Events
@ -16,19 +18,92 @@ namespace HISP.Game.Events
RiddleText = riddleText;
Answers = answers;
Reward = reward;
Active = false;
RealTimeRiddles.Add(this);
}
public int RiddleId;
public string RiddleText;
public string[] Answers;
public bool Active;
public int Reward;
private Timer riddleTimeout;
private const int RIDDLE_TIMEOUT = 30;
public static RealTimeRiddle GetRandomRiddle()
{
int randomRiddleIndex = GameServer.RandomNumberGenerator.Next(0, RealTimeRiddles.Count);
return RealTimeRiddles[randomRiddleIndex];
}
public void StartEvent()
{
Active = true;
riddleTimeout = new Timer(new TimerCallback(riddleTimedOut), null, RIDDLE_TIMEOUT * 60 * 1000, RIDDLE_TIMEOUT * 60 * 1000);
// Send riddle message to all players
byte[] riddleStartMessage = PacketBuilder.CreateChat(Messages.FormatEventRealTimeRiddleStart(RiddleText), PacketBuilder.CHAT_BOTTOM_RIGHT);
foreach(GameClient client in GameServer.ConnectedClients)
{
if (client.LoggedIn)
client.SendPacket(riddleStartMessage);
}
}
public void ShowStartMessage(GameClient client)
{
if (!Active)
return;
byte[] riddleStartMessage = PacketBuilder.CreateChat(Messages.FormatEventRealTimeRiddleStart(RiddleText), PacketBuilder.CHAT_BOTTOM_RIGHT);
client.SendPacket(riddleStartMessage);
}
public void Win(User winner)
{
if (!Active)
return;
if (Database.HasPlayerCompletedRealTimeRiddle(RiddleId, winner.Id))
return;
Database.CompleteRealTimeRiddle(RiddleId, winner.Id);
winner.TrackedItems.GetTrackedItem(Tracking.TrackableItem.RiddleWin).Count++;
if (winner.TrackedItems.GetTrackedItem(Tracking.TrackableItem.RiddleWin).Count >= 25)
winner.Awards.AddAward(Award.GetAwardById(33)); // Quick Wit
if (winner.TrackedItems.GetTrackedItem(Tracking.TrackableItem.RiddleWin).Count >= 250)
winner.Awards.AddAward(Award.GetAwardById(34)); // Riddle Genius
winner.Money += Reward;
byte[] riddleWonMessage = PacketBuilder.CreateChat(Messages.FormatEventRealTimeRiddleWonForOthers(winner.Username), PacketBuilder.CHAT_BOTTOM_RIGHT);
byte[] riddleYouWonMessage = PacketBuilder.CreateChat(Messages.FormatEventRealTimeRiddleWonForYou(Reward), PacketBuilder.CHAT_BOTTOM_RIGHT);
foreach (GameClient client in GameServer.ConnectedClients)
{
if (client.LoggedIn)
if (client.LoggedinUser.Id != winner.Id)
client.SendPacket(riddleWonMessage);
else
client.SendPacket(riddleYouWonMessage);
}
EndEvent();
}
public void EndEvent()
{
Active = false;
riddleTimeout.Dispose();
riddleTimeout = null;
}
private void riddleTimedOut(object state)
{
byte[] riddleTimedOutMessage = PacketBuilder.CreateChat(Messages.EventEndRealTimeRiddle, PacketBuilder.CHAT_BOTTOM_RIGHT);
foreach (GameClient client in GameServer.ConnectedClients)
{
if (client.LoggedIn)
client.SendPacket(riddleTimedOutMessage);
}
EndEvent();
}
public bool CheckRiddle(string message)
{
string msgCheck = message.ToLower();

View file

@ -61,7 +61,8 @@ namespace HISP.Game
// Events : Real Time Riddles
public static string EventStartRealTimeRiddleFormat;
public static string EventEndRealTimeRiddle;
public static string EventWonRealTimeRiddleFormat;
public static string EventWonRealTimeRiddleForOthersFormat;
public static string EventWonRealTimeRiddleForYouFormat;
// MultiHorses
public static string OtherPlayersHere;
@ -1100,6 +1101,20 @@ namespace HISP.Game
// Click
public static string NothingInterestingHere;
// Event : Real Time Riddle
public static string FormatEventRealTimeRiddleStart(string riddleText)
{
return EventStartRealTimeRiddleFormat.Replace("%RIDDLETEXT%", riddleText);
}
public static string FormatEventRealTimeRiddleWonForOthers(string winnerUserName)
{
return EventWonRealTimeRiddleForOthersFormat.Replace("%PLAYERNAME%", winnerUserName);
}
public static string FormatEventRealTimeRiddleWonForYou(int prize)
{
return EventWonRealTimeRiddleForYouFormat.Replace("%PRIZE%", prize.ToString("N0", CultureInfo.InvariantCulture));
}
// Mute Command
public static string FormatStoppedMutingPlayer(string username)
{

View file

@ -48,6 +48,7 @@ namespace HISP.Server
string Horses = "CREATE TABLE IF NOT EXISTS Horses(randomId INT, ownerId INT, leaseTime INT, leaser INT, breed INT, name TEXT(128), description TEXT(1028), sex TEXT(128), color TEXT(128), health INT, shoes INT, hunger INT, thirst INT, mood INT, groom INT, tiredness INT, experience INT, speed INT, strength INT, conformation INT, agility INT, endurance INT, inteligence INT, personality INT, height INT, saddle INT, saddlepad INT, bridle INT, companion INT, autoSell INT, trainTimer INT, category TEXT(128), spoiled INT, magicUsed INT, hidden TEXT(3))";
string WildHorse = "CREATE TABLE IF NOT EXISTS WildHorse(randomId INT, originalOwner INT, breed INT, x INT, y INT, name TEXT(128), description TEXT(1028), sex TEXT(128), color TEXT(128), health INT, shoes INT, hunger INT, thirst INT, mood INT, groom INT, tiredness INT, experience INT, speed INT, strength INT, conformation INT, agility INT, endurance INT, inteligence INT, personality INT, height INT, saddle INT, saddlepad INT, bridle INT, companion INT, timeout INT, autoSell INT, trainTimer INT, category TEXT(128), spoiled INT, magicUsed INT)";
string LastPlayer = "CREATE TABLE IF NOT EXISTS LastPlayer(roomId TEXT(1028), playerId INT)";
string SolvedRealTimeRiddles = "CREATE TABLE IF NOT EXISTS SolvedRealTimeRiddles(playerId INT, riddleId INT)";
string TrackingStats = "CREATE TABLE IF NOT EXISTS Tracking(playerId INT, what TEXT(128), count INT)";
string Treasure = "CREATE TABLE IF NOT EXISTS Treasure(randomId INT, x INT, y INT, value INT, type TEXT(128))";
string Ranches = "CREATE TABLE IF NOT EXISTS Ranches(ranchId INT, playerId INT, title TEXT(1028), description TEXT(1028), upgradeLevel INT, building1 INT, building2 INT, building3 INT, building4 INT, building5 INT, building6 INT, building7 INT, building8 INT, building9 INT, building10 INT, building11 INT, building12 INT, building13 INT, building14 INT, building15 INT, building16 INT, investedMoney INT)";
@ -58,6 +59,19 @@ namespace HISP.Server
string MutedPlayers = "CREATE TABLE IF NOT EXISTS MutedPlayers(playerId INT, mutePlayerId INT)";
string DeleteOnlineUsers = "DELETE FROM OnlineUsers";
try
{
MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = SolvedRealTimeRiddles;
sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
catch (Exception e)
{
Logger.WarnPrint(e.Message);
};
try
{
MySqlCommand sqlCommand = db.CreateCommand();
@ -1359,6 +1373,37 @@ namespace HISP.Server
return count;
}
}
public static bool HasPlayerCompletedRealTimeRiddle(int riddleId, int playerId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{
db.Open();
MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "SELECT COUNT(*) FROM SolvedRealTimeRiddles WHERE riddleId=@riddleId AND playerId=@playerId";
sqlCommand.Parameters.AddWithValue("@riddleId", riddleId);
sqlCommand.Parameters.AddWithValue("@playerId", playerId);
sqlCommand.Prepare();
int count = Convert.ToInt32(sqlCommand.ExecuteScalar());
sqlCommand.Dispose();
return count >= 1;
}
}
public static void CompleteRealTimeRiddle(int riddleId, int playerId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))
{
db.Open();
MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.CommandText = "INSERT INTO SolvedRealTimeRiddles VALUES(@playerId, @riddleId)";
sqlCommand.Parameters.AddWithValue("@riddleId", riddleId);
sqlCommand.Parameters.AddWithValue("@playerId", playerId);
sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
}
public static bool HasPlayerCompletedRiddle(int riddleId, int playerId)
{
using (MySqlConnection db = new MySqlConnection(ConnectionString))

View file

@ -10,6 +10,8 @@ using HISP.Game.Horse;
using HISP.Game.Items;
using System.Globalization;
using HISP.Security;
using System;
using HISP.Game.Events;
namespace HISP.Server
{
@ -815,6 +817,20 @@ namespace HISP.Server
}
}
// Register Events : Real Time Riddle
int totalRealTimeRiddles = gameData.events.real_time_riddle.Count;
for (int i = 0; i < totalRealTimeRiddles; i++)
{
int id = gameData.events.real_time_riddle[i].id;
string riddleText = gameData.events.real_time_riddle[i].text;
string[] riddleAnswers = gameData.events.real_time_riddle[i].answers.ToObject<string[]>();
int reward = gameData.events.real_time_riddle[i].money_reward;
RealTimeRiddle riddle = new RealTimeRiddle(id, riddleText, riddleAnswers, reward);
Logger.DebugPrint("Registered Riddle #" + riddle.RiddleId.ToString());
}
HorseInfo.HorseNames = gameData.horses.names.ToObject<string[]>();
Item.Present = gameData.item.special.present;
@ -828,6 +844,11 @@ namespace HISP.Server
Item.Earthworm = gameData.item.special.earthworm;
Item.BirthdayToken = gameData.item.special.birthday_token;
GameServer.IdleWarning = Convert.ToInt32(gameData.messages.disconnect.client_timeout.warn_after);
GameServer.IdleTimeout = Convert.ToInt32(gameData.messages.disconnect.client_timeout.kick_after);
Chat.PrivateMessageSound = gameData.messages.chat.pm_sound;
// New Users
Messages.NewUserMessage = gameData.messages.new_user.starting_message;
@ -900,7 +921,8 @@ namespace HISP.Server
Messages.EventStartRealTimeRiddleFormat = gameData.messages.events.real_time_riddle.event_start;
Messages.EventEndRealTimeRiddle = gameData.messages.events.real_time_riddle.event_end;
Messages.EventWonRealTimeRiddleFormat = gameData.messages.events.real_time_riddle.event_won;
Messages.EventWonRealTimeRiddleForOthersFormat = gameData.messages.events.real_time_riddle.event_won_others;
Messages.EventWonRealTimeRiddleForYouFormat = gameData.messages.events.real_time_riddle.event_won_you;
// MultiHorses
Messages.OtherPlayersHere = gameData.messages.meta.multihorses.other_players_here;
@ -1916,11 +1938,6 @@ namespace HISP.Server
Messages.IdleWarningFormat = gameData.messages.disconnect.client_timeout.warn_message;
Messages.KickReasonDuplicateLogin = gameData.messages.disconnect.dupe_login;
Chat.PrivateMessageSound = gameData.messages.chat.pm_sound;
GameServer.IdleWarning = gameData.messages.disconnect.client_timeout.warn_after;
GameServer.IdleTimeout = gameData.messages.disconnect.client_timeout.kick_after;
// Competition Gear
Messages.EquipCompetitionGearFormat = gameData.messages.equips.equip_competition_gear_format;

View file

@ -39,7 +39,7 @@ namespace HISP.Server
public static Random RandomNumberGenerator = new Random();
// Events
public static RealTimeRiddle ActiveRiddleEvent;
public static RealTimeRiddle RiddleEvent = RealTimeRiddle.GetRandomRiddle();
/*
* Private stuff
@ -144,6 +144,11 @@ namespace HISP.Server
}
}
if(totalMinutesElapsed % 30 == 0)
{
RiddleEvent = RealTimeRiddle.GetRandomRiddle();
RiddleEvent.StartEvent();
}
if (totalMinutesElapsed % 60 == 0)
{
@ -317,8 +322,8 @@ namespace HISP.Server
if (sender.LoggedinUser.MutePlayer.IsUserMuted(user))
sender.LoggedinUser.MutePlayer.UnmuteUser(user);
byte[] nowMuting = PacketBuilder.CreateChat(Messages.FormatStoppedMutingPlayer(user.Username), PacketBuilder.CHAT_BOTTOM_RIGHT);
sender.SendPacket(nowMuting);
byte[] stoppedMuting = PacketBuilder.CreateChat(Messages.FormatStoppedMutingPlayer(user.Username), PacketBuilder.CHAT_BOTTOM_RIGHT);
sender.SendPacket(stoppedMuting);
sender.LoggedinUser.MetaPriority = true;
byte[] metaPacket = PacketBuilder.CreateMetaPacket(Meta.BuildPlayerListMenu(sender.LoggedinUser));
@ -3432,6 +3437,9 @@ namespace HISP.Server
byte[] MotdData = PacketBuilder.CreateMotd();
sender.SendPacket(MotdData);
// Display Event Message
RiddleEvent.ShowStartMessage(sender);
// Send Queued Messages
string[] queuedMessages = Database.GetMessageQueue(sender.LoggedinUser.Id);
foreach(string queuedMessage in queuedMessages)
@ -5368,11 +5376,7 @@ namespace HISP.Server
Chat.ChatChannel channel = (Chat.ChatChannel)packet[1];
string message = packetStr.Substring(2, packetStr.Length - 4);
if (Chat.ProcessCommand(sender.LoggedinUser, message))
{
Logger.DebugPrint(sender.LoggedinUser.Username + " Attempting to run command '" + message + "' in channel: " + channel.ToString());
return;
}
Logger.DebugPrint(sender.LoggedinUser.Username + " Attempting to say '" + message + "' in channel: " + channel.ToString());
@ -5387,6 +5391,18 @@ namespace HISP.Server
if (message == "")
return;
if (Chat.ProcessCommand(sender.LoggedinUser, message))
{
Logger.DebugPrint(sender.LoggedinUser.Username + " Attempting to run command '" + message + "' in channel: " + channel.ToString());
return;
}
// Check events
if (RiddleEvent.Active)
if(RiddleEvent.CheckRiddle(message))
RiddleEvent.Win(sender.LoggedinUser);
// Check if player is muting channel
if( (sender.LoggedinUser.MuteGlobal && channel == Chat.ChatChannel.All) || (sender.LoggedinUser.MuteAds && channel == Chat.ChatChannel.Ads) || (sender.LoggedinUser.MuteHere && channel == Chat.ChatChannel.Here) && (sender.LoggedinUser.MuteBuddy && channel == Chat.ChatChannel.Buddies) && (sender.LoggedinUser.MuteNear && channel == Chat.ChatChannel.Near) && (sender.LoggedinUser.MuteIsland && channel == Chat.ChatChannel.Isle))