Less Lag for rank control features

This commit is contained in:
Cooldude2606
2017-09-14 17:57:29 +01:00
parent 993abf054f
commit 422c54643c
2 changed files with 29 additions and 17 deletions

View File

@@ -30,28 +30,32 @@ end
--Convert the name of a rank into the rank object --Convert the name of a rank into the rank object
function string_to_rank(string) function string_to_rank(string)
if type(string) == 'string' then if type(string) == 'string' then
local Foundranks={} local found_ranks={}
for _,rank in pairs(get_ranks()) do for _,rank in pairs(get_ranks()) do
if rank.name:lower() == string:lower() then return rank end 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 end
if #Foundranks == 1 then return Foundranks[1] end if #found_ranks == 1 then return found_ranks[1] end
end end
end end
--Send a message to all members of this rank and above, if no rank given default is mod --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) function rank_print(msg, rank, inv)
local rank = string_to_rank(rank) or string_to_rank('Mod') -- default mod or higher local rank = string_to_rank(rank) or string_to_rank('Mod') -- default mod or higher
local inv = inv or false local inv = inv or false
for _, player in pairs(game.players) do for _, player in pairs(game.players) do
rankPower = get_rank(player).power --this part uses sudo to soread it other many ticks
if inv then player_rank_power = get_rank(player).power
if rankPower >= rank.power then if inv then
player.print(('[Everyone]: '..msg)) sudo(function(player_rank_power,rank)
end if player_rank_power >= rank.power then player.print(('[Everyone]: '..msg)) end
end,{player_rank_power,rank})
else else
if rankPower <= rank.power then sudo(function(player_rank_power,rank)
if rank.short_hand ~= '' then player.print(('['..(rank.short_hand)..']: '..msg)) else player.print(('[Everyone]: '..msg)) end if player_rank_power <= rank.power then
end 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 end
end end
@@ -60,6 +64,8 @@ function give_rank(player,rank,by_player)
local by_player = by_player or 'server' local by_player = by_player or 'server'
local rank = string_to_rank(rank) or rank or string_to_rank('Guest') local rank = string_to_rank(rank) or rank or string_to_rank('Guest')
local old_rank = get_rank(player) 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 --messaging
local message = 'demoted' local message = 'demoted'
if rank.power <= old_rank.power then message = 'promoted' end 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 if found_rank then table.insert(possible_ranks,string_to_rank(found_rank)) break end
end end
end end
--Loop through rank times -- to reduce lag if the player is already higher than any time rank then it does not cheak
for _,rank in pairs(get_ranks()) do -- also there play time must be higher than the lowest required for a rank
if rank.time and tick_to_min(player.online_time) >= rank.time then table.insert(possible_ranks,string_to_rank(rank)) end 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 end
--Loop through possible ranks --Loop through possible ranks
if current_rank.name ~='Jail' then if current_rank.name ~='Jail' then
@@ -110,6 +120,7 @@ function find_new_rank(player)
end end
--Give player new rank if availble --Give player new rank if availble
if highest_rank.name == 'Guest' then if highest_rank.name == 'Guest' then
-- to avoid spam in chat
player.permission_group=game.permissions.get_group('Guest') 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')}) script.raise_event(Event.rank_change, {player=player, by_player='server', new_rank=string_to_rank('Guest'), old_rank=string_to_rank('Guest')})
else else

View File

@@ -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. 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. 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. 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 For disallow, add to the list the end part of the input action
Example: defines.input_action.drop_item -> 'drop_item' Example: defines.input_action.drop_item -> 'drop_item'
http://lua-api.factorio.com/latest/defines.html#defines.input_action 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 = { local ranks = {
--these are asigned during the rank set up --these are asigned during the rank set up
highest_timed_rank=nil, highest_timed_rank=nil,
lowest_time_for_rank=nil, lowest_timed_rank=nil,
number_of_ranks=0, number_of_ranks=0,
ranks={ ranks={
{name='Owner', {name='Owner',
@@ -163,7 +164,7 @@ for n,rank in pairs(ranks.ranks) do
end end
end end
if rank.time and not ranks.highest_timed_rank then ranks.highest_timed_rank=rank 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 ranks.number_of_ranks=ranks.number_of_ranks+1
end end
-- surches the rank for a certain allow command -- surches the rank for a certain allow command