Rank control clean

This commit is contained in:
Cooldude2606
2017-10-14 23:26:51 +01:00
parent ffc9f85c72
commit 6513e25c3f
13 changed files with 115 additions and 143 deletions

View File

@@ -5,34 +5,25 @@ This file can be used with permission but this and the credit below must remain
Contact a member of management on our discord to seek permission to use our code.
Any changes that you may make to the code are yours but that does not make the script yours.
Discord: https://discord.gg/XSsBV6b
The credit below may be used by another script do not remove.
]]
local credits = {{
name='Explosive Gaming Rank System',
owner='Explosive Gaming',
dev='Cooldude2606',
description='The very core upon which all of the others are based on',
factorio_version='0.15.23',
show=true
}}
local function credit_loop(reg) for _,cred in pairs(reg) do table.insert(credits,cred) end end
--Please Only Edit Below This Line-----------------------------------------------------------
ranking = {}
local ranking.get_ranks, ranking.get_rank_groups = require("ExpGaming - Rank Table")
--Return the rank of a given player
function get_rank(player)
function ranking.get_player_rank(player)
if player then
debug_write({'RANK','PLAYER-GET'},player.name)
for _,rank in pairs(get_ranks()) do
for _,rank in pairs(ranking.get_ranks()) do
if player.permission_group == game.permissions.get_group(rank.name) then return rank end
end
return string_to_rank_group('User').lowest_rank
return ranking.string_to_rank_group('User').lowest_rank
end
end
--Convert the name of a rank into the rank object
function string_to_rank(string)
function ranking.string_to_rank(string)
if type(string) == 'string' then
local found_ranks={}
for _,rank in pairs(get_ranks()) do
for _,rank in pairs(ranking.get_ranks()) do
if rank.name:lower() == string:lower() then return rank end
if rank.name:lower():find(string:lower()) then table.insert(found_ranks,rank) end
end
@@ -40,10 +31,10 @@ function string_to_rank(string)
end
end
--converts the name of a group to the group object
function string_to_rank_group(string)
function ranking.string_to_rank_group(string)
if type(string) == 'string' then
local found_groups={}
for _,group in pairs(get_rank_groups()) do
for _,group in pairs(ranking.get_rank_groups()) do
if group.name:lower() == string:lower() then return group end
if group.name:lower():find(string:lower()) then table.insert(found_groups,group) end
end
@@ -51,7 +42,7 @@ function string_to_rank_group(string)
end
end
-- surches the rank for a certain allow command
function rank_allowed(rank,is_allowed)
function ranking.rank_allowed(rank,is_allowed)
for _,allow in pairs(rank.allow) do
if allow == is_allowed then return true end
end
@@ -59,59 +50,59 @@ function rank_allowed(rank,is_allowed)
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_group('Moderation').lowest_rank -- default mod or higher
function ranking.rank_print(msg, rank, inv)
local rank = ranking.string_to_rank(rank) or ranking.string_to_rank_group('Moderation').lowest_rank -- default mod or higher
local inv = inv or false
debug_write({'RANK','PRINT'},rank.name..': '..msg)
for _, player in pairs(game.players) do
--this part uses sudo to soread it other many ticks
player_rank_power = get_rank(player).power
player_rank_power = ranking.get_player_rank(player).power
if inv then
sudo(function(player_rank_power,rank)
if player_rank_power >= rank.power then player.print(('[Everyone]: '..msg)) end
if player_rank_power >= rank.power then player.print({'ranking.all-rank-print',msg}) end
end,{player_rank_power,rank})
else
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
if rank.short_hand ~= '' then player.print(('['..(rank.short_hand)..']: '..msg)) else player.print({'ranking.all-rank-print',msg}) end
end
end,{player_rank_power,rank})
end
end
end
--Give the user their new rank and raise the Event.rank_change event
function give_rank(player,rank,by_player,group_tick)
function ranking.give_rank(player,rank,by_player,group_tick)
local tick = group_tick or game.tick
local by_player = by_player or 'server'
local rank = string_to_rank(rank) or rank or string_to_rank_group('User').lowest_rank
local old_rank = get_rank(player)
local rank = ranking.string_to_rank(rank) or rank or ranking.string_to_rank_group('User').lowest_rank
local old_rank = ranking.get_player_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
local message = 'ranking.rank-down'
if rank.power <= old_rank.power then message = 'ranking.rank-down' end
if by_player.name then
debug_write({'RANK','GIVE'},'player: '..player.name..' by player: '..by_player.name..' new rank: '..rank.name..' old rank: '..old_rank.name)
rank_print(player.name..' was '..message..' to '..rank.name..' by '..by_player.name,'Guest')
ranking.rank_print({message,player.name,rank.name,by_player.name},'Guest')
else
debug_write({'RANK','GIVE'},'player: '..player.name..' by player: <server> new rank: '..rank.name..' old rank: '..old_rank.name)
rank_print(player.name..' was '..message..' to '..rank.name..' by <server>','Guest')
ranking.rank_print({message,player.name,rank.name,'<server>'},'Guest')
end
if rank.name ~= string_to_rank_group('User').lowest_rank.name then player.print('You have been given the '..rank.name..' Rank!') end
if player.tag ~= old_rank.tag and player.tag ~= '' then player.print('Your Tag was reset due to a Rank change') end
if rank.name ~= ranking.string_to_rank_group('User').lowest_rank.name then player.print({'ranking.rank-given',rank.name}) end
if player.tag ~= old_rank.tag and player.tag ~= '' then player.print({'ranking.tag-reset'}) end
--rank change
player.permission_group = game.permissions.get_group(rank.name)
player.tag = get_rank(player).tag
if old_rank.name ~= 'Jail' then global.old_ranks[player.index]=old_rank.name end
player.tag = ranking.get_player_rank(player).tag
if old_rank.name ~= 'Jail' then global.exp_core.old_ranks[player.index]=old_rank.name end
script.raise_event(Event.rank_change, {tick=tick, player=player, by_player=by_player, new_rank=rank, old_rank=old_rank})
end
--Revert the user's rank to what it was before the lastest change
function revert_rank(player,by_player)
local rank = string_to_rank(global.old_ranks[player.index])
give_rank(player,rank,by_player)
function ranking.revert_rank(player,by_player)
local rank = ranking.string_to_rank(global.exp_core.old_ranks[player.index])
ranking.give_rank(player,rank,by_player)
end
--Give the player a new rank based on playtime and/or preset ranks
function find_new_rank(player,group_tick)
function ranking.find_new_rank(player,group_tick)
debug_write({'RANK','NEW-RANK','START'},player.name)
local function loop_preset_rank(players,rank)
debug_write({'RANK','NEW-RANK','LOOP-PRESET'},rank)
@@ -120,25 +111,25 @@ function find_new_rank(player,group_tick)
end
end
local tick = group_tick or game.tick
local current_rank = get_rank(player)
local old_rank = get_rank(player)
local current_rank = ranking.get_player_rank(player)
local old_rank = ranking.get_player_rank(player)
local possible_ranks = {current_rank}
--Loop through preset ranks only if playtime is less than 5 minutes
debug_write({'RANK','NEW-RANK','PRESET-CHEAK'},tick_to_min(player.online_time))
if tick_to_min(player.online_time) < 5 then
debug_write({'RANK','NEW-RANK','PRESET-START'},player.name)
for rank,players in pairs(global.preset_ranks) do
for rank,players in pairs(global.exp_core.preset_ranks) do
local found_rank = loop_preset_rank(players, rank)
if found_rank then debug_write({'RANK','NEW-RANK','ADD'},found_rank) table.insert(possible_ranks,string_to_rank(found_rank)) break end
if found_rank then debug_write({'RANK','NEW-RANK','ADD'},found_rank) table.insert(possible_ranks,ranking.string_to_rank(found_rank)) break end
end
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
debug_write({'RANK','NEW-RANK','TIME-CHEAK'},tick_to_min(player.online_time))
if current_rank.power > global.ranks.highest_timed_rank.power and tick_to_min(player.online_time) >= global.ranks.lowest_timed_rank.time then
if current_rank.power > global.exp_core.exp_core.ranks.highest_timed_rank.power and tick_to_min(player.online_time) >= global.exp_core.exp_core.ranks.lowest_timed_rank.time then
debug_write({'RANK','NEW-RANK','TIME-START'},player.name)
--Loop through rank times
for _,rank in pairs(get_ranks()) do
for _,rank in pairs(ranking.get_ranks()) do
if rank.time then debug_write({'RANK','NEW-RANK','START'},rank.name..' '..rank.time) end
if rank.time and tick_to_min(player.online_time) >= rank.time then debug_write({'RANK','NEW-RANK','ADD'},rank.name) table.insert(possible_ranks,rank) end
end
@@ -154,29 +145,29 @@ function find_new_rank(player,group_tick)
end
debug_write({'RANK','NEW-RANK','GIVE','HIGHEST'},highest_rank.name)
--Give player new rank if availble
if highest_rank.name == string_to_rank_group('User').lowest_rank.name then
if highest_rank.name == ranking.string_to_rank_group('User').lowest_rank.name then
-- to avoid spam in chat
debug_write({'RANK','NEW-RANK','GIVE','VIA-SERVER'},player.name..' '..highest_rank.name)
player.tag = highest_rank.tag
player.permission_group=game.permissions.get_group('Guest')
script.raise_event(Event.rank_change, {tick=tick, player=player, by_player='server', new_rank=string_to_rank_group('User').lowest_rank, old_rank=string_to_rank_group('User').lowest_rank})
script.raise_event(Event.rank_change, {tick=tick, player=player, by_player='server', new_rank=ranking.string_to_rank_group('User').lowest_rank, old_rank=ranking.string_to_rank_group('User').lowest_rank})
else
debug_write({'RANK','NEW-RANK','GIVE','VIA-GIVE-RANK'},player.name..' '..highest_rank.name)
if highest_rank ~= current_rank then give_rank(player,highest_rank,nil,tick) end
if highest_rank ~= current_rank then ranking.give_rank(player,highest_rank,nil,tick) end
end
debug_write({'RANK','NEW-RANK','GIVE','END'},player.name)
end
--Lose ends
if get_rank(player).power <= string_to_rank_group('Moderation').lowest_rank.power and not player.admin then rank_print(player.name..' needs to be promoted.') end
if old_rank.name ~= get_rank(player).name then global.old_ranks[player.index]=old_rank.name end
if ranking.get_player_rank(player).power <= ranking.string_to_rank_group('Moderation').lowest_rank.power and not player.admin then ranking.rank_print(player.name..' needs to be promoted.') end
if old_rank.name ~= ranking.get_player_rank(player).name then global.exp_core.old_ranks[player.index]=old_rank.name end
debug_write({'RANK','NEW-RANK','END'},player.name)
end
-- returns a list with every players current rank, or just the players of the rank given, includes online time
function get_ranked_players(rank)
function ranking.get_ranked_players(rank)
local to_return = {}
for _,player in pairs(game.players) do
if not rank or rank == get_rank(player).name then
table.insert(to_return,{player.name,tick_to_display_format(player.online_time),get_rank(player).name})
if not rank or rank == ranking.get_player_rank(player).name then
table.insert(to_return,{player.name,tick_to_display_format(player.online_time),ranking.get_player_rank(player).name})
end
end
return to_return
@@ -190,10 +181,10 @@ Event.register(Event.rank_change,function(event)
game.write_file('rank-change.log','\n'..game.tick..' Player: '..event.player.name..' Was given rank: '..event.new_rank.name..' By: '..event.by_player.name..' Their rank was: '..event.old_rank.name, true, 0)
end
end)
Event.register(-1,function()
Event.register(Event.soft_init,function()
debug_write({'RANK','SETUP'},'start')
global.old_ranks = {}
for _,rank in pairs(get_ranks()) do
global.exp_core.old_ranks = {}
for _,rank in pairs(ranking.get_ranks()) do
debug_write({'RANK','SETUP'},'added: '..rank.name)
game.permissions.create_group(rank.name)
for _,toRemove in pairs(rank.disallow) do
@@ -202,7 +193,7 @@ Event.register(-1,function()
end
end
end)
Event.register(defines.events.on_player_joined_game,function(event) find_new_rank(game.players[event.player_index]) end)
Event.register(-1,function() global.preset_ranks = {} end)
Event.register(defines.events.on_player_joined_game,function(event) ranking.find_new_rank(game.players[event.player_index]) end)
Event.register(Event.soft_init,function() global.exp_core.preset_ranks = {} end)
--Please Only Edit Above This Line-----------------------------------------------------------
return credits
return ranking