mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-29 12:16:37 +09:00
Spell Check and Lua Check
This commit is contained in:
@@ -13,7 +13,7 @@ local Color = require('FactorioStdLib.Color')
|
||||
-- @usage return commands.error('err message')
|
||||
commands.error = setmetatable({},{__call=function(...) return ... end})
|
||||
commands._add_command = commands.add_command
|
||||
local data = {}
|
||||
local commandDataStore = {}
|
||||
local middleware = {}
|
||||
|
||||
--- Used to add middle ware to the command handler, functions should return true or false
|
||||
@@ -27,10 +27,10 @@ function commands.add_middleware(callback) if not is_type(callback,'function') t
|
||||
-- @tparam ?string|table|event key the command that will be returned: string is the name, table is the command data, event is event from add_command
|
||||
-- @treturn table the command data
|
||||
setmetatable(commands,{
|
||||
__index=function(tbl,key) return is_type(key,'table') and (key.command and rawget(data,key.name) or key) or key == 'data' and data or rawget(data,key) end
|
||||
__index=function(tbl,key) return is_type(key,'table') and (key.command and rawget(commandDataStore,key.name) or key) or key == 'data' and commandDataStore or rawget(commandDataStore,key) end
|
||||
})
|
||||
|
||||
--- Collection of funcations that can be used to validate inputs
|
||||
--- Collection of functions that can be used to validate inputs
|
||||
-- @table commands.validate
|
||||
-- @usage commands.validate[type](value,event,...)
|
||||
-- @tparam string type the type that the value should be
|
||||
@@ -40,11 +40,11 @@ setmetatable(commands,{
|
||||
-- @return[2] error constant
|
||||
-- @return[2] the err message
|
||||
-- @field __comment replace _ with - the ldoc did not like me using - in the names
|
||||
-- @field string basicly does nothing but a type filed is required
|
||||
-- @field string_inf same as string but is infite in length, must be last arg
|
||||
-- @field string_len same as string but can define a max lengh
|
||||
-- @field string basically does nothing but a type filed is required
|
||||
-- @field string_inf same as string but is infinite in length, must be last arg
|
||||
-- @field string_len same as string but can define a max length
|
||||
-- @field number converts the input into a number
|
||||
-- @field number_int conerts the input to a number and floors it
|
||||
-- @field number_int converts the input to a number and floors it
|
||||
-- @field number_range allows a number in a range min < X <= max
|
||||
-- @field number_range allows a number in a range after it has been floored min < math.floor(X) <= max
|
||||
-- @field player converts the input into a valid player
|
||||
@@ -54,54 +54,54 @@ setmetatable(commands,{
|
||||
-- @field player_role-online converts the input to a player if the player is a lower rank than the user and online
|
||||
-- @field player_role_alive converts the input to a player if the player is a lower rank than the user and online and alive
|
||||
commands.validate = {
|
||||
['boolean']=function(value,event) local value = value.lower() if value == 'true' or valule == 'yes' or value == 'y' or value == '1' then return true else return false end end,
|
||||
['string']=function(value,event) return tostring(value) end,
|
||||
['string-inf']=function(value,event) return tostring(value) end,
|
||||
['string-list']=function(value,event,list)
|
||||
['boolean']=function(value) value = value.lower() if value == 'true' or value == 'yes' or value == 'y' or value == '1' then return true else return false end end,
|
||||
['string']=function(value) return tostring(value) end,
|
||||
['string-inf']=function(value) return tostring(value) end,
|
||||
['string-list']=function(value,event,list)
|
||||
local rtn = tostring(value) and table.includes(list,tostring(value)) and tostring(value) or nil
|
||||
if not rtn then return commands.error{'ExpGamingCore_Command.error-string-list',table.concat(list,', ')} end return rtn end,
|
||||
['string-len']=function(value,event,max)
|
||||
['string-len']=function(value,event,max)
|
||||
local rtn = tostring(value) and tostring(value):len() <= max and tostring(value) or nil
|
||||
if not rtn then return commands.error{'ExpGamingCore_Command.error-string-len',max} end return rtn end,
|
||||
['number']=function(value,event)
|
||||
local rtn = tonumber(value) or nil
|
||||
['number']=function(value)
|
||||
local rtn = tonumber(value) or nil
|
||||
if not rtn then return commands.error{'ExpGamingCore_Command.error-number'} end return rtn end,
|
||||
['number-int']=function(value,event)
|
||||
local rtn = tonumber(value) and math.floor(tonumber(value)) or nil
|
||||
['number-int']=function(value)
|
||||
local rtn = tonumber(value) and math.floor(tonumber(value)) or nil
|
||||
if not rtn then return commands.error{'ExpGamingCore_Command.error-number'} end return rtn end,
|
||||
['number-range']=function(value,event,min,max)
|
||||
['number-range']=function(value,event,min,max)
|
||||
local rtn = tonumber(value) and tonumber(value) > min and tonumber(value) <= max and tonumber(value) or nil
|
||||
if not rtn then return commands.error{'ExpGamingCore_Command.error-number-range',min,max} end return rtn end,
|
||||
['number-range-int']=function(value,event,min,max)
|
||||
['number-range-int']=function(value,event,min,max)
|
||||
local rtn = tonumber(value) and math.floor(tonumber(value)) > min and math.floor(tonumber(value)) <= max and math.floor(tonumber(value)) or nil
|
||||
if not rtn then return commands.error{'ExpGamingCore_Command.error-number-range',min,max} end return rtn end,
|
||||
['player']=function(value,event)
|
||||
['player']=function(value)
|
||||
local rtn = Game.get_player(value) or nil
|
||||
if not rtn then return commands.error{'ExpGamingCore_Command.error-player',value} end return rtn end,
|
||||
['player-online']=function(value,event)
|
||||
local player,err = commands.validate['player'](value)
|
||||
['player-online']=function(value)
|
||||
local player,err = commands.validate['player'](value)
|
||||
if err then return commands.error(err) end
|
||||
local rtn = player.connected and player or nil
|
||||
if not rtn then return commands.error{'ExpGamingCore_Command.error-player-online'} end return rtn end,
|
||||
['player-alive']=function(value,event)
|
||||
local player,err = commands.validate['player-online'](value)
|
||||
['player-alive']=function(value)
|
||||
local player,err = commands.validate['player-online'](value)
|
||||
if err then return commands.error(err) end
|
||||
local rtn = player.character and player.character.health > 0 and player or nil
|
||||
if not rtn then return commands.error{'ExpGamingCore_Command.error-player-alive'} end return rtn end,
|
||||
['player-rank']=function(value,event)
|
||||
local player,err = commands.validate['player'](value)
|
||||
if err then return commands.error(err) end
|
||||
['player-rank']=function(value,event)
|
||||
local player,err = commands.validate['player'](value)
|
||||
if err then return commands.error(err) end
|
||||
local rtn = player.admin and Game.get_player(event).admin and player or nil
|
||||
if not rtn then return commands.error{'ExpGamingCore_Command.error-player-rank'} end return rtn end,
|
||||
['player-rank-online']=function(value,event)
|
||||
local player,err = commands.validate['player-online'](value)
|
||||
if err then return commands.error(err) end
|
||||
local player,err = commands.validate['player-rank'](player)
|
||||
['player-rank-online']=function(value)
|
||||
local player,err = commands.validate['player-online'](value)
|
||||
if err then return commands.error(err) end
|
||||
local player,err = commands.validate['player-rank'](player)
|
||||
if err then return commands.error(err) end return player end,
|
||||
['player-rank-alive']=function(value,event)
|
||||
local player,err = commands.validate['player-alive'](value)
|
||||
if err then return commands.error(err) end
|
||||
local player,err = commands.validate['player-rank'](player)
|
||||
['player-rank-alive']=function(value)
|
||||
local player,err = commands.validate['player-alive'](value)
|
||||
if err then return commands.error(err) end
|
||||
local player,err = commands.validate['player-rank'](player)
|
||||
if err then return commands.error(err) end return player end,
|
||||
}
|
||||
--- Adds a function to the validation list
|
||||
@@ -114,7 +114,7 @@ function commands.add_validation(name,callback) if not is_type(callback,'functio
|
||||
-- @tparam ?string|table|event command the command to get the inputs of
|
||||
-- @treturn string the formated string for the inputs
|
||||
function commands.format_inputs(command)
|
||||
local command = commands[command]
|
||||
command = commands[command]
|
||||
if not is_type(command,'table') then error('Command is not valid',2) end
|
||||
local rtn = ''
|
||||
for name,data in pairs(command.inputs) do
|
||||
@@ -136,13 +136,13 @@ function commands.validate_args(event)
|
||||
local rtn = {}
|
||||
local count = 0
|
||||
local count_opt = 0
|
||||
for name,data in pairs(command.inputs) do count = count + 1 if data[1] == false then count_opt = count_opt + 1 end end
|
||||
-- checks that there is some args given if there is ment to be
|
||||
for _,data in pairs(command.inputs) do count = count + 1 if data[1] == false then count_opt = count_opt + 1 end end
|
||||
-- checks that there is some args given if there is meant to be
|
||||
if not event.parameter then
|
||||
if count == count_opt then return rtn
|
||||
else return commands.error('invalid-inputs') end
|
||||
end
|
||||
-- splits the args into words so that it can be used to asign values
|
||||
-- splits the args into words so that it can be used to assign values
|
||||
local words = string.split(event.parameter,' ')
|
||||
local index = 0
|
||||
for _,word in pairs(words) do
|
||||
@@ -158,7 +158,7 @@ function commands.validate_args(event)
|
||||
end
|
||||
end
|
||||
-- assigns the values from the words to the args
|
||||
local index = 0
|
||||
index = 0
|
||||
for name,data in pairs(command.inputs) do
|
||||
index = index+1
|
||||
local arg = words[index]
|
||||
@@ -178,10 +178,10 @@ end
|
||||
-- @tparam ?index|name|player| player the player to test as
|
||||
-- @treturn table a table containg all the commands the player can use
|
||||
function commands.get_commands(player)
|
||||
player = Game.get_player(player)
|
||||
local commands = {}
|
||||
local player = Game.get_player(player)
|
||||
if not player then return error('Invalid player',2) end
|
||||
for name,data in pairs(data) do
|
||||
for name,data in pairs(commandDataStore) do
|
||||
if #middleware > 0 then for _,callback in pairs(middleware) do
|
||||
local success, err = pcall(callback,player,name,data)
|
||||
if not success then error(err)
|
||||
@@ -207,7 +207,7 @@ end
|
||||
local function run_custom_command(command)
|
||||
local data = commands.data[command.name]
|
||||
local player = Game.get_player(command) or SERVER
|
||||
-- runs all middle ware if any, if there is no middle where then it relyis on .default_admin_only
|
||||
-- runs all middle ware if any, if there is no middle where then it relies on .default_admin_only
|
||||
if #middleware > 0 then for _,callback in pairs(middleware) do
|
||||
local success, err = pcall(callback,player,command.name,command)
|
||||
if not success then error(err)
|
||||
@@ -243,7 +243,7 @@ end
|
||||
-- @usage --see examples in file
|
||||
-- @tparam string name the name of the command
|
||||
-- @tparam[opt='No Description'] string description the description of the command
|
||||
-- @tparam[opt=an infite string] 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[opt=an infinite string] 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 callback the function to call on the event
|
||||
commands.add_command = function(name, description, inputs, callback)
|
||||
if commands[name] then error('That command is already registered',2) end
|
||||
@@ -254,11 +254,11 @@ commands.add_command = function(name, description, inputs, callback)
|
||||
end
|
||||
verbose('Created Command: '..name)
|
||||
-- test for string and then test for locale string
|
||||
local description = is_type(description,'string') and description
|
||||
description = is_type(description,'string') and description
|
||||
or is_type(description,'table') and is_type(description[1],'string') and string.find(description[1],'.+[.].+') and {description,''}
|
||||
or 'No Description'
|
||||
local inputs = is_type(inputs,'table') and inputs or {['param']={false,'string-inf'}}
|
||||
data[name] = {
|
||||
inputs = is_type(inputs,'table') and inputs or {['param']={false,'string-inf'}}
|
||||
commandDataStore[name] = {
|
||||
name=name,
|
||||
description=description,
|
||||
inputs=inputs,
|
||||
@@ -268,12 +268,12 @@ commands.add_command = function(name, description, inputs, callback)
|
||||
local help = is_type(description,'string') and commands.format_inputs(name)..'- '..description
|
||||
or is_type(description,'table') and is_type(description[1],'string') and string.find(description[1],'.+[.].+') and {description,commands.format_inputs(name)..'- '}
|
||||
or commands.format_inputs(name)
|
||||
data[name].help = help
|
||||
commandDataStore[name].help = help
|
||||
commands._add_command(name,help,function(...)
|
||||
local success, err = Manager.sandbox(run_custom_command,{},...)
|
||||
if not success then error(err) end
|
||||
end)
|
||||
return data[name]
|
||||
return commandDataStore[name]
|
||||
end
|
||||
|
||||
return commands
|
||||
@@ -289,8 +289,8 @@ return commands
|
||||
commands.add_command('foo',{'foo.description'},{
|
||||
['player']={true,'player'}, -- a required arg that must be a valid player
|
||||
['number']={true,'number-range',0,10}, -- a required arg that must be a number 0<X<=10
|
||||
['pwd']={true,function(value,event) if value == 'password123' then return true else return commands.error('Invalid Password') end} -- a requireed arg pwd that has custom validation
|
||||
['reason']={false,'string-inf'} -- an optinal arg that is and infite lengh (useful for reasons)
|
||||
['pwd']={true,function(value,event) if value == 'password123' then return true else return commands.error('Invalid Password') end} -- a required arg pwd that has custom validation
|
||||
['reason']={false,'string-inf'} -- an optional arg that is and infinite length (useful for reasons)
|
||||
},function(event,args)
|
||||
args.player.print(args.number)
|
||||
if args.reasons then args.player.print(args.reason) end
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
-- @module ExpGamingCore@Group
|
||||
-- @author Cooldude2606
|
||||
-- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE
|
||||
-- @alais Group
|
||||
-- @alias Group
|
||||
|
||||
-- Module Require
|
||||
local Game = require('FactorioStdLib.Game')
|
||||
@@ -14,24 +14,26 @@ local module_verbose = false
|
||||
-- @type Group
|
||||
-- @field _prototype the prototype of this class
|
||||
-- @field groups a table of all groups, includes auto complete on the indexing
|
||||
local _GroupSelfRef
|
||||
local Group = {
|
||||
_prototype = {},
|
||||
groups = setmetatable({},{
|
||||
__index=table.autokey,
|
||||
__newindex=function(tbl,key,value)
|
||||
rawset(tbl,key,Group.define(value))
|
||||
rawset(tbl,key,_GroupSelfRef.define(value))
|
||||
end
|
||||
}),
|
||||
on_init = function()
|
||||
if loaded_modules['ExpGamingCore.Server'] then require('ExpGamingCore.Server') end
|
||||
end,
|
||||
on_post = function(self)
|
||||
-- creats a root role that the server can use
|
||||
-- creates a root role that the server can use
|
||||
self{name='Root',disallow={}}
|
||||
-- loads the groups in config
|
||||
require(module_path..'/config',{Group=self})
|
||||
end
|
||||
}
|
||||
_GroupSelfRef=Group
|
||||
|
||||
-- Function Define
|
||||
|
||||
@@ -75,9 +77,9 @@ end
|
||||
-- @tparam ?string|LuaPermissionGroup the group to add the player to
|
||||
-- @treturn boolean was the player assigned
|
||||
function Group.assign(player,group)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player #1 given to Group.assign.',2) return end
|
||||
local group = Group.get(group)
|
||||
group = Group.get(group)
|
||||
if not group then error('Invalid group #2 given to Group.assign.',2) return end
|
||||
return group:add_player(player)
|
||||
end
|
||||
@@ -98,7 +100,7 @@ end
|
||||
-- @treturn boolean if the player was added
|
||||
function Group._prototype:add_player(player)
|
||||
if not self_test(self,'group','add_player') then return end
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player #1 given to group.add_player.',2) return end
|
||||
local raw_group = self:get_raw()
|
||||
return raw_group.add_player(player)
|
||||
@@ -110,7 +112,7 @@ end
|
||||
-- @treturn boolean if the player was removed
|
||||
function Group._prototype:remove_player(player)
|
||||
if not self_test(self,'group','remove_player') then return end
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player #1 given to group.remove_player.',2) return end
|
||||
local raw_group = self:get_raw()
|
||||
return raw_group.remove_player(player)
|
||||
@@ -134,18 +136,18 @@ end
|
||||
-- this is used to create a connected_players table
|
||||
Group._prototype.connected_players_mt = {
|
||||
__call=function(tbl) return tbl.self:get_players(true) end,
|
||||
__pairs=function(tbl)
|
||||
local players = tbl.self:get_players(true)
|
||||
local function next_pair(tbl,k)
|
||||
k, v = next(players, k)
|
||||
__pairs=function(self)
|
||||
local players = self.self:get_players(true)
|
||||
local function next_pair(tbl,key)
|
||||
local k, v = next(players, key)
|
||||
if v then return k,v end
|
||||
end
|
||||
return next_pair, players, nil
|
||||
end,
|
||||
__ipairs=function(tbl)
|
||||
local players = tbl.self:get_players(true)
|
||||
local function next_pair(tbl,k)
|
||||
k, v = next(players, k)
|
||||
__ipairs=function(self)
|
||||
local players = self.self:get_players(true)
|
||||
local function next_pair(tbl,key)
|
||||
local k, v = next(players, key)
|
||||
if v then return k,v end
|
||||
end
|
||||
return next_pair, players, nil
|
||||
@@ -156,7 +158,7 @@ Group._prototype.connected_players_mt = {
|
||||
-- @usage group.print('Hello, World!')
|
||||
-- @param rtn any value you wish to print, string not required
|
||||
-- @param colour the colour to print the message in
|
||||
-- @treturn number the number of players who recived the message
|
||||
-- @treturn number the number of players who received the message
|
||||
function Group._prototype:print(rtn,colour)
|
||||
if not self_test(self,'group','print') then return end
|
||||
if colour and not type_error(colour,'table','Invalid argument #2 to group:print, colour is not a table.') then return end
|
||||
@@ -169,7 +171,7 @@ end
|
||||
-- Event Handlers Define
|
||||
|
||||
-- creates all permission groups and links them
|
||||
script.on_event('on_init',function(event)
|
||||
script.on_event('on_init',function()
|
||||
for name,group in pairs(Group.groups) do
|
||||
local _group = game.permissions.create_group(name)
|
||||
verbose('Created Permission Group: '..name)
|
||||
|
||||
@@ -15,11 +15,11 @@ center._prototype = {}
|
||||
--- Adds a new obj to the center gui
|
||||
-- @usage Gui.center.add{name='foo',caption='Foo',tooltip='Testing',draw=function}
|
||||
-- @usage return_value(player) -- opens the center gui for that player
|
||||
-- @param obj contains the new object, needs name, fraw is opt and is function(root_frame)
|
||||
-- @param obj contains the new object, needs name, frame is opt and is function(root_frame)
|
||||
-- @return the object made, used to add tabs, calling the returned value will open the center for the given player
|
||||
function center.add(obj)
|
||||
if not is_type(obj,'table') then return end
|
||||
if not is_type(obj.name,'string') then return end
|
||||
if not is_type(obj,'table') then return end
|
||||
if not is_type(obj.name,'string') then return end
|
||||
verbose('Created Center Gui: '..obj.name)
|
||||
setmetatable(obj,{__index=center._prototype,__call=function(self,player,...) return center.open(player,self.name,...) end})
|
||||
obj.tabs = {}
|
||||
@@ -30,32 +30,32 @@ function center.add(obj)
|
||||
end
|
||||
|
||||
--- Used to get the center frame of the player, used mainly in script
|
||||
-- @usage Gui.center.get_flow(player) -- returns gui emelemt
|
||||
-- @param player a player indifier to get the flow for
|
||||
-- @usage Gui.center.get_flow(player) -- returns gui element
|
||||
-- @param player a player identifier to get the flow for
|
||||
-- @treturn table the gui element flow
|
||||
function center.get_flow(player)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player',2) end
|
||||
return player.gui.center.exp_center or player.gui.center.add{name='exp_center',type='flow'}
|
||||
end
|
||||
|
||||
--- Used to open a center frame for a player, extra params are sent to open event
|
||||
-- @usage Gui.center.open(player,'server-info') -- return true
|
||||
-- @param player a player indifier to get the flow for
|
||||
-- @tparam string center the name of the center frame to open
|
||||
-- @treturn boelon based on if it successed or not
|
||||
function center.open(player,center,...)
|
||||
local player = Game.get_player(player)
|
||||
-- @param player a player identifier to get the flow for
|
||||
-- @tparam string center_name the name of the center frame to open
|
||||
-- @treturn boolean based on if it succeeded or not
|
||||
function center.open(player,center_name,...)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player',2) return false end
|
||||
Gui.center.clear(player)
|
||||
if not Gui.data.center[center] then return false end
|
||||
local self = Gui.data.center[center]
|
||||
if not Gui.data.center[center_name] then return false end
|
||||
local self = Gui.data.center[center_name]
|
||||
-- this function is the draw function passed to the open event
|
||||
self:open(player,function(...) Gui.center._draw(self,...) end,...)
|
||||
return true
|
||||
end
|
||||
|
||||
-- used as a pice of middle ware for the open event
|
||||
-- used as a piece of middle ware for the open event
|
||||
function center._draw(self,frame,...)
|
||||
game.players[frame.player_index].opened=frame
|
||||
if is_type(self.draw,'function') then
|
||||
@@ -66,37 +66,37 @@ end
|
||||
|
||||
--- Used to open a center frame for a player
|
||||
-- @usage Gui.center.open_tab(player,'readme','rules') -- return true
|
||||
-- @param player a player indifier to get the flow for
|
||||
-- @param player a player identifier to get the flow for
|
||||
-- @tparam string center the name of the center frame to open
|
||||
-- @tparam string tab the name of the tab to open
|
||||
-- @treturn boelon based on if it successed or not
|
||||
function center.open_tab(player,center,tab)
|
||||
local player = Game.get_player(player)
|
||||
-- @treturn boolean based on if it succeeded or not
|
||||
function center.open_tab(player,center_name,tab)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player',2) end
|
||||
if not Gui.center.open(player,center) then return false end
|
||||
local name = center..'_'..tab
|
||||
if not Gui.center.open(player,center_name) then return false end
|
||||
local name = center_name..'_'..tab
|
||||
if not Gui.data.inputs_button[name] then return false end
|
||||
Gui.data.inputs_button[name].events[defines.events.on_gui_click]{
|
||||
element=Gui.center.get_flow(player)[center].tab_bar.tab_bar_scroll.tab_bar_scroll_flow[name],
|
||||
element=Gui.center.get_flow(player)[center_name].tab_bar.tab_bar_scroll.tab_bar_scroll_flow[name],
|
||||
}
|
||||
return true
|
||||
end
|
||||
|
||||
--- Used to clear the center frame of the player, used mainly in script
|
||||
-- @usage Gui.center.clear(player)
|
||||
-- @param player a player indifier to get the flow for
|
||||
-- @param player a player identifier to get the flow for
|
||||
function center.clear(player)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
center.get_flow(player).clear()
|
||||
end
|
||||
|
||||
-- opens this gui for this player, draw is the draw function when event is called from center.open
|
||||
-- this is the default function it can be overriden when the gui is defined, simply call draw on the frame you create
|
||||
-- this is the default function it can be overridden when the gui is defined, simply call draw on the frame you create
|
||||
-- extra values passed to draw will also be passed to the draw event
|
||||
-- extra values from center.draw and passed to the open event
|
||||
function center._prototype:open(player,draw,...)
|
||||
local player = Game.get_player(player)
|
||||
local draw = draw or function(...) center._draw(self,...) end
|
||||
player = Game.get_player(player)
|
||||
draw = draw or function(...) center._draw(self,...) end
|
||||
local center_flow = center.get_flow(player)
|
||||
if center_flow[self.name] then Gui.center.clear(player) return end
|
||||
local center_frame = center_flow.add{
|
||||
@@ -110,8 +110,8 @@ function center._prototype:open(player,draw,...)
|
||||
draw(center_frame,...)
|
||||
end
|
||||
|
||||
-- this is the default draw function if one is not provided, can be overriden
|
||||
-- not recomented for direct use see Gui.center.open
|
||||
-- this is the default draw function if one is not provided, can be overridden
|
||||
-- not recommended for direct use see Gui.center.open
|
||||
function center._prototype:draw(frame)
|
||||
Gui.bar(frame,510)
|
||||
local tab_bar = frame.add{
|
||||
@@ -123,8 +123,8 @@ function center._prototype:draw(frame)
|
||||
tab_bar.style.width = 510
|
||||
tab_bar.style.height = 65
|
||||
local tab_bar_scroll = tab_bar.add{
|
||||
type='scroll-pane',
|
||||
name='tab_bar_scroll',
|
||||
type='scroll-pane',
|
||||
name='tab_bar_scroll',
|
||||
horizontal_scroll_policy='auto-and-reserve-space',
|
||||
vertical_scroll_policy='never'
|
||||
}
|
||||
@@ -147,7 +147,7 @@ function center._prototype:draw(frame)
|
||||
tab.style.height = 305
|
||||
local tab_scroll = tab.add{
|
||||
type ='scroll-pane',
|
||||
name='tab_scroll',
|
||||
name='tab_scroll',
|
||||
horizontal_scroll_policy='never',
|
||||
vertical_scroll_policy='auto'
|
||||
}
|
||||
@@ -155,8 +155,8 @@ function center._prototype:draw(frame)
|
||||
tab_scroll.style.vertically_stretchable = true
|
||||
tab_scroll.style.width = 500
|
||||
local tab_scroll_flow = tab_scroll.add{
|
||||
type='flow',
|
||||
name='tab_scroll_flow',
|
||||
type='flow',
|
||||
name='tab_scroll_flow',
|
||||
direction='vertical'
|
||||
}
|
||||
tab_scroll_flow.style.width = 480
|
||||
@@ -171,7 +171,7 @@ function center._prototype:draw(frame)
|
||||
frame.parent.add{type='frame',name='temp'}.destroy()--recenter the GUI
|
||||
end
|
||||
|
||||
--- If deafult draw is used then you can add tabs to the gui with this function
|
||||
--- If default draw is used then you can add tabs to the gui with this function
|
||||
-- @usage _center:add_tab('foo','Foo','Just a tab',function)
|
||||
-- @tparam string name this is the name of the tab
|
||||
-- @tparam string caption this is the words that appear on the tab button
|
||||
@@ -214,7 +214,7 @@ end)
|
||||
|
||||
script.on_event(defines.events.on_player_respawned,center.clear)
|
||||
|
||||
function center:on_init()
|
||||
function center.on_init()
|
||||
if loaded_modules['ExpGamingCore.Role'] then script.on_event(defines.events.on_role_change,center.clear) end
|
||||
end
|
||||
-- calling will attempt to add a new gui
|
||||
|
||||
@@ -16,7 +16,7 @@ local global = global()
|
||||
-- @tparam string location the location to get/set the data, center left etc...
|
||||
-- @tparam[opt] string key the name of the gui to set the value of
|
||||
-- @param[opt] value the data that will be set can be any value but table advised
|
||||
-- @treturn[1] table all the gui data that is locationed in that location
|
||||
-- @treturn[1] table all the gui data that is located in that location
|
||||
Gui.data = setmetatable({},{
|
||||
__call=function(tbl,location,key,value)
|
||||
if not location then return tbl end
|
||||
@@ -31,7 +31,7 @@ Gui.data = setmetatable({},{
|
||||
-- @usage Gui.bar(frame,100)
|
||||
-- @param frame the frame to draw the line to
|
||||
-- @param[opt=10] width the width of the bar
|
||||
-- @return the line that was made type is progressbar
|
||||
-- @return the line that was made type is progress bar
|
||||
function Gui.bar(frame,width)
|
||||
local line = frame.add{
|
||||
type='progressbar',
|
||||
@@ -44,11 +44,11 @@ function Gui.bar(frame,width)
|
||||
return line
|
||||
end
|
||||
|
||||
--- Adds a lable that is centered
|
||||
-- @usage Gui.centered_label(frane, 'Hello, world!')
|
||||
--- Adds a label that is centered
|
||||
-- @usage Gui.centered_label(frame, 'Hello, world!')
|
||||
-- @tparam LuaGuiElement frame the parent frame to add the label to
|
||||
-- @tparam string string the string that the lable will have
|
||||
function Gui.centered_label(frane, string)
|
||||
-- @tparam string string the string that the label will have
|
||||
function Gui.centered_label(frame, string)
|
||||
local flow = frame.add {frame = 'flow'}
|
||||
local flow_style = flow.style
|
||||
flow_style.align = 'center'
|
||||
@@ -62,13 +62,13 @@ function Gui.centered_label(frane, string)
|
||||
return label
|
||||
end
|
||||
|
||||
--- Used to set the index of a drop down to a certian item
|
||||
--- Used to set the index of a drop down to a certain item
|
||||
-- @usage Gui.set_dropdown_index(dropdown,player.name) -- will select the index with the players name as the value
|
||||
-- @param dropdown the dropdown that is to be effected
|
||||
-- @param _item this is the item to look for
|
||||
-- @return returns the dropdown if it was successful
|
||||
function Gui.set_dropdown_index(dropdown,_item)
|
||||
if dropdown and dropdown.valid and dropdown.items and _item then else return end
|
||||
if not dropdown or not dropdown.valid or not dropdown.items or not _item then return end
|
||||
local _index = 1
|
||||
for index, item in pairs(dropdown.items) do
|
||||
if item == _item then _index = index break end
|
||||
@@ -88,7 +88,7 @@ end
|
||||
-- @field surface this will over ride the surface that the camera follows on, allowing for a 'ghost surface' while keeping same position
|
||||
-- @field respawn_open if set to true then the camera will auto re link to the player after a respawn
|
||||
|
||||
--- Adds a camera that updates every tick (or less depeading on how many are opening) it will move to follow an entity
|
||||
--- Adds a camera that updates every tick (or less depending on how many are opening) it will move to follow an entity
|
||||
-- @usage Gui.cam_link{entity=game.player.character,frame=frame,width=50,hight=50,zoom=1}
|
||||
-- @usage Gui.cam_link{entity=game.player.character,cam=frame.camera,surface=game.surfaces['testing']}
|
||||
-- @tparam table data contains all other params given below
|
||||
@@ -103,7 +103,7 @@ function Gui.cam_link(data)
|
||||
data.cam.name='camera'
|
||||
data.cam.position= data.entity.position
|
||||
data.cam.surface_index= data.surface and data.surface.index or data.entity.surface.index
|
||||
data.cam.zomm = data.zoom
|
||||
data.cam.zoom = data.zoom
|
||||
data.cam = data.frame.add(data.cam)
|
||||
data.cam.style.width = data.width or 100
|
||||
data.cam.style.height = data.height or 100
|
||||
@@ -147,7 +147,7 @@ script.on_event('on_tick', function(event)
|
||||
if global.cam_index >= #global.cams then global.cam_index = 1 end
|
||||
if update > #global.cams then update = #global.cams end
|
||||
for cam_offset = 0,update do
|
||||
local _cam = global.cams[global.cam_index]
|
||||
local _cam = global.cams[global.cam_index+cam_offset]
|
||||
if not _cam then break end
|
||||
if not _cam.cam.valid then table.remove(global.cams,global.cam_index)
|
||||
elseif not _cam.entity.valid then table.remove(global.cams,global.cam_index)
|
||||
@@ -162,6 +162,7 @@ script.on_event('on_player_respawned',function(event)
|
||||
if loaded_modules['ExpGamingCore.Server'] then return end
|
||||
if global.players and is_type(global.players,'table') and #global.players > 0 and global.players[event.player_index] then
|
||||
local remove = {}
|
||||
local player = Game.get_player(event)
|
||||
for index,cam in pairs(global.players[event.player_index]) do
|
||||
if cam.valid then table.insert(global.cams,{cam=cam,entity=player.character,surface=player.surface})
|
||||
else table.insert(remove,index) end
|
||||
@@ -173,14 +174,14 @@ script.on_event('on_player_respawned',function(event)
|
||||
end)
|
||||
|
||||
function Gui:on_init()
|
||||
if loaded_modules['ExpGamingCore.Server'] then
|
||||
Server = require('ExpGamingCore.Server')
|
||||
verbose('ExpGamingCore.Server is installed; Loading server src')
|
||||
script.on_init(require(module_path..'/src/server',{Gui=self}))
|
||||
if loaded_modules['ExpGamingCore.Server'] then
|
||||
Server = require('ExpGamingCore.Server')
|
||||
verbose('ExpGamingCore.Server is installed; Loading server src')
|
||||
script.on_init(require(module_path..'/src/server',{Gui=self}))
|
||||
end
|
||||
end
|
||||
|
||||
function Gui:on_post()
|
||||
function Gui.on_post()
|
||||
Gui.test = require(module_path..'/src/test',{Gui=Gui})
|
||||
end
|
||||
|
||||
|
||||
@@ -78,8 +78,8 @@ function inputs._prototype:draw(root)
|
||||
local success, err = pcall(self.data._items,player,root)
|
||||
if success then data.items = err else error(err) end
|
||||
if is_type(self.data._index,'function') then
|
||||
local success, err = pcall(self.data._index,player,root)
|
||||
if success then data.selected_index = err else error(err) end
|
||||
local _success, _err = pcall(self.data._index,player,root)
|
||||
if _success then data.selected_index = _err else error(_err) end
|
||||
end
|
||||
return root.add(data)
|
||||
else
|
||||
@@ -87,25 +87,25 @@ function inputs._prototype:draw(root)
|
||||
end
|
||||
end
|
||||
|
||||
--- Add a new input, this is the same as doing frame.add{} but returns a diffrent object
|
||||
--- Add a new input, this is the same as doing frame.add{} but returns a different object
|
||||
-- @usage Gui.inputs.add{type='button',name='test',caption='Test'}
|
||||
-- @usage return_value(frame) -- draws the button onto that frame
|
||||
-- @tparam table obj the new element to add if caption is a sprite path then sprite is used
|
||||
-- @treturn table the custom input object, calling the returned calue will draw the button
|
||||
-- @treturn table the custom input object, calling the returned value will draw the button
|
||||
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' or
|
||||
type == 'drop-down'
|
||||
then else return end
|
||||
if type ~= 'button'
|
||||
and type ~= 'sprite-button'
|
||||
and type ~= 'choose-elem-button'
|
||||
and type ~= 'checkbox'
|
||||
and type ~= 'radiobutton'
|
||||
and type ~= 'textfield'
|
||||
and type ~= 'text-box'
|
||||
and type ~= 'slider'
|
||||
and type ~= 'drop-down'
|
||||
then return end
|
||||
verbose('Created Input: '..obj.name..' ('..obj.type..')')
|
||||
if obj.type == 'button' or obj.type == 'sprite-button' then obj.style = mod_gui.button_style end
|
||||
obj.draw_data = table.deepcopy(obj)
|
||||
@@ -139,23 +139,23 @@ end
|
||||
script.on_event(inputs.events,inputs._event_handler)
|
||||
inputs.events.error = {}
|
||||
|
||||
-- the folwing functions are just to make inputs easier but if what you want is not include use inputs.add(obj)
|
||||
-- the following 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
|
||||
-- @param callbacks can either be a single function or a list of function pairs see examples 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{
|
||||
local rtn_button = inputs.add{
|
||||
type='button',
|
||||
name=name,
|
||||
caption=display,
|
||||
tooltip=tooltip
|
||||
}
|
||||
button.data._callbacks = callbacks
|
||||
button:on_event('click',function(event)
|
||||
rtn_button.data._callbacks = callbacks
|
||||
rtn_button:on_event('click',function(event)
|
||||
local elements = Gui.data['inputs_'..event.element.type] or {}
|
||||
local button = elements[event.element.name]
|
||||
if not button and event.element.type == 'sprite-button' then
|
||||
@@ -166,19 +166,19 @@ function inputs.add_button(name,display,tooltip,callbacks)
|
||||
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
|
||||
local btn_callbacks = button.data._callbacks
|
||||
if is_type(btn_callbacks,'function') then btn_callbacks = {{function() return true end,btn_callbacks}} end
|
||||
for _,data in pairs(btn_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
|
||||
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
|
||||
end)
|
||||
return button
|
||||
return rtn_button
|
||||
end
|
||||
|
||||
--- Used to define a choose-elem-button callback only on elem_changed
|
||||
@@ -221,20 +221,19 @@ end
|
||||
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{
|
||||
local rtn_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)
|
||||
if is_type(default,'function') then rtn_checkbox.data._state = default end
|
||||
rtn_checkbox.data._true = callback_true
|
||||
rtn_checkbox.data._false = callback_false
|
||||
rtn_checkbox:on_event('state',function(event)
|
||||
local checkbox = Gui.data['inputs_'..event.element.type][event.element.name]
|
||||
local player = Game.get_player(event)
|
||||
local state = event.element.state
|
||||
if state then
|
||||
if event.element.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
|
||||
@@ -246,10 +245,10 @@ function inputs.add_checkbox(name,radio,display,default,callback_true,callback_f
|
||||
else error('Invalid Callback') end
|
||||
end
|
||||
end)
|
||||
return checkbox
|
||||
return rtn_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.
|
||||
--- Used to reset the state of radio buttons, recommended to be called on_state_change to reset any radio buttons it is meant 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)
|
||||
@@ -287,23 +286,23 @@ end
|
||||
-- @treturn table the text object that was made, to allow a custom error event if wanted
|
||||
function inputs.add_text(name,box,text,callback)
|
||||
local type = 'textfield'; if box then type='text-box' end
|
||||
local textbox = inputs.add{
|
||||
local rtn_textbox = inputs.add{
|
||||
type=type,
|
||||
name=name,
|
||||
text=text
|
||||
}
|
||||
textbox.data._callback = callback
|
||||
textbox:on_event('text',function(event)
|
||||
rtn_textbox.data._callback = callback
|
||||
rtn_textbox:on_event('text',function(event)
|
||||
local textbox = Gui.data['inputs_'..event.element.type][event.element.name]
|
||||
local player = Game.get_player(event)
|
||||
local element = event.element
|
||||
local callback = textbox.data._callback
|
||||
if is_type(callback,'function') then
|
||||
local success, err = pcall(callback,player,element.text,element)
|
||||
local event_callback = textbox.data._callback
|
||||
if is_type(event_callback,'function') then
|
||||
local success, err = pcall(event_callback,player,element.text,element)
|
||||
if not success then error(err) end
|
||||
else error('Invalid Callback Condition Format') end
|
||||
end)
|
||||
return textbox
|
||||
return rtn_textbox
|
||||
end
|
||||
|
||||
--- Used to define a slider callback only on value_changed
|
||||
@@ -348,28 +347,28 @@ end
|
||||
-- @tparam function callback the callback which is called when a new index is selected function(player,selected,items,element)
|
||||
-- @treturn table the drop-down object that was made, to allow a custom error event if wanted
|
||||
function inputs.add_drop_down(name,items,index,callback)
|
||||
local drop_down = inputs.add{
|
||||
local rtn_dropdown = inputs.add{
|
||||
type='drop-down',
|
||||
name=name,
|
||||
items=items,
|
||||
selected_index=index
|
||||
}
|
||||
drop_down.data._items = items
|
||||
drop_down.data._index = index
|
||||
drop_down.data._callback = callback
|
||||
drop_down:on_event('selection',function(event)
|
||||
local drop_down = Gui.data['inputs_'..event.element.type][event.element.name]
|
||||
rtn_dropdown.data._items = items
|
||||
rtn_dropdown.data._index = index
|
||||
rtn_dropdown.data._callback = callback
|
||||
rtn_dropdown:on_event('selection',function(event)
|
||||
local dropdown = Gui.data['inputs_'..event.element.type][event.element.name]
|
||||
local player = Game.get_player(event)
|
||||
local element = event.element
|
||||
local items = element.items
|
||||
local selected = items[element.selected_index]
|
||||
local callback = drop_down.data._callback
|
||||
if is_type(callback,'function') then
|
||||
local success, err = pcall(callback,player,selected,items,element)
|
||||
local drop_items = element.items
|
||||
local selected = drop_items[element.selected_index]
|
||||
local drop_callback = dropdown.data._callback
|
||||
if is_type(drop_callback,'function') then
|
||||
local success, err = pcall(drop_callback,player,selected,drop_items,element)
|
||||
if not success then error(err) end
|
||||
else error('Invalid Callback Condition Format') end
|
||||
end)
|
||||
return drop_down
|
||||
return rtn_dropdown
|
||||
end
|
||||
|
||||
-- calling will attempt to add a new input
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
--- Adds a organiser for left gui ellements which will automaticaly update there information and have open requirements
|
||||
--- Adds a organiser for left gui elements which will automatically update there information and have open requirements
|
||||
-- @module ExpGamingCore.Gui.Left
|
||||
-- @alias left
|
||||
-- @author Cooldude2606
|
||||
@@ -12,7 +12,7 @@ local Color = require('FactorioStdLib.Color')
|
||||
local mod_gui = require('mod-gui')
|
||||
local Gui = require('ExpGamingCore.Gui')
|
||||
local order_config = require(module_path..'/order_config')
|
||||
local Role -- this is optional and is hanndled by it being present, it is loaded on init
|
||||
local Role -- this is optional and is handled by it being present, it is loaded on init
|
||||
|
||||
local left = {}
|
||||
left._prototype = {}
|
||||
@@ -35,11 +35,11 @@ function left.override_open(state)
|
||||
end
|
||||
--- Used to add a left gui frame
|
||||
-- @usage Gui.left.add{name='foo',caption='Foo',tooltip='just testing',open_on_join=true,can_open=function,draw=function}
|
||||
-- @usage return_value(player) -- toggles visiblity for that player, if no player then updates for all players
|
||||
-- @param obj this is what will be made, needs a name and a draw function(root_frame), open_on_join can be used to set the deaful state true/false, can_open is a test to block it from opening but is not needed
|
||||
-- @return the object that is made, calling the returned value with out a param will update the gui, else will toggle visiblity for that player
|
||||
-- @usage return_value(player) -- toggles visibility for that player, if no player then updates for all players
|
||||
-- @param obj this is what will be made, needs a name and a draw function(root_frame), open_on_join can be used to set the default state true/false, can_open is a test to block it from opening but is not needed
|
||||
-- @return the object that is made, calling the returned value with out a param will update the gui, else will toggle visibility for that player
|
||||
function left.add(obj)
|
||||
if not is_type(obj,'table') then return end
|
||||
if not is_type(obj,'table') then return end
|
||||
if not is_type(obj.name,'string') then return end
|
||||
verbose('Created Left Gui: '..obj.name)
|
||||
setmetatable(obj,{__index=left._prototype,__call=function(self,player) if player then return self:toggle(player) else return left.update(self.name) end end})
|
||||
@@ -54,18 +54,18 @@ end
|
||||
-- @param[opt] players the player to update for, if not given all players are updated, can be one player
|
||||
function left.update(frame,players)
|
||||
if not Server or not Server._thread then
|
||||
local players = is_type(players,'table') and #players > 0 and {unpack(players)} or is_type(players,'table') and {players} or Game.get_player(players) and {Game.get_player(players)} or game.connected_players
|
||||
players = is_type(players,'table') and #players > 0 and {unpack(players)} or is_type(players,'table') and {players} or Game.get_player(players) and {Game.get_player(players)} or game.connected_players
|
||||
for _,player in pairs(players) do
|
||||
local frames = Gui.data.left or {}
|
||||
if frame then frames = {[frame]=frames[frame]} or {} end
|
||||
for name,left in pairs(frames) do
|
||||
if _left then left:first_open(player) end
|
||||
for _,left_frame in pairs(frames) do
|
||||
if left_frame then left_frame:first_open(player) end
|
||||
end
|
||||
end
|
||||
else
|
||||
local frames = Gui.data.left or {}
|
||||
if frame then frames = {[frame]=frames[frame]} or {} end
|
||||
local players = is_type(players,'table') and #players > 0 and {unpack(players)} or is_type(players,'table') and {players} or Game.get_player(players) and {Game.get_player(players)} or game.connected_players
|
||||
players = is_type(players,'table') and #players > 0 and {unpack(players)} or is_type(players,'table') and {players} or Game.get_player(players) and {Game.get_player(players)} or game.connected_players
|
||||
Server.new_thread{
|
||||
data={players=players,frames=frames}
|
||||
}:on_event('tick',function(thread)
|
||||
@@ -74,8 +74,8 @@ function left.update(frame,players)
|
||||
Server.new_thread{
|
||||
data={player=player,frames=thread.data.frames}
|
||||
}:on_event('resolve',function(thread)
|
||||
for name,left in pairs(thread.data.frames) do
|
||||
if left then left:first_open(thread.data.player) end
|
||||
for _,left_frame in pairs(thread.data.frames) do
|
||||
if left_frame then left_frame:first_open(thread.data.player) end
|
||||
end
|
||||
end):queue()
|
||||
end):open()
|
||||
@@ -91,14 +91,14 @@ function left.open(left_name,player)
|
||||
local _left = Gui.data.left[left_name]
|
||||
if not _left then return end
|
||||
if not Server or not Server._thread then
|
||||
for _,player in pairs(players) do _left:open(player) end
|
||||
for _,next_player in pairs(players) do _left:open(next_player) end
|
||||
else
|
||||
Server.new_thread{
|
||||
data={players=players}
|
||||
}:on_event('tick',function(thread)
|
||||
if #thread.data.players == 0 then thread:close() return end
|
||||
local player = table.remove(thread.data.players,1)
|
||||
_left:open(player)
|
||||
local next_player = table.remove(thread.data.players,1)
|
||||
_left:open(next_player)
|
||||
end):open()
|
||||
end
|
||||
end
|
||||
@@ -112,14 +112,14 @@ function left.close(left_name,player)
|
||||
local _left = Gui.data.left[left_name]
|
||||
if not _left then return end
|
||||
if not Server or not Server._thread or player then
|
||||
for _,player in pairs(players) do _left:close(player) end
|
||||
for _,next_player in pairs(players) do _left:close(next_player) end
|
||||
else
|
||||
Server.new_thread{
|
||||
data={players=players}
|
||||
}:on_event('tick',function(thread)
|
||||
if #thread.data.players == 0 then thread:close() return end
|
||||
local player = table.remove(thread.data.players,1)
|
||||
_left:close(player)
|
||||
local next_player = table.remove(thread.data.players,1)
|
||||
_left:close(next_player)
|
||||
end):open()
|
||||
end
|
||||
end
|
||||
@@ -129,7 +129,7 @@ end
|
||||
-- @usage left:open(player)
|
||||
-- @tparam luaPlayer player the player to open the gui for
|
||||
function left._prototype:open(player)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid Player') end
|
||||
local left_flow = mod_gui.get_frame_flow(player)
|
||||
if not left_flow[self.name] then self:first_open(player) end
|
||||
@@ -141,7 +141,7 @@ end
|
||||
-- @usage left:open(player)
|
||||
-- @tparam luaPlayer player the player to close the gui for
|
||||
function left._prototype:close(player)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid Player') end
|
||||
local left_flow = mod_gui.get_frame_flow(player)
|
||||
if not left_flow[self.name] then self:first_open(player) end
|
||||
@@ -156,11 +156,11 @@ end
|
||||
-- @tparam LuaPlayer player the player to draw the gui for
|
||||
-- @treturn LuaFrame the frame made/updated
|
||||
function left._prototype:first_open(player)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
local left_flow = mod_gui.get_frame_flow(player)
|
||||
local frame = nil
|
||||
if left_flow[self.name] then
|
||||
frame = left_flow[self.name]
|
||||
local frame
|
||||
if left_flow[self.name] then
|
||||
frame = left_flow[self.name]
|
||||
frame.clear()
|
||||
else
|
||||
if not left_flow['gui-left-hide'] then left.hide(left_flow).style.maximal_width=15 end
|
||||
@@ -172,15 +172,15 @@ function left._prototype:first_open(player)
|
||||
return frame
|
||||
end
|
||||
|
||||
--- Toggles the visiblity of the gui based on some conditions
|
||||
--- Toggles the visibility of the gui based on some conditions
|
||||
-- @usage left:toggle(player) -- returns new state
|
||||
-- @tparam LuaPlayer player the player to toggle the gui for, remember there are condition which need to be met
|
||||
-- @treturn boolean the new state that the gui is in
|
||||
function left._prototype:toggle(player)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
local left_flow = mod_gui.get_frame_flow(player)
|
||||
if not left_flow[self.name] then self:first_open(player) end
|
||||
local left = left_flow[self.name]
|
||||
local left_frame = left_flow[self.name]
|
||||
local open = false
|
||||
if is_type(self.can_open,'function') then
|
||||
local success, err = pcall(self.can_open,player)
|
||||
@@ -198,18 +198,18 @@ function left._prototype:toggle(player)
|
||||
else open = {'ExpGamingCore_Gui.unauthorized'} end
|
||||
else open = true end
|
||||
end
|
||||
if open == true and left.style.visible ~= true then
|
||||
left.style.visible = true
|
||||
if open == true and left_frame.style.visible ~= true then
|
||||
left_frame.style.visible = true
|
||||
left_flow['gui-left-hide'].style.visible = true
|
||||
else
|
||||
left.style.visible = false
|
||||
left_frame.style.visible = false
|
||||
local count = 0
|
||||
for _,child in pairs(left_flow.children) do if child.style.visible then count = count+1 end if count > 1 then break end end
|
||||
if count == 1 and left_flow['gui-left-hide'] then left_flow['gui-left-hide'].style.visible = false end
|
||||
end
|
||||
if open == false then player_return({'ExpGamingCore_Gui.cant-open-no-reason'},defines.textcolor.crit,player) player.play_sound{path='utility/cannot_build'}
|
||||
elseif open ~= true then player_return({'ExpGamingCore_Gui.cant-open',open},defines.textcolor.crit,player) player.play_sound{path='utility/cannot_build'} end
|
||||
return left.style.visible
|
||||
return left_frame.style.visible
|
||||
end
|
||||
|
||||
script.on_event(defines.events.on_player_joined_game,function(event)
|
||||
@@ -220,14 +220,14 @@ script.on_event(defines.events.on_player_joined_game,function(event)
|
||||
if not left_flow['gui-left-hide'] then left.hide(left_flow).style.maximal_width=15 end
|
||||
local done = {}
|
||||
for _,name in pairs(order_config) do
|
||||
local left = Gui.data.left[name]
|
||||
if left then
|
||||
local left_frame = Gui.data.left[name]
|
||||
if left_frame then
|
||||
done[name] = true
|
||||
left:first_open(player)
|
||||
left_frame:first_open(player)
|
||||
end
|
||||
end
|
||||
for name,left in pairs(frames) do
|
||||
if not done[name] then left:first_open(player) end
|
||||
for name,left_frame in pairs(frames) do
|
||||
if not done[name] then left_frame:first_open(player) end
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -237,7 +237,7 @@ script.on_event(defines.events.on_tick,function(event)
|
||||
end
|
||||
end)
|
||||
|
||||
function left:on_init()
|
||||
function left.on_init()
|
||||
if loaded_modules['ExpGamingCore.Role'] then Role = require('ExpGamingCore.Role') end
|
||||
end
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ end
|
||||
|
||||
-- this is used by the script to find the popup flow
|
||||
function popup.flow(player)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid Player',2) end
|
||||
local flow = mod_gui.get_frame_flow(player).popups
|
||||
if not flow then flow = mod_gui.get_frame_flow(player).add{name='popups',type='flow',direction='vertical'} flow.style.visible=false end
|
||||
@@ -47,8 +47,8 @@ end
|
||||
-- @tparam[opt=game.connected_players] table players the players to open the popup for
|
||||
function popup.open(style,data,players)
|
||||
local _popup = Gui.data.popup[style]
|
||||
local players = players or game.connected_players
|
||||
local data = data or {}
|
||||
players = players or game.connected_players
|
||||
data = data or {}
|
||||
if not _popup then return end
|
||||
if not Server or not Server._thread then
|
||||
for _,player in pairs(players) do
|
||||
@@ -107,11 +107,11 @@ function popup._prototype:add_left(obj)
|
||||
self.left = Gui.left(obj)
|
||||
end
|
||||
|
||||
function popup:on_init()
|
||||
function popup.on_init()
|
||||
if loaded_modules['ExpGamingCore.Server'] then Server = require('ExpGamingCore.Server') end
|
||||
end
|
||||
|
||||
function popup:on_post()
|
||||
function popup.on_post()
|
||||
popup._prototype.close = Gui.inputs.add{
|
||||
type='button',
|
||||
name='popup-close',
|
||||
|
||||
@@ -7,7 +7,7 @@ local Server = require('ExpGamingCore.Server')
|
||||
Server.add_module_to_interface('ExpGui','ExpGamingCore.Gui')
|
||||
|
||||
--- Adds a server thread that allows the camera follows to be toggled off and on
|
||||
return function(event)
|
||||
return function()
|
||||
Server.new_thread{
|
||||
name='camera-follow',
|
||||
data={cams={},cam_index=1,players={}}
|
||||
|
||||
@@ -53,30 +53,30 @@ local input_test = Gui.inputs.add_button('test-inputs','Try RMB','alt,ctrl,shift
|
||||
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)
|
||||
}):on_event('error',function(err) game.print('this is error handling') end)
|
||||
|
||||
local elem_test = Gui.inputs.add_elem_button('test-elem','item','Testing Elems',function(player,element,elem)
|
||||
player_return(elem.type..' '..elem.value,nil,player)
|
||||
end)
|
||||
|
||||
local check_test = Gui.inputs.add_checkbox('test-check',false,'Cheat Mode',function(player,parent)
|
||||
return game.players[parent.player_index].cheat_mode
|
||||
end,function(player,element)
|
||||
player.cheat_mode = true
|
||||
local check_test = Gui.inputs.add_checkbox('test-check',false,'Cheat Mode',function(player,parent)
|
||||
return game.players[parent.player_index].cheat_mode
|
||||
end,function(player,element)
|
||||
player.cheat_mode = true
|
||||
end,function(player,element)
|
||||
player.cheat_mode = false
|
||||
end)
|
||||
|
||||
local radio_test = Gui.inputs.add_checkbox('test-radio',true,'Kill Self',function(player,parent)
|
||||
local radio_test = Gui.inputs.add_checkbox('test-radio',true,'Kill Self',function(player,parent)
|
||||
return false
|
||||
end,function(player,element)
|
||||
end,function(player,element)
|
||||
if player.character then player.character.die() end
|
||||
Gui.inputs.reset_radio(element.parent['test-radio-reset'])
|
||||
end)
|
||||
|
||||
local radio_test_reset = Gui.inputs.add_checkbox('test-radio-reset',true,'Reset Kill Self',function(player,parent)
|
||||
local radio_test_reset = Gui.inputs.add_checkbox('test-radio-reset',true,'Reset Kill Self',function(player,parent)
|
||||
return not parent['test-radio'].state
|
||||
end,function(player,element)
|
||||
end,function(player,element)
|
||||
Gui.inputs.reset_radio(element.parent['test-radio'])
|
||||
end)
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ local Game = require('FactorioStdLib.Game')
|
||||
local mod_gui = require('mod-gui')
|
||||
local Gui = require('ExpGamingCore.Gui')
|
||||
local order_config = require(module_path..'/order_config')
|
||||
local Role -- this is optional and is hanndled by it being present, it is loaded on init
|
||||
local Role -- this is optional and is handled by it being present, it is loaded on init
|
||||
|
||||
local toolbar = {}
|
||||
|
||||
@@ -52,7 +52,7 @@ end
|
||||
-- @usage toolbar.draw(1)
|
||||
-- @param player the player to draw the tool bar of
|
||||
function toolbar.draw(player)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then return end
|
||||
local toolbar_frame = mod_gui.get_button_flow(player)
|
||||
toolbar_frame.clear()
|
||||
@@ -81,7 +81,7 @@ function toolbar.draw(player)
|
||||
end
|
||||
end
|
||||
|
||||
function toolbar:on_init()
|
||||
function toolbar.on_init()
|
||||
if loaded_modules['ExpGamingCore.Role'] then Role = require('ExpGamingCore.Role') end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
Role.add_flag('is_default') -- this must be included in atleast one role
|
||||
Role.add_flag('is_default') -- this must be included in at least one role
|
||||
Role.add_flag('is_root',function(player,state)
|
||||
if state then game.print('--- !!!ALERT!!! --- '..player.name..' has been given a role with ROOT ACCESS --- !!!ALERT!!! ---') end
|
||||
if player.character then player.character.destructible = not state end
|
||||
end) -- the SERVER role will have root but you may assign this to other roles
|
||||
-- the two above and used internaly and should NOT have their names changed and should NOT be removed but the state function MAY be changed
|
||||
-- the two above and used internally and should NOT have their names changed and should NOT be removed but the state function MAY be changed
|
||||
Role.add_flag('is_antiroot',function(player,state) if player.character then player.character.destructible = not state end end) -- not required but setting true will disallow everything for that role
|
||||
Role.add_flag('is_admin',function(player,state) player.admin = state end) -- highly recomented but not required
|
||||
Role.add_flag('is_admin',function(player,state) player.admin = state end) -- highly recommenced but not required
|
||||
Role.add_flag('is_spectator',function(player,state) player.spectator = state end)
|
||||
Role.add_flag('is_jail',function(player,state) if player.character then player.character.active = not state end end)
|
||||
Role.add_flag('allow_afk_kick')
|
||||
Role.add_flag('is_donator')
|
||||
Role.add_flag('is_timed')
|
||||
Role.add_flag('is_varified')
|
||||
Role.add_flag('is_verified')
|
||||
Role.add_flag('not_reportable')
|
||||
|
||||
-- Root
|
||||
@@ -64,7 +64,7 @@ Role{
|
||||
colour={r=233,g=63,b=233},
|
||||
is_admin=true,
|
||||
is_spectator=true,
|
||||
is_varified=true,
|
||||
is_verified=true,
|
||||
not_reportable=true,
|
||||
allow={
|
||||
['game-settings']=true,
|
||||
@@ -80,7 +80,7 @@ Role{
|
||||
colour={r=0,g=170,b=0},
|
||||
is_admin=true,
|
||||
is_spectator=true,
|
||||
is_varified=true,
|
||||
is_verified=true,
|
||||
not_reportable=true,
|
||||
allow={
|
||||
['set-home']=true,
|
||||
@@ -97,7 +97,7 @@ Role{
|
||||
group='Admin',
|
||||
colour={r=0,g=196,b=137},
|
||||
is_spectator=true,
|
||||
is_varified=true,
|
||||
is_verified=true,
|
||||
not_reportable=true,
|
||||
allow={
|
||||
['go-to']=true,
|
||||
@@ -173,7 +173,7 @@ Role{
|
||||
group='HiMember',
|
||||
colour={r=140,g=120,b=200},
|
||||
is_timed=true,
|
||||
is_varified=true,
|
||||
is_verified=true,
|
||||
allow_afk_kick=true,
|
||||
time=600, -- 10 hours
|
||||
allow={
|
||||
@@ -190,13 +190,13 @@ Role{
|
||||
tag='[Member]',
|
||||
group='Member',
|
||||
colour={r=24,g=172,b=188},
|
||||
is_varified=true,
|
||||
is_verified=true,
|
||||
allow_afk_kick=true,
|
||||
allow={
|
||||
['edit-tasklist']=true,
|
||||
['make-warp']=true,
|
||||
['nuke']=true,
|
||||
['varified']=true
|
||||
['verified']=true
|
||||
}
|
||||
}
|
||||
Role{
|
||||
|
||||
@@ -2,17 +2,18 @@
|
||||
-- @module ExpGamingCore.Role@4.0.0
|
||||
-- @author Cooldude2606
|
||||
-- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE
|
||||
-- @alais Role
|
||||
-- @alias Role
|
||||
|
||||
-- Module Require
|
||||
local Group = require('ExpGamingCore.Group')
|
||||
local Game = require('FactorioStdLib.Game')
|
||||
|
||||
-- Local Varibles
|
||||
-- Local Variables
|
||||
local role_change_event_id = script.generate_event_name('on_role_change')
|
||||
local RoleGlobal
|
||||
|
||||
-- Module Define
|
||||
local _RoleSelfReference
|
||||
local module_verbose = false
|
||||
local Role = {
|
||||
_prototype={},
|
||||
@@ -24,7 +25,7 @@ local Role = {
|
||||
roles=setmetatable({},{
|
||||
__index=table.autokey,
|
||||
__newindex=function(tbl,key,value)
|
||||
rawset(tbl,key,Role.define(value))
|
||||
rawset(tbl,key,_RoleSelfReference.define(value))
|
||||
end
|
||||
}),
|
||||
on_init=function(self)
|
||||
@@ -57,10 +58,11 @@ local Role = {
|
||||
else error('Invalid roles, no roles to load.') end
|
||||
end
|
||||
}
|
||||
_RoleSelfReference=Role
|
||||
|
||||
-- Global Define
|
||||
local global = global{
|
||||
change_chache_length=15,
|
||||
change_cache_length=15,
|
||||
changes={},
|
||||
latest_change={},
|
||||
preassign={},
|
||||
@@ -79,7 +81,7 @@ function Role.set_preassign(tbl) if game then global.preassign = tbl else Role.p
|
||||
-- @usage Role{name='Root',short_hand='Root',tag='[Root]',group='Root',colour={r=255,b=255,g=255},is_root=true,allow={}} -- returns new role
|
||||
-- @tparam table obj contains the strings: name,short_hand,tag a table called allow a table called colour and a pointer to a permission group
|
||||
-- @treturn Role the role which has been made
|
||||
function Role.define(obj)
|
||||
function Role.define(obj)
|
||||
if not type_error(game,nil,'Cant define Role during runtime.') then return end
|
||||
if not type_error(obj.name,'string','Role creation is invalid: role.name is not a string') then return end
|
||||
if not is_type(obj.short_hand,'string') then obj.short_hand = obj.name:sub(1,3) end
|
||||
@@ -101,19 +103,19 @@ end
|
||||
--- Used to get the role of a player or the role by name
|
||||
-- @usage Role.get('foo') -- returns group foo
|
||||
-- @usage Role.get(player) -- returns group of player
|
||||
-- @tparam ?LuaPlayer|pointerToPlayer|string mixed can either be the name of the role or a player indenifier
|
||||
-- @tparam ?LuaPlayer|pointerToPlayer|string mixed can either be the name of the role or a player identifier
|
||||
-- @treturn table the group which was found or nil
|
||||
function Role.get(mixed,forceIsRole)
|
||||
local player = game and Game.get_player(mixed)
|
||||
if player == SERVER then return {Role.meta.server} end
|
||||
if not forceIsRole and player then
|
||||
if not forceIsRole and player then
|
||||
local rtn = {}
|
||||
if not global.players[player.index] then return Role.meta.default and {Role.meta.default} or {} end
|
||||
for _,role in pairs(global.players[player.index]) do table.insert(rtn,Role.get(role)) end
|
||||
return rtn
|
||||
elseif is_type(mixed,'table') and mixed.group then return mixed
|
||||
elseif is_type(mixed,'string') then return Role.roles[mixed]
|
||||
elseif is_type(mixed,'number') then
|
||||
elseif is_type(mixed,'number') then
|
||||
for _,role in pairs(Role.roles) do
|
||||
if role.index == mixed then return role end
|
||||
end
|
||||
@@ -124,25 +126,25 @@ end
|
||||
-- @usage Role.assign(player,'Root')
|
||||
-- @usage Role.assign(player,{'Root','Foo'})
|
||||
-- @tparam ?LuaPlayer|pointerToPlayer player the player to assign the role to
|
||||
-- @tparam ?string|role|table the role to add the player to, if its a table then it will act recursly though the table
|
||||
-- @tparam ?string|role|table the role to add the player to, if its a table then it will act recursively though the table
|
||||
-- @tparam[opt='<server>'] ?LuaPlayer|pointerToPlayer by_player the player who assigned the roles to the player
|
||||
-- @tparam[opt] table batch this is used internally to provent multiple event calls, conatins {role_index_in_batch,batch}
|
||||
-- @tparam[opt] table batch this is used internally to prevent multiple event calls, contains {role_index_in_batch,batch}
|
||||
-- @treturn boolean was the player assigned the roles
|
||||
function Role.assign(player,role,by_player,batch)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player #1 given to Role.assign.',2) return end
|
||||
verbose('Assigning Roles: '..serpent.line(role)..' to: '..player.name)
|
||||
-- this loops over a table of role if given; will return if ipairs returns, else will asume it was ment to be a role and error
|
||||
if is_type(role,'table') and not role.name then
|
||||
local ctn = 0
|
||||
-- this loops over a table of role if given; will return if ipairs returns, else will assume it was meant to be a role and error
|
||||
if is_type(role,'table') and not role.name then
|
||||
local ctn = 0
|
||||
for n,_role in ipairs(role) do ctn=ctn+1 Role.assign(player,_role,by_player,{n,role}) end
|
||||
if ctn > 0 then if not batch then table.insert(global.changes[player.index],{'assign',role}) global.latest_change = {player.index,'assign',role} end return end
|
||||
if ctn > 0 then if not batch then table.insert(global.changes[player.index],{'assign',role}) global.latest_change = {player.index,'assign',role} end return end
|
||||
end
|
||||
local role = Role.get(role)
|
||||
role = Role.get(role)
|
||||
if not role then error('Invalid role #2 given to Role.assign.',2) return end
|
||||
-- this acts as a way to provent the global table getting too full
|
||||
-- this acts as a way to prevent the global table getting too full
|
||||
if not global.changes[player.index] then global.changes[player.index]={} end
|
||||
if #global.changes[player.index] > global.change_chache_length then table.remove(global.changes[player.index],1) end
|
||||
if #global.changes[player.index] > global.change_cache_length then table.remove(global.changes[player.index],1) end
|
||||
if not batch then table.insert(global.changes[player.index],{'assign',role.name}) global.latest_change = {player.index,'assign',role.name} end
|
||||
return role:add_player(player,by_player,batch)
|
||||
end
|
||||
@@ -150,31 +152,31 @@ end
|
||||
--- Used to remove a player from a role(s)
|
||||
-- @usage Role.unassign(player,'Root')
|
||||
-- @tparam ?LuaPlayer|pointerToPlayer player the player to unassign the role to
|
||||
-- @tparam ?string|role|table role the role to remove the player from, if its a table then it will act recursly though the table
|
||||
-- @tparam ?string|role|table role the role to remove the player from, if its a table then it will act recursively though the table
|
||||
-- @tparam[opt='<server>'] ?LuaPlayer|pointerToPlayer by_player the player who unassigned the roles from the player
|
||||
-- @tparam[opt] table batch this is used internally to provent multiple event calls
|
||||
-- @tparam[opt] table batch this is used internally to prevent multiple event calls
|
||||
-- @treturn boolean was the player unassigned the roles
|
||||
function Role.unassign(player,role,by_player,batch)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player #1 given to Role.unassign.',2) return end
|
||||
verbose('Assigning Roles: '..serpent.line(role)..' to: '..player.name)
|
||||
-- this loops over a table of role if given; will return if ipairs returns, else will asume it was ment to be a role and error
|
||||
-- this loops over a table of role if given; will return if ipairs returns, else will assume it was meant to be a role and error
|
||||
if is_type(role,'table') and not role.name then
|
||||
local ctn = 0
|
||||
for n,_role in ipairs(role) do ctn=ctn+1 Role.unassign(player,_role,by_player,{n,role}) end
|
||||
if ctn > 0 then if not batch then table.insert(global.changes[player.index],{'unassign',role}) global.latest_change = {player.index,'unassign',role} end return end
|
||||
end
|
||||
local role = Role.get(role)
|
||||
role = Role.get(role)
|
||||
if not role then error('Invalid role #2 given to Role.unassign.',2) return end
|
||||
if not global.changes[player.index] then global.changes[player.index]={} end
|
||||
-- this acts as a way to provent the global table getting too full
|
||||
if #global.changes[player.index] > global.change_chache_length then table.remove(global.changes[player.index],1) end
|
||||
-- this acts as a way to prevent the global table getting too full
|
||||
if #global.changes[player.index] > global.change_cache_length then table.remove(global.changes[player.index],1) end
|
||||
if not batch then table.insert(global.changes[player.index],{'unassign',role.name}) global.latest_change = {player.index,'unassign',role.name} end
|
||||
return role:remove_player(player,by_player,batch)
|
||||
end
|
||||
|
||||
--- Returns the highest role given in a list, if a player is passed then it returns the highest role of the player
|
||||
-- @usage Role.get_highest{'Root','Admin','Mod'} -- retuns Root (given that root is highest)
|
||||
-- @usage Role.get_highest{'Root','Admin','Mod'} -- returns Root (given that root is highest)
|
||||
-- @usage Role.get_highest(player) -- returns the players highest role
|
||||
-- @tparam ?table|LuaPlayer|pointerToPlayer options table of options or a player
|
||||
-- @treturn role the highest role given in the options
|
||||
@@ -192,19 +194,19 @@ function Role.get_highest(options)
|
||||
return highest
|
||||
end
|
||||
|
||||
--- Uses the change chache to revert changes to players roles
|
||||
--- Uses the change cache to revert changes to players roles
|
||||
-- @usage Role.revert(player) -- reverts the last change to the players roles
|
||||
-- @tparam ?LuaPlayer|pointerToPlayer player the player to revert the changes of
|
||||
-- @tparam[opt] ?LuaPlayer|pointerToPlayer the player who proformed the role revert
|
||||
-- @tparam[opt=1] count the number of reverts to do, if 0 all changes chached are reverted
|
||||
-- @treturn number the number of changes that occured
|
||||
-- @tparam[opt] ?LuaPlayer|pointerToPlayer the player who preformed the role revert
|
||||
-- @tparam[opt=1] count the number of reverts to do, if 0 all changes cached are reverted
|
||||
-- @treturn number the number of changes that occurred
|
||||
function Role.revert(player,by_player,count)
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player #1 given to Role.revert.',2) return end
|
||||
if count and not type_error(count,'number','Invalid argument #2 to Role.revert, count is not a number.') then return end
|
||||
local changes = global.changes[player.index] or {}
|
||||
if #changes == 0 then error('Player has no role changes logged, can not revert.') end
|
||||
local count = count or 1
|
||||
count = count or 1
|
||||
local ctn = 0
|
||||
if count > #changes or count == 0 then count = #changes end
|
||||
for i = 1,count do
|
||||
@@ -220,7 +222,7 @@ end
|
||||
|
||||
--- Adds a flag which can be set on roles; these flags act as a quick way to access general role changes
|
||||
-- @usage Role.add_flag('is_admin',function(player,state) player.admin = state end) -- the function is passed player and if the flag is true or false
|
||||
-- @tparam string flag the name of the falg that is being added
|
||||
-- @tparam string flag the name of the flag that is being added
|
||||
-- @tparam[opt] function callback the function(player,state) which is called when a player loses or gains a flag, if nil no function is called
|
||||
function Role.add_flag(flag,callback)
|
||||
if not type_error(flag,'string','Invalid argument #1 to Role.add_flag, flag is not a string.') then return end
|
||||
@@ -229,7 +231,7 @@ function Role.add_flag(flag,callback)
|
||||
Role.flags[flag] = callback or true
|
||||
end
|
||||
|
||||
--- Checks if a player or role has the requested flag, if player all roles of player are checked (true has pirortiy)
|
||||
--- Checks if a player or role has the requested flag, if player all roles of player are checked (true has priority)
|
||||
-- @usage Role.has_flag(role,'is_admin') -- returns true if this role has is_admin set
|
||||
-- @tparam role|LuaPlayer|pointerToPlayer mixed the player or role that will be tested
|
||||
-- @tparam string flag the flag to test for
|
||||
@@ -253,7 +255,7 @@ function Role.add_action(action)
|
||||
table.insert(Role.actions,action)
|
||||
end
|
||||
|
||||
--- Checks if a player or role is allowed the requested action, if player all roles of player are checked (true has pirortiy)
|
||||
--- Checks if a player or role is allowed the requested action, if player all roles of player are checked (true has priority)
|
||||
-- @usage Role.allowed(role,'foo') -- returns true if this role is allowed 'foo'
|
||||
-- @tparam ?role|LuaPlayer|pointerToPlayer mixed the player or role that will be tested
|
||||
-- @tparam string action the action to test for
|
||||
@@ -269,14 +271,14 @@ function Role.allowed(mixed,action)
|
||||
end
|
||||
|
||||
--- Prints to all roles and players of those roles which are greater than the given role (or if inv then all below)
|
||||
-- @usage Role.print('Admin','Hello, World!') -- returns the number of players who recived the message
|
||||
-- @usage Role.print('Admin','Hello, World!') -- returns the number of players who received the message
|
||||
-- @tparam ?role|string role the role which acts as the turning point of the print (always included regardless of inv value)
|
||||
-- @param rtn the value that will be returned to the players
|
||||
-- @tparam[opt] table colour the colour that you want the message printed in
|
||||
-- @tparam[opt=false] boolean inv true to print to roles below, false to print to roles above
|
||||
-- @treturn number the number of players who recived the message
|
||||
-- @treturn number the number of players who received the message
|
||||
function Role.print(role,rtn,colour,inv)
|
||||
local role = Role.get(role,true)
|
||||
role = Role.get(role,true)
|
||||
if not type_error(role,'table','Invalid argument #1 to Role.print, role is invalid.') then return end
|
||||
if colour and not type_error(colour,'table','Invalid argument #3 to Role.print, colour is not a table.') then return end
|
||||
if inv and not type_error(inv,'boolean','Invalid argument #4 to Role.print, inv is not a boolean.') then return end
|
||||
@@ -293,11 +295,11 @@ function Role.print(role,rtn,colour,inv)
|
||||
return ctn
|
||||
end
|
||||
|
||||
--- Prints all registed roles and there important infomation (debug)
|
||||
--- Prints all registered roles and there important information (debug)
|
||||
-- @tparam[opt] ?role|string the role to print the info of, if nil then all roles are printed in order of power
|
||||
-- @tparam[opt=game.player] ?LuaPlayer|pointerToPlayer the player to print the info to, default the player who ran command
|
||||
function Role.debug_output(role,player)
|
||||
local player = Game.get_player(player) or game.player
|
||||
player = Game.get_player(player) or game.player
|
||||
if not player then error('Invalid player #2 given to Role.debug_output.',2) return end
|
||||
local function _output(_role)
|
||||
local flags = {};for flag in pairs(Role.flags) do if _role:has_flag(flag) then table.insert(flags,flag) end end
|
||||
@@ -306,7 +308,7 @@ function Role.debug_output(role,player)
|
||||
player_return(rtn,_role.colour,player)
|
||||
end
|
||||
if role then
|
||||
local role = Role.get(mixed)
|
||||
role = Role.get(role)
|
||||
if not type_error(role,'table','Invalid argument #1 to Role.print, role is invalid.') then return end
|
||||
_output(role)
|
||||
else for index,_role in pairs(Role.roles) do _output(_role) end end
|
||||
@@ -354,18 +356,18 @@ end
|
||||
-- this is used to create a connected_players table
|
||||
Role._prototype.players_mt = {
|
||||
__call=function(tbl) return tbl.self:get_players(tbl.connected) end,
|
||||
__pairs=function(tbl)
|
||||
local players = tbl.self:get_players(tbl.connected)
|
||||
local function next_pair(tbl,k)
|
||||
k, v = next(players, k)
|
||||
__pairs=function(tbl)
|
||||
local players = tbl.self:get_players(tbl.connected)
|
||||
local function next_pair(tbl,key)
|
||||
local k, v = next(players, key)
|
||||
if v then return k,v end
|
||||
end
|
||||
return next_pair, players, nil
|
||||
end,
|
||||
__ipairs=function(tbl)
|
||||
local players = tbl.self:get_players(tbl.connected)
|
||||
local function next_pair(tbl,k)
|
||||
k, v = next(players, k)
|
||||
local players = tbl.self:get_players(tbl.connected)
|
||||
local function next_pair(tbl,key)
|
||||
local k, v = next(players, key)
|
||||
if v then return k,v end
|
||||
end
|
||||
return next_pair, players, nil
|
||||
@@ -374,10 +376,10 @@ Role._prototype.players_mt = {
|
||||
|
||||
|
||||
--- Prints a message to all players who have this role
|
||||
-- @usage role:print('Hello, World!') -- retuns number of players who recived the message
|
||||
-- @usage role:print('Hello, World!') -- returns number of players who received the message
|
||||
-- @param rtn the message to print to the players
|
||||
-- @tparam[opt] table colour the colour to print the message in
|
||||
-- @treturn number the number of players who recived the message
|
||||
-- @treturn number the number of players who received the message
|
||||
function Role._prototype:print(rtn,colour)
|
||||
if not self_test(self,'role','print') then return end
|
||||
if colour and not type_error(colour,'table','Invalid argument #2 to Role.print, colour is not a table.') then return end
|
||||
@@ -387,8 +389,8 @@ function Role._prototype:print(rtn,colour)
|
||||
end
|
||||
|
||||
--- Returns a table that describes all the permissions and which this role is allowed
|
||||
-- @usage role:get_permissions() -- retuns table of permissions
|
||||
-- @treturn table a table of permisions, only includes ones which were defined with Role.add_action
|
||||
-- @usage role:get_permissions() -- returns table of permissions
|
||||
-- @treturn table a table of permissions, only includes ones which were defined with Role.add_action
|
||||
function Role._prototype:get_permissions()
|
||||
if not self_test(self,'role','get_permissions') then return end
|
||||
local rtn = {}
|
||||
@@ -400,12 +402,12 @@ end
|
||||
-- @usage role:add_player(player)
|
||||
-- @tparam ?LuaPlayer|PointerToPlayer player the player to add
|
||||
-- @tparam[opt] ?LuaPlayer|PointerToPlayer by_player the player who ran the command
|
||||
-- @tparam[opt] table batch this is used internally to provent multiple event calls
|
||||
-- @tparam[opt] table batch this is used internally to prevent multiple event calls
|
||||
function Role._prototype:add_player(player,by_player,batch)
|
||||
if not self_test(self,'role','add_player') then return end
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player #1 given to role:add_player.',2) return end
|
||||
local by_player = Game.get_player(by_player) or SERVER
|
||||
by_player = Game.get_player(by_player) or SERVER
|
||||
if not global.roles[self.name] then global.roles[self.name] = {} end
|
||||
if not global.players[player.index] then global.players[player.index] = {} end
|
||||
local highest = Role.get_highest(player) or Role.meta.default
|
||||
@@ -429,12 +431,12 @@ end
|
||||
-- @usage role:remove_player(player)
|
||||
-- @tparam ?LuaPlayer|PointerToPlayer player the player to remove
|
||||
-- @tparam[opt] ?LuaPlayer|PointerToPlayer by_player the player who ran the command
|
||||
-- @tparam[opt] table batch this is used internally to provent multiple event calls
|
||||
-- @tparam[opt] table batch this is used internally to prevent multiple event calls
|
||||
function Role._prototype:remove_player(player,by_player,batch)
|
||||
if not self_test(self,'role','add_player') then return end
|
||||
local player = Game.get_player(player)
|
||||
player = Game.get_player(player)
|
||||
if not player then error('Invalid player #1 given to role:remove_player.',2) return end
|
||||
local by_player = Game.get_player(by_player) or SERVER
|
||||
by_player = Game.get_player(by_player) or SERVER
|
||||
if not global.roles[self.name] then global.roles[self.name] = {} end
|
||||
if not global.players[player.index] then global.players[player.index] = {} end
|
||||
local highest = Role.get_highest(player) or Role.meta.default
|
||||
@@ -458,13 +460,13 @@ end
|
||||
|
||||
-- Event Handlers Define
|
||||
script.on_event(role_change_event_id,function(event)
|
||||
-- varible init
|
||||
-- variable init
|
||||
local player = Game.get_player(event)
|
||||
local by_player = Game.get_player(event.by_player_index) or SERVER
|
||||
local role = Role.get(event.role_name)
|
||||
local role = Role.get(event.role_name)
|
||||
local highest = Role.get_highest(player)
|
||||
if not highest then Role.meta.default:add_player(player) highest = Role.meta.default end
|
||||
-- gets the falgs the player currently has
|
||||
-- gets the flags the player currently has
|
||||
for flag,callback in pairs(Role.flags) do if is_type(callback,'function') then callback(player,Role.has_flag(player,flag)) end end
|
||||
-- assign new tag and group of highest role
|
||||
Group.assign(player,highest.group)
|
||||
@@ -476,7 +478,7 @@ script.on_event(role_change_event_id,function(event)
|
||||
-- send a message to other players
|
||||
if event.batch_index == 1 then
|
||||
local names = {}
|
||||
for _,name in pairs(event.batch) do local role = Role.get(name) if role then table.insert(names,role.name) end end
|
||||
for _,name in pairs(event.batch) do local next_role = Role.get(name) if next_role then table.insert(names,next_role.name) end end
|
||||
if event.effect == 'assign' then
|
||||
if not role.is_jail then player.play_sound{path='utility/achievement_unlocked'} end
|
||||
game.print{'ExpGamingCore-Role.default-print',{'ExpGamingCore-Role.assign',player.name,table.concat(names,', '),by_player.name}}
|
||||
@@ -524,4 +526,4 @@ end)
|
||||
|
||||
-- Module Return
|
||||
-- calling will attempt to define a new role
|
||||
return setmetatable(Role,{__call=function(tbl,...) return tbl.define(...) end})
|
||||
return setmetatable(Role,{__call=function(tbl,...) return tbl.define(...) end})
|
||||
@@ -43,7 +43,7 @@ end
|
||||
Sync.add_update('roles',function()
|
||||
if not game then return {'Offline'} end
|
||||
local _rtn = {}
|
||||
for name,role in pairs(Role.roles) do
|
||||
for _,role in pairs(Role.roles) do
|
||||
local players = role:get_players()
|
||||
local _players = {}
|
||||
for k,player in pairs(players) do _players[k] = player.name end
|
||||
@@ -57,20 +57,20 @@ end)
|
||||
|
||||
-- Adds a caption to the info gui that shows the rank given to the player
|
||||
if Sync.add_to_gui then
|
||||
Sync.add_to_gui(function(player,frame)
|
||||
Sync.add_to_gui(function(player)
|
||||
local names = {}
|
||||
for _,role in pairs(Role.get(player)) do table.insert(names,role.name) end
|
||||
return 'You have been assigned the roles: '..table.concat(names,', ')
|
||||
end)
|
||||
end
|
||||
|
||||
-- adds a discord emit for rank chaning
|
||||
-- adds a discord emit for rank changing
|
||||
script.on_event('on_role_change',function(event)
|
||||
local role = Role.get(event.role_name)
|
||||
local player = Game.get_player(event)
|
||||
local by_player = Game.get_player(event.by_player_index) or SERVER
|
||||
if role.is_jail and RoleGlobal.last_change[1] ~= player.index then
|
||||
Sync.emit_embeded{
|
||||
Sync.emit_embedded{
|
||||
title='Player Jail',
|
||||
color=Color.to_hex(defines.textcolor.med),
|
||||
description='There was a player jailed.',
|
||||
|
||||
@@ -17,7 +17,7 @@ local module_verbose = false --true|false
|
||||
-- @field tick an index for threads which will run every tick (contains uuids)
|
||||
-- @field timeout an index for threads which will timeout (contains uuids)
|
||||
-- @field events an index of threads based on event ids (contains uuids)
|
||||
-- @field paused an index of pasued threads (contains uuids)
|
||||
-- @field paused an index of paused threads (contains uuids)
|
||||
-- @field named a name index for thread uuids
|
||||
-- @field print_to contains players that event details will be printed to
|
||||
-- @field uuid contains the random number generator for the uuid system
|
||||
@@ -39,7 +39,7 @@ local global = global{
|
||||
-- @treturn string the new uuid
|
||||
Server.uuid = add_metatable({},function()
|
||||
-- when it is called as a function
|
||||
local uuid = 0
|
||||
local uuid
|
||||
if game then
|
||||
global.uuid=global.uuid+1
|
||||
uuid=global.uuid+pre_load_uuid
|
||||
@@ -68,8 +68,8 @@ Server.threads = setmetatable({},{
|
||||
__newindex=function(tbl,key,value) rawset(global.all,key,value) end,
|
||||
__pairs=function(tbl)
|
||||
local tbl = global.all
|
||||
local function next_pair(tbl,k)
|
||||
k, v = next(tbl, k)
|
||||
local function next_pair(tbl,key)
|
||||
local k, v = next(tbl, key)
|
||||
if type(v) ~= nil and k ~= '_n' then return k,v
|
||||
else return next(tbl, k) end
|
||||
end
|
||||
@@ -79,15 +79,15 @@ Server.threads = setmetatable({},{
|
||||
|
||||
--- Generates a new thread object
|
||||
-- @usage Server.new_thread{name='foo',data={}}
|
||||
-- @tparam table obj the atributes to give to the thread
|
||||
-- @tparam table obj the attributes to give to the thread
|
||||
-- @treturn Server._thread the new thread created
|
||||
function Server.new_thread(obj) return Server._thread:create(obj) end
|
||||
|
||||
--- Used to get a thread via uuid or name (if one is assied)
|
||||
--- Used to get a thread via uuid or name (if one is assigned)
|
||||
-- @usage Server.get_thread('decon') -- return thread
|
||||
-- @param mixed either a uuid or the name given to a thread
|
||||
-- @treturn[1] Server._thread the thread by that name or uuid
|
||||
-- @treturn[2] boolean if false is returned then no thread existes
|
||||
-- @treturn[2] boolean if false is returned then no thread exists
|
||||
function Server.get_thread(mixed)
|
||||
local threads = global
|
||||
if threads.named[mixed] then return threads.all[threads.named[mixed]]
|
||||
@@ -99,7 +99,7 @@ end
|
||||
--- Adds a thread into the resolve queue, can be used to lower lag
|
||||
-- @usage Server.queue_thread(thread) -- return true/false
|
||||
-- @tparam Server._thread thread_to_queue the thread to be added to the queue, must be open and have a on_resolve function
|
||||
-- @treturn boolean was it added successfuly
|
||||
-- @treturn boolean was it added successfully
|
||||
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
|
||||
@@ -109,15 +109,15 @@ end
|
||||
|
||||
--- Closes all active threads, can use force if it causes errors
|
||||
-- @usage Server.close_all_threads() -- asks all threads to close
|
||||
-- @usage Server.close_all_threads(true) -- forcefuly close all threads
|
||||
-- @tparam bolean with_force use force when closing
|
||||
-- @usage Server.close_all_threads(true) -- forcefully close all threads
|
||||
-- @tparam boolean with_force use force when closing
|
||||
function Server.close_all_threads(with_force)
|
||||
if not with_force then
|
||||
for uuid,thread in pairs(Server.threads) do thread:close() end
|
||||
else global.Server(true) end -- idk why you cant just use global even when global is defined at the top to be over ridren
|
||||
else global.Server(true) end -- idk why you cant just use global even when global is defined at the top to be overridden
|
||||
end
|
||||
|
||||
--- Runs all the theads which have opened with an on_tick event
|
||||
--- Runs all the threads which have opened with an on_tick event
|
||||
-- @usage Server.run_tick_threads()
|
||||
function Server.run_tick_threads()
|
||||
table.each(global.tick,function(uuid)
|
||||
@@ -146,8 +146,8 @@ end
|
||||
-- @tparam ?name|index event the event that info will be returned fo
|
||||
-- @tparam[opt=toggle] boolean state will info be returned, nil to toggle current state
|
||||
function Server._thread_debuger(player,event,state)
|
||||
local player = Game.get_player(player)
|
||||
local event = tonumber(event) or Manager.event.names[event]
|
||||
player = Game.get_player(player)
|
||||
event = tonumber(event) or Manager.event.names[event]
|
||||
local print_to = global.print_to
|
||||
print_to[player.index] = print_to[player.index] or {}
|
||||
if state then print_to[player.index][event] = state
|
||||
@@ -155,12 +155,12 @@ function Server._thread_debuger(player,event,state)
|
||||
else print_to[player.index][event] = true end
|
||||
end
|
||||
|
||||
--- Calles all threads on a certain game event (used with script.on_event)
|
||||
--- Calls all threads on a certain game event (used with script.on_event)
|
||||
-- @local Server._thread_handler
|
||||
-- @usage script.on_event(defines.events,Server._thread_handler) -- adds this handler
|
||||
-- @tparam table event the event that is called
|
||||
function Server._thread_handler(event)
|
||||
-- returns to players who have set _thread_debuger to trye
|
||||
-- returns to players who have set _thread_debuger to true
|
||||
table.each(global.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
|
||||
@@ -189,7 +189,7 @@ end
|
||||
--- 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
|
||||
-- @treturn boolean if the handler was added
|
||||
function Server.add_thread_handler(event)
|
||||
if not is_type(event,'number') then return false end
|
||||
local threads = global
|
||||
@@ -204,9 +204,9 @@ end
|
||||
|
||||
--- Acts as a bypass for running functions, can accept a string
|
||||
-- @usage Server.interface('local x = 1+1 print(x) return x') -- return 2
|
||||
-- @usage Server.interface('local x = 1+1 print(x)',true) -- will creat a thread to run as root (this is the bypass)
|
||||
-- @usage Server.interface('local x = 1+1 print(x)',true) -- will create a thread to run as root (this is the bypass)
|
||||
-- @tparam ?string|function callback function to be ran
|
||||
-- @tparam[opt] ?Server._thread|true use_thread run the command on a premade thread or let it make its own
|
||||
-- @tparam[opt] ?Server._thread|true use_thread run the command on a pre-made thread or let it make its own
|
||||
-- @tparam[opt] table env run the env to run the command in must have _env key as true to be
|
||||
-- @param[opt] ... any args you want to pass to the function
|
||||
-- @treturn[1] boolean if no thread then it will return a true for the success
|
||||
@@ -219,15 +219,15 @@ function Server.interface(callback,use_thread,env,...)
|
||||
if use_thread == true then use_thread = Server.new_thread{data={callback,env,...}} end
|
||||
-- creates the resolve function for the thread
|
||||
use_thread:on_event('resolve',function(thread)
|
||||
local callback = table.remove(thread.data,1)
|
||||
callback = is_type(callback,'function') and callback or loadstring(callback)
|
||||
local env = table.remove(thread.data,1)
|
||||
if is_type(env,'table') and not is_type(env.__self,'userdata') and env._env == true then
|
||||
local success, err = Manager.sandbox(callback,env,unpack(thread.data))
|
||||
local thread_callback = table.remove(thread.data,1)
|
||||
thread_callback = is_type(thread_callback,'function') and thread_callback or loadstring(thread_callback)
|
||||
local thread_env = table.remove(thread.data,1)
|
||||
if is_type(thread_env,'table') and not is_type(thread_env.__self,'userdata') and thread_env._env == true then
|
||||
local success, err = Manager.sandbox(thread_callback,thread_env,unpack(thread.data))
|
||||
if not success then error(err) end
|
||||
return err
|
||||
else
|
||||
local success, err = Manager.sandbox(callback,{},env,unpack(thread.data))
|
||||
local success, err = Manager.sandbox(thread_callback,{},env,unpack(thread.data))
|
||||
if not success then error(err) end
|
||||
return err
|
||||
end
|
||||
@@ -254,13 +254,13 @@ function Server.interface(callback,use_thread,env,...)
|
||||
end
|
||||
end
|
||||
|
||||
--- The class for the server threads, allows abbilty to run async function
|
||||
--- The class for the server threads, allows ability to run async function
|
||||
-- @type Thread
|
||||
-- @alias Server._thread
|
||||
-- @field name the name that is given to the thread, use for easy later indexing
|
||||
-- @field timeout the time in ticks that the thread will have before it times out
|
||||
-- @field reopen when true the thread will reopen itself untill set to false, combine with timeout to create a long on_nth_tick wait
|
||||
-- @field data any data that the thread will beable to access
|
||||
-- @field reopen when true the thread will reopen itself until set to false, combine with timeout to create a long on_nth_tick wait
|
||||
-- @field data any data that the thread will be able to access
|
||||
Server._thread = {}
|
||||
|
||||
local _env_metatable = {
|
||||
@@ -271,15 +271,15 @@ local _env_metatable = {
|
||||
-- @usage new_thread = thread:create()
|
||||
-- @tparam[opt={}] table obj all values are opt {timeout=int,name=str,data=any}
|
||||
-- @treturn Server._thread the new thread object
|
||||
function Server._thread:create(obj)
|
||||
local obj = obj or {}
|
||||
function Server._thread.create(obj)
|
||||
obj = obj or {}
|
||||
setmetatable(obj,{__index=Server._thread})
|
||||
obj.uuid = tostring(Server.uuid)
|
||||
-- creates a varible containg all upvalus for the thread (stops desyncs)
|
||||
-- creates a variable containing all upvalus for the thread (stops desyncs)
|
||||
obj._env = get_upvalues(2)
|
||||
obj._env._env = true
|
||||
obj._env._ENV = nil -- provents infinte recusion
|
||||
-- removes any modules from the _env to save space in global (less time to serizle)
|
||||
obj._env._ENV = nil -- prevents infinite recursion
|
||||
-- removes any modules from the _env to save space in global (less time to serialize)
|
||||
obj._env._modules = {}
|
||||
for name,value in pairs(obj._env) do if is_type(value,'table') and value._moduleName and loaded_modules[value._moduleName] == value then obj._env._modules[name] = value._moduleName obj._env[name] = nil end end
|
||||
setmetatable(obj._env,_env_metatable)
|
||||
@@ -288,21 +288,21 @@ function Server._thread:create(obj)
|
||||
return obj
|
||||
end
|
||||
|
||||
--- Opens and queses a thread
|
||||
--- Opens and queues a thread
|
||||
-- @usage Server._thread:queue() -- returns true/false
|
||||
-- @treturn boolean was the thread queued successfuly
|
||||
-- @treturn boolean was the thread queued successfully
|
||||
-- @see Server.queue_thread
|
||||
function Server._thread:queue()
|
||||
self:open()
|
||||
return Server.queue_thread(self)
|
||||
end
|
||||
|
||||
--- Test if the thread has all requied parts
|
||||
--- Test if the thread has all required parts
|
||||
-- @usage if thread:valid() then end -- basic test for valid
|
||||
-- @tparam[opt=false] bolean skip_location_check true to skip the location checking
|
||||
-- @tparam[opt=false] boolean skip_location_check true to skip the location checking
|
||||
-- @treturn boolean is the thread valid
|
||||
function Server._thread:valid(skip_location_check)
|
||||
local skip_location_check = skip_location_check or false
|
||||
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(global.all[self.uuid],'table') and
|
||||
@@ -314,34 +314,34 @@ function Server._thread:valid(skip_location_check)
|
||||
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
|
||||
-- all above must be true to be vaild, must accept nil and function
|
||||
-- all above must be true to be valid, must accept nil and function
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
--- Opens the thread; indexs this thread in the global index
|
||||
--- Opens the thread; indexes this thread in the global index
|
||||
-- @usage thread:open() -- return true
|
||||
-- @treturn bolean if the thread was opened successfuly
|
||||
-- @treturn boolean if the thread was opened successfully
|
||||
function Server._thread:open()
|
||||
-- if the thread is valid and not already opended
|
||||
-- if the thread is valid and not already opened
|
||||
if not self:valid(true) or self.opened then return false end
|
||||
local uuid = self.uuid
|
||||
-- sets the thread to open, this is the tick it was opened
|
||||
self.opened = game.tick
|
||||
-- creats the global index
|
||||
-- creates the global index
|
||||
global.all[uuid] = global.all[uuid] or self
|
||||
global.all._n = global.all._n+1
|
||||
-- indexs the thread in other places if it has more function
|
||||
-- indexes the thread in other places if it has more function
|
||||
-- if it was paused before (ie did not run any events) then the index is removed from the paused index
|
||||
if global.paused[self.name] then global.paused[self.name] = nil end
|
||||
-- if it has a timeout or on_tick handler then it is indexed in those indexs
|
||||
-- if it has a timeout or on_tick handler then it is indexed in those indexes
|
||||
if is_type(self.timeout,'number') then table.insert(global.timeout,uuid) end
|
||||
if is_type(self._tick,'function') then table.insert(global.tick,uuid) end
|
||||
-- if the thread is given a name then a index from the name to uuid is made
|
||||
if is_type(self.name,'string') then global.named[self.name] = global.named[self.name] or self.uuid end
|
||||
-- if there are event handlers then it will loop over them and add them to the event index
|
||||
if is_type(self._events,'table') then
|
||||
if is_type(self._events,'table') then
|
||||
table.each(self._events,function(callback,event,global,uuid)
|
||||
-- cant be used V
|
||||
--Server.add_thread_handler(event)
|
||||
@@ -354,7 +354,7 @@ function Server._thread:open()
|
||||
return true
|
||||
end
|
||||
|
||||
--- Inverse of thread:open() - Removes all indexs to this thread, most cases this will cause it to become inassible
|
||||
--- Inverse of thread:open() - Removes all indexes to this thread, most cases this will cause it to become inaccessible
|
||||
-- @usage thread:close() -- return true
|
||||
-- @treturn boolean if the thread had a on_close function
|
||||
function Server._thread:close()
|
||||
@@ -363,17 +363,17 @@ function Server._thread:close()
|
||||
-- if there is a call to the threads on close event, will later return true
|
||||
if is_type(self._close,'function') then Manager.sandbox(self._close,self._env,self) _return = true end
|
||||
-- will search every possible location for this thread and remove it
|
||||
local value,key = table.find(global.queue,function(v,k,uuid) return v == uuid end,uuid)
|
||||
local _,key = table.find(global.queue,function(v,k,uuid) return v == uuid end,uuid)
|
||||
if key then table.remove(global.queue,key) end -- queue
|
||||
local value,key = table.find(global.timeout,function(v,k,uuid) return v == uuid end,uuid)
|
||||
_,key = table.find(global.timeout,function(v,k,uuid) return v == uuid end,uuid)
|
||||
if key then table.remove(global.timeout,key) end -- timeouts
|
||||
local value,key = table.find(global.tick,function(v,k,uuid) return v == uuid end,uuid)
|
||||
_,key = table.find(global.tick,function(v,k,uuid) return v == uuid end,uuid)
|
||||
if key then table.remove(global.tick,key) end -- on_tick
|
||||
-- then will look for it in the event handlers and remove it if found
|
||||
if is_type(self._events,'table') then
|
||||
table.each(self._events,function(callback,event)
|
||||
if global.events[event] then
|
||||
local value,key = table.find(global.events[event],function(v,k,uuid) return v == uuid end,uuid)
|
||||
local _,key = table.find(global.events[event],function(v,k,uuid) return v == uuid end,uuid)
|
||||
if key then table.remove(global.events[event],key) end
|
||||
-- cant be used V
|
||||
--if #global.events[event] == 0 then Event.remove(event,Server.game_event) global.events[event] = nil end
|
||||
@@ -382,9 +382,9 @@ function Server._thread:close()
|
||||
end
|
||||
-- sets the thread to closed
|
||||
self.opened=nil
|
||||
-- unless the thread has a name or is assied to be reopened
|
||||
-- unless the thread has a name or is assigned to be reopened
|
||||
if self.reopen == true then self:open() else
|
||||
-- if it has a name but not assied to reopen then it will become 'pasued'
|
||||
-- if it has a name but not assigned to reopen then it will become 'paused'
|
||||
if is_type(self.name,'string') then global.paused[self.name]=self.uuid
|
||||
-- else it will just be wiped from the global index
|
||||
else global.all[uuid] = nil global.all._n = global.all._n-1 end
|
||||
@@ -397,19 +397,19 @@ 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
|
||||
-- @treturn boolean true if the thread called on_success or on_error
|
||||
function Server._thread:resolve(...)
|
||||
local _return = false
|
||||
-- checks if the resolve haddler is still present
|
||||
if is_type(self._resolve,'function') then
|
||||
-- checks if the resolve handler is still present
|
||||
if is_type(self._resolve,'function') then
|
||||
local success, err = Manager.sandbox(self._resolve,self._env,self,...)
|
||||
if success then
|
||||
-- if it was successful then it will attemp to call the success handler
|
||||
-- if it was successful then it will attempt to call the success handler
|
||||
if is_type(self._success,'function') then
|
||||
-- interface is used as a way to delay the success call till the next tick
|
||||
Server.interface(function(thread,err)
|
||||
local success, err = Manager.sandbox(thread._success,thread._env,thread,err)
|
||||
if not success then thread:error(err) end
|
||||
local _success, _err = Manager.sandbox(thread._success,thread._env,thread,err)
|
||||
if not _success then thread:error(_err) end
|
||||
end,true,self,err)
|
||||
-- later returns true if there was a call to the success handler
|
||||
_return = true
|
||||
@@ -422,9 +422,9 @@ function Server._thread:resolve(...)
|
||||
return _return
|
||||
end
|
||||
|
||||
--- Checks the timeout on a thread - if timed out then it calles on_timeout and closes
|
||||
--- Checks the timeout on a thread - if timed out then it calls on_timeout and closes
|
||||
-- @usage thread:check_timeout() -- return true
|
||||
-- @treturn bolean if the thread timed out
|
||||
-- @treturn boolean if the thread timed out
|
||||
function Server._thread:check_timeout()
|
||||
local _return = false
|
||||
-- makes sure the thread is still valid
|
||||
@@ -436,7 +436,7 @@ function Server._thread:check_timeout()
|
||||
Manager.sandbox(self._timeout,self._env,self)
|
||||
end
|
||||
_return = true
|
||||
-- closes the thread to provent any further event calls
|
||||
-- closes the thread to prevent any further event calls
|
||||
self:close()
|
||||
end
|
||||
return _return
|
||||
@@ -444,7 +444,7 @@ end
|
||||
|
||||
--- Used to check and raise the error handler of the thread, if not present it raises an error
|
||||
-- @usage thread:error(err) -- return true
|
||||
-- @tparam string err the err to be rasied
|
||||
-- @tparam string err the err to be raised
|
||||
-- @treturn boolean did the thread have an error handler
|
||||
function Server._thread:error(err)
|
||||
local _return = false
|
||||
@@ -453,12 +453,12 @@ function Server._thread:error(err)
|
||||
_return = true
|
||||
else
|
||||
self:close() -- no matter what happens next this thread will be closed
|
||||
error('Thread Error (no handler): '..err)
|
||||
error('Thread Error (no handler): '..err)
|
||||
end
|
||||
return _return
|
||||
end
|
||||
|
||||
--- Set function to run then an event is triggered, none of them are 'needed' but you are advised to have atleast one
|
||||
--- Set function to run then an event is triggered, none of them are 'needed' but you are advised to have at least one
|
||||
-- @usage thread:on_event('close',function) -- if event is not one below then a game event is used
|
||||
-- @usage thread_only_events = ['close','timeout','tick','resolve','success','error']
|
||||
-- @tparam ?string|index event the name of the event that the function should be called on
|
||||
@@ -466,7 +466,7 @@ end
|
||||
-- @treturn table returns self so that they can be chained together
|
||||
function Server._thread:on_event(event,callback)
|
||||
local events = {'close','timeout','tick','resolve','success','error'}
|
||||
-- seaches the above list for the event
|
||||
-- searches the above list for the event
|
||||
local value = table.find(events,function(v,k,find) return v == string.lower(find) end,event)
|
||||
if value and is_type(callback,'function') then
|
||||
-- if it is a thread_only_event then it will add it to its core values
|
||||
@@ -492,7 +492,7 @@ script.on_event(defines.events.on_tick,function(event)
|
||||
end)
|
||||
|
||||
script.on_load(function(event)
|
||||
-- sets up metatable again so that threads contiune to work
|
||||
-- sets up metatable again so that threads continue to work
|
||||
for uuid,thread in pairs(Server.threads) do
|
||||
setmetatable(thread,{__index=Server._thread})
|
||||
setmetatable(thread._env,_env_metatable)
|
||||
@@ -535,7 +535,7 @@ return Server
|
||||
end)
|
||||
thread:open()
|
||||
|
||||
all on_event functions can be chained from the thread creation rather than use varibles eg:
|
||||
all on_event functions can be chained from the thread creation rather than use variables eg:
|
||||
Server.new_thread{
|
||||
name='tree-decon',
|
||||
data={}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
--- This file will be loaded when ExpGamingCore.Command is present
|
||||
-- @function _comment
|
||||
|
||||
local Game = require('FactorioStdLib.Game')
|
||||
local Server = Server
|
||||
|
||||
Server.interfaceCallbacks = {}
|
||||
@@ -40,7 +39,7 @@ commands.add_command('interface','Runs the given input from the script', {
|
||||
env.tile = game.player.surface.get_tile(game.player.position)
|
||||
end
|
||||
-- adds custom callbacks to the interface
|
||||
for name,callback in pairs(Server.interfaceCallbacks) do env[name] = callback() end
|
||||
for name,custom_callback in pairs(Server.interfaceCallbacks) do env[name] = custom_callback() end
|
||||
-- runs the function
|
||||
local success, err = Server.interface(callback,false,env)
|
||||
-- if there is an error then it will remove the stacktrace and return the error
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
--- Allows syncing with an outside server and info panle.
|
||||
--- Allows syncing with an outside server and info panel.
|
||||
-- @module ExpGamingCore.Sync
|
||||
-- @alias Sync
|
||||
-- @author Cooldude2606
|
||||
@@ -16,10 +16,10 @@ local module_verbose = false --true|false
|
||||
-- @field server_name the server name
|
||||
-- @field server_description a short description of the server
|
||||
-- @field reset_time the reset time of the server
|
||||
-- @field time the last knowen irl time
|
||||
-- @field time the last known irl time
|
||||
-- @field time_set the last in game time that the time was set
|
||||
-- @field last_update the last time that this info was updated
|
||||
-- @field time_period how often this infomation is updated
|
||||
-- @field time_period how often this information is updated
|
||||
-- @field players a list of different player related states
|
||||
-- @field ranks a list of player ranks
|
||||
-- @field rockets the number of rockets launched
|
||||
@@ -58,9 +58,9 @@ local global = global{
|
||||
-- @field afk_players the number of afk players
|
||||
-- @field times the play times of every player
|
||||
|
||||
--- Used to standidise the tick format for any sync info
|
||||
--- Used to standardise the tick format for any sync info
|
||||
-- @usage Sync.tick_format(60) -- return {60,'1.00M'}
|
||||
-- @treturn {number,string} table containg both the raw number and clean version of a time
|
||||
-- @treturn {number,string} table containing both the raw number and clean version of a time
|
||||
function Sync.tick_format(tick)
|
||||
if not is_type(tick,'number') then error('Tick was not given to Sync.tick_format',2) end
|
||||
return {tick,tick_to_display_format(tick)}
|
||||
@@ -70,7 +70,7 @@ end
|
||||
-- @usage Sync.print('Test','Cooldude2606')
|
||||
-- @tparam string player_message the message to be printed in chat
|
||||
-- @tparam string player_name the name of the player sending the message
|
||||
-- @tparam[opt] string player_tag the tag apllied to the player's name
|
||||
-- @tparam[opt] string player_tag the tag applied to the player's name
|
||||
-- @tparam[opt] string player_colour the colour of the message, either hex or named colour
|
||||
-- @tparam[opt] string prefix add a prefix before the chat eg [IRC]
|
||||
function Sync.print(player_message,player_name,player_tag,player_colour,prefix)
|
||||
@@ -78,8 +78,8 @@ function Sync.print(player_message,player_name,player_tag,player_colour,prefix)
|
||||
local player = game.player or game.players[player_name]
|
||||
local tag = player_tag and player_tag ~= '' and ' '..player_tag or ''
|
||||
local colour = type(player_colour) == 'string' and player_colour or '#FFFFFF'
|
||||
local prefix = prefix and prefix..' ' or ''
|
||||
-- if it is an ingame player it will over ride the given params
|
||||
prefix = prefix and prefix..' ' or ''
|
||||
-- if it is an in game player it will over ride the given params
|
||||
if player then
|
||||
tag = ' '..player.tag
|
||||
colour = player.chat_color
|
||||
@@ -92,7 +92,7 @@ function Sync.print(player_message,player_name,player_tag,player_colour,prefix)
|
||||
game.print(prefix..player_name..tag..': '..player_message,colour)
|
||||
end
|
||||
|
||||
--- Outline of the paramaters accepted by Sync.emit_embeded
|
||||
--- Outline of the parameters accepted by Sync.emit_embedded
|
||||
-- @table EmitEmbededParamaters
|
||||
-- @field title the tile of the embed
|
||||
-- @field color the color given in hex you can use Color.to_hex{r=0,g=0,b=0}
|
||||
@@ -102,11 +102,11 @@ end
|
||||
-- @field fieldtwo the filed to add to the embed (key is name) (value is text) (start value with <<inline>> to make inline)
|
||||
|
||||
--- Logs an embed to the json.data we use a js script to add things we cant here
|
||||
-- @usage Sync.emit_embeded{title='BAN',color='0x0',description='A player was banned' ... }
|
||||
-- @tparam table args a table which contains everything that the embeded will use
|
||||
-- @usage Sync.emit_embedded{title='BAN',color='0x0',description='A player was banned' ... }
|
||||
-- @tparam table args a table which contains everything that the embedded will use
|
||||
-- @see EmitEmbededParamaters
|
||||
function Sync.emit_embeded(args)
|
||||
if not is_type(args,'table') then error('Args table not given to Sync.emit_embeded',2) end
|
||||
function Sync.emit_embedded(args)
|
||||
if not is_type(args,'table') then error('Args table not given to Sync.emit_embedded',2) end
|
||||
if not game then error('Game has not loaded',2) end
|
||||
local title = is_type(args.title,'string') and args.title or ''
|
||||
local color = is_type(args.color,'string') and args.color:find("0x") and args.color or '0x0'
|
||||
@@ -130,14 +130,14 @@ function Sync.emit_embeded(args)
|
||||
table.insert(fields,f)
|
||||
end
|
||||
end
|
||||
-- forms the data that will be emited to the file
|
||||
-- forms the data that will be emitted to the file
|
||||
local log_data = {
|
||||
title=title,
|
||||
description=description,
|
||||
color=color,
|
||||
fields=fields
|
||||
}
|
||||
game.write_file('embeded.json',table.json(log_data)..'\n',true,0)
|
||||
game.write_file('embedded.json',table.json(log_data)..'\n',true,0)
|
||||
end
|
||||
|
||||
--- The error handle setup by sync to emit a discord embed for any errors
|
||||
@@ -147,7 +147,7 @@ end
|
||||
error.addHandler('Discord Emit',function(err)
|
||||
if not game then return error(error()) end
|
||||
local color = Color and Color.to_hex(defines.textcolor.bg) or '0x0'
|
||||
Sync.emit_embeded{title='SCRIPT ERROR',color=color,description='There was an error in the script @Developers ',Error=err}
|
||||
Sync.emit_embedded{title='SCRIPT ERROR',color=color,description='There was an error in the script @Developers ',Error=err}
|
||||
end)
|
||||
|
||||
--- Used to get the number of admins currently online
|
||||
@@ -169,7 +169,7 @@ end
|
||||
-- @treturn number the number of afk players
|
||||
function Sync.count_afk_times(time)
|
||||
if not game then return 0 end
|
||||
local time = time or 7200
|
||||
time = time or 7200
|
||||
local rtn = {}
|
||||
for _,player in pairs(game.connected_players) do
|
||||
if player.afk_time > time then rtn[player.name] = Sync.tick_format(player.afk_time) end
|
||||
@@ -236,15 +236,15 @@ Sync.info = setmetatable({},{
|
||||
return true
|
||||
end,
|
||||
__pairs=function(tbl)
|
||||
local tbl = global
|
||||
local function next_pair(tbl,k)
|
||||
k, v = next(tbl, k)
|
||||
tbl = global
|
||||
local function next_pair(tbl,key)
|
||||
local k, v = next(tbl, key)
|
||||
if type(v) ~= nil then return k,v end
|
||||
end
|
||||
return next_pair, tbl, nil
|
||||
end,
|
||||
__ipairs=function(tbl)
|
||||
local tbl = global
|
||||
tbl = global
|
||||
local function next_pair(tbl, i)
|
||||
i = i + 1
|
||||
local v = tbl[i]
|
||||
@@ -305,7 +305,7 @@ end
|
||||
-- @usage Sync.time('Sun Apr 1 18:44:30 UTC 2018')
|
||||
-- @usage Sync.time -- string
|
||||
-- @tparam[opt=nil] string set the date time to be set
|
||||
-- @treturn boolean if the datetime set was successful
|
||||
-- @treturn boolean if the date time set was successful
|
||||
Sync.time=add_metatable({},function(full,date)
|
||||
local info = Sync.info
|
||||
if not is_type(full,'string') then return false end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
--- Allows syncing with an outside server and info panle.
|
||||
--- Allows syncing with an outside server and info panel.
|
||||
-- @submodule ExpGamingCore.Sync
|
||||
-- @alias Sync
|
||||
-- @author Cooldude2606
|
||||
@@ -7,17 +7,16 @@
|
||||
--- This file will be loaded when ExpGamingCore.Gui is present
|
||||
-- @function _comment
|
||||
|
||||
local Game = require('FactorioStdLib.Game')
|
||||
local Gui = require('ExpGamingCore.Gui')
|
||||
local Sync = Sync -- this is to force sync to remain in the ENV
|
||||
|
||||
local Sync_gui_functions = {}
|
||||
local logo_sprite_path = 'file'..string.sub(module_path,2)..'/src/logo.png'
|
||||
|
||||
--- Adds a emeltent to the sever info gui
|
||||
--- Adds a element to the sever info gui
|
||||
-- @usage Sync.add_to_gui('string') -- return true
|
||||
-- @param element see examples before for what can be used, it can also be a return from Gui.inputs.add
|
||||
-- @treturn bolean based on weather it was successful or not
|
||||
-- @treturn boolean based on weather it was successful or not
|
||||
function Sync.add_to_gui(element,...)
|
||||
if game then return false end
|
||||
if is_type(element,'function') then
|
||||
@@ -29,10 +28,10 @@ function Sync.add_to_gui(element,...)
|
||||
return true
|
||||
end
|
||||
|
||||
Sync.add_to_gui('Welcome to the Explosive Gaming comunity! This is one of many servers which we host.')
|
||||
Sync.add_to_gui('Welcome to the Explosive Gaming community! This is one of many servers which we host.')
|
||||
Sync.add_to_gui(function(player,frame) return 'This server\'s next reset: '..Sync.info.reset_time end)
|
||||
|
||||
--- Formats a lable to be a certain format
|
||||
--- Formats a label to be a certain format
|
||||
-- @local label_format
|
||||
local function label_format(label,width)
|
||||
label.style.width = width
|
||||
@@ -49,7 +48,7 @@ Sync.info_gui = Gui.center{
|
||||
draw=function(self,frame)
|
||||
frame.caption = ''
|
||||
local info = Sync.info
|
||||
local frame = frame.add{type='flow',direction='vertical'}
|
||||
frame = frame.add{type='flow',direction='vertical'}
|
||||
local h_flow = frame.add{type='flow'}
|
||||
h_flow.add{type='sprite',sprite=logo_sprite_path}
|
||||
local v_flow = h_flow.add{type='flow',direction='vertical'}
|
||||
@@ -68,7 +67,7 @@ Sync.info_gui = Gui.center{
|
||||
Gui.bar(_flow,110)
|
||||
Gui.bar(frame,600)
|
||||
local _frame = frame
|
||||
local frame = frame.add{
|
||||
frame = frame.add{
|
||||
type='frame',
|
||||
direction='vertical',
|
||||
style='image_frame'
|
||||
|
||||
Reference in New Issue
Block a user