From 387e2727639073f46a0a005346ee79756855770c Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Fri, 8 Jan 2021 23:17:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4tick=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=EF=BC=8C=E5=86=85=E5=AE=B9=E7=A7=BB=E8=87=B3gametoolfunc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.lua | 1 - parts/gametoolfunc.lua | 70 +++++++++++++++++++++++++++++++++++++++ parts/scenes/login.lua | 2 +- parts/scenes/main.lua | 4 +-- parts/scenes/net_chat.lua | 2 +- parts/tick.lua | 63 ----------------------------------- 6 files changed, 74 insertions(+), 68 deletions(-) delete mode 100644 parts/tick.lua diff --git a/main.lua b/main.lua index f3e3f794..1808e296 100644 --- a/main.lua +++ b/main.lua @@ -109,7 +109,6 @@ SKIN= require"parts/skin" PLY= require"parts/player" AIFUNC= require"parts/ai" MODES= require"parts/modes" -TICK= require"parts/tick" --First start for phones if not fs.getInfo("conf/settings")and MOBILE then diff --git a/parts/gametoolfunc.lua b/parts/gametoolfunc.lua index 4ee09e7a..8fb34941 100644 --- a/parts/gametoolfunc.lua +++ b/parts/gametoolfunc.lua @@ -7,6 +7,8 @@ local sub=string.sub local char,byte=string.char,string.byte local ins,rem=table.insert,table.remove + + --Encoding Functions --Sep symbol: 33 (!) --Safe char: 34~126 @@ -252,6 +254,7 @@ function pasteQuestArgs(str) end + --Royale mode function randomTarget(P)--Return a random opponent for P if #PLAYERS.alive>1 then @@ -344,6 +347,8 @@ function royaleLevelup() end end + + --Game function freshDate() local date=os.date("%Y/%m/%d") @@ -790,4 +795,69 @@ do--lnk_CUS/SETXXX(k) function lnk_SETrev(k)return function()s[k]=not s[k]end end function lnk_CUSsto(k)return function(i)c[k]=i end end function lnk_SETsto(k)return function(i)s[k]=i end end +end + + + +--Network tick function +function TICK_httpREQ_getAccessToken(task) + local time=0 + while true do + coroutine.yield() + local response,request_error=client.poll(task) + if response then + if response.code==200 then + local res=json.decode(response.body) + if res then + LOG.print(text.accessSuccessed) + USER.access_token=res.access_token + FILE.save(USER,"conf/user") + SCN.swapTo("net_menu") + else + LOG.print(text.netErrorCode..response.code..": "..res.message,"warn") + end + else + LOGIN=false + USER.access_token=false + USER.auth_token=false + local err=json.decode(response.body) + if err then + LOG.print(text.loginFailed..": "..text.netErrorCode..response.code.."-"..err.message,"warn") + else + LOG.print(text.loginFailed..": "..text.netErrorCode,"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.loginFailed..": "..text.httpTimeout,"message") + return + end + end +end +function TICK_wsRead() + while true do + coroutine.yield() + if not WSCONN then return end + local messages,readErr=client.read(WSCONN) + if messages then + if SCN.socketRead then + for i=1,#messages do + SCN.socketRead(messages[i]) + end + else + return + end + elseif readErr then + wsWrite("/quit") + WSCONN=false + LOG.print(text.wsError..tostring(readErr),"warn") + SCN.back() + return + end + end end \ No newline at end of file diff --git a/parts/scenes/login.lua b/parts/scenes/login.lua index 830f3425..419a19c3 100644 --- a/parts/scenes/login.lua +++ b/parts/scenes/login.lua @@ -14,7 +14,7 @@ local function tick_httpREQ_newLogin(task) FILE.save(USER,"conf/user","q") httpRequest( - TICK.httpREQ_getAccessToken, + TICK_httpREQ_getAccessToken, PATH.api..PATH.access, "POST", {["Content-Type"]="application/json"}, diff --git a/parts/scenes/main.lua b/parts/scenes/main.lua index 052a061d..0aa5451a 100644 --- a/parts/scenes/main.lua +++ b/parts/scenes/main.lua @@ -11,7 +11,7 @@ local function tick_httpREQ_checkAccessToken(task) SCN.go("net_menu") elseif response.code==403 or response.code==401 then httpRequest( - TICK.httpREQ_getAccessToken, + TICK_httpREQ_getAccessToken, PATH.api..PATH.access, "POST", {["Content-Type"]="application/json"}, @@ -94,7 +94,7 @@ scene.widgetList={ ) else httpRequest( - TICK.httpREQ_getAccessToken, + TICK_httpREQ_getAccessToken, PATH.api..PATH.access, "POST", {["Content-Type"]="application/json"}, diff --git a/parts/scenes/net_chat.lua b/parts/scenes/net_chat.lua index 3114db73..d6a43810 100644 --- a/parts/scenes/net_chat.lua +++ b/parts/scenes/net_chat.lua @@ -40,7 +40,7 @@ function scene.sceneInit() end scrollPos=#texts TASK.new(focusAtTextbox)--Widgets are not initialized, so active after 1 frame - TASK.new(TICK.ws_read) + TASK.new(TICK_wsRead) BG.set("none") end function scene.sceneBack() diff --git a/parts/tick.lua b/parts/tick.lua deleted file mode 100644 index 7003d440..00000000 --- a/parts/tick.lua +++ /dev/null @@ -1,63 +0,0 @@ -local Tick={} -function Tick.httpREQ_getAccessToken(task) - local time=0 - while true do - coroutine.yield() - local response,request_error=client.poll(task) - if response then - if response.code==200 then - local res=json.decode(response.body) - if res then - LOG.print(text.accessSuccessed) - USER.access_token=res.access_token - FILE.save(USER,"conf/user") - SCN.swapTo("net_menu") - else - LOG.print(text.netErrorCode..response.code..": "..res.message,"warn") - end - else - LOGIN=false - USER.access_token=false - USER.auth_token=false - local err=json.decode(response.body) - if err then - LOG.print(text.loginFailed..": "..text.netErrorCode..response.code.."-"..err.message,"warn") - else - LOG.print(text.loginFailed..": "..text.netErrorCode,"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.loginFailed..": "..text.httpTimeout,"message") - return - end - end -end - -function Tick.ws_read() - while true do - coroutine.yield() - if not WSCONN then return end - local messages,readErr=client.read(WSCONN) - if messages then - if SCN.socketRead then - for i=1,#messages do - SCN.socketRead(messages[i]) - end - else - return - end - elseif readErr then - wsWrite("/quit") - WSCONN=false - LOG.print(text.wsDisconnected,"warn") - return - end - end -end -return Tick \ No newline at end of file