Moved All Code Out Of Locale

This commit is contained in:
Cooldude2606
2017-12-16 00:33:27 +00:00
parent 4f12463c73
commit 592761c4af
17 changed files with 4 additions and 4 deletions

280
ExpCore/GuiParts/inputs.lua Normal file
View File

@@ -0,0 +1,280 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
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/r6dC2uK
]]
local inputs = {}
inputs._input = {}
-- these are just so you can have short cuts to this
inputs.events = {
error='error',
state=defines.events.on_gui_checked_state_changed,
click=defines.events.on_gui_click,
elem=defines.events.on_gui_elem_changed,
selection=defines.events.on_gui_selection_state_changed,
text=defines.events.on_gui_text_changed,
slider=defines.events.on_gui_value_changed
}
--- Sets the input to trigger on an certain event
-- @usage button:on_event(defines.events.on_gui_click,player_return)
-- @param event the event to raise callback on | can be number of the event | can be a key of inputs.events
-- @tparam function callback the function you want to run on the event
-- @treturn table returns self so you can chain together
function inputs._input:on_event(event,callback)
if not is_type(callback,'function') then return self end
if inputs.events[event] then event = inputs.events[event] end
if event == 'error' then self._error = callback return self end
self.events[event] = callback
return self
end
--- Draw the input into the root element
-- @usage button:draw(frame)
-- @param root the element you want to add the input to
-- @return returns the element that was added
function inputs._input:draw(root)
if is_type(self.draw_data.caption,'string') and game.player.gui.is_valid_sprite_path(self.draw_data.caption) then
local data = table.deepcopy(self.draw_data)
data.type = 'sprite-button'
return root.add(data)
elseif is_type(self.draw_data.sprite,'string') and game.player.gui.is_valid_sprite_path(self.draw_data.sprite) then
local data = table.deepcopy(self.draw_data)
data.type = 'sprite-button'
return root.add(data)
elseif is_type(self.data._state,'function') then
local data = table.deepcopy(self.draw_data)
local success, err = pcall(self.data._state,root)
if success then data.state = err else error(err) end
return root.add(data)
else
return root.add(self.draw_data)
end
end
--- Add a new input, this is the same as doing frame.add{} but returns a diffrent object
-- @usage Gui.inputs.add{type='button',name='test',caption='Test'}
-- @tparam table obj the new element to add if caption is a sprite path then sprite is used
-- @treturn table the custom input object
function inputs.add(obj)
if not is_type(obj,'table') then return end
if not is_type(obj.type,'string') then return end
local type = obj.type
if type == 'button' or
type == 'sprite-button' or
type == 'choose-elem-button' or
type == 'checkbox' or
type == 'radiobutton' or
type == 'textfield' or
type == 'text-box' or
type == 'slider'
then else return end
if obj.type == 'button' or obj.type == 'sprite-button' then obj.style = mod_gui.button_style end
obj.draw_data = table.deepcopy(obj)
obj.data = {}
obj.events = {}
setmetatable(obj,{__index=inputs._input})
Gui._add_data('inputs_'..type,obj.name,obj)
return obj
end
-- this just runs the events given to inputs
function inputs._event_handler(event)
local elements = Gui._get_data('inputs_'..event.element.type) or {}
local element = elements[event.element.name]
if not element and event.element.type == 'sprite-button' then
elements = Gui._get_data('inputs_button') or {}
element = elements[event.element.name]
end
if element then
if not is_type(element.events[event.name],'function') then return end
local success, err = pcall(element.events[event.name],event)
if not success then
if is_type(element._error,'function') then pcall(element._error)
else error(err) end
end
end
end
Event.register(inputs.events.state,inputs._event_handler)
Event.register(inputs.events.click,inputs._event_handler)
Event.register(inputs.events.elem,inputs._event_handler)
Event.register(inputs.events.state,inputs._event_handler)
Event.register(inputs.events.text,inputs._event_handler)
-- the folwing functions are just to make inputs easier but if what you want is not include use inputs.add(obj)
--- Used to define a button, can have many function
-- @usage Gui.inputs.add_button('test','Test','Just for testing',{{condition,callback},...})
-- @tparam string name the name of this button
-- @tparam string the display for this button, either text or sprite path
-- @tparam string tooltip the tooltip to show on the button
-- @param callbacks can either be a single function or a list of function pairs see exaplmes at bottom
-- @treturn table the button object that was made, to allow a custom error event if wanted
function inputs.add_button(name,display,tooltip,callbacks)
local button = inputs.add{
type='button',
name=name,
caption=display,
tooltip=tooltip
}
button.data._callbacks = callbacks
button:on_event('click',function(event)
local player = Game.get_player(event)
local mouse = event.button
local keys = {alt=event.alt,ctrl=event.control,shift=event.shift}
local element = event.element
local callbacks = button.data._callbacks
if is_type(callbacks,'function') then callbacks = {function(...) return true end,callbacks} end
for _,data in pairs(callbacks) do
if is_type(data[1],'function') and is_type(data[2],'function') then
local success, err = pcall(data[1],player,mouse,keys,event)
if success and err == true then
local success, err = pcall(data[2],player,element,event)
if not success then error(err) end
elseif not success then error(err) end
else error('Invalid Callback Condition Format') end
end
end)
return button
end
--- Used to define a choose-elem-button callback only on elem_changed
-- @usage Gui.inputs.add_elem_button('test','Test','Just for testing',function)
-- @tparam string name the name of this button
-- @tparam string the display for this button, either text or sprite path
-- @tparam string tooltip the tooltip to show on the button
-- @tparam function the callback to call on change function(player,element,elem)
-- @treturn table the button object that was made, to allow a custom error event if wanted
function inputs.add_elem_button(name,elem_type,tooltip,callback)
local button = inputs.add{
type='choose-elem-button',
name=name,
elem_type=elem_type,
tooltip=tooltip
}
button.data._callback = callback
button:on_event('elem',function(event)
local player = Game.get_player(event)
local element = event.element or {elem_type=nil,elem_value=nil}
local elem = {type=element.elem_type,value=element.elem_value}
if is_type(button.data._callback,'function') then
local success, err = pcall(button.data._callback,player,element,elem)
if not success then error(err) end
else error('Invalid Callback') end
end)
return button
end
--- Used to define a checkbox callback only on state_changed
-- @usage Gui.inputs.add_checkbox('test',false,'Just for testing',function,function,funvtion)
-- @tparam string name the name of this button
-- @tparam string the display for this button, either text or sprite path
-- @tparam string tooltip the tooltip to show on the button
-- @tparam function the callback to call on change function(player,element,elem)
-- @treturn table the button object that was made, to allow a custom error event if wanted
function inputs.add_checkbox(name,radio,display,default,callback_true,callback_false)
local type = 'checkbox'; if radio then type='radiobutton' end
local state = false; if is_type(default,'boolean') then state = default end
local checkbox = inputs.add{
type=type,
name=name,
caption=display,
state=state
}
if is_type(default,'function') then checkbox.data._state = default end
checkbox.data._true = callback_true
checkbox.data._false = callback_false
checkbox:on_event('state',function(event)
local player = Game.get_player(event)
local state = event.element.state
if state then
if is_type(checkbox.data._true,'function') then
local success, err = pcall(checkbox.data._true,player,event.element)
if not success then error(err) end
else error('Invalid Callback') end
else
if is_type(checkbox.data._false,'function') then
local success, err = pcall(checkbox.data._false,player,event.element)
if not success then error(err) end
else error('Invalid Callback') end
end
end)
return checkbox
end
--- Used to reset the state of radio buttons, recomened to be called on_state_change to reset any radio buttons it is ment to work with.
-- @usage Gui.inputs.reset_radio{radio1,radio2,...}
-- @param elements can be a list of elements or a single element
function inputs.reset_radio(elements)
if #elements > 0 then
for _,element in pairs(elements) do
if element.valid then
local _elements = Gui._get_data('inputs_'..element.type) or {}
local _element = _elements[element.name]
local state = false
local success, err = pcall(_element.data._state,element.parent)
if success then state = err else error(err) end
element.state = state
end
end
else
if elements.valid then
local _elements = Gui._get_data('inputs_'..elements.type) or {}
local _element = _elements[elements.name]
local state = false
local success, err = pcall(_element.data._state,elements.parent)
if success then state = err else error(err) end
element.state = state
end
end
end
return inputs
--[[
Input Example
-- Basic Button Using Gui.inputs.add
local test = Gui.inputs.add{
name='test-button',
type='button',
caption='Test'
}
test:on_event(Gui.inputs.events.click,function(event) game.print('test') end)
-- then later in code
local frame = player.gui.top.add{name='test',type='frame'}
test:draw(frame)
-- Mutly Function Button Using Gui.inputs.add_button
Gui.inputs.add_button('test-inputs','Try RMB','alt,ctrl,shift and mouse buttons',{
{
function(player,mouse,keys) return mouse == defines.mouse_button_type.left and keys.alt end,
function(player,element) player_return('Left: Alt',nil,player) end
},
{
function(player,mouse,keys) return mouse == defines.mouse_button_type.left and keys.ctrl end,
function(player,element) player_return('Left: Ctrl',nil,player) end
},
{
function(player,mouse,keys) return mouse == defines.mouse_button_type.left and keys.shift end,
function(player,element) player_return('Left: Shift',nil,player) end
},
{
function(player,mouse,keys) return mouse == defines.mouse_button_type.right and keys.alt end,
function(player,element) player_return('Right: Alt',nil,player) end
},
{
function(player,mouse,keys) return mouse == defines.mouse_button_type.right and keys.ctrl end,
function(player,element) player_return('Right: Ctrl',nil,player) end
},
{
function(player,mouse,keys) return mouse == defines.mouse_button_type.right and keys.shift end,
function(player,element) player_return('Right: Shift',nil,player) end
}
}):on_event('error',function(err) game.print('this is error handliling') end)
]]

