From 422c54643c403bd4bcaff708b295e6a60f0e76e5 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 14 Sep 2017 17:57:29 +0100 Subject: [PATCH] Less Lag for rank control features --- .../ExpGaming - Rank Control.lua | 41 ++++++++++++------- .../ExpGaming-Core/ExpGaming - Rank Table.lua | 5 ++- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/locale/ExpGaming-Core/ExpGaming - Rank Control.lua b/locale/ExpGaming-Core/ExpGaming - Rank Control.lua index 922d72e5..aaf96ad2 100644 --- a/locale/ExpGaming-Core/ExpGaming - Rank Control.lua +++ b/locale/ExpGaming-Core/ExpGaming - Rank Control.lua @@ -30,28 +30,32 @@ end --Convert the name of a rank into the rank object function string_to_rank(string) if type(string) == 'string' then - local Foundranks={} + local found_ranks={} for _,rank in pairs(get_ranks()) do if rank.name:lower() == string:lower() then return rank end - if rank.name:lower():find(string:lower()) then table.insert(Foundranks,rank) end + if rank.name:lower():find(string:lower()) then table.insert(found_ranks,rank) end end - if #Foundranks == 1 then return Foundranks[1] end + if #found_ranks == 1 then return found_ranks[1] end end end --Send a message to all members of this rank and above, if no rank given default is mod +--inv sends message to all lower ranks rather than higher function rank_print(msg, rank, inv) local rank = string_to_rank(rank) or string_to_rank('Mod') -- default mod or higher local inv = inv or false - for _, player in pairs(game.players) do - rankPower = get_rank(player).power - if inv then - if rankPower >= rank.power then - player.print(('[Everyone]: '..msg)) - end + for _, player in pairs(game.players) do + --this part uses sudo to soread it other many ticks + player_rank_power = get_rank(player).power + if inv then + sudo(function(player_rank_power,rank) + if player_rank_power >= rank.power then player.print(('[Everyone]: '..msg)) end + end,{player_rank_power,rank}) else - if rankPower <= rank.power then - if rank.short_hand ~= '' then player.print(('['..(rank.short_hand)..']: '..msg)) else player.print(('[Everyone]: '..msg)) end - end + sudo(function(player_rank_power,rank) + if player_rank_power <= rank.power then + if rank.short_hand ~= '' then player.print(('['..(rank.short_hand)..']: '..msg)) else player.print(('[Everyone]: '..msg)) end + end + end,{player_rank_power,rank}) end end end @@ -60,6 +64,8 @@ function give_rank(player,rank,by_player) local by_player = by_player or 'server' local rank = string_to_rank(rank) or rank or string_to_rank('Guest') local old_rank = get_rank(player) + -- to reducse lag if the ranks are all ready given it does not cheak + if old_rank == rank then return end --messaging local message = 'demoted' if rank.power <= old_rank.power then message = 'promoted' end @@ -98,9 +104,13 @@ function find_new_rank(player) if found_rank then table.insert(possible_ranks,string_to_rank(found_rank)) break end end end - --Loop through rank times - for _,rank in pairs(get_ranks()) do - if rank.time and tick_to_min(player.online_time) >= rank.time then table.insert(possible_ranks,string_to_rank(rank)) end + -- to reduce lag if the player is already higher than any time rank then it does not cheak + -- also there play time must be higher than the lowest required for a rank + if current_rank.power > ranks.highest_timed_rank.power and player.online_time >= ranks.lowest_time_for_rank.time then + --Loop through rank times + for _,rank in pairs(get_ranks()) do + if rank.time and tick_to_min(player.online_time) >= rank.time then table.insert(possible_ranks,string_to_rank(rank)) end + end end --Loop through possible ranks if current_rank.name ~='Jail' then @@ -110,6 +120,7 @@ function find_new_rank(player) end --Give player new rank if availble if highest_rank.name == 'Guest' then + -- to avoid spam in chat player.permission_group=game.permissions.get_group('Guest') script.raise_event(Event.rank_change, {player=player, by_player='server', new_rank=string_to_rank('Guest'), old_rank=string_to_rank('Guest')}) else diff --git a/locale/ExpGaming-Core/ExpGaming - Rank Table.lua b/locale/ExpGaming-Core/ExpGaming - Rank Table.lua index 6d060370..a1cef53b 100644 --- a/locale/ExpGaming-Core/ExpGaming - Rank Table.lua +++ b/locale/ExpGaming-Core/ExpGaming - Rank Table.lua @@ -27,6 +27,7 @@ time is used for auto-rank feature where you are moved to the rank after a cert colour is the RGB value that can be used to emphasise GUI elements based on rank. power is not in the list below as it is auto-defined by index but allows comparison between ranks. disallow is a list containing input actions that the user can not perform. +allow is a list of custom commands and effects that that rank can use, all defined in the sctips. For disallow, add to the list the end part of the input action Example: defines.input_action.drop_item -> 'drop_item' http://lua-api.factorio.com/latest/defines.html#defines.input_action @@ -34,7 +35,7 @@ http://lua-api.factorio.com/latest/defines.html#defines.input_action local ranks = { --these are asigned during the rank set up highest_timed_rank=nil, - lowest_time_for_rank=nil, + lowest_timed_rank=nil, number_of_ranks=0, ranks={ {name='Owner', @@ -163,7 +164,7 @@ for n,rank in pairs(ranks.ranks) do end end if rank.time and not ranks.highest_timed_rank then ranks.highest_timed_rank=rank end - if rank.time then ranks.lowest_time_for_rank=rank end + if rank.time then ranks.lowest_timed_rank=rank end ranks.number_of_ranks=ranks.number_of_ranks+1 end -- surches the rank for a certain allow command