整理tick函数,大多数不再是全局,而是直接放到使用的场景作为local变量

This commit is contained in:
MrZ626
2020-12-06 02:53:00 +08:00
parent ebd2cce1db
commit bcacee9d6e
7 changed files with 319 additions and 314 deletions

View File

@@ -4,6 +4,69 @@ local Timer=love.timer.getTime
local max,min,sin=math.max,math.min,math.sin
local function tick_httpREQ_launch(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
local res=json.decode(response.body)
if res then
if response.code==200 then
LOG.print(res.notice,360,COLOR.sky)
if VERSION_CODE>=res.version_code then
LOG.print(text.versionIsNew,360,COLOR.sky)
else
LOG.print(string.gsub(text.versionIsOld,"$1",res.version_name),"warn")
end
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.getNoticeFail..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
function tick_httpREQ_autoLogin(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
LOGIN=true
local res=json.decode(response.body)
if res then
LOG.print(text.loginSuccessed)
end
else
LOGIN=false
local err=json.decode(response.body)
if err then
LOG.print(text.loginFailed..": "..text.netErrorCode..response.code.."-"..err.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
local scene={}
function scene.sceneInit()
@@ -118,10 +181,10 @@ function scene.update()
LOADED=true
SFX.play("welcome_sfx")
VOC.play("welcome_voc")
httpRequest(TICK.httpREQ_launch,PATH.api..PATH.appInfo)
httpRequest(tick_httpREQ_launch,PATH.api..PATH.appInfo)
if ACCOUNT.auth_token and ACCOUNT.email then
httpRequest(
TICK.httpREQ_autoLogin,
tick_httpREQ_autoLogin,
PATH.api..PATH.auth,
"GET",
{["Content-Type"]="application/json"},

View File

@@ -1,15 +1,55 @@
local scene={}
local function tick_httpREQ_newLogin(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
local res=json.decode(response.body)
LOGIN=response.code==200
if res then
if LOGIN then
LOG.print(text.loginSuccessed)
ACCOUNT.email=res.email
ACCOUNT.auth_token=res.auth_token
FILE.save(ACCOUNT,"account","")
function login()
httpRequest(
TICK.httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=ACCOUNT.email,
auth_token=ACCOUNT.auth_token,
}
)
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
local function login()
local email= WIDGET.active.email.value
local password= WIDGET.active.password.value
if #email==0 or not email:match("^[a-zA-Z0-9_]+@[a-zA-Z0-9_-]+%.[a-zA-Z0-9_]+$") then
if #email==0 or not email:match("^[a-zA-Z0-9_]+@[a-zA-Z0-9_-]+%.[a-zA-Z0-9_]+$")then
LOG.print(text.wrongEmail)return
elseif #password==0 then
LOG.print(text.noPassword)return
end
httpRequest(
TICK.httpREQ_newLogin,
tick_httpREQ_newLogin,
PATH.api..PATH.auth,
"GET",
{["Content-Type"]="application/json"},
@@ -20,6 +60,8 @@ function login()
)
end
local scene={}
function scene.keyDown(key)
if key=="escape"then
SCN.back()

View File

@@ -1,5 +1,45 @@
local gc=love.graphics
local function tick_httpREQ_checkAccessToken(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
LOG.print(text.accessSuccessed)
SCN.pop()
SCN.go("netgame")
elseif response.code==403 or response.code==401 then
httpRequest(
TICK.httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=ACCOUNT.email,
auth_token=ACCOUNT.auth_token,
}
)
else
local err=json.decode(response.body)
if err then
LOG.print(text.netErrorCode..response.code..": "..err.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
local scene={}
function scene.sceneInit()
@@ -41,7 +81,7 @@ scene.widgetList={
if LOGIN then
if ACCOUNT.access_token then
httpRequest(
TICK.httpREQ_checkAccessToken,
tick_httpREQ_checkAccessToken,
PATH.api..PATH.access,
"GET",
{["Content-Type"]="application/json"},

View File

@@ -1,3 +1,30 @@
local function tick_httpREQ_register(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
local res=json.decode(response.body)
if res then
if response.code==200 then
LOG.print(text.registerSuccessed..": "..res.message)
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
local scene={}
function scene.keyDown(key)
@@ -16,7 +43,7 @@ function scene.keyDown(key)
LOG.print(text.diffPassword)return
end
httpRequest(
TICK.httpREQ_register,
tick_httpREQ_register,
PATH.api..PATH.auth,
"POST",
{["Content-Type"]="application/json"},