Skip to content

Commit

Permalink
improve: eventscheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Nov 13, 2024
1 parent acb51f8 commit ef78a00
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 29 deletions.
File renamed without changes.
184 changes: 184 additions & 0 deletions data/json/eventscheduler/events.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
{
"events": [
{
"name": "Forge Time",
"startdate": "11/12/2024",
"enddate": "11/17/2024",
"ingame": {
"forgechance": 120
},
"description": "Increases the success rate of forges by 20%.",
"colors": {
"colordark": "#2b3e50",
"colorlight": "#3d5a73"
},
"details": {
"displaypriority": 5,
"isseasonal": 0,
"specialevent": 1
}
},
{
"name": "Double Bestiary",
"startdate": "11/12/2024",
"enddate": "11/17/2024",
"ingame": {
"doublebestiary": true
},
"description": "Doubles the bestiary counter when defeating monsters.",
"colors": {
"colordark": "#3a4f2a",
"colorlight": "#4f713a"
},
"details": {
"displaypriority": 5,
"isseasonal": 0,
"specialevent": 1
}
},
{
"name": "Fast Exercise",
"startdate": "11/12/2024",
"enddate": "11/17/2024",
"ingame": {
"doubleexercise": true
},
"description": "Exercise weapons are faster, doubling their speed.",
"colors": {
"colordark": "#5a3a2a",
"colorlight": "#7b4f3a"
},
"details": {
"displaypriority": 5,
"isseasonal": 0,
"specialevent": 1
}
},
{
"name": "50% Loot Bonus",
"startdate": "11/12/2024",
"enddate": "11/17/2024",
"ingame": {
"lootrate": 150
},
"description": "Increases loot by 50%.",
"colors": {
"colordark": "#2b1e10",
"colorlight": "#4a2e18"
},
"details": {
"displaypriority": 6,
"isseasonal": 0,
"specialevent": 1
}
},
{
"name": "50% Exp Bonus",
"startdate": "11/12/2024",
"enddate": "11/17/2024",
"ingame": {
"exprate": 150
},
"description": "Increases experience by 50%.",
"colors": {
"colordark": "#234d00",
"colorlight": "#3a7500"
},
"details": {
"displaypriority": 6,
"isseasonal": 0,
"specialevent": 1
}
},
{
"name": "Boss Cooldown Reduction",
"startdate": "11/12/2024",
"enddate": "11/17/2024",
"ingame": {
"bosscooldown": 150
},
"description": "Reduces boss cooldown time by 50%.",
"colors": {
"colordark": "#2d3c5a",
"colorlight": "#4a5f7d"
},
"details": {
"displaypriority": 6,
"isseasonal": 0,
"specialevent": 1
}
},
{
"name": "Double Exp",
"startdate": "11/12/2024",
"enddate": "11/17/2024",
"ingame": {
"exprate": 200
},
"description": "Double experience when hunting monsters.",
"colors": {
"colordark": "#002d00",
"colorlight": "#004400"
},
"details": {
"displaypriority": 6,
"isseasonal": 1,
"specialevent": 1
}
},
{
"name": "Double Loot",
"startdate": "11/12/2024",
"enddate": "11/17/2024",
"ingame": {
"lootrate": 200
},
"description": "Doubles the amount of loot obtained from monsters.",
"colors": {
"colordark": "#2a1d00",
"colorlight": "#4c3300"
},
"details": {
"displaypriority": 6,
"isseasonal": 1,
"specialevent": 1
}
},
{
"name": "Double Bosstiary",
"startdate": "11/12/2024",
"enddate": "11/17/2024",
"ingame": {
"doublebosstiary": true
},
"description": "Doubles the bestiary counter for bosses.",
"colors": {
"colordark": "#3a005a",
"colorlight": "#4e0075"
},
"details": {
"displaypriority": 5,
"isseasonal": 0,
"specialevent": 1
}
},
{
"name": "Fast Respawn",
"startdate": "11/01/2024",
"enddate": "11/15/2024",
"ingame": {
"spawnrate": 200
},
"description": "Monsters respawn twice as fast.",
"colors": {
"colordark": "#4d2f00",
"colorlight": "#6e3e00"
},
"details": {
"displaypriority": 6,
"isseasonal": 1,
"specialevent": 1
}
}
]
}
4 changes: 4 additions & 0 deletions data/json/eventscheduler/scripts/example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local globalEvent = GlobalEvent("EventScheduleExample")
function globalEvent.onStartup() end