View File

@@ -0,0 +1,48 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
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/r6dC2uK
]]
local toolbar = {}
--- Add a button to the toolbar, ranks need to be allowed to use these buttons if ranks is preset
-- @usage toolbar.add('foo','Foo','Test',function() game.print('test') end)
-- @tparam string name the name of the button
-- @tparam string caption can be a sprite path or text to show
-- @tparma string tooltip the help to show for the button
-- @tparam function callback the function which is called on_click
-- @treturn table the button object that was made
function toolbar.add(name,caption,tooltip,callback)
local button = Gui.inputs.add{type='button',name=name,caption=caption,tooltip=tooltip}
button:on_event(Gui.inputs.events.click,callback)
Gui._add_data('toolbar',name,button)
return button
end
--- Draws the toolbar for a certain player
-- @usage toolbar.draw(1)
-- @param player the player to draw the tool bar of
function toolbar.draw(player)
local player = Game.get_player(player)
if not player then return end
local toolbar_frame = mod_gui.get_button_flow(player)
toolbar_frame.clear()
for name,button in pairs(Gui._get_data('toolbar')) do
if is_type(Ranking,'table') and Ranking._presets and Ranking._presets().meta.rank_count > 0 then
local rank = Ranking.get_rank(player)
if rank:allowed(name) then
button:draw(toolbar_frame)
end
else button:draw(toolbar_frame) end
end
end
if defines.events.rank_change then
Event.register(defines.events.rank_change,toolbar.draw)
end
return toolbar

139
ExpCore/commands.lua Normal file
View File

