diff --git a/features/rank_system.lua b/features/rank_system.lua index a8a25255..9af3903a 100644 --- a/features/rank_system.lua +++ b/features/rank_system.lua @@ -13,6 +13,7 @@ local Global = require 'utils.global' local table = require 'utils.table' local Token = require 'utils.token' local Utils = require 'utils.core' +local math = require 'utils.math' local Server = require 'features.server' local Ranks = require 'resources.ranks' local Colors = require 'resources.color_presets' @@ -22,16 +23,28 @@ local Config = global.config.rank_system -- Localized functions local format = string.format local index_in_array = table.index_of_in_array +local clamp = math.clamp -- Constants local ranking_data_set = 'rankings' local nth_tick = 215983 -- nearest prime to 1 hour in ticks local rank_name_lookup = {} +local sorted_ranks = {} +local rank_to_index = {} for k, v in pairs(Ranks) do rank_name_lookup[v] = k end +for k, v in pairs(Ranks) do + sorted_ranks[#sorted_ranks + 1] = v +end +table.sort(sorted_ranks) + +for k, v in pairs(sorted_ranks) do + rank_to_index[v] = k +end + -- Local vars local Public = {} @@ -52,6 +65,15 @@ Global.register( -- Local functions +--- Changes a rank +local function change_rank(current_rank, change) + local index = rank_to_index[current_rank] + + local new_index = clamp(index + change, 1, #sorted_ranks) + + return sorted_ranks[new_index] +end + --- Gets a player's rank. Intentionally not exposed. local function get_player_rank(player_name) return player_ranks[player_name] or 0 @@ -205,7 +227,12 @@ end -- @param player_name -- @return new rank name or nil if already at highest rank function Public.increase_player_rank(player_name) - local new_rank = (get_player_rank(player_name) + 1) + local current_rank = (get_player_rank(player_name)) + local new_rank = change_rank(current_rank, 1) + if current_rank == new_rank then + return nil + end + local new_rank_name = rank_name_lookup[new_rank] if new_rank_name then player_ranks[player_name] = (new_rank) @@ -219,7 +246,12 @@ end -- @param player_name -- @return new rank name or nil if already at lowest rank function Public.decrease_player_rank(player_name) - local new_rank = (get_player_rank(player_name) - 1) + local current_rank = (get_player_rank(player_name)) + local new_rank = change_rank(current_rank, -1) + if current_rank == new_rank then + return nil + end + local new_rank_name = rank_name_lookup[new_rank] if new_rank_name then player_ranks[player_name] = (new_rank) diff --git a/resources/ranks.lua b/resources/ranks.lua index 49df822e..977bc9e5 100644 --- a/resources/ranks.lua +++ b/resources/ranks.lua @@ -1,8 +1,8 @@ -- When adding/removing/changing ranks, rank_system has a migrate_data() function you can use to adjust the existing data. return { - probation = -1, + probation = -10, guest = 0, - auto_trusted = 1, - regular = 2, - admin = 3, + auto_trusted = 10, + regular = 20, + admin = 30, }