globalEvent:register()
Empty file.
6 changes: 0 additions & 6 deletions data/json/scripts/double_bestiary.lua

This file was deleted.

6 changes: 0 additions & 6 deletions data/json/scripts/double_bosstiary.lua

This file was deleted.

6 changes: 0 additions & 6 deletions data/json/scripts/fast_exercise.lua

This file was deleted.

6 changes: 0 additions & 6 deletions data/json/scripts/forge_time.lua

This file was deleted.

2 changes: 1 addition & 1 deletion src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ void CanaryServer::loadModules() {

// It needs to be loaded after the revscript is read in order to use the scripting interface
modulesLoadHelper(g_eventsScheduler().loadScheduleEventFromXml(), "XML/events.xml");
modulesLoadHelper(g_eventsScheduler().loadScheduleEventFromJson(), "json/events.json");
modulesLoadHelper(g_eventsScheduler().loadScheduleEventFromJson(), "json/eventscheduler/events.json");

g_game().loadBoostedCreature();
g_ioBosstiary().loadBoostedBoss();
Expand Down
44 changes: 40 additions & 4 deletions src/game/scheduling/events_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ bool EventsScheduler::loadScheduleEventFromJson() {
g_kv().scoped("eventscheduler")->remove("double-bestiary");
g_kv().scoped("eventscheduler")->remove("double-bosstiary");
g_kv().scoped("eventscheduler")->remove("fast-exercise");
g_kv().scoped("eventscheduler")->remove("boss-cooldown");

using json = nlohmann::json;
auto coreFolder = g_configManager().getString(CORE_DIRECTORY);
auto folder = coreFolder + "/json/events.json";
auto folder = coreFolder + "/json/eventscheduler/events.json";
std::ifstream file(folder);
if (!file.is_open()) {
g_logger().error("{} - Unable to open file '{}'", __FUNCTION__, folder);
Expand Down Expand Up @@ -56,8 +57,7 @@ bool EventsScheduler::loadScheduleEventFromJson() {
}

int startYear, startMonth, startDay, endYear, endMonth, endDay;
if (sscanf(event["startdate"].get<std::string>().c_str(), "%d/%d/%d", &startMonth, &startDay, &startYear) != 3 ||
sscanf(event["enddate"].get<std::string>().c_str(), "%d/%d/%d", &endMonth, &endDay, &endYear) != 3) {
if (sscanf(event["startdate"].get<std::string>().c_str(), "%d/%d/%d", &startMonth, &startDay, &startYear) != 3 || sscanf(event["enddate"].get<std::string>().c_str(), "%d/%d/%d", &endMonth, &endDay, &endYear) != 3) {
g_logger().warn("{} - Invalid date format for event '{}'", __FUNCTION__, eventName);
continue;
}
Expand Down Expand Up @@ -94,26 +94,62 @@ bool EventsScheduler::loadScheduleEventFromJson() {
static_cast<uint32_t>(event.contains("ingame") && event["ingame"].contains("lootrate") ? event["ingame"].value("lootrate", 100) : 100),
static_cast<uint32_t>(event.contains("ingame") && event["ingame"].contains("bosslootrate") ? event["ingame"].value("bosslootrate", 100) : 100),
static_cast<uint32_t>(event.contains("ingame") && event["ingame"].contains("spawnrate") ? event["ingame"].value("spawnrate", 100) : 100),
static_cast<uint16_t>(event.contains("ingame") && event["ingame"].contains("skillrate") ? event["ingame"].value("skillrate", 100) : 100)
static_cast<uint16_t>(event.contains("ingame") && event["ingame"].contains("skillrate") ? event["ingame"].value("skillrate", 100) : 100),
static_cast<uint8_t>(event.contains("ingame") && event["ingame"].contains("forgechance") ? event["ingame"].value("forge-chance", 100) : 100),
static_cast<uint8_t>(event.contains("ingame") && event["ingame"].contains("bosscooldown") ? event["ingame"].value("bosscooldown", 100) : 100),
event.contains("ingame") && event["ingame"].contains("doublebestiary") ? event["ingame"].value("doublebestiary", false) : false,
event.contains("ingame") && event["ingame"].contains("doublebosstiary") ? event["ingame"].value("doublebosstiary", false) : false,
event.contains("ingame") && event["ingame"].contains("fastexercise") ? event["ingame"].value("fastexercise", false) : false,
};

for (const auto &[existingEventName, rates] : eventsOnSameDay) {
std::vector<std::string> modifiedRates;

if (rates.exprate != 100 && currentEventRates.exprate != 100 && rates.exprate == currentEventRates.exprate) {
modifiedRates.emplace_back("exprate");
g_eventsScheduler().setExpSchedule(rates.exprate);
}
if (rates.lootrate != 100 && currentEventRates.lootrate != 100 && rates.lootrate == currentEventRates.lootrate) {
modifiedRates.emplace_back("lootrate");
g_eventsScheduler().setLootSchedule(rates.lootrate);
}
if (rates.bosslootrate != 100 && currentEventRates.bosslootrate != 100 && rates.bosslootrate == currentEventRates.bosslootrate) {
modifiedRates.emplace_back("bosslootrate");
g_eventsScheduler().setBossLootSchedule(rates.bosslootrate);
}
if (rates.spawnrate != 100 && currentEventRates.spawnrate != 100 && rates.spawnrate == currentEventRates.spawnrate) {
modifiedRates.emplace_back("spawnrate");
g_eventsScheduler().setSpawnMonsterSchedule(rates.spawnrate);
}
if (rates.skillrate != 100 && currentEventRates.skillrate != 100 && rates.skillrate == currentEventRates.skillrate) {
modifiedRates.emplace_back("skillrate");
g_eventsScheduler().setSkillSchedule(rates.skillrate);
}

// KV changes
if (rates.forgeChance != 100 && currentEventRates.forgeChance != 100 && rates.forgeChance == currentEventRates.forgeChance) {
modifiedRates.emplace_back("forge-chance");
g_kv().scoped("eventscheduler")->set("forge-chance", rates.forgeChance - 100);
}

if (rates.doubleBestiary != false && currentEventRates.doubleBestiary != false && rates.doubleBestiary == currentEventRates.doubleBestiary) {
modifiedRates.emplace_back("double-bestiary");
g_kv().scoped("eventscheduler")->set("double-bestiary", true);
}

if (rates.doubleBossTiary != false && currentEventRates.doubleBossTiary != false && rates.doubleBossTiary == currentEventRates.doubleBossTiary) {
modifiedRates.emplace_back("double-bosstiary");
g_kv().scoped("eventscheduler")->set("double-bosstiary", true);
}

if (rates.fastExercise != false && currentEventRates.fastExercise != false && rates.fastExercise == currentEventRates.fastExercise) {
modifiedRates.emplace_back("fast-exercise");
g_kv().scoped("eventscheduler")->set("fast-exercise", true);
}

if (rates.bosscooldown != 100 && currentEventRates.bosscooldown != 100 && rates.bosscooldown == currentEventRates.bosscooldown) {
modifiedRates.emplace_back("bosscooldown");
g_kv().scoped("eventscheduler")->set("boss-cooldown", rates.bosscooldown - 100);
}

if (!modifiedRates.empty()) {
Expand Down
5 changes: 5 additions & 0 deletions src/game/scheduling/events_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ struct EventRates {
uint32_t bosslootrate = 100;
uint32_t spawnrate = 100;
uint16_t skillrate = 100;
uint8_t forgeChance = 100;
uint8_t bosscooldown = 100;
bool doubleBestiary {};
bool doubleBossTiary {};
bool fastExercise {};
};

class EventsScheduler {
Expand Down
8 changes: 8 additions & 0 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9131,7 +9131,15 @@ void ProtocolGame::sendBosstiaryCooldownTimer() {
if (!timerValue || !timerValue.has_value()) {
continue;
}

auto scheduleTimerOpt = g_kv().scoped("eventscheduler")->get("boss-cooldown");
uint8_t schedulePercentage = 0;
if (scheduleTimerOpt) {
schedulePercentage = static_cast<uint8_t>(scheduleTimerOpt->getNumber());
}

auto timer = timerValue->getNumber();
timer = static_cast<uint32_t>(timer * schedulePercentage / 100);
uint64_t sendTimer = timer > 0 ? static_cast<uint64_t>(timer) : 0;
msg.add<uint32_t>(bossRaceId); // bossRaceId
msg.add<uint64_t>(sendTimer); // Boss cooldown in seconds
Expand Down

0 comments on commit ef78a00

Please sign in to comment.