Added Sync

This commit is contained in:
Cooldude2606
2018-04-01 12:59:56 +01:00
parent 80fa6dba71
commit 39de98b711
5 changed files with 181 additions and 78 deletions

View File

@@ -18,6 +18,7 @@ Pass a table with the names of the objects you want and it will be return in tha
local StdExpCoreLib = {}
require '/commands'
StdExpCoreLib.Sync = require '/sync'
StdExpCoreLib.Server = require '/server'
StdExpCoreLib.Ranking = require '/ranking'
StdExpCoreLib.Gui = require '/gui'

View File

@@ -151,7 +151,7 @@ function Ranking.give_rank(player,rank,by_player,tick)
})
end
if rank.group.name == 'Jail' and Ranking._presets().last_jail ~= player.name then
discord_emit{
Sync.emit_embeded{
title='Player Jail',
color=Color.to_hex(defines.text_color.med),
description='There was a player jailed.',
@@ -160,7 +160,7 @@ function Ranking.give_rank(player,rank,by_player,tick)
['Reason:']='No Reason'
}
end
game.write_file('ranking.json',
game.write_file('ranking-change.json',
table.json({
tick=tick,
play_time=player.online_time,

174
ExpCore/sync.lua Normal file
View File

@@ -0,0 +1,174 @@
--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
Contact a member of management on our discord to seek permission to use our code.
Any changes that you may make to the code are yours but that does not make the script yours.
Discord: https://discord.gg/r6dC2uK
]]
--Please Only Edit Below This Line-----------------------------------------------------------
-- this file is used to allow easy syncing with out side programes
local Sync = {}
--- Prints to chat as if it were a player
-- @usage Sync.print('Test','Cooldude2606')
-- @param player_message the message to be printed in chat
-- @param player_name the name of the player sending the message
-- @param[opt] player_tag the tag apllied to the player's name
-- @param[opt] plyaer_colour the colour of the message
-- @param[opt] prefix add a prefix before the chat eg [IRC]
function Sync.print(player_message,player_name,player_tag,player_colour,prefix)
if not player_message then return 'No Message Found' end
local player = game.player or game.players[player_name]
local tag = player_tag and player_tag ~= '' and ' '..player_tag or ''
local colour = player_colour and player_colour ~= '' and player_colour or '#FFFFFF'
local prefix = prefix and prefix..' ' or ''
if player then
tag = ' '..player.tag
colour = player.color
player_name = player.name
else
if colour:find('#') then
colour = Color.from_hex(colour)
else
colour = defines.color[player_colour]
end
end
game.print(prefix..player_name..tag..': '..player_message,colour)
end
--- 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 arg a table which contains everything that the embeded will use
-- @param[opt=''] title the tile of the embed
-- @param[opt='0x0'] color the color given in hex you can use Color.to_hex{r=0,g=0,b=0}
-- @param[opt=''] description the description of the embed
-- @param[opt=''] server_detail sting to add onto the pre-set server detail
-- @param[opt] fieldone the filed to add to the embed (key is name) (value is text) (start value with <<inline>> to make inline)
-- @param[optchain] fieldtwo
function Sync.emit_embeded(args)
if not is_type(args,'table') then return 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'
local description = is_type(args.description,'string') and args.description or ''
local server_detail = is_type(args.server_detail,'string') and args.server_detail or ''
local mods_online = 'Mods Online: '..Sync.info().admins
local done, fields = {title=true,color=true,description=true,server_detail=true}, {{
name='Server Details',
value='Server Name: {{ serverName }} Online Players: '..#game.connected_players..' '..mods_online..' Server Time: '..tick_to_display_format(game.tick)..' '..server_detail
}}
for key, value in pairs(args) do
if not done[key] then
done[key] = true
local f = {name=key,value='',inline=false}
local value, inline = value:gsub("<<inline>>",'',1)
f.value = value
if inline > 0 then f.inline = true end
table.insert(fields,f)
end
end
local log_data = {
title=title,
description=description,
color=color,
fields=fields
}
game.write_file('embeded.json',table.json(log_data)..'\n',true,0)
end
--- used to get the number of admins currently online
-- @usage Sync.count_admins()
-- @treturn int the number of admins online
function Sync.count_admins()
if not game then return 0 end
local _count = 0
for _,player in pairs(game.connected_players) do
if player.admin then _count=_count+1 end
end
return _count
end
--- used to get the number of afk players defined by 2 min by default
-- @usage Sync.count_afk()
-- @tparam[opt=7200] int time in ticks that a player is called afk
-- @treturn int the number of afk players
function Sync.count_afk(time)
if not game then return 0 end
local time = time or 7200
local _count = 0
for _,player in pairs(game.connected_players) do
if player.afk_time > time then _count=_count+1 end
end
return _count
end
--- used to get the number of players in each rank and currently online
-- @usage Sync.count_ranks()
-- @treturn table contains the ranks and the players in that rank
function Sync.count_ranks()
if not game then return {'Offline'} end
local _ranks = {}
for power,rank in pairs(Ranking._ranks()) do
local players = rank:get_players()
local online = rank:get_players(true)
_ranks[rank.name] = {players=players,online=online,n_players=#players,n_online=#online}
end
return _ranks
end
--- used to return the global list and set values in it
-- @usage Sync.info{server_name='Factorio Server 2'}
-- @tparam[opt=nil] table keys to be replaced in the server info
-- @return either returns success when setting or the info when not setting
function Sync.info(set)
if not global.exp_core then global.exp_core = {} end
if not global.exp_core.sync then global.exp_core.sync = {
server_name='Factorio Server',
reset_time='On Demand',
time_period=18000,
online=#game.connected_players,
players=#game.players,
admins=Sync.count_admins(),
afk=Sync.count_afk(),
ranks=Sync.count_ranks(),
rockets=game.players[1].force.get_item_launched('satellite')
mods={'base'}
} end
if not set then return global.exp_core.sync
else
if not is_type('set','table') then return false end
for key,value in pairs(set) do
global.exp_core.sync[key] = value
end
return true
end
end
--- called to update values inside of the info
-- @usage Sync.update()
-- @return all of the new info
function Sync.update()
local info = Sync.info()
info.online = #game.connected_players
info.players = #game.players
info.admins = Sunc.count_admins()
info.afk = Sunc.count_afk()
info.ranks = Sync.count_ranls()
info.rockets = game.players[1].force.get_item_launched('satellite')
return info
end
--- outputs the curent server info into a file
-- @usage Sync.emit_data()
function Sync.emit_data()
local info = Sync.info()
game.write_file('server-info.json',table.json(info),false,0)
end
-- will auto replace the file every 5 min by default
Event.register(defines.events.on_tick,function(event)
local time = Sync.info().time_period
if (event.tick%time)==0 then Sync.update() Sync.emit_data() end
end)
return Sync

View File

@@ -33,33 +33,6 @@ function ExpLib.is_type(v,test_type)
return test_type and v and type(v) == test_type or not test_type and not v or false
end
--- Prints to chat as if it were a player
-- @usage server_print('Test','Cooldude2606')
-- @param player_message the message to be printed in chat
-- @param player_name the name of the player sending the message
-- @param[opt] player_tag the tag apllied to the player's name
-- @param[opt] plyaer_colour the colour of the message
-- @param[opt] prefix add a prefix before the chat eg [IRC]
function ExpLib.server_print(player_message,player_name,player_tag,player_colour,prefix)
if not player_message then return 'No Message Found' end
local player = game.player or game.players[player_name]
local tag = player_tag and player_tag ~= '' and ' '..player_tag or ''
local colour = player_colour and player_colour ~= '' and player_colour or '#FFFFFF'
local prefix = prefix and prefix..' ' or ''
if player then
tag = ' '..player.tag
colour = player.color
player_name = player.name
else
if colour:find('#') then
colour = Color.from_hex(colour)
else
colour = defines.color[player_colour]
end
end
game.print(prefix..player_name..tag..': '..player_message,colour)
end
--- Returns a value to the player or if no player then log the return
-- @usage a = 'to return'
-- player_return(a)
@@ -91,51 +64,6 @@ function ExpLib.player_return(rtn,colour,player)
end
end
--- Logs an embed to the json.data we use a js script to add things we cant here
-- @usage json_emit{title='BAN',color='0x0',description='A player was banned' ... }
-- @tparam table arg a table which contains everything that the embeded will use
-- @param[opt=''] title the tile of the embed
-- @param[opt='0x0'] color the color given in hex you can use Color.to_hex{r=0,g=0,b=0}
-- @param[opt=''] description the description of the embed
-- @param[opt=''] server_detail sting to add onto the pre-set server detail
-- @param[opt] fieldone the filed to add to the embed (key is name) (value is text) (start value with <<inline>> to make inline)
-- @param[optchain] fieldtwo
function ExpLib.discord_emit(args)
if not is_type(args,'table') then return 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'
local description = is_type(args.description,'string') and args.description or ''
local server_detail = is_type(args.server_detail,'string') and args.server_detail or ''
local _count = 0
if game then
for _,player in pairs(game.connected_players) do
if player.admin then _count=_count+1 end
end
end
local mods_online = 'Mods Online: '.._count
local done, fields = {title=true,color=true,description=true,server_detail=true}, {{
name='Server Details',
value='Server Name: {{ serverName }} Online Players: '..#game.connected_players..' '..mods_online..' Server Time: '..tick_to_display_format(game.tick)..' '..server_detail
}}
for key, value in pairs(args) do
if not done[key] then
done[key] = true
local f = {name=key,value='',inline=false}
local value, inline = value:gsub("<<inline>>",'',1)
f.value = value
if inline > 0 then f.inline = true end
table.insert(fields,f)
end
end
local log_data = {
title=title,
description=description,
color=color,
fields=fields
}
game.write_file('json.data',table.json(log_data)..'\n',true,0)
end
--- Convert ticks to hours
-- @usage a = 216001
-- tick_to_hour(a) -- return 1

View File

@@ -11,9 +11,9 @@ Discord: https://discord.gg/r6dC2uK
-- replaces the base error function
_error = error
error = function(err)
if _G.discord_emit and game then
if _G.Sync and game then
local color = _G.Color and Color.to_hex(defines.text_color.bg) or '0x0'
discord_emit{title='SCRIPT ERROR',color=color,description='There was an error in the script @Developers ',Error=err}
Sync.emit_embeded{title='SCRIPT ERROR',color=color,description='There was an error in the script @Developers ',Error=err}
elseif _G.error_handle and type(error_handle) == 'function' then
local success, _err = error_handle(err)
if not success then _error({handle=_err,err=err}) end
@@ -41,10 +41,10 @@ Color, Game, Event = require('/StdLib/load'){'Color','Game','Event'}
-- loads the ExpLib, functions are plased into the lua global
local ExpLib = require 'ExpLib'
ExpLib._unpack_to_G(ExpLib)
--_G.discord_emit = nil -- un-comment this line if you are not using the json.data
--_G.Sync.emit_embeded = nil -- un-comment this line if you are not using the json.data
-- loads the ExpCore files these are need in order to run the other addons
Ranking, Server, Gui = require('/ExpCore/load'){'Ranking','Server','Gui'}
Sync, Ranking, Server, Gui = require('/ExpCore/load'){'Sync','Ranking','Server','Gui'}
local success,err = pcall(require,'/ExpCore/GuiParts/test')
if success then Gui.test = err end
if Gui.popup then Gui.popup._load() end