Add StdLib and Rank Table

This commit is contained in:
Cooldude2606
2017-07-01 17:20:18 +01:00
parent c96c7e85d8
commit aa3362d1ec
8 changed files with 412 additions and 6 deletions

View File

@@ -18,12 +18,10 @@ local credits = {{
}}
local function credit_loop(reg) for _,cred in pairs(reg) do table.insert(credits,cred) end end
--Please Only Edit Below This Line-----------------------------------------------------------
credit_loop(require("locale/StdLib/event"))
credit_loop(require("locale/file-header"))
script.on_init(function() global.credits = credits end)
Event.register(-1,function() global.credits = credits end)
----TEMP-------------------------------------------------------------------------------------
function table.val_to_str ( v )

View File

@@ -0,0 +1,23 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
Contact a member of management on our discord to seek permission to use our code.
Any changes that you may make to the code are yours but that does not make the script yours.
Discord: https://discord.gg/XSsBV6b
The credit below may be used by another script do not remove.
]]
local credits = {{
name='ExpGaming - Rank Control Functions',
owner='Explosive Gaming',
dev='Cooldude2606',
description='The Core for the Rank Controls',
factorio_version='0.15.23',
show=true
}}
local function credit_loop(reg) for _,cred in pairs(reg) do table.insert(credits,cred) end end
--Please Only Edit Below This Line-----------------------------------------------------------
--Please Only Edit Above This Line-----------------------------------------------------------
return credits

View File

@@ -0,0 +1,147 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
Contact a member of management on our discord to seek permission to use our code.
Any changes that you may make to the code are yours but that does not make the script yours.
Discord: https://discord.gg/XSsBV6b
The credit below may be used by another script do not remove.
]]
local credits = {{
name='ExpGaming - Ranks Table',
owner='Explosive Gaming',
dev='Cooldude2606',
description='Table holding the ranks used',
factorio_version='0.15.23',
show=false
}}
local function credit_loop(reg) for _,cred in pairs(reg) do table.insert(credits,cred) end end
--Please Only Edit Below This Line-----------------------------------------------------------
--[[
Rank Powers:
0: root
1: no restrictions - management ranks
2: unable to use scripting commands - must be with sudo()
3: able to jail people - must use /promote to be able to ban
4: highest user power
5: people who are trusted
6: default power level
7: full restrictions
For disallow add to the list the end part of the input action
Example: defines.input_action.drop_item -> 'drop_item'
http://lua-api.factorio.com/latest/defines.html#defines.input_action
--]]
local ranks = {
{name='Owner',
shortHand='Owner',
tag='[Owner]',
power=0,
colour={r=170,g=0,b=0},
disallow={}},
{name='Community Manager',
shortHand='Com Mngr',
tag='[Com Mngr]',
power=1,
colour={r=150,g=68,b=161},
disallow={}},
{name='Developer',
shortHand='Dev',
tag='[Dev]',
power=1,
colour={r=179,g=125,b=46},
disallow={}},
{name='Admin',
shortHand='Admin',
tag='[Admin]',
power=2,
colour={r=233,g=63,b=233},
disallow={
'set_allow_commands',
'edit_permission_group',
'delete_permission_group',
'add_permission_group'}
},
{name='Mod',
shortHand='Mod',
tag='[Mod]',
power=3,
colour={r=0,g=170,b=0},
disallow={
'server_command'}
},
{name='Donator',
shortHand='P2W',
tag='[P2W]',
power=4,
colour={r=233,g=63,b=233},
disallow={}},
{name='Veteran',
shortHand='Vet',
tag='[Veteran]',
power=4,
colour={r=140,g=120,b=200},
disallow={}},
{name='Member',
shortHand='Mem',
tag='[Member]',
power=5,
colour={r=24,g=172,b=188},
disallow={}},
{name='Regular',
shortHand='Reg',
tag='[Regular]',
power=5,
colour={r=24,g=172,b=188},
disallow={
'set_auto_launch_rocket',
'change_programmable_speaker_alert_parameters',
'reset_assembling_machine',
'drop_item'}
},
{name='Guest',
shortHand='',
tag='[Guest]',
power=6,
colour={r=255,g=159,b=27},
disallow={
'build_terrain',
'remove_cables',
'launch_rocket',
'cancel_research'}
},
{name='Jail',
shortHand='Jail',
tag='[Jail]',
power=7,
colour={r=50,g=50,b=50},
disallow={
'open_character_gui',
'begin_mining',
'start_walking',
'player_leave_game'}
}
}
-- This For Loop cauess the disallows of each rank to be feed into the one below its self
for n,rank in pairs(ranks) do
if ranks[n-1] then
for _,disallow in pairs(ranks[n-1].disallow) do
table.insert(rank.disallow,disallow)
end
end
end
-- Moves The Ranks to the global array
Event.register(-1,function() global.ranks = ranks end)
--Please Only Edit Above This Line-----------------------------------------------------------
return credits, ranks