@@ -0,0 +1,139 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
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/r6dC2uK
]]
--Please Only Edit Below This Line-----------------------------------------------------------
local command_calls = {}
local command_data = {}
--- Uses a commands data to return the inputs as a string
-- @usage command = command_data[command_name]
-- command_inputs(command) -- returns "<input1> <input2> "
-- @tparam table the data for the command being run
-- @treturn string the inputs in string format
local function command_inputs(command)
if not is_type(command,'table') then return end
local inputs = ''
for _,input in pairs(command.inputs) do
if input == true then break end
inputs = inputs..'<'..input..'> '
end
return inputs
end
--- Uses the command data and the event to return a table of the args
-- @usage command = command_data[command_name]
-- command_args(event,command) -- return {input1='one',input2='two'}
-- @tparam defines.events.on_console_command event the event rasied by the command
-- @tparam table command the data for the command being run
-- @treturn table a table version of the event.parameter based on expected inputs
local function command_args(event,command)
if not event or not is_type(command,'table') then return end
local args = {}
-- haddles no parameters given
if not event.parameter then
if #command.inputs > 0 then return args, false
else return args, true
end
end
-- finds all the words and cheaks if the right number were given
local words = string.split(event.parameter,' ')
if table.last(command.inputs) == true then
if #words < #command.inputs-1 then return args, false end
else
if #words < #command.inputs then return args, false end
end
-- if it is the right number then process and return the args
for index,input in pairs(command.inputs) do
if command.inputs[index+1] == true then
args[input] = table.concat(words,' ',index)
break
else
args[input] = words[index]
end
end
return args, true
end
--- Used to return all the commands a player can use
-- @usage get_commands(1) -- return {{command data},{command data}}
-- @param player the player refreced by string|number|LuaPlayer|event
-- @treturn table a table containg all the commands the player can use
function get_commands(player)
local commands = {}
local player = Game.get_player(player)
if not player then return commands end
local rank = Ranking.get_rank(player)
for name,data in pairs(command_data) do
if Ranking.rank_allowed(rank,name) then table.insert(commands,data) end
end
return commands
end
--- Used to call the custom commands
-- @usage You dont its an internal command
-- @tparam defines.events.on_console_command event the event rasied by the command=
local function run_custom_command(command)
local command_data = command_data[command.name]
local player_name = Game.get_player(command) and Game.get_player(command).name or 'server'
-- is the player allowed to use this command
if is_type(Ranking,'table') and Ranking._presets and Ranking._presets().meta.rank_count > 0 and not Ranking.get_rank(player_name):allowed(command.name) then
player_return({'commands.unauthorized'},defines.text_color.crit)
if game.player then game.player.play_sound{path='utility/cannot_build'} end
game.write_file('commands.log','\n'..game.tick
..' Player: '..player_name
..' Failed to use command (Unauthorized): '..command.name
..' With args of: '..table.to_string(command_args(command,command_data))
, true, 0)
return
end
-- gets the args for the command
local args, valid = command_args(command,command_data)
if not valid then
player_return({'commands.invalid-inputs',command.name,command_inputs(command_data)},defines.text_color.high)
if game.player then game.player.play_sound{path='utility/deconstruct_big'} end
game.write_file('commands.log','\n'..game.tick
..' Player: '..player_name
..' Failed to use command (Invalid Args): '..command.name
..' With args of: '..table.to_string(args)
, true, 0)
return
end
-- runs the command
local success, err = pcall(command_calls[command.name],event,args)
if err then error(err) end
player_return({'commands.command-ran'},defines.text_color.info)
game.write_file('commands.log','\n'..game.tick
..' Player: '..player_name
..' Used command: '..command.name
..' With args of: '..table.to_string(args)
, true, 0)
end
commands._add_command = commands.add_command
commands._expgaming = true
--- Used to define commands
-- @usage inputs = {'player','reason',true}
-- commands.add_command('ban','bans a player',inputs,function() return end)
-- @tparam string name the name of the command
-- @tparam[opt='No Description'] string description the description of the command
-- @tparam[opt={'parameter',true}] table inputs a table of the inputs to be used, last index being true makes the last parameter open ended (longer than one word)
-- @tparam function event the function to call on the event
commands.add_command = function(name, description, inputs, event)
if command_calls[name] then return end
if not is_type(name,'string') then return end
if not is_type(event,'function') then return end
local description = is_type(description,'string') and description or 'No Description'
local inputs = is_type(inputs,'table') and inputs or {'parameter',true}
command_data[name] = {
name=name,
description=description,
inputs=inputs
}
command_calls[name] = event
commands._add_command(name,command_inputs(command_data[name])..description,run_custom_command)
end

28
ExpCore/gui.lua Normal file
View File

@@ -0,0 +1,28 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
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/r6dC2uK
]]
local Gui = {}
local Gui_data = {}
-- this is to enforce the read only propetry of the gui
function Gui._add_data(key,value_key,value)
if game then return end
if not Gui_data[key] then Gui_data[key] = {} end
Gui_data[key][value_key] = value
end
function Gui._get_data(key) return Gui_data[key] end
function Gui:_load_parts(parts)
for _,part in pairs(parts) do
self[part] = require('/GuiParts/'..part)
end
end
return Gui

40
ExpCore/load.lua Normal file
View File

@@ -0,0 +1,40 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
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/r6dC2uK
]]
--[[
ExpCore
This file allow you to only require this one file to return the diffent libarys.
This file will return a function which can be used to access only the part you want.
Pass a table with the names of the objects you want and it will be return in that order
]]
local StdExpCoreLib = {}
require '/commands'
StdExpCoreLib.Server = require '/server'
StdExpCoreLib.Ranking = require '/ranking'
StdExpCoreLib.Gui = require '/gui'
StdExpCoreLib.Gui:_load_parts{
'inputs',
'toolbar',
--'center',
--'left',
--'popup'
}
return function(rtn)
local _return = {}
for _,name in pairs(rtn) do
if StdExpCoreLib[name] then
table.insert(_return,StdExpCoreLib[name])
end
end
return unpack(_return)
end

312
ExpCore/ranking.lua Normal file
View File

