From 1957370b5c680944b46b3cb2c302da340bf9689f Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Wed, 13 Jan 2021 22:13:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E5=AE=A4=E6=94=B9=E7=94=A8=E6=96=B0=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parts/scenes/net_chat.lua | 54 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/parts/scenes/net_chat.lua b/parts/scenes/net_chat.lua index d6a43810..f01e0909 100644 --- a/parts/scenes/net_chat.lua +++ b/parts/scenes/net_chat.lua @@ -16,15 +16,17 @@ local function focusAtTextbox() end local function sendMessage() local W=WIDGET.active.text - if #W.value>0 and wsWrite(W.value)then + if #W.value>0 and wsWrite("T"..W.value)then W.value="" end end +local function pushText(t) + ins(texts,t) +end local function clearHistory() while #texts>1 do rem(texts)end scrollPos=1 SFX.play("fall") - collectgarbage() end local scene={} @@ -34,9 +36,9 @@ function scene.sceneInit() remain=false if #texts==0 then - ins(texts,{COLOR.dG,text.chatStart}) + pushText{COLOR.dG,text.chatStart} elseif #texts>1 and texts[#texts][1]~=COLOR.dG then - ins(texts,{COLOR.dG,text.chatHistory}) + pushText{COLOR.dG,text.chatHistory} end scrollPos=#texts TASK.new(focusAtTextbox)--Widgets are not initialized, so active after 1 frame @@ -44,7 +46,7 @@ function scene.sceneInit() BG.set("none") end function scene.sceneBack() - wsWrite("/quit") + wsWrite("Q") WSCONN=false LOG.print(text.wsDisconnected,"warn") end @@ -75,28 +77,24 @@ function scene.keyDown(k) 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"or cmd=="L"then - sep=data:find("@") - local num=data:find("#") - remain=tonumber(data:sub(1,sep-1)) - ins(texts,{ - COLOR.lR,data:sub(sep+1,num-1), - COLOR.dY,data:sub(num).." ", - COLOR.Y,(cmd=="J"and text.chatJoin or text.chatLeave), - }) - end - else--user message - local sep=mes:find(":") - local num=mes:find("#") - ins(texts,{ - COLOR.W,mes:sub(1,num-1), - COLOR.dY,mes:sub(num,sep-1).." ", - COLOR.sky,mes:sub(sep+1), - }) + local cmd=mes:sub(1,1) + local args=splitStr(mes:sub(2),":") + if cmd=="J"or cmd=="L"then + pushText{ + COLOR.lR,args[1], + COLOR.dY,args[2].." ", + COLOR.Y,text[cmd=="J"and"chatJoin"or"chatLeave"] + } + remain=tonumber(args[3]) + elseif cmd=="T"then + pushText{ + COLOR.W,args[1], + COLOR.dY,args[2].." ", + COLOR.sky,args[3] + } + else + LOG.print("Illegal message: "..mes,30,COLOR.green) + return end if scrollPos==#texts-1 then scrollPos=scrollPos+1 @@ -110,7 +108,7 @@ function scene.update(dt) heartBeatTimer=heartBeatTimer+dt if heartBeatTimer>42 then heartBeatTimer=0 - wsWrite("/ping") + wsWrite("P") end end function scene.draw()