View File

@@ -0,0 +1,23 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
Contact a member of management on our discord to seek permission to use our code.
Any changes that you may make to the code are yours but that does not make the script yours.
Discord: https://discord.gg/XSsBV6b
The credit below may be used by another script do not remove.
]]
local credits = {{
name='File Header - ExpGaming-Core-GUI',
owner='Explosive Gaming',
dev='Cooldude2606',
description='Just A File Header To Organise Code',
factorio_version='0.15.23',
show=false
}}
local function credit_loop(reg) for _,cred in pairs(reg) do table.insert(credits,cred) end end
--Please Only Edit Below This Line-----------------------------------------------------------
--Please Only Edit Above This Line-----------------------------------------------------------
return credits

View File

@@ -9,7 +9,7 @@ Discord: https://discord.gg/XSsBV6b
The credit below may be used by another script do not remove.
]]
local credits = {{
name='File Header - ExpGaming-Core',
name='File Header - Stand-Alone',
owner='Explosive Gaming',
dev='Cooldude2606',
description='Just A File Header To Organise Code',
@@ -18,6 +18,8 @@ local credits = {{
}}
local function credit_loop(reg) for _,cred in pairs(reg) do table.insert(credits,cred) end end
--Please Only Edit Below This Line-----------------------------------------------------------
credit_loop(require("GUI/file-header"))
credit_loop(require("ExpGaming - Rank Table"))
credit_loop(require("ExpGaming - Rank Control"))
--Please Only Edit Above This Line-----------------------------------------------------------
return credits

16
locale/StdLib/core.lua Normal file
View File

@@ -0,0 +1,16 @@
--- Core module
-- @module Core
Core = {} --luacheck: allow defined top
--- Errors if the variable evaluates to false, with an optional msg
-- @param var variable to evaluate
-- @tparam[opt='missing value'] string msg message
function Core.fail_if_missing(var, msg)
if not var then
error(msg or "Missing value", 3)
end
return false
end
return Core

171
locale/StdLib/event.lua Normal file
View File

@@ -0,0 +1,171 @@
--- Makes working with events in factorio a lot more simple.
-- <p>Factorio can only have one handler registered per event. This module
-- allows you to easily register multiple handlers for each event.
-- Using this module is as simple as replacing script.on_event(...) with Event.register(...)</p>
-- @module Event
-- @usage require('stdlib/event/event')
local credits = {{
name='StdLib',
owner='Afforess',
dev='Afforess',
description='The Factorio StdLib used here for the event handler',
factorio_version='0.15.23',
show=false
}}
local function credit_loop(reg) for _,cred in pairs(reg) do table.insert(credits,cred) end end
local fail_if_missing = require 'core'['fail_if_missing']
local Game = require 'game'
Event = { --luacheck: allow defined top
_registry = {},
core_events = {
init = -1,
load = -2,
configuration_changed = -3,
_register = function(id)
if id == Event.core_events.init then
script.on_init(
function()
Event.dispatch({name = Event.core_events.init, tick = game.tick})
end
)
elseif id == Event.core_events.load then
script.on_load(
function()
Event.dispatch({name = Event.core_events.load, tick = -1})
end
)
elseif id == Event.core_events.configuration_changed then
script.on_configuration_changed(
function(event)
event.name = Event.core_events.configuration_changed
event.data = event -- for backwards compatibilty
Event.dispatch(event)
end
)
end
end
}
}
--- Registers a function for a given event. If a nil handler is passed remove all events and stop listening for that event.
-- Events are dispatched in the order they are registered.
-- @usage Event.register(defines.events.on_tick, function(event) print event.tick end)
-- -- creates an event that prints the current tick every tick.
-- @tparam defines.events|{defines.events,...} event events to register
-- @tparam function handler Function to call when event is triggered
-- @treturn Event
function Event.register(event, handler)
fail_if_missing(event, "missing event argument")
event = (type(event) == "table" and event) or {event}
for _, event_id in pairs(event) do
if not (type(event_id) == "number" or type(event_id) == "string") then
error("Invalid Event Id, Must be string or int, or array of strings and/or ints", 2)
end
if handler == nil then
Event._registry[event_id] = nil
script.on_event(event_id, nil)
else
if not Event._registry[event_id] then
Event._registry[event_id] = {}
if type(event_id) == "string" or event_id >= 0 then
script.on_event(event_id, Event.dispatch)
elseif event_id < 0 then
Event.core_events._register(event_id)
end
end
table.insert(Event._registry[event_id], handler)
end
end
return Event
end
--- Calls the registerd handlers
-- Will stop dispatching remaning handlers if any handler passes invalid event userdata.
-- Handlers are dispatched in the order they were created
-- @tparam table event LuaEvent as created by script.raise_event
-- @see https://forums.factorio.com/viewtopic.php?t=32039#p202158 Invalid Event Objects
function Event.dispatch(event)
if event then
local _registry = event.name and Event._registry[event.name] or event.input_name and Event._registry[event.input_name]
if _registry then
local force_crc = Event.force_crc
for idx, handler in ipairs(_registry) do
-- Check for userdata and stop processing further handlers if not valid
for _, val in pairs(event) do
if type(val) == "table" and val.__self == "userdata" and not val.valid then
return
end
end
setmetatable(event, { __index = { _handler = handler } })
-- Call the handler
local success, err = pcall(handler, event)
-- If the handler errors lets make sure someone notices
if not success then
if _G.game then -- may be nil in on_load
if Game.print_all(err) == 0 then
error(err) -- no players received the message, force a real error so someone notices
end
else
error(err) -- no way to handle errors cleanly when the game is not up
end
-- continue processing the remaning handlers. In most cases they won't be related to the failed code.
end
-- force a crc check if option is enabled. This is a debug option and will hamper perfomance if enabled
if (force_crc or event.force_crc) and _G.game then
local msg = 'CRC check called for event '..event.name..' handler #'..idx
log(msg) -- log the message to factorio-current.log
game.force_crc()
end
-- if present stop further handlers for this event
if event.stop_processing then
return
end
end
end
else
error('missing event argument')
end
end
--- Removes the handler from the event. If it removes the last handler for an event stop listening for that event.
-- @tparam defines.events|{defines.events,...} event events to remove the handler for
-- @tparam function handler to remove
-- @return Event
function Event.remove(event, handler)
fail_if_missing(event, "missing event argument")
fail_if_missing(handler, "missing handler argument")
event = (type(event) == "table" and event) or {event}
for _, event_id in pairs(event) do
if not (type(event_id) == "number" or type(event_id) == "string") then
error("Invalid Event Id, Must be string or int, or array of strings and/or ints", 2)
end
if Event._registry[event_id] then
for i=#Event._registry[event_id], 1, -1 do
if Event._registry[event_id][i] == handler then
table.remove(Event._registry[event_id], i)
end
end
if #Event._registry[event_id] == 0 then
Event._registry[event_id] = nil
script.on_event(event_id, nil)
end
end
end
return Event
end
return credits, Event

26
locale/StdLib/game.lua Normal file
View File

@@ -0,0 +1,26 @@
--- Game module
-- @module Game
-- @usage local Game = require('stdlib/game')
Game = {} --luacheck: allow defined top
Game.VALID_FILTER = function(v)
return v.valid
end
--- Messages all players currently connected to the game
-- @tparam string msg message to send to players
-- @param[opt] condition condition to be true for the player to be messaged
-- @treturn int the number of players who received the message. Offline players are not counted as having received the message.
function Game.print_all(msg, condition)
local num = 0
for _, player in pairs(game.connected_players) do
if condition == nil or select(2, pcall(condition, player)) then
player.print(msg)
num = num + 1
end
end
return num
end
return Game