@@ -0,0 +1,312 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
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/r6dC2uK
]]
--Please Only Edit Below This Line-----------------------------------------------------------
local Ranking = {}
defines.events.rank_change = script.generate_event_name()
Ranking._rank = {}
Ranking._group = {}
-- this function is to avoid errors - see /ranks.lua
function Ranking._ranks(names)
return {}
end
-- this function is to avoid errors - see /ranks.lua
function Ranking._groups(names)
return {}
end
-- this function is to avoid errors - see /ranks.lua
function Ranking._meta()
return {}
end
-- this function is to avoid errors - see addons/playerRanks.lua
function Ranking._base_preset(table)
Ranking._presets().current = table
end
-- this returns a global list
function Ranking._presets()
if not global.exp_core then global.exp_core = {} end
if not global.exp_core.ranking then global.exp_core.ranking = {meta=Ranking._meta(),old={},current={}} end
return global.exp_core.ranking
end
--- Returns a rank object given a player or rank name
-- @usage Ranking.get_rank(game.player)
-- Ranking.get_rank('admin')
-- @param mixed player|player index|player name|rank name|rank|'server'|'root' what rank to get
-- @treturn table the rank that is linked to mixed
function Ranking.get_rank(mixed)
if not mixed then return false end
local ranks = Ranking._ranks(true)
local _return = false
if is_type(mixed,'table') then
if mixed.index then
_return = game.players[mixed.index] and ranks[mixed.permission_group.name] or false
else
_return = mixed.group and mixed or false
end
else
_return = game.players[mixed] and ranks[game.players[mixed].permission_group.name]
or table.autokey(ranks,mixed) and table.autokey(ranks,mixed)
or string.contains(mixed,'server') and Ranking.get_rank(Ranking._presets().meta.root)
or string.contains(mixed,'root') and Ranking.get_rank(Ranking._presets().meta.root)
or false
end
return _return
end
--- Returns the group object used to sort ranks given group name or see Ranking.get_rank
-- @usage Ranking.get_group(game.player)
-- Ranking.get_group('root')
-- @param mixed player|player index|player name|rank name|rank|'server'|'root'|group name|group what group to get
-- @treturn table the group that is linked to mixed
function Ranking.get_group(mixed)
if not mixed then return false end
local groups = Ranking._groups(true)
local rank = Ranking.get_rank(mixed)
return rank and rank.group
or is_type(mixed,'table') and mixed.ranks and mixed
or is_type(mixed,'string') and table.autokey(groups,mixed) and table.autokey(groups,mixed)
or false
end
--- Prints to all rank of greater/lower power of the rank given
-- @usage Ranking.print('admin','We got a grifer')
-- @param rank_base the rank that acts as the cut off point (rank is always included)
-- @param rtn what do you want to return to the players
-- @tparam bolean below if true rank below base are printed to
function Ranking.print(rank_base,rtn,colour,below)
local colour = colour or defines.color.white
local rank_base = Ranking.get_rank(rank_base)
local ranks = Ranking._ranks()
if below then
for power,rank in pairs(ranks) do
if rank_base.power >= power then rank:print(rtn,colour) end
end
else
for power,rank in pairs(ranks) do
if rank_base.power <= power then rank:print(rtn,colour) end
end
end
end
--- Gives a user a rank
-- @usage Ranking.give_rank(1,'admin')
-- @param player the player to give the rank to
-- @param rank the rank to give to the player
-- @param[opt='server'] by_player the player who is giving the rank
-- @param[opt=game.tick] tick the tick that the rank is being given on
function Ranking.give_rank(player,rank,by_player,tick)
local print_colour = defines.text_color.info
local tick = tick or game.tick
local by_player_name = Game.get_player(by_player) and Game.get_player(by_player).name or game.player and game.player.name or 'server'
local rank = Ranking.get_rank(rank) or Ranking.get_rank(Ranking._presets().meta.default)
local player = Game.get_player(player) or error('No Player To Give Rank')
local old_rank = Ranking.get_rank(player) or Ranking.get_rank(Ranking._presets().meta.default)
local message = 'ranking.rank-down'
-- messaging
if old_rank.name == rank.name then return end
if rank.power < old_rank.power then message = 'ranking.rank-up' player.play_sound{path='utility/achievement_unlocked'}
else player.play_sound{path='utility/game_lost'} end
game.print({message,player.name,rank.name,by_player_name},print_colour)
if rank.group.name ~= 'User' then player_return({'ranking.rank-given',rank.name},print_colour,player) end
if player.tag ~= old_rank.tag then player_return({'ranking.tag-reset'},print_colour,player) end
-- rank change
player.permission_group = game.permissions.get_group(rank.name)
player.tag = rank.tag
if not old_rank.group.name == 'Jail' then Ranking._presets().old[player.index] = rank.name end
if defines.events.rank_change then
script.raise_event(defines.events.rank_change,{
name=defines.events.rank_change,
tick=tick,
player_index=player.index,
by_player_name=by_player_name,
new_rank=rank,
old_rank=old_rank
})
end
end
--- Revert the last change to a players rank
-- @usage Ranking.revert(1)
-- @param player the player to revert the rank of
-- @param[opt=nil] by_player the player who is doing the revert
function Ranking.revert(player,by_player)
local player = Game.get_player(player)
Ranking.give_rank(player,Ranking._presets().old[player.index],by_player)
end
--- Given the player has a rank in the preset table it is given
-- @usage Ranking.find_preset(1)
-- @param player the player to test for an auto rank
-- @tparam[opt=nil] tick the tick it happens on
function Ranking.find_preset(player,tick)
local presets = Ranking._presets().current
local meta_data = Ranking._presets().meta
local default = Ranking.get_rank(meta_data.default)
local player = Game.get_player(player)
local current_rank = Ranking.get_rank(player) or {power=-1,group={name='not jail'}}
local ranks = {default}
if current_rank.group.name == 'Jail' then return end
if presets[string.lower(player.name)] then
local rank = Ranking.get_rank(presets[string.lower(player.name)])
if current_rank.power >= rank.power then return end
table.insert(ranks,rank)
end
if current_rank.power < meta_data.time_highest and tick_to_min(player.online_time) > meta_data.time_lowest then
for _,rank_name in pairs(meta_data.time_ranks) do
local rank = Ranking.get_rank(rank_name)
if tick_to_min(player.online_time) > rank.time then
table.insert(ranks,rank)
end
end
end
local _rank = nil
for _,rank in pairs(ranks) do
if rank.power > current_rank.power then _rank = rank end
end
if _rank then
if _rank.name == default.name then
player.tag = _rank.tag
player.permission_group = game.permissions.get_group(_rank.name)
else
Ranking.give_rank(player,_rank,nil,tick)
end
end
end
-- this is the base rank object, do not store in global
--- Is this rank allowed to open this gui or use this command etc.
-- @usage rank:allowed('server-interface')
-- @tparam teh action to test for
-- @treturn bolean is it allowed
function Ranking._rank:allowed(action)
return self.allow[action] or self.is_root or false
end
--- Get all the players in this rank
-- @usage rank:get_players()
-- @tparam bolean online get only online players
-- @treturn table a table of all players in this rank
function Ranking._rank:get_players(online)
local players = game.permissions.get_group(rank.name).players
local _return = {}
if online then
for _,player in pairs(players) do
if player.connected then table.insert(_return,player) end
end
else
_return = players
end
return _return
end
--- Print a message to all players of this rank
-- @usage rank:print('foo')
-- @param rtn any value you want to return
function Ranking._rank:print(rtn,colour)
local colour = colour or defines.color.white
if not Server or not Server._thread then
for _,player in pairs(self:get_players()) do
player_return(rtn,colour,player)
end
else
-- using threads to make less lag
Server.new_thread{
data={rank=self,rtn=rtn}
}:on_event('resolve',function(thread)
return thread.data.rank:get_players(true)
end):on_event('success',function(thread,players)
for _,player in pairs(players) do
player_return(thread.data.rtn,colour,player)
end
end):queue()
end
end
-- this is used to edit a group once made key is what is being edited and set_value makes it over ride the current value
-- see Addons/playerRanks for examples
function Ranking._rank:edit(key,set_value,value)
if game then return end
if set_value then self[key] = value return end
if key == 'disallow' then
self.disallow = table.merge(self.disallow,value,true)
elseif key == 'allow' then
self.allow = table.merge(self.allow,value)
end
Ranking._update_rank(self)
end
-- this is the base group object, do not store in global, these cant be used in game
-- this makes a new group
-- {name='root',allow={},disallow={}}
function Ranking._group:create(obj)
if game then return end
if not is_type(obj.name,'string') then return end
setmetatable(obj,{__index=Ranking._group})
self.index = #Ranking._groups(names)+1
obj.ranks = {}
obj.allow = obj.allow or {}
obj.disallow = obj.disallow or {}
Ranking._add_group(obj)
return obj
end
-- this makes a new rank in side this group
-- {name='Root',short_hand='Root',tag='[Root]',time=nil,colour=defines.colors.white,allow={},disallow={}}
function Ranking._group:add_rank(obj)
if game then return end
if not is_type(obj.name,'string') or
not is_type(obj.short_hand,'string') or
not is_type(obj.tag,'string') or
not is_type(obj.colour,'table') then return end
setmetatable(obj,{__index=Ranking._rank})
obj.group = self
obj.allow = obj.allow or {}
obj.disallow = obj.disallow or {}
setmetatable(obj.allow,{__index=self.allow})
setmetatable(obj.disallow,{__index=self.disallow})
Ranking._add_rank(obj,obj.power)
Ranking._set_rank_power()
table.insert(self.ranks,obj)
if not self.highest or obj.power > self.highest.power then self.highest = obj end
if not self.lowest or obj.power < self.lowest.power then self.lowest = obj end
end
-- this is used to edit a group once made key is what is being edited and set_value makes it over ride the current value
-- see Addons/playerRanks for examples
function Ranking._group:edit(key,set_value,value)
if game then return end
if set_value then self[key] = value return end
if key == 'disallow' then
self.disallow = table.merge(self.disallow,value,true)
elseif key == 'allow' then
self.allow = table.merge(self.allow,value)
end
Ranking._update_group(self)
end
Event.register(defines.events.on_player_joined_game,function(event)
Ranking.find_preset(event.player_index)
end)
Event.register(-1,function(event)
for power,rank in pairs(Ranking._ranks()) do
local perm = game.permissions.create_group(rank.name)
for _,toRemove in pairs(rank.disallow) do
perm.set_allows_action(defines.input_action[toRemove],false)
end
end
end)
return Ranking

