From f3face791fa1c6fa4b878d44e5a5003267282218 Mon Sep 17 00:00:00 2001 From: MrZ_26 <1046101471@qq.com> Date: Thu, 29 Sep 2022 02:41:00 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=B8=8D=E8=83=BD=E7=94=A8=E3=80=91?= =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E6=94=B9=E8=81=94=E7=BD=91=EF=BC=8C=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=B9=B6=E6=9B=BF=E6=8D=A2=E4=B8=80=E4=BA=9B=E5=8F=91?= =?UTF-8?q?=E9=80=81ws=E8=AF=B7=E6=B1=82=E7=9A=84=E6=96=B9=E6=B3=95=20?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E8=B7=9F=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework | 2 +- main.lua | 3 +- parts/net.lua | 378 +++++++++++++++++++---------------- parts/player/player.lua | 2 +- parts/scenes/error.lua | 2 +- parts/scenes/net_game.lua | 27 ++- parts/scenes/net_menu.lua | 4 +- parts/scenes/net_newRoom.lua | 2 +- parts/scenes/net_rooms.lua | 4 +- 9 files changed, 230 insertions(+), 194 deletions(-) diff --git a/Zframework b/Zframework index e89e97ef..4de4d3eb 160000 --- a/Zframework +++ b/Zframework @@ -1 +1 @@ -Subproject commit e89e97ef9068a49377e0849cb9e5ac0644e78627 +Subproject commit 4de4d3eb8671ca7e1ecb26566d9ecdfe803a2dce diff --git a/main.lua b/main.lua index 319e9dfd..268495f4 100644 --- a/main.lua +++ b/main.lua @@ -178,6 +178,7 @@ Z.setOnFnKeys({ function()for k,v in next,_G do print(k,v)end end, function()if love['_openConsole']then love['_openConsole']()end end, }) +Z.setVersionText(VERSION.string) Z.setDebugInfo{ {"Cache",gcinfo}, {"Tasks",TASK.getCount}, @@ -691,6 +692,6 @@ if TABLE.find(arg,'--test')then love.event.quit(1) end) end -WS.switchHost('cafuuchino1.3322.org','10026','/tech/socket/v1') +WS.switchHost('cafuuchino1.3322.org','10026','/techmino/ws/v1') HTTP.setHost("cafuuchino1.3322.org:10026") HTTP.setThreadCount(1) diff --git a/parts/net.lua b/parts/net.lua index 07007c2b..a10e46f4 100644 --- a/parts/net.lua +++ b/parts/net.lua @@ -1,5 +1,3 @@ -local loveEncode,loveDecode=love.data.encode,love.data.decode - local WS=WS local PLAYERS=PLAYERS @@ -61,7 +59,7 @@ end --------------------------- +-------------------------- local function getMsg(request,timeout) HTTP(request) local totalTime=0 @@ -127,13 +125,13 @@ function NET.codeLogin(code) if res then if res.code==200 then - USER.rToken=res.refreshToken - USER.aToken=res.accessToken - -- TODO: connect WS + USER.rToken=res.data.refreshToken + USER.aToken=res.data.accessToken + NET.connectWS() SCN.pop()SCN.go('net_menu') elseif res.code==201 then - USER.rToken=res.refreshToken - USER.aToken=res.accessToken + USER.rToken=res.data.refreshToken + USER.aToken=res.data.accessToken SCN.pop()SCN.push('net_menu') SCN.fileDropped(3) else @@ -207,7 +205,7 @@ function NET.autoLogin() if res then if res.code==200 then - -- TODO: connect WS + NET.connectWS() SCN.go('net_menu') WAIT.interrupt() return @@ -228,9 +226,9 @@ function NET.autoLogin() if res then if res.code==200 then - USER.rToken=res.refreshToken - USER.aToken=res.accessToken - -- TODO: connect WS + USER.rToken=res.data.refreshToken + USER.aToken=res.data.accessToken + NET.connectWS() MES.new('info',"Login successed",5) SCN.go('net_menu') WAIT.interrupt() @@ -253,10 +251,11 @@ function NET.autoLogin() }, },6.26) if res then + print(TABLE.dump(res)) if res.code==200 then - USER.rToken=res.refreshToken - USER.aToken=res.accessToken - -- TODO: connect WS + USER.rToken=res.data.refreshToken + USER.aToken=res.data.accessToken + NET.connectWS() MES.new('info',"Login successed",5) SCN.go('net_menu') WAIT.interrupt() @@ -298,9 +297,9 @@ function NET.pwLogin(email,pw) if res.code==200 then USER.email=email USER.password=pw - USER.rToken=res.refreshToken - USER.aToken=res.accessToken - -- TODO: connect WS + USER.rToken=res.data.refreshToken + USER.aToken=res.data.accessToken + NET.connectWS() SCN.go('net_menu') else MES.new('error',res.message,5) @@ -319,30 +318,200 @@ function NET.pwLogin(email,pw) timeout=12.6, } end --------------------------- +-------------------------- +local function wsSend(act,data) + WS.send('game',JSON.encode{ + action=act, + data=data, + }) +end +--Room +NET.room={} +function NET.room.chat(mes,rid) + wsSend(1300,{ + message=mes, + roomId=rid,--Admin + }) +end +function NET.room.create(roomName,description,capacity,roomType,roomData,password) + if TASK.lock('enterRoom',2)then + NET.roomState.private=not not password + NET.roomState.capacity=capacity + wsSend(1301,{ + capacity=capacity, + info={ + name=roomName, + type=roomType, + version=VERSION.room, + description=description, + }, + data=roomData, ---Connect -function NET.wsconn() + password=password, + }) + end +end +function NET.room.getData(rid) + wsSend(1302,{ + roomId=rid,--Admin + }) +end +function NET.room.setData(data,rid) + wsSend(1303,{ + data=data, + roomId=rid,--Admin + }) +end +function NET.room.getInfo(rid) + wsSend(1304,{ + roomId=rid,--Admin + }) +end +function NET.room.setInfo(info,rid) + wsSend(1305,{ + info=info, + roomId=rid,--Admin + }) +end +function NET.room.enter(rid,password) + if TASK.lock('enterRoom',6)then + SFX.play('reach',.6) + wsSend(1306,{ + data={ + rid=rid, + password=password, + } + }) + end +end +function NET.room.kick(pid,rid) + wsSend(1307,{ + playerId=pid,--Host + roomId=rid,--Admin + }) +end +function NET.room.leave() + wsSend(1308) +end +function NET.room.fetch() + if TASK.lock('fetchRoom',3)then + wsSend(1309,{ + data={ + pageIndex=0, + pageSize=26, + } + }) + end +end +function NET.room.setPW(pw,rid) + if TASK.lock('fetchRoom',3)then + wsSend(1310,{ + data={ + password=pw, + roomId=rid,--Admin + } + }) + end +end +function NET.room.remove(rid) + wsSend(1311,{ + roomId=rid--Admin + }) +end + +--Player +NET.player={} +function NET.player.updateConf() + wsSend(1200,dumpBasicConfig()) +end +function NET.player.finish(mes)--what mes? + wsSend(1201,mes) +end +function NET.player.joinGroup(gid) + wsSend(1202,gid) +end +function NET.player.setReady(bool) + wsSend(1203,bool) +end +function NET.player.setHost(pid) + wsSend(1204,{ + playerId=pid, + role='Admin', + }) +end +function NET.player.setState(state)-- what state? + wsSend(1205,state) +end +function NET.player.stream(stream) + wsSend(1206,stream) +end +function NET.player.setPlaying(playing) + wsSend(1207,playing and 'Gamer' or 'Spectator') +end + +--WS +function NET.connectWS() if WS.status('game')=='dead'then - NET.roomState.start=true - WS.connect('stream','/stream',JSON.encode{ - accessToken=USER.aToken, + WS.connect('game','',{ + ['x-access-token']=USER.aToken, },6) - TASK.new(NET.updateWS_stream) + TASK.new(NET.updateWS) + end +end +function NET.closeWS() + WS.close('game') +end +function NET.updateWS() + while WS.status('game')~='dead'do + coroutine.yield() + local message,op=WS.read('game') + if message then + if op=='ping'then + elseif op=='pong'then + elseif op=='close'then + _closeMessage(message) + return + else + local res=_parse(message) + if res then + if res.type=='Connect'then + MES.new('info','Connected!') + elseif res.action==1100 then-- TODO + elseif res.action==1101 then-- TODO + elseif res.action==1102 then-- TODO + elseif res.action==1201 then-- TODO + elseif res.action==1202 then-- TODO + elseif res.action==1203 then-- TODO + elseif res.action==1204 then-- TODO + elseif res.action==1205 then-- TODO + elseif res.action==1206 then-- TODO + elseif res.action==1207 then-- TODO + elseif res.action==1301 then-- TODO + elseif res.action==1302 then-- TODO + elseif res.action==1303 then-- TODO + elseif res.action==1304 then-- TODO + elseif res.action==1305 then-- TODO + elseif res.action==1306 then-- TODO + elseif res.action==1307 then-- TODO + elseif res.action==1308 then-- TODO + elseif res.action==1309 then-- TODO + elseif res.action==1310 then-- TODO + elseif res.action==1311 then-- TODO + end + else + WS.alert('user') + end + end + end end end ---Disconnect -function NET.wsclose() - -- WS.close() -end - +-------------------------- --Account & User function NET.getUserInfo(uid) - WS.send('game',JSON.encode{ - action=1, + wsSend({ data={ uid=uid, hash=USERS.getHash(uid), @@ -353,7 +522,7 @@ end --Save function NET.uploadSave() if TASK.lock('uploadSave',8)then - WS.send('game',JSON.encode{action=2,data={sections={ + wsSend({data={sections={ {section=1,data=STRING.packTable(STAT)}, {section=2,data=STRING.packTable(RANKS)}, {section=3,data=STRING.packTable(SETTING)}, @@ -367,7 +536,7 @@ function NET.uploadSave() end function NET.downloadSave() if TASK.lock('downloadSave',8)then - WS.send('game',JSON.encode{action=3,data={sections={1,2,3,4,5,6,7}}}) + wsSend({data={sections={1,2,3,4,5,6,7}}}) MES.new('info',"Downloading") end end @@ -415,145 +584,4 @@ function NET.loadSavedData(sections) end end ---Room -function NET.fetchRoom() - if TASK.lock('fetchRoom',3)then - WS.send('game',JSON.encode{ - action=0, - data={ - type=nil, - begin=0, - count=10, - } - }) - end -end -function NET.createRoom(roomName,description,capacity,roomType,roomData,password) - if TASK.lock('enterRoom',2)then - NET.roomState.private=not not password - NET.roomState.capacity=capacity - WS.send('game',JSON.encode{ - action=1, - data={ - capacity=capacity, - password=password, - roomInfo={ - name=roomName, - type=roomType, - version=VERSION.room, - description=description, - }, - roomData=roomData, - - config=dumpBasicConfig(), - } - }) - end -end -function NET.enterRoom(room,password) - if TASK.lock('enterRoom',6)then - SFX.play('reach',.6) - WS.send('game',JSON.encode{ - action=2, - data={ - rid=room.rid, - config=dumpBasicConfig(), - password=password, - } - }) - end -end - ---Play -function NET.checkPlayDisconn() - return WS.status('game')~='running' -end -function NET.signal_quit() - if TASK.lock('quit',3)then - WS.send('game',JSON.encode{action=3}) - end -end -function NET.sendMessage(mes) - WS.send('game',JSON.encode{action=3,data={message=mes}}) -end -function NET.changeConfig() - WS.send('game',JSON.encode{action=5,data={config=dumpBasicConfig()}}) -end -function NET.signal_setMode(mode) - if not NET.roomState.start and TASK.lock('ready',3)then - WS.send('game',JSON.encode{action=6,data={mode=mode}}) - end -end -function NET.signal_die() - WS.send('game',JSON.encode{action=4,data={score=0,survivalTime=0}}) -end -function NET.uploadRecStream(stream) - WS.send('game',JSON.encode{action=5,data={stream=loveEncode('string','base64',stream)}}) -end - ---Chat -function NET.sendChatMes(mes) - WS.send('game',"T"..loveEncode('string','base64',mes)) -end -function NET.quitChat() - WS.send('game','q') -end - ---WS task funcs -function NET.freshPlayerCount() - while WS.status('game')=='running'do - TEST.yieldN(260) - if TASK.lock('freshPlayerCount',10)then - WS.send('game',JSON.encode{action=3}) - end - end -end -function NET.updateWS_user() - while WS.status('game')~='dead'do - coroutine.yield() - local message,op=WS.read('user') - if message then - if op=='ping'then - elseif op=='pong'then - elseif op=='close'then - _closeMessage(message) - return - else - local res=_parse(message) - if res then - if res.type=='Connect'then - if res.uid then - USER.uid=res.uid - USER.authToken=res.authToken - if SCN.cur=='login'then - SCN.back() - end - end - MES.new('check',text.loginOK) - - --Get self infos - NET.getUserInfo(USER.uid) - TASK.unlock('wsc_user') - elseif res.action==0 then--Get accessToken - NET.accessToken=res.accessToken - MES.new('check',text.accessOK) - NET.wsconn() - elseif res.action==1 then--Get userInfo - USERS.updateUserData(res.data) - elseif res.action==2 then--Upload successed - TASK.unlock('uploadSave') - MES.new('check',text.exportSuccess) - elseif res.action==3 then--Download successed - TASK.unlock('downloadSave') - NET.loadSavedData(res.data.sections) - MES.new('check',text.importSuccess) - end - else - WS.alert('user') - end - end - end - end -end - return NET diff --git a/parts/player/player.lua b/parts/player/player.lua index 85ff96e0..7d3e2d3e 100644 --- a/parts/player/player.lua +++ b/parts/player/player.lua @@ -2845,7 +2845,7 @@ function Player:lose(force) gameOver() self:newTask(#PLAYERS>1 and task_lose or task_finish) if GAME.net and not NET.spectate then - NET.signal_die() + NET.player.finish() else TASK.new(task_autoPause) end diff --git a/parts/scenes/error.lua b/parts/scenes/error.lua index 232731e2..dd2629d7 100644 --- a/parts/scenes/error.lua +++ b/parts/scenes/error.lua @@ -9,7 +9,7 @@ function scene.sceneInit() sysAndScn=SYSTEM.."-"..VERSION.string.." scene:"..Z.getErr('#').scene errorText=LOADED and text.errorMsg or"An error has occurred while the game was loading.\nAn error log has been created so you can send it to the author." errorShot,errorInfo=Z.getErr('#').shot,Z.getErr('#').mes - NET.wsclose() + NET.closeWS() if SETTING then SFX.fplay('error',SETTING.voc*.8 or 0) end diff --git a/parts/scenes/net_game.lua b/parts/scenes/net_game.lua index 1c7abcba..27b31695 100644 --- a/parts/scenes/net_game.lua +++ b/parts/scenes/net_game.lua @@ -27,16 +27,24 @@ local function _hideReadyUI() TASK.getLock('ready') end -local function _setCancel()NET.signal_setMode(0)end -local function _setReady()NET.signal_setMode(1)end -local function _setSpectate()NET.signal_setMode(2)end +local function _setCancel() + NET.player.setPlaying(true) + NET.player.setReady(true) +end +local function _setReady() + NET.player.setReady(true) +end +local function _setSpectate() + NET.player.setPlaying(false) +end + local function _gotoSetting() GAME.prevBG=BG.cur SCN.go('setting_game') end local function _quit() if tryBack()then - NET.signal_quit() + NET.room.leave() if SCN.stack[#SCN.stack-1]=='net_newRoom'then SCN.pop() end @@ -69,14 +77,13 @@ function scene.sceneInit() newMessageTimer=0 if SCN.prev=='setting_game'then - NET.changeConfig() + NET.player.updateConf() end if GAME.prevBG then BG.set(GAME.prevBG) GAME.prevBG=false end if NET.specSRID then - NET.wsconn_stream(NET.specSRID) NET.specSRID=false end end @@ -137,7 +144,7 @@ function scene.keyDown(key,isRep) elseif key=='return'then local mes=STRING.trim(inputBox:getText()) if not inputBox.hide and #mes>0 then - NET.sendMessage(mes) + NET.room.chat(mes) inputBox:clear() else _switchChat() @@ -231,8 +238,8 @@ function scene.socketRead(cmd,d) end function scene.update(dt) - if NET.checkPlayDisconn()then - NET.wsclose() + if WS.status('game')~='running' then + NET.closeWS() SCN.back() return end @@ -261,7 +268,7 @@ function scene.update(dt) elseif #stream%3==2 then stream=stream.."\0\0\0\0" end - NET.uploadRecStream(stream) + NET.player.stream(stream) lastUpstreamTime=PLAYERS[1].alive and P1.frameRun or 1e99 end else diff --git a/parts/scenes/net_menu.lua b/parts/scenes/net_menu.lua index 1d6ea5e1..e4809b18 100644 --- a/parts/scenes/net_menu.lua +++ b/parts/scenes/net_menu.lua @@ -4,7 +4,7 @@ function scene.sceneInit() BG.set() end function scene.sceneBack() - NET.wsclose() + NET.closeWS() end function scene.draw() @@ -21,7 +21,7 @@ scene.widgetList={ code=function() if tryBack()then if USER.uid then - NET.wsclose() + NET.closeWS() USER.uid=false USER.authToken=false SCN.back() diff --git a/parts/scenes/net_newRoom.lua b/parts/scenes/net_newRoom.lua index d9c86655..45806ce3 100644 --- a/parts/scenes/net_newRoom.lua +++ b/parts/scenes/net_newRoom.lua @@ -36,7 +36,7 @@ local function _createRoom() if #roomname==0 then roomname=(USERS.getUsername(USER.uid)or"Anonymous").."'s room" end - NET.createRoom( + NET.room.create( roomname, descriptionBox.value, ROOMENV.capacity, diff --git a/parts/scenes/net_rooms.lua b/parts/scenes/net_rooms.lua index ea9ce6b3..6c7a7622 100644 --- a/parts/scenes/net_rooms.lua +++ b/parts/scenes/net_rooms.lua @@ -51,7 +51,7 @@ local passwordBox=WIDGET.newInputBox{name='password',x=350,y=505,w=500,h=50,secr }]] local function _fetchRoom() fetchTimer=10 - NET.fetchRoom() + NET.room.fetch() end local scene={} @@ -70,7 +70,7 @@ function scene.keyDown(key) local R=roomList:getSel() if TASK.getLock('fetchRoom')or not R then return end if R.roomInfo.version==VERSION.room then - NET.enterRoom(R,passwordBox.value) + NET.room.enter(R,passwordBox.value) else MES.new('error',text.versionNotMatch) end