Skip to content

Commit

Permalink
feat: add event scheduler by json
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Nov 8, 2024
1 parent e6c0995 commit e5eb93e
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 20 deletions.
1 change: 1 addition & 0 deletions cmake/modules/BaseConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ find_package(pugixml CONFIG REQUIRED)
find_package(spdlog REQUIRED)
find_package(unofficial-argon2 CONFIG REQUIRED)
find_package(unofficial-libmariadb CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)

find_path(BOOST_DI_INCLUDE_DIRS "boost/di.hpp")

Expand Down
1 change: 1 addition & 0 deletions cmake/modules/CanaryLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ target_link_libraries(${PROJECT_NAME}_lib
spdlog::spdlog
unofficial::argon2::libargon2
unofficial::libmariadb
nlohmann_json::nlohmann_json
protobuf
)

Expand Down
146 changes: 146 additions & 0 deletions data/json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Events Scheduler Configuration - README

This README provides instructions on how to configure and use the `events.json` file for scheduling events in the Otservbr server environment.

## Overview

The `events.json` file allows you to schedule various in-game events, defining their properties such as start and end dates, experience rate, loot rate, and more. This allows you to create custom in-game experiences at specific times.

The file contains an array of events, with each event having various attributes that control the properties of the event.

## JSON Structure

Here is a breakdown of the `events.json` structure:

```json
{
"events": [
{
"name": "Otservbr example 1",
"startdate": "11/03/2020",
"enddate": "12/30/2025",
"script": "example.lua",
"ingame": {
"exprate": 100,
"lootrate": 100,
"bosslootrate": 100,
"spawnrate": 100,
"skillrate": 100
},
"description": "Otserver br example 1 description double exp and a half, double loot !chance!, regular spawn and double skill",
"colors": {
"colordark": "#235c00",
"colorlight": "#2d7400"
},
"details": {
"displaypriority": 6,
"isseasonal": 0,
"specialevent": 0
}
},
{
"name": "Otservbr example 2",
"startdate": "2/2/2022",
"enddate": "12/31/2025",
"script": "",
"ingame": {
"exprate": 100,
"lootrate": 100,
"bosslootrate": 100,
"spawnrate": 100,
"skillrate": 100
},
"description": "Otserver br example 2 description 50% less exp, triple loot !chance!, 50% faster spawn and regular skill",
"colors": {
"colordark": "#735D10",
"colorlight": "#8B6D05"
},
"details": {
"displaypriority": 6,
"isseasonal": 0,
"specialevent": 0
}
}
]
}
```

## Attributes Explained

### Event Attributes

- **name**: The name of the event.
- **startdate**: The start date of the event in the format `mm/dd/yyyy`.
- **enddate**: The end date of the event in the format `mm/dd/yyyy`.
- **script**: The script to be executed during the event (optional).

### In-game Attributes (`ingame`)

- **exprate**: The experience rate during the event.
- **lootrate**: The loot rate during the event.
- **bosslootrate**: The boss loot rate during the event.
- **spawnrate**: The spawn rate of monsters during the event.
- **skillrate**: The skill rate during the event.

### Description

- **description**: A textual description of the event, explaining its features.

### Colors (`colors`)

- **colordark**: The dark color associated with the event (in hexadecimal).
- **colorlight**: The light color associated with the event (in hexadecimal).

### Event Details (`details`)

- **displaypriority**: The priority level for displaying the event.
- **isseasonal**: Indicates whether the event is seasonal (`0` for no, `1` for yes).
- **specialevent**: Indicates whether the event is considered a special event (`0` for no, `1` for yes).

## How to Add a New Event

To add a new event:
1. Open the `events.json` file.
2. Add a new object to the `events` array with the appropriate details. Make sure to follow the structure outlined above.
3. Ensure that the `startdate` and `enddate` are in the correct format (`mm/dd/yyyy`), and adjust the in-game rates and other properties as needed.

### Example
Here is an example of a new event you could add:

```json
{
"name": "Double XP Weekend",
"startdate": "01/01/2026",
"enddate": "01/03/2026",
"script": "double_xp.lua",
"ingame": {
"exprate": 200,
"lootrate": 100,
"bosslootrate": 150,
"spawnrate": 120,
"skillrate": 150
},
"description": "Enjoy a double XP weekend with increased boss loot and spawn rates!",
"colors": {
"colordark": "#1a2b3c",
"colorlight": "#3c4d5e"
},
"details": {
"displaypriority": 5,
"isseasonal": 0,
"specialevent": 1
}
}
```

## Notes
- Make sure the JSON file is well-formed. You can use an online JSON validator to check for syntax errors.
- Always back up your `events.json` file before making major changes.

