From 4df9cfb475b3db6f62df4cf7704d144a817ee8d4 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 21 Dec 2023 15:19:59 -0500 Subject: [PATCH] Added XP Rate command --- modules/commands/set-xp-rate.readme.md | 15 ++++ modules/commands/set-xp-rate.ts | 116 +++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 modules/commands/set-xp-rate.readme.md create mode 100644 modules/commands/set-xp-rate.ts diff --git a/modules/commands/set-xp-rate.readme.md b/modules/commands/set-xp-rate.readme.md new file mode 100644 index 0000000..f2807e1 --- /dev/null +++ b/modules/commands/set-xp-rate.readme.md @@ -0,0 +1,15 @@ +## Set XP Rate Command + +This will allow players to use their chat window to set a custom xp rate with a limit. This allows individual players to either choose to enjoy the store or get to the end game content. + +Configuration: + +**MAX_XP_RATE**: number - defaults to 5 + +Chat commands + +``` +#xprate show - displays players current xp rate +#xprate set [number] - sets the players xp growth rate + +``` diff --git a/modules/commands/set-xp-rate.ts b/modules/commands/set-xp-rate.ts new file mode 100644 index 0000000..ccd7edf --- /dev/null +++ b/modules/commands/set-xp-rate.ts @@ -0,0 +1,116 @@ +/** + * @file set-xp-rate.ts + * @date 2023-11-15 + * @author ben-of-codecraft + * + * Type: Command + * Adds a command that allows players to set their own XP rate for their character up to 5x normal xp rate. + * + */ + +/** + * Configuration options + */ +const MAX_XP_RATE = 5; + +// Command to show the current xp rate +const xpCmd = "#xprate"; +const showXPcmd = "#xprate show"; +const setXPcmd = "#xprate set"; + +const XP_RATE_SETTING = "xp_rate"; + +import { PlayerStats } from "../classes/stats"; + +let xpRateCache = new Map(); + +const XPRateHandler: player_event_on_chat = (event: number, player: Player, message: string) => { + + if(message.includes(xpCmd)) { + + const args = message.split(" "); + const cmd = args[1]; + + + const playerCustom = new PlayerStats(player); + playerCustom.load(); + + if(cmd == "show") { + const xpRate = xpRateCache.get(player.GetGUIDLow()); + if(xpRate != undefined) { + player.SendBroadcastMessage(`Your current XP rate is ${xpRate}x`); + } else { + player.SendBroadcastMessage(`Your current XP rate is 1x`); + } + + } else if(cmd == "set") { + const rate = args[2]; + const rateNum = parseInt(rate); + + if(rateNum > MAX_XP_RATE) { + player.SendNotification(`You cannot set your XP rate higher then ${MAX_XP_RATE}x`); + return false; + } + + playerCustom.setStat(XP_RATE_SETTING, rateNum); + playerCustom.save(); + xpRateCache.set(player.GetGUIDLow(), rateNum); + + player.SendBroadcastMessage(`Your XP rate has been set to ${rateNum}x`); + } + else { + player.SendBroadcastMessage(`Usage: ${xpCmd} [show|set] [rate]`); + } + + return false; + } + + return true; +}; + +/** + * Gives players extra XP based on their rate + * @param event \ + * @param player + * @param amount + * @param victim + */ +const XPBonus: player_event_on_give_xp = (event: number, player: Player, amount: number, victim: Unit) => { + + const xpRate = xpRateCache.get(player.GetGUIDLow()); + if(xpRate && xpRate > 1) { + player.GiveXP(amount * xpRate); + } + +} + +const XPRateLoader: player_event_on_login = (event: number, player: Player) => { + + + const playerCustom = new PlayerStats(player); + playerCustom.load(); + + const xpRate = playerCustom.getStat(XP_RATE_SETTING); + if(xpRate) { + xpRateCache.set(player.GetGUIDLow(), xpRate.value); + } else { + xpRateCache.set(player.GetGUIDLow(), 1); + } + +}; + +// Grants players extra XP Based on their rate +RegisterPlayerEvent(PlayerEvents.PLAYER_EVENT_ON_GIVE_XP, (...args) => XPBonus(...args)); + +// Register the command +RegisterPlayerEvent(PlayerEvents.PLAYER_EVENT_ON_CHAT, + (...args) => XPRateHandler(...args) +); + +// Loads the cache of seetings on login +RegisterPlayerEvent(PlayerEvents.PLAYER_EVENT_ON_LOGIN, (...args) => XPRateLoader(...args)); + +// reloads the cache of settings when the lua state is opened +RegisterServerEvent(ServerEvents.ELUNA_EVENT_ON_LUA_STATE_OPEN, (...args) => { + xpRateCache = PlayerStats.GetStatsByType('player', XP_RATE_SETTING); +}); \ No newline at end of file