237
ExpCore/ranks.lua Normal file
View File

@@ -0,0 +1,237 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
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/r6dC2uK
]]
--Please Only Edit Below This Line-----------------------------------------------------------
local groups = {}
local ranks = {}
function Ranking._add_group(group) if game then return end table.insert(groups,group) end
function Ranking._add_rank(rank,pos) if game then return end if pos then table.insert(ranks,pos,rank) else table.insert(ranks,rank) end end
function Ranking._set_rank_power() if game then return end for power,rank in pairs(ranks) do rank.power = power end end
function Ranking._update_rank(rank) if game then return end ranks[rank.power] = rank end
function Ranking._update_group(group) if game then return end groups[group.index] = group end
--[[
How to use groups:
name the name that you can use to refence it.
disallow if present then all ranks in this group will have this added to their disallow.
allow if present then all ranks in this group will have this added to their allow.
highest is asigned by the script to show the highest rank in this group.
lowest is asigned by the script to show the lowest rank in this group.
How to add ranks:
Name is what will be used in the scripts and is often the best choice for display in text.
short_hand is what can be used when short on space but the rank still need to be displayed.
tag is the tag the player will gain when moved to the rank, it can be nil.
time is used for auto-rank feature where you are moved to the rank after a certain play time in minutes.
colour is the RGB value that can be used to emphasise GUI elements based on rank.
power is asigned by the script based on their index in ranks, you can insert new ranks between current ones, lower is better
group is asigned by the script to show the group this rank is in
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 allow, add the allow as the key and the value as true
Example: test for 'server-interface' => allow['server-interface'] = true
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
--]]
-- If you wish to add more groups please use addons/playerRanks.lua
-- If you wish to add to these rank groups use addons/playerRanks.lua
-- Ranks will inherite from each other ie higher ranks can do everything lower ranks can
-- But groups do not inherite from each other
-- DO NOT REMOVE ANY OF THESE GROUPS
local root = Ranking._group:create{
name='Root',
allow={
['interface'] = true
},
disallow={}
}
local admin = Ranking._group:create{
name='Admin',
allow={},
disallow={
'set_allow_commands',
'edit_permission_group',
'delete_permission_group',
'add_permission_group'
}
}
local user = Ranking._group:create{
name='User',
allow={},
disallow={
'set_allow_commands',
'edit_permission_group',
'delete_permission_group',
'add_permission_group'
}
}
local jail = Ranking._group:create{
name='Jail',
allow={},
disallow={
'set_allow_commands',
'edit_permission_group',
'delete_permission_group',
'add_permission_group'
}
}
-- If you wish to add more ranks please use addons/playerRanks.lua
-- If you wish to add to these rank use addons/playerRanks.lua
root:add_rank{
name='Root',
short_hand='Root',
tag='[Root]',
colour=defines.color.white,
is_root=true
}
root:add_rank{
name='Owner',
short_hand='Owner',
tag='[Owner]',
time=nil,
colour={r=170,g=0,b=0}
}
root:add_rank{
name='Community Manager',
short_hand='Com Mngr',
tag='[Com Mngr]',
colour={r=150,g=68,b=161}
}
root:add_rank{
name='Developer',
short_hand='Dev',
tag='[Dev]',
colour={r=179,g=125,b=46}
}
admin:add_rank{
name='Admin',
short_hand='Admin',
tag='[Admin]',
colour={r=233,g=63,b=233}
}
admin:add_rank{
name='Mod',
short_hand='Mod',
tag='[Mod]',
colour={r=0,g=170,b=0},
disallow={
'server_command'
}
}
user:add_rank{
name='Donator',
short_hand='P2W',
tag='[P2W]',
colour={r=233,g=63,b=233}
}
user:add_rank{
name='Member',
short_hand='Mem',
tag='[Member]',
colour={r=24,g=172,b=188},
disallow={
'set_auto_launch_rocket',
'change_programmable_speaker_alert_parameters',
'drop_item'
}
}
user:add_rank{
name='Guest',
short_hand='',
tag='',
colour={r=255,g=159,b=27},
is_default=true,
disallow={
'build_terrain',
'remove_cables',
'launch_rocket',
'reset_assembling_machine',
'cancel_research'
}
}
jail:add_rank{
name='Jail',
short_hand='Jail',
tag='[Jail]',
colour={r=50,g=50,b=50},
disallow={
'open_character_gui',
'begin_mining',
'start_walking',
'player_leave_game'
}
}
function Ranking._auto_edit_ranks()
for power,rank in pairs(ranks) do
if ranks[power-1] then
rank:edit('disallow',false,ranks[power-1].disallow)
end
end
for power = #ranks, 1, -1 do
rank = ranks[power]
if ranks[power+1] then
rank:edit('allow',false,ranks[power+1].allow)
end
end
end
-- used to force rank to be read-only
function Ranking._groups(name)
if name then
if name then
local _return = {}
for power,group in pairs(groups) do
_return[group.name] = group
end
return _return
end
end
return groups
end
function Ranking._ranks(name)
if name then
local _return = {}
for power,rank in pairs(ranks) do
_return[rank.name] = rank
end
return _return
end
return ranks
end
-- used to save lag by doing some calculation at the start
function Ranking._meta()
local meta = {time_ranks={}}
for power,rank in pairs(ranks) do
meta.rank_count = power
if rank.is_default then
meta.default = rank.name
end
if rank.is_root then
meta.root = rank.name
end
if rank.time then
table.insert(meta.time_ranks,rank.name)
if not meta.time_highest or power < meta.time_highest then meta.time_highest = power end
if not meta.time_lowest or rank.time < meta.time_lowest then meta.time_lowest = power.time end
end
meta.time_highest = meta.time_highest or 0
meta.time_lowest = meta.time_lowest or 0
end
return meta
end