## Troubleshooting
- **JSON Parsing Errors**: If you encounter JSON parsing errors, check for missing commas or incorrect formatting in your file.
- **Missing Event Details**: Ensure all required attributes are properly defined for each event.

## Contact
For additional help, contact the support team or refer to the project's documentation.

50 changes: 50 additions & 0 deletions data/json/events.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"events": [
{
"name": "Otservbr example 1",
"startdate": "11/03/2020",
"enddate": "12/30/2025",
"script": "first-example.lua",
"ingame": {
"exprate": 100,
"lootrate": 100,
"bosslootrate": 100,
"spawnrate": 100,
"skillrate": 100
},
"description": "Otserver br example 1 description double exp and a half, double loot !chance!, regular spawn and double skill",
"colors": {
"colordark": "#235c00",
"colorlight": "#2d7400"
},
"details": {
"displaypriority": 6,
"isseasonal": 0,
"specialevent": 0
}
},
{
"name": "Otservbr example 2",
"startdate": "2/2/2022",
"enddate": "12/31/2025",
"script": "second-example.lua",
"ingame": {
"exprate": 100,
"lootrate": 100,
"bosslootrate": 100,
"spawnrate": 100,
"skillrate": 100
},
"description": "Otserver br example 2 description 50% less exp, triple loot !chance!, 50% faster spawn and regular skill",
"colors": {
"colordark": "#735D10",
"colorlight": "#8B6D05"
},
"details": {
"displaypriority": 6,
"isseasonal": 0,
"specialevent": 0
}
}
]
}
17 changes: 17 additions & 0 deletions data/json/scripts/first-example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- [OtServerBr]
-- Event scheduler lua scripts, on this file is possible to load any kind
-- of global values, create functions or create and register GlobalEvents using the revscript system.
-- For example you can load a 'local Example = GlobalEvent("example")' and register it with 'Example:register()',
-- adding the 'Example.onStartup()' or 'Example.onThink(interval)' with 'Example:interval(time)'.
-- With 'onStartup()' you can load any raid, for example loading a entire map/hunt and the choseen spawns.

-- Examples:
-- Loading map: Game.loadMap(DATA_DIRECTORY.. '/world/myMapFolder/myMapFile.otbm')
-- Loading spawn: addEvent(function() Game.loadSpawnFile(DATA_DIRECTORY.. '/world/mySpawnFolder/mySpawnFile.xml) end, 30 * 1000)

local globalEvent = GlobalEvent("ExampleOne")
function globalEvent.onStartup()
logger.info("Example one started")
end

globalEvent:register()
17 changes: 17 additions & 0 deletions data/json/scripts/second-example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- [OtServerBr]
-- Event scheduler lua scripts, on this file is possible to load any kind
-- of global values, create functions or create and register GlobalEvents using the revscript system.
-- For example you can load a 'local Example = GlobalEvent("example")' and register it with 'Example:register()',
-- adding the 'Example.onStartup()' or 'Example.onThink(interval)' with 'Example:interval(time)'.
-- With 'onStartup()' you can load any raid, for example loading a entire map/hunt and the choseen spawns.

-- Examples:
-- Loading map: Game.loadMap(DATA_DIRECTORY.. '/world/myMapFolder/myMapFile.otbm')
-- Loading spawn: addEvent(function() Game.loadSpawnFile(DATA_DIRECTORY.. '/world/mySpawnFolder/mySpawnFile.xml) end, 30 * 1000)

local globalEvent = GlobalEvent("ExampleTwo")
function globalEvent.onStartup()
logger.info("Example one started")
end

globalEvent:register()
5 changes: 4 additions & 1 deletion src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ void CanaryServer::loadModules() {

// Load XML folder dependencies (order matters)
modulesLoadHelper(g_vocations().loadFromXml(), "XML/vocations.xml");
modulesLoadHelper(g_eventsScheduler().loadScheduleEventFromXml(), "XML/events.xml");
modulesLoadHelper(Outfits::getInstance().loadFromXml(), "XML/outfits.xml");
modulesLoadHelper(Familiars::getInstance().loadFromXml(), "XML/familiars.xml");
modulesLoadHelper(g_imbuements().loadFromXml(), "XML/imbuements.xml");
Expand All @@ -375,6 +374,10 @@ void CanaryServer::loadModules() {
modulesLoadHelper(g_scripts().loadScripts(datapackFolder + "/monster", false, false), datapackFolder + "/monster");
modulesLoadHelper((g_npcs().load(false, true)), "npc");

// 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");

g_game().loadBoostedCreature();
g_ioBosstiary().loadBoostedBoss();
g_ioprey().initializeTaskHuntOptions();
Expand Down
Loading

0 comments on commit e5eb93e

Please sign in to comment.