-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Loyalty System (10.74) #4148
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
function onUpdateDatabase() | ||
return false | ||
print("> Updating database to version 33 (loyalty system)") | ||
db.query("ALTER TABLE `accounts` ADD COLUMN `loyalty_points` SMALLINT UNSIGNED NOT NULL DEFAULT 0 AFTER `premium_ends_at`") | ||
db.query("INSERT INTO `server_config` (`config`, `value`) VALUES ('loyalty_updated', '0')") | ||
return true | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function onUpdateDatabase() | ||
return false | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,32 @@ ConfigManager::ConfigManager() { string[CONFIG_FILE] = "config.lua"; } | |
|
||
namespace { | ||
|
||
LoyaltyBonuses loadLuaLoyaltyBonuses(lua_State* L) | ||
{ | ||
LoyaltyBonuses bonuses; | ||
|
||
lua_getglobal(L, "loyaltyBonuses"); | ||
if (!lua_istable(L, -1)) { | ||
return {}; | ||
} | ||
|
||
lua_pushnil(L); | ||
while (lua_next(L, -2) != 0) { | ||
const auto tableIndex = lua_gettop(L); | ||
uint16_t min = LuaScriptInterface::getField<uint16_t>(L, tableIndex, "min"); | ||
uint16_t max = LuaScriptInterface::getField<uint16_t>(L, tableIndex, "max"); | ||
float bonus = LuaScriptInterface::getField<float>(L, tableIndex, "bonus"); | ||
|
||
max = max == 0 ? std ::numeric_limits<std::uint16_t>::max() : max; | ||
bonuses.emplace_back(min, max, bonus); | ||
lua_pop(L, 4); | ||
} | ||
lua_pop(L, 1); | ||
|
||
std::sort(bonuses.begin(), bonuses.end()); | ||
return bonuses; | ||
}; | ||
|
||
ExperienceStages loadLuaStages(lua_State* L) | ||
{ | ||
ExperienceStages stages; | ||
|
@@ -230,6 +256,7 @@ bool ConfigManager::load() | |
boolean[REMOVE_ON_DESPAWN] = getGlobalBoolean(L, "removeOnDespawn", true); | ||
boolean[PLAYER_CONSOLE_LOGS] = getGlobalBoolean(L, "showPlayerLogInConsole", true); | ||
boolean[TWO_FACTOR_AUTH] = getGlobalBoolean(L, "enableTwoFactorAuth", true); | ||
boolean[LOYALTY_SYSTEM] = getGlobalBoolean(L, "loyaltyEnabled", true); | ||
|
||
string[DEFAULT_PRIORITY] = getGlobalString(L, "defaultPriority", "high"); | ||
string[SERVER_NAME] = getGlobalString(L, "serverName", ""); | ||
|
@@ -286,6 +313,9 @@ bool ConfigManager::load() | |
} | ||
expStages.shrink_to_fit(); | ||
|
||
loyaltyBonuses = loadLuaLoyaltyBonuses(L); | ||
loyaltyBonuses.shrink_to_fit(); | ||
|
||
loaded = true; | ||
lua_close(L); | ||
|
||
|
@@ -334,6 +364,19 @@ float ConfigManager::getExperienceStage(uint32_t level) const | |
return std::get<2>(*it); | ||
} | ||
|
||
float ConfigManager::getLoyaltyBonus(uint16_t points) const | ||
{ | ||
auto it = std::find_if(loyaltyBonuses.begin(), loyaltyBonuses.end(), [points](LoyaltyBonuses::value_type bonus) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should just be |
||
return points >= std::get<0>(bonus) && points <= std::get<1>(bonus); | ||
}); | ||
|
||
if (it == loyaltyBonuses.end()) { | ||
return 0; | ||
} | ||
|
||
return std::get<2>(*it); | ||
} | ||
|
||
bool ConfigManager::setString(string_config_t what, const std::string& value) | ||
{ | ||
if (what >= LAST_STRING_CONFIG) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2108,6 +2108,7 @@ void LuaScriptInterface::registerFunctions() | |
registerEnumIn("configKeys", ConfigManager::SERVER_SAVE_CLOSE); | ||
registerEnumIn("configKeys", ConfigManager::SERVER_SAVE_SHUTDOWN); | ||
registerEnumIn("configKeys", ConfigManager::ONLINE_OFFLINE_CHARLIST); | ||
registerEnumIn("configKeys", ConfigManager::LOYALTY_SYSTEM); | ||
|
||
registerEnumIn("configKeys", ConfigManager::MAP_NAME); | ||
registerEnumIn("configKeys", ConfigManager::HOUSE_RENT_PERIOD); | ||
|
@@ -2699,6 +2700,10 @@ void LuaScriptInterface::registerFunctions() | |
|
||
registerMethod("Player", "getIdleTime", LuaScriptInterface::luaPlayerGetIdleTime); | ||
|
||
registerMethod("Player", "getLoyaltyStatus", LuaScriptInterface::luaPlayerGetLoyaltyStatus); | ||
registerMethod("Player", "addLoyalty", LuaScriptInterface::luaPlayerAddLoyalty); | ||
registerMethod("Player", "removeLoyalty", LuaScriptInterface::luaPlayerRemoveLoyalty); | ||
|
||
// Monster | ||
registerClass("Monster", "Creature", LuaScriptInterface::luaMonsterCreate); | ||
registerMetaMethod("Monster", "__eq", LuaScriptInterface::luaUserdataCompare); | ||
|
@@ -10800,6 +10805,49 @@ int LuaScriptInterface::luaPlayerGetIdleTime(lua_State* L) | |
return 1; | ||
} | ||
|
||
int LuaScriptInterface::luaPlayerGetLoyaltyStatus(lua_State* L) | ||
{ | ||
// player:getLoyaltyStatus() | ||
const Player* const player = getUserdata<Player>(L, 1); | ||
if (player) { | ||
lua_createtable(L, 0, 2); | ||
setField(L, "points", player->getLoyaltyPoints()); | ||
setField(L, "bonus", player->getLoyaltyBonus()); | ||
} else { | ||
lua_pushnil(L); | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
int LuaScriptInterface::luaPlayerAddLoyalty(lua_State* L) | ||
{ | ||
// player:addLoyalty([points = 1]) | ||
Player* player = getUserdata<Player>(L, 1); | ||
if (!player) { | ||
lua_pushnil(L); | ||
} | ||
|
||
uint16_t points = getNumber<uint16_t>(L, 2, 1); | ||
pushBoolean(L, player->addLoyalty(points)); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove newline |
||
return 1; | ||
} | ||
|
||
int LuaScriptInterface::luaPlayerRemoveLoyalty(lua_State* L) | ||
{ | ||
// player:removeLoyalty([points = 1]) | ||
Player* player = getUserdata<Player>(L, 1); | ||
if (!player) { | ||
lua_pushnil(L); | ||
} | ||
|
||
uint16_t points = getNumber<uint16_t>(L, 2, 1); | ||
pushBoolean(L, player->addLoyalty(-points)); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove newline |
||
return 1; | ||
} | ||
|
||
// Monster | ||
int LuaScriptInterface::luaMonsterCreate(lua_State* L) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can drop this flag and let users effectively disable the loyalty system by removing all tiers.