From 2a5242aa6e8171d8ae760ec7ce5d394a514ffd78 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Sat, 5 Dec 2020 16:53:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E5=AE=A4=E6=B7=BB=E5=8A=A0ui?= =?UTF-8?q?=EF=BC=8Cwebsocket=E8=BF=9E=E6=8E=A5/=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=8B=93=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/scene.lua | 2 + Zframework/toolfunc.lua | 98 ++++++++++++++++++++---------- parts/language/lang_en.lua | 12 +++- parts/language/lang_fr.lua | 12 +++- parts/language/lang_sp.lua | 12 +++- parts/language/lang_zh.lua | 16 +++-- parts/scenes/chat.lua | 121 +++++++++++++++++++++++++++++++++---- parts/scenes/history.lua | 19 ------ parts/scenes/music.lua | 19 ------ parts/tick.lua | 11 ++-- 10 files changed, 223 insertions(+), 99 deletions(-) diff --git a/Zframework/scene.lua b/Zframework/scene.lua index eaf39a3a..fcfac74c 100644 --- a/Zframework/scene.lua +++ b/Zframework/scene.lua @@ -33,6 +33,7 @@ local SCN={ keyUp=nil, gamepadDown=nil, gamepadUp=nil, + socketRead=nil, }--Scene datas, returned function SCN.add(name,scene) @@ -72,6 +73,7 @@ function SCN.init(s,org) SCN.keyUp=S.keyUp SCN.gamepadDown=S.gamepadDown SCN.gamepadUp=S.gamepadUp + SCN.socketRead=S.socketRead if SCN.sceneInit then SCN.sceneInit(org)end end function SCN.push(tar,style) diff --git a/Zframework/toolfunc.lua b/Zframework/toolfunc.lua index cca298d5..9de13323 100644 --- a/Zframework/toolfunc.lua +++ b/Zframework/toolfunc.lua @@ -544,41 +544,75 @@ do--urlencode end do--httpRequest & wsConnect 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.newNet(tick,task) - else - LOG.print("NETlib error: "..err,"warn") + if client then + function httpRequest(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.newNet(tick,task) + else + LOG.print("NETlib error: "..err,"warn") + end + TASK.netTaskCount=TASK.netTaskCount+1 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", - header=header, - } - if task then - TASK.newNet(tick,task) - else - LOG.print("NETlib error: "..err,"warn") + function wsConnect(tick,path,header) + local task,err=client.wsraw{ + url="ws://krakens.tpddns.cn:10026"..path, + origin="krakens.tpddns.cn", + header=header, + } + if task then + TASK.newNet(tick,task) + else + LOG.print("NETlib error: "..err,"warn") + end + TASK.netTaskCount=TASK.netTaskCount+1 + end + + function wsWrite(data) + if WSCONN then + local writeErr=client.write(WSCONN,data) + if writeErr then + LOG.print(writeErr,"error") + return + end + return true + else + LOG.print(text.wsNoConn,"warn") + end + end + else + local function noNetLib() + LOG.print("[NO NETlib]",5,COLOR.yellow) + end + httpRequest=noNetLib + wsConnect=noNetLib + wsWrite=noNetLib + end +end +do--wheelScroll + local floatWheel=0 + function wheelScroll(y) + if y>0 then + if floatWheel<0 then floatWheel=0 end + floatWheel=floatWheel+y^1.2 + elseif y<0 then + if floatWheel>0 then floatWheel=0 end + floatWheel=floatWheel-(-y)^1.2 + end + while floatWheel>=1 do + love.keypressed("up") + floatWheel=floatWheel-1 + end + while floatWheel<=-1 do + love.keypressed("down") + floatWheel=floatWheel+1 end - TASK.netTaskCount=TASK.netTaskCount+1 - end or - function() - LOG.print("[NO NETlib]",5,COLOR.yellow) end end function copyList(org) diff --git a/parts/language/lang_en.lua b/parts/language/lang_en.lua index d17478c4..a6b6282e 100644 --- a/parts/language/lang_en.lua +++ b/parts/language/lang_en.lua @@ -86,8 +86,15 @@ return{ loginFailed="Login failed", accessSuccessed="Successfully authorized!", accessFailed="Authorization failed", - wsSuccessed="Websocket connected!", - wsFailed="Websocket failed to connect", + -- wsSuccessed="WS连接成功", + -- wsFailed="WS连接失败", + -- wsDisconnected="WS连接断开", + -- wsNoConn="WS未连接", + + -- chatJoin="有人来了,当前人数:", + -- chatLeave="有人离开了,当前人数:", + -- chatRemain="人数:", + -- chatHistory="------以上是历史消息------", errorMsg="An error has occurred and Techmino needs to restart.\nError info has been created, and you can send it to the author.", @@ -215,6 +222,7 @@ return{ chat="Chat", }, chat={ + clear="Clear", send="Send", }, setting_game={ diff --git a/parts/language/lang_fr.lua b/parts/language/lang_fr.lua index cb61394e..8f177f3d 100644 --- a/parts/language/lang_fr.lua +++ b/parts/language/lang_fr.lua @@ -89,8 +89,15 @@ return{ -- loginFailed="Login failed", -- accessSuccessed="Successfully authorized!", -- accessFailed="Authorization failed", - -- wsSuccessed="Websocket connected!", - -- wsFailed="Websocket failed to connect", + -- wsSuccessed="WS连接成功", + -- wsFailed="WS连接失败", + -- wsDisconnected="WS连接断开", + -- wsNoConn="WS未连接", + + -- chatJoin="有人来了,当前人数:", + -- chatLeave="有人离开了,当前人数:", + -- chatRemain="人数:", + -- chatHistory="------以上是历史消息------", errorMsg="Une erreur est survenue et Techmino doit redémarrer.\nDes informations concernant l'erreur ont été créées, et vous pouvez les envoyer au créateur.", @@ -217,6 +224,7 @@ return{ -- chat="Chat", }, chat={ + -- clear="Clear", -- send="Send", }, setting_game={ diff --git a/parts/language/lang_sp.lua b/parts/language/lang_sp.lua index 82d2e549..42dd07d5 100644 --- a/parts/language/lang_sp.lua +++ b/parts/language/lang_sp.lua @@ -90,8 +90,15 @@ return{ -- loginFailed="Login failed", -- accessSuccessed="Successfully authorized!", -- accessFailed="Authorization failed", - -- wsSuccessed="Websocket connected!", - -- wsFailed="Websocket failed to connect", + -- wsSuccessed="WS连接成功", + -- wsFailed="WS连接失败", + -- wsDisconnected="WS连接断开", + -- wsNoConn="WS未连接", + + -- chatJoin="有人来了,当前人数:", + -- chatLeave="有人离开了,当前人数:", + -- chatRemain="人数:", + -- chatHistory="------以上是历史消息------", errorMsg="Ha ocurrido un error y Techmino necesita reiniciarse.\nSe creó un registro de error, puedes enviarlo al autor.", @@ -221,6 +228,7 @@ return{ -- chat="Chat", }, chat={ + -- clear="Clear", -- send="Send", }, setting_game={ diff --git a/parts/language/lang_zh.lua b/parts/language/lang_zh.lua index 02b0650e..4aa8f5a7 100644 --- a/parts/language/lang_zh.lua +++ b/parts/language/lang_zh.lua @@ -83,12 +83,19 @@ return{ diffPassword="两次密码不一致", registerSuccessed="注册成功!", registerFailed="注册失败", - loginSuccessed="登录成功!", + loginSuccessed="登录成功", loginFailed="登录失败", - accessSuccessed="授权成功!", + accessSuccessed="授权成功", accessFailed="授权失败", - wsSuccessed="连接成功!", - wsFailed="连接失败", + wsSuccessed="WS连接成功", + wsFailed="WS连接失败", + wsDisconnected="WS连接断开", + wsNoConn="WS未连接", + + chatJoin="有人来了,当前人数:", + chatLeave="有人离开了,当前人数:", + chatRemain="人数:", + chatHistory="------以上是历史消息------", errorMsg="Techmino遭受了雷击,需要重新启动.\n我们已收集了一些错误信息,你可以向作者进行反馈.", @@ -241,6 +248,7 @@ return{ chat="聊天室", }, chat={ + clear="清空", send="发送", }, setting_game={ diff --git a/parts/scenes/chat.lua b/parts/scenes/chat.lua index 70f2a187..2c8e6f6f 100644 --- a/parts/scenes/chat.lua +++ b/parts/scenes/chat.lua @@ -1,36 +1,66 @@ -local function socketWrite(message) - if WSCONN then - local writeErr=client.write(WSCONN,message) - if writeErr then print(writeErr,"warn")end - else - LOG.print("尚未连接到服务器","warn") - end -end +local gc=love.graphics +local Timer=love.timer.getTime + +local ins=table.insert +local max,min=math.max,math.min + +local texts={} +local remain--People in chat room +local scroll--Bottom message no. +local newMessasge=false--If there is a new message local function sendMessage() local W=WIDGET.active.text - if #W.value>0 then - socketWrite(W.value) + if #W.value>0 and wsWrite(W.value)then W.value="" end end +local function clearHistory() + if #texts>0 then + texts={} + scroll=0 + SFX.play("fall") + collectgarbage() + end +end local scene={} function scene.sceneInit() + remain=nil + + scroll=#texts + if scroll>0 then + if texts[scroll][1]~=COLOR.green then + ins(texts,{COLOR.green,text.chatHistory}) + scroll=scroll+1 + end + end + WIDGET.sel=WIDGET.active.text BG.set("none") wsConnect( TICK.wsCONN_connect, PATH.socket..PATH.chat.."?email="..urlEncode(ACCOUNT.email).."&access_token="..urlEncode(ACCOUNT.access_token) ) end - function scene.sceneBack() - WSCONN=nil + wsWrite("/quit") + WSCONN=nil + LOG.print(text.wsDisconnected,"warn") end +function scene.wheelMoved(_,y) + wheelScroll(y) +end function scene.keyDown(k) - if k=="return"then + if k=="up"then + scroll=max(scroll-1,min(#texts,12)) + elseif k=="down"then + scroll=min(scroll+1,#texts) + if scroll==#texts then + newMessasge=false + end + elseif k=="return"then sendMessage() elseif k=="escape"then SCN.back() @@ -39,8 +69,73 @@ function scene.keyDown(k) end end +function scene.socketRead(mes) + if mes:byte()==35 then--system message + local sep=mes:find(":") + local cmd=mes:sub(2,sep-1) + local data=mes:sub(sep+1) + if cmd=="J"then + remain=tonumber(data) + if remain<=10 then + ins(texts,{COLOR.yellow,text.chatJoin..remain}) + end + elseif cmd=="L"then + remain=tonumber(data) + if remain<=10 then + ins(texts,{COLOR.yellow,text.chatLeave..remain}) + end + end + else--user message + local sep=mes:find(":") + local num=mes:find("#") + ins(texts,{ + COLOR.white,mes:sub(1,num-1), + COLOR.dY,mes:sub(num,sep-1).." ", + COLOR.sky,mes:sub(sep+1), + }) + end + if scroll==#texts-1 then + scroll=scroll+1 + else + SFX.play("spin_0",.8) + newMessasge=true + end +end + +function scene.draw() + setFont(25) + gc.setColor(1,1,1) + gc.printf(text.chatRemain,800,10,400,"right") + gc.print(remain or"?",1205,10) + + setFont(30) + for i=max(scroll-11,1),scroll do + gc.printf(texts[i],40,449-39*(scroll-i),1240) + end + + --Slider + if #texts>12 then + gc.setLineWidth(2) + gc.rectangle("line",10,30,20,450) + local len=450*12/#texts + gc.rectangle("fill",10,30+(450-len)*(scroll-12)/(#texts-12),20,len) + end + + --Draw + if scroll~=#texts then + setFont(40) + if newMessasge then + gc.setColor(1,Timer()%.4<.2 and 1 or 0,0) + else + gc.setColor(1,1,1) + end + gc.print("v",8,480) + end +end + scene.widgetList={ WIDGET.newTextBox{name="text", x=40, y=500,w=980,h=180,font=40}, + WIDGET.newButton{name="clear", x=1140, y=440,w=170,h=80,font=40,code=clearHistory}, WIDGET.newButton{name="send", x=1140, y=540,w=170,h=80,font=40,code=sendMessage}, WIDGET.newButton{name="back", x=1140, y=640,w=170,h=80,font=40,code=WIDGET.lnk_BACK}, } diff --git a/parts/scenes/history.lua b/parts/scenes/history.lua index 4c2348d8..6d7ba0c0 100644 --- a/parts/scenes/history.lua +++ b/parts/scenes/history.lua @@ -1,25 +1,6 @@ local gc=love.graphics local max,min=math.max,math.min -local floatWheel=0 -local function wheelScroll(y) - if y>0 then - if floatWheel<0 then floatWheel=0 end - floatWheel=floatWheel+y^1.2 - elseif y<0 then - if floatWheel>0 then floatWheel=0 end - floatWheel=floatWheel-(-y)^1.2 - end - while floatWheel>=1 do - love.keypressed("up") - floatWheel=floatWheel-1 - end - while floatWheel<=-1 do - love.keypressed("down") - floatWheel=floatWheel+1 - end -end - local scene={} function scene.sceneInit() diff --git a/parts/scenes/music.lua b/parts/scenes/music.lua index 3a8e5a16..10f34100 100644 --- a/parts/scenes/music.lua +++ b/parts/scenes/music.lua @@ -3,25 +3,6 @@ local Timer=love.timer.getTime local sin=math.sin -local floatWheel=0 -local function wheelScroll(y) - if y>0 then - if floatWheel<0 then floatWheel=0 end - floatWheel=floatWheel+y^1.2 - elseif y<0 then - if floatWheel>0 then floatWheel=0 end - floatWheel=floatWheel-(-y)^1.2 - end - while floatWheel>=1 do - love.keypressed("up") - floatWheel=floatWheel-1 - end - while floatWheel<=-1 do - love.keypressed("down") - floatWheel=floatWheel+1 - end -end - local scene={} function scene.sceneInit() diff --git a/parts/tick.lua b/parts/tick.lua index 4bee7ea8..896ac941 100644 --- a/parts/tick.lua +++ b/parts/tick.lua @@ -323,6 +323,7 @@ function Tick.wsCONN_connect(task) if wsconn then WSCONN=wsconn TASK.new(Tick.wsCONN_read) + LOG.print(text.wsSuccessed,"warn") return elseif connErr then LOG.print(text.wsFailed..": "..connErr,"warn") @@ -341,15 +342,13 @@ function Tick.wsCONN_read() if not WSCONN then return end local messages,readErr=client.read(WSCONN) if messages then - if messages[1]then - LOG.print(messages[1]) + for i=1,#messages do + SCN.socketRead(messages[i]) end elseif readErr then - print("Read error: "..readErr) - if readErr=="EOF"then - LOG.print("Socket closed!","warn") - end + wsWrite("/quit") WSCONN=nil + LOG.print(text.wsDisconnected,"warn") return end end