diff --git a/parts/globalTables.lua b/parts/globalTables.lua index 710fbf59..858dbb25 100644 --- a/parts/globalTables.lua +++ b/parts/globalTables.lua @@ -282,6 +282,16 @@ SETTING={ VKAlpha=.3, } +ACCOUNT={ + email=nil, + auth_token=nil, + access_token=nil, + + username=nil, + motto=nil, + avatar=nil, +} + STAT={ version=VERSION_NAME, run=0,game=0,time=0, diff --git a/parts/scenes/load.lua b/parts/scenes/load.lua index fd6efa41..75d4c294 100644 --- a/parts/scenes/load.lua +++ b/parts/scenes/load.lua @@ -117,6 +117,23 @@ function Tmr.load() SFX.play("welcome_sfx") VOC.play("welcome_voc") httpRequest(TICK.httpREQ_launch,"/tech/api/v1/app/info") + if ACCOUNT.auth_token then + local success,data=json.encode({ + email=ACCOUNT.email, + auth_token=ACCOUNT.auth_token, + }) + if not success then + LOG.print(text.jsonError,"warn") + else + httpRequest( + TICK.httpREQ_autoLogin, + "/tech/api/v1/users", + "GET", + {["Content-Type"]="application/json"}, + data + ) + end + end end if S.tar then S.cur=S.cur+1 diff --git a/parts/scenes/login.lua b/parts/scenes/login.lua index a789c88e..7a1cf7d3 100644 --- a/parts/scenes/login.lua +++ b/parts/scenes/login.lua @@ -16,7 +16,7 @@ function keyDown.login(key) return end httpRequest( - TICK.httpREQ_login, + TICK.httpREQ_newLogin, "/tech/api/v1/users", "GET", {["Content-Type"]="application/json"}, diff --git a/parts/tick.lua b/parts/tick.lua index a1dc61d1..b876d9ef 100644 --- a/parts/tick.lua +++ b/parts/tick.lua @@ -149,20 +149,54 @@ function Tick.httpREQ_register(data) return true end end -function Tick.httpREQ_login(data) +function Tick.httpREQ_newLogin(data) local response,request_error=client.poll(data.task) if response then if response.code==200 then + LOGIN=true local success,content=json.decode(response.body) if success then LOG.print(text.loginSuccessed..": "..content.message) - ACCOUNT.username=content.username + local _success,_content=json.decode(data) + ACCOUNT.email=_content.email ACCOUNT.auth_token=content.auth_token - FILE.saveAccount() + FILE.save(ACCOUNT,"account","") else LOG.print(text.jsonError,"warn") end else + LOGIN=false + local success,content=json.decode(response.body) + if success then + LOG.print(text.netErrorCode..response.code..": "..content.message,"warn") + else + LOG.print(text.netErrorCode..response.code,"warn") + end + end + return true + elseif request_error then + LOG.print(text.registerFailed..": "..request_error,"warn") + return true + end + data.time=data.time+1 + if data.time==360 then + LOG.print(text.httpTimeout,"message") + return true + end +end +function Tick.httpREQ_autoLogin(data) + local response,request_error=client.poll(data.task) + if response then + if response.code==200 then + LOGIN=true + local success,content=json.decode(response.body) + if success then + LOG.print(text.loginSuccessed..": "..content.message) + else + LOG.print(text.jsonError,"warn") + end + else + LOGIN=false local success,content=json.decode(response.body) if success then LOG.print(text.netErrorCode..response.code..": "..content.message,"warn")