From 37d7dd5fe08ed60664d15f30acaf70c7ce2ca167 Mon Sep 17 00:00:00 2001 From: Particle_G <38580644+ParticleG@users.noreply.github.com> Date: Thu, 3 Dec 2020 16:09:25 +0800 Subject: [PATCH] Implemented chatting room (#17) --- Zframework/task.lua | 7 ++++ Zframework/toolfunc.lua | 81 ++++++++++++++++++----------------- Zframework/widget.lua | 31 ++++++++++++++ parts/globalTables.lua | 4 +- parts/language/lang_en.lua | 2 + parts/language/lang_fr.lua | 2 + parts/language/lang_sp.lua | 2 + parts/language/lang_zh.lua | 2 + parts/scenes/chat.lua | 36 +++++++--------- parts/scenes/main.lua | 2 +- parts/tick.lua | 86 +++++++++++++++++++++++++++++++++++--- 11 files changed, 189 insertions(+), 66 deletions(-) diff --git a/Zframework/task.lua b/Zframework/task.lua index 3f4bee7d..462e7164 100644 --- a/Zframework/task.lua +++ b/Zframework/task.lua @@ -45,6 +45,13 @@ function TASK.removeTask_data(data) end end end +function TASK.checkTask_code(code) + for i=#tasks,1,-1 do + if tasks[i].code==code then + return true + end + end +end function TASK.clear() local i=#tasks while i>0 do diff --git a/Zframework/toolfunc.lua b/Zframework/toolfunc.lua index e5d40775..df117645 100644 --- a/Zframework/toolfunc.lua +++ b/Zframework/toolfunc.lua @@ -166,44 +166,6 @@ do--dumpTable return s..tabs[t-1].."}" end end -do--httpRequest - client=LOADLIB("NETlib") - httpRequest= - client and function(tick,path,method,header,body) - local task,err=client.httpraw{ - url="http://krakens.tpddns.cn:10026"..path, - method=method or"GET", - header=header, - body=body, - } - if task then - TASK.new(tick,{task=task,time=0,net=true}) - else - LOG.print("NETlib error: "..err,"warn") - end - TASK.netTaskCount=TASK.netTaskCount+1 - end or - function() - LOG.print("[NO NETlib]",5,COLOR.yellow) - end - - wsConnect= - client and function(tick,path,header) - local task,err=client.wsraw{ - url="http://krakens.tpddns.cn:10026"..path, - header=header, - } - if task then - TASK.new(tick,{wsconntask=task,time=0,net=true}) - else - LOG.print("NETlib error: "..err,"warn") - end - TASK.netTaskCount=TASK.netTaskCount+1 - end or - function() - LOG.print("[NO NETlib]",5,COLOR.yellow) - end -end do--json -- -- json.lua @@ -564,7 +526,6 @@ do--json end end end - do--urlencode local rshift=bit.rshift local b16={[0]="0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"} @@ -581,6 +542,48 @@ do--urlencode return out end end +do--httpRequest + client=LOADLIB("NETlib") + httpRequest= + client and function(tick,path,method,header,body) + local task,err=client.httpraw{ + url="http://krakens.tpddns.cn:10026"..path, + -- url="http://127.0.0.1:10026"..path, + method=method or"GET", + header=header, + body=body, + } + if task then + TASK.new(tick,{task=task,time=0,net=true}) + else + LOG.print("NETlib error: "..err,"warn") + end + TASK.netTaskCount=TASK.netTaskCount+1 + end or + function() + LOG.print("[NO NETlib]",5,COLOR.yellow) + end + + wsConnect= + client and function(tick,path,header) + local task,err=client.wsraw{ + url="ws://krakens.tpddns.cn:10026"..path, + origin="krakens.tpddns.cn", + -- url="ws://127.0.0.1:10026"..path, + -- origin="127.0.0.1", + header=header, + } + if task then + TASK.new(tick,{wsconntask=task,time=0,net=true}) + else + LOG.print("NETlib error: "..err,"warn") + end + TASK.netTaskCount=TASK.netTaskCount+1 + end or + function() + LOG.print("[NO NETlib]",5,COLOR.yellow) + end +end function copyList(org) local L={} for i=1,#org do diff --git a/Zframework/widget.lua b/Zframework/widget.lua index 34dd9a9c..aabb8c8f 100644 --- a/Zframework/widget.lua +++ b/Zframework/widget.lua @@ -806,6 +806,37 @@ function WIDGET.lnk_STPeq(k,v) return function() return sceneTemp[k]==v end e function WIDGET.lnk_pressKey(k) return function() love.keypressed(k) end end function WIDGET.lnk_goScene(t,s) return function() SCN.go(t,s) end end function WIDGET.lnk_swapScene(t,s) return function() SCN.swapTo(t,s) end end +function WIDGET.lnk_goNetgame() + if LOGIN then + if ACCOUNT.access_token then + local res=json.encode{ + email=ACCOUNT.email, + access_token=ACCOUNT.access_token, + } + httpRequest( + TICK.httpREQ_checkAccessToken, + PATH.api..PATH.access, + "GET", + {["Content-Type"]="application/json"}, + res + ) + else + local payload=json.encode{ + email=ACCOUNT.email, + auth_token=ACCOUNT.auth_token, + } + httpRequest( + TICK.httpREQ_getAccessToken, + PATH.api..PATH.access, + "POST", + {["Content-Type"]="application/json"}, + payload + ) + end + else + SCN.go("login") + end +end local indexMeta={ __index=function(L,k) diff --git a/parts/globalTables.lua b/parts/globalTables.lua index 846795f5..3db7d768 100644 --- a/parts/globalTables.lua +++ b/parts/globalTables.lua @@ -311,11 +311,13 @@ ACCOUNT={ PATH={ api="/tech/api/v1", + socket="/tech/socket/v1", appInfo="/app/info", users="/users", auth="/auth", access="/auth/access", - versus="/online/versus" + versus="/online/versus", + chat="/chat", } keyMap={ diff --git a/parts/language/lang_en.lua b/parts/language/lang_en.lua index 0262b8fb..a48394b4 100644 --- a/parts/language/lang_en.lua +++ b/parts/language/lang_en.lua @@ -81,6 +81,8 @@ return{ registerFailed="Registration failed", loginSuccessed="Successfully logged in!", loginFailed="Login failed", + accessSuccessed="Successfully authorized!", + accessFailed="Authorization failed", wsSuccessed="Websocket connected!", wsFailed="Websocket failed to connect", diff --git a/parts/language/lang_fr.lua b/parts/language/lang_fr.lua index 4c965939..31670780 100644 --- a/parts/language/lang_fr.lua +++ b/parts/language/lang_fr.lua @@ -84,6 +84,8 @@ return{ registerFailed="Erreur d'enregistrement", -- loginSuccessed="Successfully logged in!", -- loginFailed="Login failed", + -- accessSuccessed="Successfully authorized!", + -- accessFailed="Authorization failed", -- wsSuccessed="Websocket connected!", -- wsFailed="Websocket failed to connect", diff --git a/parts/language/lang_sp.lua b/parts/language/lang_sp.lua index cad181b2..811729ba 100644 --- a/parts/language/lang_sp.lua +++ b/parts/language/lang_sp.lua @@ -85,6 +85,8 @@ return{ registerFailed="Registro fallido.", -- loginSuccessed="Successfully logged in!", -- loginFailed="Login failed", + -- accessSuccessed="Successfully authorized!", + -- accessFailed="Authorization failed", -- wsSuccessed="Websocket connected!", -- wsFailed="Websocket failed to connect", diff --git a/parts/language/lang_zh.lua b/parts/language/lang_zh.lua index e82cd236..7252d980 100644 --- a/parts/language/lang_zh.lua +++ b/parts/language/lang_zh.lua @@ -82,6 +82,8 @@ return{ registerFailed="注册失败", loginSuccessed="登录成功!", loginFailed="登录失败", + accessSuccessed="授权成功!", + accessFailed="授权失败", wsSuccessed="连接成功!", wsFailed="连接失败", diff --git a/parts/scenes/chat.lua b/parts/scenes/chat.lua index 6163ccb9..457a40a3 100644 --- a/parts/scenes/chat.lua +++ b/parts/scenes/chat.lua @@ -1,27 +1,8 @@ -local function send() - local W=WIDGET.active.text - --sendMessage(W.value) - W.value="" -end - -function sceneInit.chat() - BG.set("none") -end - -local function socketConnect() - wsConnect( - TICK.wsCONN_connect, - "/solo?room_id=114", - {} - ) -end -local function socketWrite() +local function socketWrite(message) if not WSCONN then LOG.print("尚未连接到服务器","warn") return end - local message = WIDGET.active.message.value - print("TextBox: "..message) local writeErr = client.write(WSCONN, message) if writeErr then print(writeErr, "warn") @@ -29,6 +10,21 @@ local function socketWrite() return true end +local function send() + local W=WIDGET.active.text + socketWrite(W.value) + W.value="" +end + +function sceneInit.chat() + BG.set("none") + wsConnect( + TICK.wsCONN_connect, + PATH.socket..PATH.chat.."?email="..urlEncode(ACCOUNT.email).."&access_token="..urlEncode(ACCOUNT.access_token), + {} + ) +end + WIDGET.init("chat",{ WIDGET.newTextBox{name="text", x=40, y=500,w=980,h=180,font=40}, WIDGET.newButton{name="send", x=1140, y=540,w=170,h=80,font=40,code=send}, diff --git a/parts/scenes/main.lua b/parts/scenes/main.lua index 3b61199a..70766dfc 100644 --- a/parts/scenes/main.lua +++ b/parts/scenes/main.lua @@ -35,7 +35,7 @@ end WIDGET.init("main",{ WIDGET.newButton{name="offline",x=150,y=220,w=200,h=140,color="lRed", font=40,code=WIDGET.lnk_goScene("mode")}, - WIDGET.newButton{name="online", x=370,y=220,w=200,h=140,color="lCyan", font=40,code=function()SCN.go(LOGIN and"netgame"or"login")end}, + WIDGET.newButton{name="online", x=370,y=220,w=200,h=140,color="lCyan", font=40,code=WIDGET.lnk_goNetgame}, WIDGET.newButton{name="custom", x=590,y=220,w=200,h=140,color="lBlue", font=40,code=WIDGET.lnk_goScene("customGame")}, WIDGET.newButton{name="setting",x=150,y=380,w=200,h=140,color="lOrange",font=40,code=WIDGET.lnk_goScene("setting_game")}, WIDGET.newButton{name="stat", x=370,y=380,w=200,h=140,color="lGreen", font=40,code=WIDGET.lnk_goScene("stat")}, diff --git a/parts/tick.lua b/parts/tick.lua index ed151418..812f3a05 100644 --- a/parts/tick.lua +++ b/parts/tick.lua @@ -145,8 +145,18 @@ function Tick.httpREQ_newLogin(data) ACCOUNT.email=res.email ACCOUNT.auth_token=res.auth_token FILE.save(ACCOUNT,"account","") - SCN.pop() - SCN.go("netgame") + + local payload=json.encode{ + email=ACCOUNT.email, + auth_token=ACCOUNT.auth_token, + } + httpRequest( + TICK.httpREQ_getAccessToken, + PATH.api..PATH.access, + "POST", + {["Content-Type"]="application/json"}, + payload + ) else LOG.print(text.netErrorCode..response.code..": "..res.message,"warn") end @@ -169,6 +179,38 @@ function Tick.httpREQ_autoLogin(data) 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 true + elseif request_error then + LOG.print(text.loginFailed..": "..request_error,"warn") + return true + end + return checkTimeout(data,360) +end +function Tick.httpREQ_checkAccessToken(data) + local response,request_error=client.poll(data.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 + local payload=json.encode{ + email=ACCOUNT.email, + auth_token=ACCOUNT.auth_token, + } + httpRequest( + TICK.httpREQ_getAccessToken, + PATH.api..PATH.access, + "POST", + {["Content-Type"]="application/json"}, + payload + ) + else local err=json.decode(response.body) if err then LOG.print(text.netErrorCode..response.code..": "..err.message,"warn") @@ -181,8 +223,43 @@ function Tick.httpREQ_autoLogin(data) end return checkTimeout(data,360) end +function Tick.httpREQ_getAccessToken(data) + local response,request_error=client.poll(data.task) + if response then + if response.code==200 then + local res=json.decode(response.body) + if res then + LOG.print(text.accessSuccessed) + ACCOUNT.access_token=res.access_token + FILE.save(ACCOUNT,"account","") + SCN.pop() + SCN.go("netgame") + else + LOG.print(text.netErrorCode..response.code..": "..res.message,"warn") + SCN.pop() + SCN.go("main") + end + else + LOGIN=false + ACCOUNT.access_token=nil + ACCOUNT.auth_token=nil + 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 + SCN.pop() + SCN.go("main") + end + return true + elseif request_error then + LOG.print(text.loginFailed..": "..request_error,"warn") + return true + end + return checkTimeout(data,360) +end function Tick.wsCONN_connect(data) - print("Running wsconntask...") if data.wsconntask then local wsconn,connErr=client.poll(data.wsconntask) if wsconn then @@ -203,8 +280,7 @@ function Tick.wsCONN_read(data) local messages,readErr=client.read(WSCONN) if messages then if messages[1] then - print(messages[1]) - LOG.print("Message: "..messages[1]) + LOG.print(messages[1]) end elseif readErr then print("Read error: "..readErr)