- Format codes

This commit is contained in:
ParticleG
2024-11-03 01:07:11 +08:00
parent b4b602ae9d
commit 01efc218bf

View File

@@ -1,73 +1,76 @@
local __requestQueue = {} local __requestQueue={}
local _requestCount = 0 local _requestCount=0
local _Request = local _Request=
{ {
command = "", command="",
currentTime = 0, currentTime=0,
timeOut = 2, timeOut=2,
id = '0' id='0',
} }
local __defaultErrorFunction = nil local __defaultErrorFunction=nil
local isDebugActive = false local isDebugActive=false
local JS = {} local JS={}
function JS.callJS(funcToCall) function JS.callJS(funcToCall)
print("callJavascriptFunction " .. funcToCall) print("callJavascriptFunction "..funcToCall)
end end
--You can pass a set of commands here and, it is a syntactic sugar for executing many commands inside callJS, as it only calls a function --You can pass a set of commands here and, it is a syntactic sugar for executing many commands inside callJS, as it only calls a function
--If you pass arguments to the func beyond the string, it will perform automatically string.format --If you pass arguments to the func beyond the string, it will perform automatically string.format
--Return statement is possible inside this structure --Return statement is possible inside this structure
--This will return a string containing a function to be called by JS.callJS --This will return a string containing a function to be called by JS.callJS
function JS.stringFunc(str, ...) function JS.stringFunc(str,...)
str = "(function(){"..str.."})()" str="(function(){"..str.."})()"
if(#arg > 0) then if (#arg>0) then
str = str:format(unpack(arg)) str=str:format(unpack(arg))
end end
str = str:gsub("[\n\t]", "") str=str:gsub("[\n\t]", "")
return str return str
end end
--The call will store in the webDB the return value from the function passed --The call will store in the webDB the return value from the function passed
--it timeouts --it timeouts
local function retrieveJS(funcToCall, id) local function retrieveJS(funcToCall,filename)
--Used for retrieveData function --Used for retrieveData function
JS.callJS("FS.writeFile('"..love.filesystem.getSaveDirectory().."/__temp"..id.."', "..funcToCall..");") JS.callJS(("FS.writeFile('%s/%s',%s);"):format(love.filesystem.getSaveDirectory(),filename,funcToCall))
end end
--Call JS.newRequest instead --Call JS.newRequest instead
function _Request:new(isPromise, command, onDataLoaded, onError, timeout, id) function _Request:new(isPromise,command,onDataLoaded,onError,timeout,id)
local obj = {} local obj={}
setmetatable(obj, self) setmetatable(obj, self)
obj.command = command obj.command=command
obj.onError = onError or __defaultErrorFunction obj.onError=onError or __defaultErrorFunction
if not isPromise then if not isPromise then
retrieveJS(command, id) retrieveJS(command, self.filename)
else else
JS.callJS(command) JS.callJS(command)
end end
obj.onDataLoaded = onDataLoaded obj.onDataLoaded=onDataLoaded
obj.timeOut = (timeout == nil) and obj.timeOut or timeout obj.timeOut=(timeout==nil) and obj.timeOut or timeout
obj.id = id obj.id=id
obj.filename="__temp"..id
function obj:getData() function obj:getData()
--Try to read from webdb --Try to read from webdb
return love.filesystem.read("__temp"..self.id) if love.filesystem.getInfo(self.filename) then
return love.filesystem.read(self.filename)
end
end end
function obj:purgeData() function obj:purgeData()
--Data must be purged for not allowing old data to be retrieved --Data must be purged for not allowing old data to be retrieved
love.filesystem.remove("__temp"..self.id) love.filesystem.remove(self.filename)
end end
function obj:update(dt) function obj:update(dt)
self.timeOut = self.timeOut - dt self.timeOut=self.timeOut-dt
local retData = self:getData() local retData=self:getData()
if((retData ~= nil and retData ~= "nil") or self.timeOut <= 0) then if ((retData~=nil and retData~="nil") or self.timeOut<=0) then
if(retData ~= nil and retData:match("ERROR") == nil) then if (retData~=nil and retData:match("ERROR")==nil) then
if isDebugActive then if isDebugActive then
print("Data has been retrieved "..retData) print("Data has been retrieved "..retData)
end end
@@ -86,16 +89,16 @@ end
--Place this function on love.update and set it to return if it returns false (This API is synchronous) --Place this function on love.update and set it to return if it returns false (This API is synchronous)
function JS.retrieveData(dt) function JS.retrieveData(dt)
local isRetrieving = #__requestQueue ~= 0 local isRetrieving=#__requestQueue~=0
local deadRequests = {} local deadRequests={}
for i = 1, #__requestQueue do for i=1,#__requestQueue do
local isUpdating =__requestQueue[i]:update(dt) local isUpdating=__requestQueue[i]:update(dt)
if not isUpdating then if not isUpdating then
table.insert(deadRequests, i) table.insert(deadRequests, i)
end end
end end
for i = 1, #deadRequests do for i=1,#deadRequests do
if(isDebugActive) then if (isDebugActive) then
print("Request died: "..deadRequests[i]) print("Request died: "..deadRequests[i])
end end
table.remove(__requestQueue, deadRequests[i]) table.remove(__requestQueue, deadRequests[i])
@@ -104,17 +107,17 @@ function JS.retrieveData(dt)
end end
--May only be used for functions that don't return a promise --May only be used for functions that don't return a promise
function JS.newRequest(funcToCall, onDataLoaded, onError, timeout, optionalId) function JS.newRequest(funcToCall,onDataLoaded,onError,timeout,optionalId)
table.insert(__requestQueue, _Request:new(false, funcToCall, onDataLoaded, onError, timeout or 5, optionalId or _requestCount)) table.insert(__requestQueue, _Request:new(false, funcToCall, onDataLoaded, onError, timeout or 5, optionalId or _requestCount))
end end
--This function can be handled manually (in JS code) --This function can be handled manually (in JS code)
--How to: add the function call when your events resolve: FS.writeFile("Put love.filesystem.getSaveDirectory here", "Pass a string here (NUMBER DONT WORK")) --How to: add the function call when your events resolve: FS.writeFile("Put love.filesystem.getSaveDirectory here", "Pass a string here (NUMBER DONT WORK"))
--Or it can be handled by Lua, it auto sets your data if you write the following command: --Or it can be handled by Lua, it auto sets your data if you write the following command:
-- _$_(yourStringOrFunctionHere) -- _$_(yourStringOrFunctionHere)
function JS.newPromiseRequest(funcToCall, onDataLoaded, onError, timeout, optionalId) function JS.newPromiseRequest(funcToCall,onDataLoaded,onError,timeout,optionalId)
optionalId = optionalId or _requestCount optionalId=optionalId or _requestCount
funcToCall = funcToCall:gsub("_$_%(", "FS.writeFile('"..love.filesystem.getSaveDirectory().."/__temp"..optionalId.."', ") funcToCall=funcToCall:gsub("_$_%(", "FS.writeFile('"..love.filesystem.getSaveDirectory().."/__temp"..optionalId.."', ")
table.insert(__requestQueue, _Request:new(true, funcToCall, onDataLoaded, onError, timeout or 5, optionalId)) table.insert(__requestQueue, _Request:new(true, funcToCall, onDataLoaded, onError, timeout or 5, optionalId))
end end
@@ -122,14 +125,14 @@ end
--It receives the ID from ther request --It receives the ID from ther request
--Don't try printing the request.command, as it will execute the javascript command --Don't try printing the request.command, as it will execute the javascript command
function JS.setDefaultErrorFunction(func) function JS.setDefaultErrorFunction(func)
__defaultErrorFunction = func __defaultErrorFunction=func
end end
JS.setDefaultErrorFunction(function(id, error) JS.setDefaultErrorFunction(function(id,error)
if( isDebugActive ) then if (isDebugActive) then
local msg = "Data could not be loaded for id:'"..id.."'" local msg="Data could not be loaded for id:'"..id.."'"
if(error)then if (error) then
msg = msg.."\nError: "..error msg=msg.."\nError: "..error
end end
print(msg) print(msg)
end end
@@ -140,6 +143,6 @@ JS.callJS(JS.stringFunc(
[[ [[
__getWebDB("%s"); __getWebDB("%s");
]] ]]
, "__LuaJSDB")) , "__LuaJSDB"))
return JS return JS