421
ExpCore/server.lua Normal file
View File

@@ -0,0 +1,421 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
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/r6dC2uK
]]
--Please Only Edit Below This Line-----------------------------------------------------------
-- server allows control over threads and other features the devs missed out
local Server = {}
Server._thread = {}
--- Returns a un-used uuid (better system needed)
-- @usage obj.uuid = Server.new_uuid()
-- @treturn string the new uuid
function Server.new_uuid()
local uuid = tostring(Server._uuid()())
uuid = string.to_hex('uuid'..uuid)
return uuid
end
-- use this to change the location of the server uuids
function Server._uuid(reset)
global.exp_core = not reset and global.exp_core or {}
global.exp_core.uuids = not reset and global.exp_core.uuids or game.create_random_generator()
return global.exp_core.uuids
end
--- Returns either the number of threads or a able of threads
-- @usage Server.threads() -- return {...}
-- Server.threads(true) -- return int
-- @tparam[opt=nil] bolean count true to return the number of threads
-- @return either a list of threads or a number
function Server.threads(count)
return count and Server._threads().all._n or Server._threads().all
end
-- use this to change the location of the server threads
-- all stores the threads indexed uuid, the other three only store the uuid's to index in the all table
function Server._threads(reset)
global.exp_core = not reset and global.exp_core or {}
global.exp_core.threads = not reset and global.exp_core.threads or {print_to={},queue={},tick={},timeout={},events={},all={_n=0},paused={},named={}}
return global.exp_core.threads
end
-- see thread:create (this was done so thread can remain local)
function Server.new_thread(obj)
return Server._thread:create(obj)
end
--- Used to get a thread via it's uuid or by name if one is given
-- @usage Server.get_thread('decon') -- return thread
-- @param mixed either a uuid or the name given to a thread
-- @treturn table the thread by that name or uuid
function Server.get_thread(mixed)
local threads = Server._threads()
if threads.named[mixed] then return threads.all[threads.named[mixed]]
elseif threads.paused[mixed] then return threads.all[threads.paused[mixed]]
elseif threads.all[mixed] then return threads.all[mixed]
else return false end
end
--- Adds a thread into the resolve queue, can be used to lower lag
-- @usage Server.queue_thread(thread) -- return true/false
-- @tparam table the thread to add to the queue must have a resolve function (must be open)
-- @treturn bolean was the thread added
function Server.queue_thread(thread_to_queue)
if not thread_to_queue and not thread_to_queue.valid and not thread_to_queue:valid() then return false end
if not thread_to_queue._resolve then return false end
table.insert(Server._threads().queue,thread_to_queue.uuid)
return true
end
--- Closes all active threads, can use force if it causes errors
-- @usage Server.close_all_threads()
-- Server.close_all_threads(true) -- use if no force makes errors
-- @tparam bolean with_force use force when closing
function Server.close_all_threads(with_force)
if not with_force then
for uuid,next_thread in pairs(Server.threads()) do
if uuid ~= '_n' then next_thread:close() end
end
else
Server._threads(true)
end
end
--- Runs all the theads which have opened with an on_tick event
-- @ussage Server.run_tick_threads()
function Server.run_tick_threads()
table.each(Server._threads().tick,function(uuid)
local next_thread = Server.get_thread(uuid)
if next_thread and next_thread:valid() and next_thread._tick then
local success, err = pcall(next_thread._tick,next_thread)
if not success then next_thread:error(err) end
end
end)
end
--- Checks the timeout on all active timeout threads
-- @ussage Server.check_timeouts()
function Server.check_timeouts()
table.each(Server._threads().timeout,function(uuid)
local next_thread = Server.get_thread(uuid)
if next_thread and next_thread:valid() then
next_thread:check_timeout()
end
end)
end
-- for use in debuging
function Server._thread_handler_debuger(player,event,state)
local player = Game.get_player(player)
local print_to = Server._threads().print_to
print_to[player.index] = print_to[player.index] or {}
print_to[player.index][event] = state
end
--- Calles all threads on a certain game event (used with script.on_event)
-- @tparam table event the event that is called
function Server._thread_handler(event)
table.each(Server._threads().print_to,function(print_to,player_index,event)
if event.name == defines.events.on_tick then return true end
if print_to[event.name] then
player_return(event,defines.text_color.bg,player_index)
end
end,event)
local event_id = event.name
local threads = Server._threads().events[event_id]
if not threads then return end
table.each(threads,function(uuid)
local next_thread = Server.get_thread(uuid)
if next_thread and next_thread:valid() then
if is_type(next_thread._events[event_id],'function') then
local success, err = pcall(next_thread._events[event_id],next_thread,event)
if not success then next_thread:error(err) end
end
end
end)
end
for _,event in pairs(defines.events) do Event.register(event,Server._thread_handler) end
--[[ cant be used V
--- Adds a event handler to tell threads about events
-- @usage Server.add_thread_handler(defines.event)
-- @tparam number event the event to run the thread handler on
-- @treturn bolean if the handler was added
function Server.add_thread_handler(event)
if not is_type(event,'number') then return false end
local threads = Server._threads()
if not threads.events[event] then
threads.events[event] = {}
Event.register(event,Server._thread_handler)
return true
end
return false
end
]]
--- Given a string or function it will run that function and return any values
-- @usage Server.interface('local x = 1+1 print(x) return x') -- return 2
-- Server.interface('local x = 1+1 print(x)',thread) -- no return
-- @param callback either a function or string which will be ran via pcall
-- @param[opt] use_thread give a thread for the interface to run on (does not need to be open, but cant use on_resolve)
-- @param[opt] ... any args you want to pass to the function
function Server.interface(callback,use_thread,...)
if use_thread then
if use_thread == true then use_thread = Server.new_thread{data={callback,...}} end
use_thread:on_event('resolve',function(thread)
if is_type(thread.data[1],'function') then
local success, err = pcall(unpack(thread.data))
if not success then error(err) end
return err
else
local callback = table.remove(thread.data,1)
local success, err = pcall(loadstring(callback),unpack(thread.data))
if not success then error(err) end
return err
end
end)
use_thread:open()
Server.queue_thread(use_thread)
else
if is_type(callback,'function') then
local success, err = pcall(callback,...)
return success, err
else
local success, err = pcall(loadstring(callback),...)
return success, err
end
return false
end
end
if commands._expgaming then
commands.add_command('interface', 'Runs the given input from the script', {'code',true}, function(event,args)
local callback = args.code
if not string.find(callback,'%s') and not string.find(callback,'return') then callback = 'return '..callback end
if game.player then callback = 'local player, surface, force = game.player, game.player.surface, game.player.force '..callback end
if Ranking and Ranking.get_rank and game.player then callback = 'local rank = Ranking.get_rank(game.player) '..callback end
local success, err = Server.interface(callback)
player_return(err)
end)
end
-- thread allows you to run fuinction async to the main game
--- Returns a new thread object
-- @usage new_thread = thread:create()
-- @tparam[opt={}] table obj all are opt {timeout=int,name=str,data=any} advanced users can prefix with _function to avoid the on_function functions
-- @treturn table the new thread object
function Server._thread:create(obj)
local obj = obj or {}
setmetatable(obj,{__index=Server._thread})
obj.uuid = Server.new_uuid()
return obj
end
-- see Server.queue_thread - this just opens it first
function Server._thread:queue()
self:open()
return Server.queue_thread(self)
end
--- Test if the thread has all requied parts
-- @usage if thread:valid() then end
-- @tparam bolean skip_location_check true to skip the location check
-- @treturn bolean is the thread valid
function Server._thread:valid(skip_location_check)
local skip_location_check = skip_location_check or false
if is_type(self.uuid,'string') and
skip_location_check or is_type(self.opened,'number') and
skip_location_check or is_type(Server._threads().all[self.uuid],'table') and
is_type(self.timeout) or is_type(self.timeout,'number') and
is_type(self.name) or is_type(self.name,'string') and
is_type(self._close) or is_type(self._close,'function') and
is_type(self._timeout) or is_type(self._timeout,'function') and
is_type(self._tick) or is_type(self._tick,'function') and
is_type(self._resolve) or is_type(self._resolve,'function') and
is_type(self._success) or is_type(self._success,'function') and
is_type(self._error) or is_type(self._error,'function') then
return true
end
return false
end
--- Opens the thread by storing it in a place the server object can find it
-- @usage thread:open() -- return true
-- @treturn bolean if the thread was opened
function Server._thread:open()
if not self:valid(true) or self.opened then return false end
local threads = Server._threads()
local uuid = self.uuid
self.opened = game.tick
threads.all[uuid] = threads.all[uuid] or self
threads.all._n = threads.all._n+1
if is_type(self.timeout,'number') then table.insert(threads.timeout,uuid) end
if is_type(self._tick,'function') then table.insert(threads.tick,uuid) end
if is_type(self.name,'string') then threads.named[self.name] = threads.named[self.name] or self.uuid end
if is_type(self._events,'table') then
table.each(self._events,function(callback,event,threads,uuid)
-- cant be used V
--Server.add_thread_handler(event)
if not threads.events[event] then threads.events[event] = {} end
table.insert(threads.events[event],uuid)
end,threads,self.uuid)
end
return true
end
--- Inverse of thread:open() - it removes the thread and calles on_close
-- @usage thread:close() -- return true
-- @treturn bolean if the thread had a on_close function
function Server._thread:close()
local threads = Server._threads()
local uuid = self.uuid
local _return = false
if is_type(self._close,'function') then pcall(self._close,self) _return = true end
local value,key = table.find(threads.queue,function(v,k,uuid) return v == uuid end,uuid)
if key then table.remove(threads.queue,key) end
local value,key = table.find(threads.timeout,function(v,k,uuid) return v == uuid end,uuid)
if key then table.remove(threads.timeout,key) end
local value,key = table.find(threads.tick,function(v,k,uuid) return v == uuid end,uuid)
if key then table.remove(threads.tick,key) end
if is_type(self._events,'table') then
table.each(self._events,function(callback,event)
if threads.events[event] then
local value,key = table.find(threads.events[event],function(v,k,uuid) return v == uuid end,uuid)
if key then table.remove(threads.events[event],key) end
-- cant be used V
--if #threads.events[event] == 0 then Event.remove(event,Server.game_event) threads.events[event] = nil end
end
end)
end
if is_type(self.name,'string') then threads.paused[self.name] = self.uuid self.opened = nil
else threads.all[uuid] = nil threads.all._n = threads.all._n-1 end
return _return
end
--- Trigger the on_resolve function and closes the thread - error and success called based on result of pcall (useful for async)
-- @usage thread:resolve(x,y,z) -- return true
-- @param[opt] ... any arguments you want to pass to the resolve function
-- @treturn bolean true if the thread called on_success or on_error
function Server._thread:resolve(...)
local _return = false
if is_type(self._resolve,'function') then
local success, err = pcall(self._resolve,self,...)
if success then
if is_type(self._success,'function') then
Server.interface(function(thread,err)
local success,err = pcall(thread._success,thread,err)
if not success then thread:error(err) end
end,true,self,err)
_return = true
end
else
_return = self:error(err)
end
end
self:close()
return _return
end
--- Checks the timeout on a thread - if timedout then it calles on_timeout and closes
-- @usage thread:check_timeout() -- return true
-- @treturn bolean if the thread timedout
function Server._thread:check_timeout()
local _return = false
if not self:valid() then return false end
if is_type(self.timeout,'number') and game.tick >= (self.opened+self.timeout) then
if is_type(self._timeout,'function') then
pcall(self._timeout,self)
end
_return = true
self:close()
end
return _return
end
--- Rasies an error on this thread
-- @usage thread:error(err) -- return true
-- @param err the err to be rasied
-- @treturn bolean did the thread handdle the error
function Server._thread:error(err)
local _return = false
if is_type(self._error,'function') then
pcall(self._error,self,err)
_return = true
else
error(err)
end
return _return
end
--- Set function to run then an event is called on a thread, none of them are 'needed' but you are advised to have atleast one
-- @usage thread:on_event('close',function) -- return true
-- events = ['close','timeout','tick','resolve','success','error']
-- if event is a number then it is asumed to be a game event
-- @tparam string event the name of the event that it is called on
-- @tparam function callback the function which is called on the event
-- @treturn table returns self so that there can be chained
function Server._thread:on_event(event,callback)
local events = {'close','timeout','tick','resolve','success','error'}
local value = table.find(events,function(v,k,find) return v == string.lower(find) end,event)
if value and is_type(callback,'function') then
self['_'..value] = callback
elseif is_type(event,'number') and is_type(callback,'function') then
if not self._events then self._events = {} end
self._events[event] = callback
end
return self
end
Event.register(defines.events.on_tick,function(event)
local threads = Server._threads()
if #threads.tick > 0 then Server.run_tick_threads() end
if #threads.timeout > 0 then Server.check_timeouts() end
if #threads.queue > 0 then
local current_thread = threads.all[threads.queue[1]]
if current_thread and current_thread:valid() then current_thread:resolve() end
end
end)
Event.register(-2,function(event)
local threads = Server.threads()
for uuid,thread in pairs(threads) do
if uuid ~= '_n' then setmetatable(thread,{__index=Server._thread}) end
end
end)
return Server
--[[
Thread Example:
local thread = Server.new_thread{name='tree-decon',data={}}
-- user thread:on_event('tick') rather than thread:on_event(defines.events.on_tick) as it makes less lag
thread:on_event('tick',function(self)
local trees = self.data
if #trees == 0 then return end
local tree = table.remove(trees,1)
if tree.valid then tree.destroy() end
end)
thread:on_event('error',function(self,err)
-- cant see how this can cause an error
-- but this is where error handling goes
-- any event including on_resolve and on_tick can raise this
end)
thread:on_event(defines.events.on_marked_for_deconstruction,function(self,event)
if event.entity.type == 'tree' then
table.insert(self.data,event.entity)
end
end)
thread:open()
local thread = Server.new_thread{name='print-place',data={}}
thread:on_event(defines.events.on_built_entity,function(self,event)
game.print('Events')
end)
thread:open()
all on_event functions can be chained from the thread creation rather than use varibles
]]