From 64a3af8e8bcd84a86bcfc35734e46f3dfe00d046 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Wed, 29 Nov 2017 21:21:39 +0000 Subject: [PATCH] Lib done --- ExpLib.lua | 147 +++++++++++++++++++++++++ control.lua | 8 ++ locale/StdLib/{StdLib.lua => load.lua} | 6 +- locale/StdLib/table.lua | 35 +++++- 4 files changed, 191 insertions(+), 5 deletions(-) create mode 100644 ExpLib.lua rename locale/StdLib/{StdLib.lua => load.lua} (88%) diff --git a/ExpLib.lua b/ExpLib.lua new file mode 100644 index 00000000..1550b220 --- /dev/null +++ b/ExpLib.lua @@ -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/r6dC2uK +]] +--Please Only Edit Below This Line----------------------------------------------------------- + +-- @module ExpLib +-- @usage require('/ExpLib') + +local ExpLib = { + text_hex = { + ['']='#0', + info='#36F2FF', + alert='#000000', + low='#2dc42d', + med='#ffe242', + high='#ff5400', + crit='#FF0000' + }, + text_rgb = { + ['']={0,0,0}, + info={54,242,255}, + alert={0,0,0}, + low={45,196,45}, + med={255,226,66}, + high={255,84,0}, + crit={255,0,0} + } +} + +--- Loads a table into the global lua table +-- @usage a = {k1='foo',k2='bar'} +-- _load_to_G(a) +-- @tparam table tbl table to add to the global lua table +function ExpLib._load_to_G(tbl) + if not is_type(tbl,'table') then return end + for name,value in pairs(tbl) do + if not _G[name] then _G[name] = value end + end +end + +--- Returns a bolean based on the type of v matching the test type +-- @usage a = 'foo' +-- is_type(a,'string') -- return true +-- @param v the value to be tested +-- @tparam string test_type the type to test for +-- @treturn bolean is v a matching type +function ExpLib.is_type(v,test_type) + return v and type(v) == test_type or false +end + +--- Returns a value to the player or if no player then log the return +-- @usage a = 'to return' +-- player_return(a) +-- @param rtn the value to return +function ExpLib.player_return(rtn) + if game.player then + if is_type(rtn,'table') then game.player.print(table.to_string(rtn)) + elseif is_type(rtn,'function') then game.player.print('Cant Display Functions') + elseif is_type(rtn,'userdata') then game.player.print('Cant Display Userdata') + else game.player.print(tostring(rtn)) + end + else + if is_type(rtn,'table') then log(table.to_string(rtn)) + elseif is_type(rtn,'function') then log('Cant Display Functions') + elseif is_type(rtn,'userdata') then log('Cant Display Userdata') + else log(tostring(rtn)) + end + end +end + +--- Logs data to the json file, we use this for discord +-- @usage a = 'some data' +-- json_emit('data','info',a) +-- @tparam string type the type of emit your programe will look for +-- @tparam string colour the colour based on the the text_hex use '' for no colour +-- @param data any data which you want to include this will also be conevert to json +function ExpLib.json_emit(type,colour,data) + if not is_type(type,'string') or + not is_type(colour,'string') or + not text_hex[colour] or + not data then return end + local log_data = { + type=string.upper(type) + tick=game.tick, + online=#game.connected_players, + colour=text_hex[colour], + data=data + } + game.write_file('json.data',table.json(log_data),true,0) +end +--- Convert ticks to hours +-- @usage a = 216001 +-- tick_to_hour(a) -- return 1 +-- @tparam number tick to convert to hours +-- @treturn number the number of whole hours from this tick +function ExpLib.tick_to_hour(tick) + if not is_type(tick,'number') then return 0 end + return math.floor(tick/(216000*game.speed)) +end + +--- Convert ticks to minutes +-- @usage a = 3601 +-- tick_to_hour(a) -- return 1 +-- @tparam number tick to convert to minutes +-- @treturn number the number of whole minutes from this tick +function ExpLib.tick_to_min (tick) + if not is_type(tick,'number') then return 0 end + return math.floor(tick/(3600*game.speed)) +end + +--- Returns a tick in a displayable format +-- @usage a = 3600 +-- tick_to_display_format(a) -- return '1.00 M' +-- @usage a = 234000 +-- tick_to_display_format(a) -- return '1 H 5 M' +-- @tparam number tick to convert +-- @treturn string the formated string +function ExpLib.tick_to_display_format(tick) + if not is_type(tick,'number') then return '0H 0M' end + if tick_to_min(tick) < 10 then + return string.format('%.2f M',tick/(3600*game.speed)) + else + return string.format('%d H %d M', + tick_to_hour(tick), + tick_to_min(tick)-60*tick_to_hour(tick) + ) + end +end + +--- Returns a string as a hex format (also a string) +-- @usage a = 'foo' +-- string.to_hex(a) -- return '666f6f' +-- @tparam string str the string to encode +-- @treturn string the hex format of the string +function string.to_hex(str) + if not is_type(str,'string') then return '' end + return str:gsub('.',function (c) + return string.format('%02X',string.byte(c)) + end) +end + +return ExpLib \ No newline at end of file diff --git a/control.lua b/control.lua index f2b6e152..88200d7b 100644 --- a/control.lua +++ b/control.lua @@ -7,3 +7,11 @@ Any changes that you may make to the code are yours but that does not make the s Discord: https://discord.gg/r6dC2uK ]] --Please Only Edit Below This Line----------------------------------------------------------- + +-- loads the stdlib and allows Core Game and Event +local StdLib = require '/locale/StdLib/load' +local Core, Game, Event = StdLib.Core, StdLib.Game, StdLib.Event + +-- loads the ExpLib, functions are plased into the lua global +local ExpLib = require 'ExpLib' +ExpLib._load_to_G(ExpLib) \ No newline at end of file diff --git a/locale/StdLib/StdLib.lua b/locale/StdLib/load.lua similarity index 88% rename from locale/StdLib/StdLib.lua rename to locale/StdLib/load.lua index 9b591a46..2d4ec45d 100644 --- a/locale/StdLib/StdLib.lua +++ b/locale/StdLib/load.lua @@ -16,9 +16,9 @@ The array returned contains the stdlib modules so there can be decleared after t local StdLib = {} -require 'core' -require 'game' -require 'table' +StdLib.Core = require 'core' +StdLib.Game = require 'game' StdLib.Event = require 'event' +require '/table' return StdLib \ No newline at end of file diff --git a/locale/StdLib/table.lua b/locale/StdLib/table.lua index d3f7995c..5388752a 100644 --- a/locale/StdLib/table.lua +++ b/locale/StdLib/table.lua @@ -435,8 +435,8 @@ end --- Returns a table in a form able to be read as a table -- @usage local a = {k1='foo',k2='bar'} --- table.tostring(a) -- return '"value"' --- @param tbl table to convert +-- table.tostring(a) -- return '{["k1"]="foo",["k2"]="bar"}' +-- @tparam table tbl table to convert -- @treturn string the converted table function table.to_string(tbl) local result, done = {}, {} @@ -453,3 +453,34 @@ function table.to_string(tbl) return "{"..table.concat(result,",") .."}" end +--- Simmilar to table.to_string but converts a lua table to a json one +-- @usage local a = {k1='foo',k2='bar'} +-- talbe.json(a) -- return '{"k1":"foo","k2":"bar"}' +-- @tparam table lua_table the table to convert +-- @treturn string the table in a json format +function table.json(lua_table) + local result, done, only_indexs = {}, {}, true + for key,value in ipairs(lua_table) do + done[key] = true + if type(value) == 'table' then table.insert(result,json_log(value,true)) + elseif type(value) == 'string' then table.insert(result,'"'..value..'"') + elseif type(value) == 'number' then table.insert(result,value) + elseif type(value) == 'boolean' then table.insert(result,tostring(value)) + else table.insert(result,'null') + end + end + for key,value in pairs(lua_table) do + if not done[key] then + only_indexs = false + if type(value) == 'table' then table.insert(result,'"'..key..'":'..json_log(value,true)) + elseif type(value) == 'string' then table.insert(result,'"'..key..'":"'..value..'"') + elseif type(value) == 'number' then table.insert(result,'"'..key..'":'..value) + elseif type(value) == 'boolean' then table.insert(result,'"'..key..'":'..tostring(value)) + else table.insert(result,'"'..key..'":null') + end + end + end + if only_indexs then return "["..table.concat(result,",").."]" + else return "{"..table.concat(result,",").."}" + end +end \ No newline at end of file