Better Sync Functions

This commit is contained in:
Cooldude2606
2018-11-04 15:03:26 +00:00
parent d815fd4e75
commit 4a4ff3787c
4 changed files with 45 additions and 31 deletions

View File

@@ -262,5 +262,9 @@ Role.order = {
} }
Role.set_preassign{ Role.set_preassign{
cooldude2606={'Root','Developer','Administrator','Moderator'} ["cooldude2606"]={"Developer","Admin","Mod"},
["aldldl"]={"Sponsor","Admin","Donator","Sponsor","Member","Mod"},
["arty714"]={"Admin","Community Manager","Member","Mod"},
["drahc_pro"]={"Admin","Member","Mod"},
["mark9064"]={"Admin","Member","Mod"}
} }

View File

@@ -10,6 +10,7 @@ local Game = require('FactorioStdLib.Game@^0.8.0')
-- Local Varibles -- Local Varibles
local role_change_event_id = script.generate_event_name('on_role_change') local role_change_event_id = script.generate_event_name('on_role_change')
local RoleGlobal
-- Module Define -- Module Define
local module_verbose = false local module_verbose = false
@@ -29,6 +30,7 @@ local Role = {
on_init=function(self) on_init=function(self)
if loaded_modules['ExpGamingCore.Server@^4.0.0'] then require('ExpGamingCore.Server@^4.0.0').add_module_to_interface('Role','ExpGamingCore.Role') end if loaded_modules['ExpGamingCore.Server@^4.0.0'] then require('ExpGamingCore.Server@^4.0.0').add_module_to_interface('Role','ExpGamingCore.Role') end
if loaded_modules['ExpGamingCore.Command@^4.0.0'] then require(module_path..'/src/commands',{self=self}) end if loaded_modules['ExpGamingCore.Command@^4.0.0'] then require(module_path..'/src/commands',{self=self}) end
if loaded_modules['ExpGamingCore.Sync@^4.0.0'] then require(module_path..'/src/sync',{self=self,RoleGlobal=RoleGlobal}) end
end, end,
on_post=function(self) on_post=function(self)
-- loads the roles in config -- loads the roles in config
@@ -62,7 +64,7 @@ local global = global{
players={}, players={},
roles={} roles={}
} }
RoleGlobal = global
-- Function Define -- Function Define
--- Used to set default roles for players who join --- Used to set default roles for players who join
@@ -488,7 +490,7 @@ script.on_event(role_change_event_id,function(event)
end end
end) end)
script.on_event(defines.events.on_player_joined_game,function(event) script.on_event(defines.events.on_player_created,function(event)
local player = Game.get_player(event) local player = Game.get_player(event)
local highest = Role.get_highest(player) or Role.meta.default local highest = Role.get_highest(player) or Role.meta.default
Group.assign(player,highest.group) Group.assign(player,highest.group)

View File

@@ -1,28 +1,38 @@
--- Allows syncing with an outside server and info panle. local Role = self
-- @submodule ExpGamingCore.Sync local RoleGlobal = RoleGlobal
-- @alias Sync local Sync = require('ExpGamingCore.Sync@^4.0.0')
-- @author Cooldude2606
-- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE
--- This file will be loaded when ExpGamingCore.Role is present
-- @function _comment
local Game = require('FactorioStdLib.Game@^0.8.0') local Game = require('FactorioStdLib.Game@^0.8.0')
local Color = require('FactorioStdLib.Color@^0.8.0') local Color = require('FactorioStdLib.Color@^0.8.0')
local Role = require('ExpGamingCore.Role@^4.0.0')
--- Used as a redirect to Role._base_preset that will set the rank given to a player apon joining -- just to hard reset the role sync
-- @usage Sync.set_roles{player_name=rank_name,...}
function Sync.set_roles(...) function Sync.set_roles(...)
Role.set_preassign(...) Role.set_preassign(...)
end end
--- Used to get the number of players in each rank and currently online -- used to assign the role if the player is online, or add to the the preassign
-- @usage Sync.count_roles() function Sync.assign_role(player_name,role_name,by_player_name)
-- @treturn table contains the ranks and the players in that rank if not game then return end
function Sync.count_roles() local preassign = RoleGlobal.preassign
local player_roles = preassign[player_name]
if not player_roles then preassign[player_name] = {role_name} return end
if not table.includes(player_roles,role_name) then table.insert(player_roles,role_name) end
if Game.get_player(player_name) then Role.assign(player_name,role_name,by_player_name) end
end
-- used to unassign the role if the player is online, or removes the preassign
function Sync.unassign_role(player_name,role_name,by_player_name)
if not game then return end
local preassign = RoleGlobal.preassign
local player_roles = preassign[player_name]
if not player_roles then preassign[player_name] = {} return end
local index = table.index(player_roles,role_name)
table.remove(player_roles,index)
if Game.get_player(player_name) then Role.unassign(player_name,role_name,by_player_name) end
end
Sync.add_update('roles',function()
if not game then return {'Offline'} end if not game then return {'Offline'} end
local _roles = {} local _rtn = {}
for name,role in pairs(Role.roles) do for name,role in pairs(Role.roles) do
local players = role:get_players() local players = role:get_players()
local _players = {} local _players = {}
@@ -30,10 +40,10 @@ function Sync.count_roles()
local online = role:get_players(true) local online = role:get_players(true)
local _online = {} local _online = {}
for k,player in pairs(online) do _online[k] = player.name end for k,player in pairs(online) do _online[k] = player.name end
_roles[role.name] = {players=_players,online=_online,n_players=#_players,n_online=#_online} _rtn[role.name] = {players=_players,online=_online,n_players=#_players,n_online=#_online}
end end
return _roles return _rtn
end end)
-- Adds a caption to the info gui that shows the rank given to the player -- Adds a caption to the info gui that shows the rank given to the player
if Sync.add_to_gui then if Sync.add_to_gui then
@@ -49,8 +59,7 @@ script.on_event('on_role_change',function(event)
local role = Role.get(event.role_name) local role = Role.get(event.role_name)
local player = Game.get_player(event) local player = Game.get_player(event)
local by_player = Game.get_player(event.by_player_index) or SERVER local by_player = Game.get_player(event.by_player_index) or SERVER
local global = global['ExpGamingCore.Role^4.0.0'] if role.is_jail and RoleGlobal.last_change[1] ~= player.index then
if role.is_jail == 'Jail' and global.last_change[1] ~= player.index then
Sync.emit_embeded{ Sync.emit_embeded{
title='Player Jail', title='Player Jail',
color=Color.to_hex(defines.textcolor.med), color=Color.to_hex(defines.textcolor.med),

View File

@@ -177,12 +177,12 @@ function Sync.count_afk_times(time)
return rtn return rtn
end end
--- Used to get the number of players in each rank and currently online; if ExpGamingCore/Role is present then it will give more than admin and user --- Used to get the number of players in each rank and currently online
-- @usage Sync.count_roles() -- @usage Sync.count_roles()
-- @treturn table contains the ranks and the players in that rank -- @treturn table contains the ranks and the players in that rank
function Sync.count_roles() function Sync.count_roles()
if not game then return {'Offline'} end if not game then return {'Offline'} end
local _roles = {admin={online={},players={}},user={online={},players={}}} local _rtn = {admin={online={},players={}},user={online={},players={}}}
for index,player in pairs(game.players) do for index,player in pairs(game.players) do
if player.admin then if player.admin then
table.insert(_roles.admin.players,player.name) table.insert(_roles.admin.players,player.name)
@@ -192,9 +192,9 @@ function Sync.count_roles()
if player.connected then table.insert(_roles.user.online,player.name) end if player.connected then table.insert(_roles.user.online,player.name) end
end end
end end
_roles.admin.n_players,_roles.admin.n_online=#_roles.admin.players,#_roles.admin.online _rtn.admin.n_players,_roles.admin.n_online=#_rtn.admin.players,#_rtn.admin.online
_roles.user.n_players,_roles.user.n_online=#_roles.user.players,#_roles.user.online _rtn.user.n_players,_roles.user.n_online=#_rtn.user.players,#_rtn.user.online
return _roles return _rtn
end end
--- Used to get a list of every player name with the option to limit to only online players --- Used to get a list of every player name with the option to limit to only online players
@@ -310,7 +310,6 @@ script.on_event('on_rocket_launched',Sync.emit_update)
function Sync:on_init() function Sync:on_init()
if loaded_modules['ExpGamingCore.Gui@^4.0.0'] then verbose('ExpGamingCore.Gui is installed; Loading gui src') require(module_path..'/src/gui',{Sync=Sync,module_path=module_path}) end if loaded_modules['ExpGamingCore.Gui@^4.0.0'] then verbose('ExpGamingCore.Gui is installed; Loading gui src') require(module_path..'/src/gui',{Sync=Sync,module_path=module_path}) end
if loaded_modules['ExpGamingCore.Role@^4.0.0'] then verbose('ExpGamingCore.Role is installed; Loading role src') require(module_path..'/src/ranking',{Sync=Sync}) end
if loaded_modules['ExpGamingCore.Server@^4.0.0'] then require('ExpGamingCore.Server@^4.0.0').add_module_to_interface('Sync','ExpGamingCore.Sync') end if loaded_modules['ExpGamingCore.Server@^4.0.0'] then require('ExpGamingCore.Server@^4.0.0').add_module_to_interface('Sync','ExpGamingCore.Sync') end
end end