继续整联网,数字action填入一个大表方便统一管理

This commit is contained in:
MrZ_26
2022-10-06 22:30:35 +08:00
parent 8e99565a9d
commit 37b76a1cb4
9 changed files with 194 additions and 128 deletions

View File

@@ -229,7 +229,7 @@ do-- Z.setOnFocus
end) end)
end end
Z.setOnBeforeQuit(function() Z.setOnBeforeQuit(function()
NET.ws.close() NET.ws_close()
TASK.new(function() TASK.new(function()
TEST.yieldT(.26) TEST.yieldT(.26)
love.event.quit() love.event.quit()

View File

@@ -141,7 +141,7 @@ function NET.codeLogin(code)
if res.code==200 then if res.code==200 then
USER.rToken=res.data.refreshToken USER.rToken=res.data.refreshToken
USER.aToken=res.data.accessToken USER.aToken=res.data.accessToken
NET.ws.connect() NET.ws_connect()
SCN.pop()SCN.go('net_menu') SCN.pop()SCN.go('net_menu')
elseif res.code==201 then elseif res.code==201 then
USER.rToken=res.data.refreshToken USER.rToken=res.data.refreshToken
@@ -189,10 +189,7 @@ function NET.setPW(code,pw)
end) end)
end end
function NET.autoLogin() function NET.autoLogin()
if not USER.password then if not USER.password then SCN.go('login') return end
SCN.go('login')
return
end
if not TASK.lock('autoLogin') then return end if not TASK.lock('autoLogin') then return end
TASK.new(function() TASK.new(function()
WAIT{ WAIT{
@@ -212,7 +209,7 @@ function NET.autoLogin()
if res then if res then
if res.code==200 then if res.code==200 then
NET.ws.connect() NET.ws_connect()
SCN.go('net_menu') SCN.go('net_menu')
WAIT.interrupt() WAIT.interrupt()
return return
@@ -233,7 +230,7 @@ function NET.autoLogin()
if res.code==200 then if res.code==200 then
USER.rToken=res.data.refreshToken USER.rToken=res.data.refreshToken
USER.aToken=res.data.accessToken USER.aToken=res.data.accessToken
NET.ws.connect() NET.ws_connect()
SCN.go('net_menu') SCN.go('net_menu')
WAIT.interrupt() WAIT.interrupt()
return return
@@ -256,7 +253,7 @@ function NET.autoLogin()
if res.code==200 then if res.code==200 then
USER.rToken=res.data.refreshToken USER.rToken=res.data.refreshToken
USER.aToken=res.data.accessToken USER.aToken=res.data.accessToken
NET.ws.connect() NET.ws_connect()
SCN.go('net_menu') SCN.go('net_menu')
WAIT.interrupt() WAIT.interrupt()
return return
@@ -299,7 +296,7 @@ function NET.pwLogin(email,pw)
USER.password=pw USER.password=pw
USER.rToken=res.data.refreshToken USER.rToken=res.data.refreshToken
USER.aToken=res.data.accessToken USER.aToken=res.data.accessToken
NET.ws.connect() NET.ws_connect()
SCN.go('net_menu') SCN.go('net_menu')
end end
end end
@@ -309,6 +306,33 @@ function NET.pwLogin(email,pw)
end end
--------------------------<NEW WS API> --------------------------<NEW WS API>
local actMap={
global_getOnlineCount= 1000,
match_finish= 1100,
match_ready= 1101,
match_start= 1102,
player_updateConf= 1200,
player_finish= 1201,
player_joinGroup= 1202,
player_setReady= 1203,
player_setHost= 1204,
player_setState= 1205,
player_stream= 1206,
player_setPlaying= 1207,
room_chat= 1300,
room_create= 1301,
room_getData= 1302,
room_setData= 1303,
room_getInfo= 1304,
room_setInfo= 1305,
room_enter= 1306,
room_kick= 1307,
room_leave= 1308,
room_fetch= 1309,
room_setPW= 1310,
room_remove= 1311,
} for k,v in next,actMap do actMap[v]=k end
local function wsSend(act,data) local function wsSend(act,data)
-- print("SEND ACT: "..act) -- print("SEND ACT: "..act)
WS.send('game',JSON.encode{ WS.send('game',JSON.encode{
@@ -317,15 +341,24 @@ local function wsSend(act,data)
}) })
end end
-- Room -- Global
NET.room={} function NET.global_getOnlineCount()
function NET.room.chat(msg,rid) wsSend(actMap.global_getOnlineCount)
wsSend(1300,{
message=msg,
roomId=rid,-- Admin
})
end end
function NET.room.create(roomName,description,capacity,roomType,roomData,password)
-- Room
function NET.room_chat(msg,rid)
if not TASK.lock('chatLimit',2.6) then
MES.new('warn',"Talk too fast")
elseif #msg>0 then
wsSend(1300,{
message=msg,
roomId=rid,-- Admin
})
return true
end
end
function NET.room_create(roomName,description,capacity,roomType,roomData,password)
if not TASK.lock('createRoom',6) then return end if not TASK.lock('createRoom',6) then return end
WAIT{ WAIT{
quit=function() quit=function()
@@ -333,7 +366,7 @@ function NET.room.create(roomName,description,capacity,roomType,roomData,passwor
end, end,
timeout=1e99, timeout=1e99,
} }
wsSend(1301,{ wsSend(actMap.room_create,{
capacity=capacity, capacity=capacity,
info={ info={
name=roomName, name=roomName,
@@ -345,115 +378,174 @@ function NET.room.create(roomName,description,capacity,roomType,roomData,passwor
password=password, password=password,
}) })
end end
function NET.room.getData(rid) function NET.room_getData(rid)
wsSend(1302,{ wsSend(actMap.room_getData,{
roomId=rid,-- Admin roomId=rid,-- Admin
}) })
end end
function NET.room.setData(data,rid) function NET.room_setData(data,rid)
wsSend(1303,{ wsSend(actMap.room_setData,{
data=data, data=data,
roomId=rid,-- Admin roomId=rid,-- Admin
}) })
end end
function NET.room.getInfo(rid) function NET.room_getInfo(rid)
wsSend(1304,{ wsSend(actMap.room_getInfo,{
roomId=rid,-- Admin roomId=rid,-- Admin
}) })
end end
function NET.room.setInfo(info,rid) function NET.room_setInfo(info,rid)
wsSend(1305,{ wsSend(actMap.room_setInfo,{
info=info, info=info,
roomId=rid,-- Admin roomId=rid,-- Admin
}) })
end end
function NET.room.enter(rid,password) function NET.room_enter(rid,password)
if not TASK.lock('enterRoom',6) then return end if not TASK.lock('enterRoom',6) then return end
SFX.play('reach',.6) SFX.play('reach',.6)
wsSend(1306,{ wsSend(actMap.room_enter,{
data={ data={
roomId=rid, roomId=rid,
password=password, password=password,
} }
}) })
end end
function NET.room.kick(pid,rid) function NET.room_kick(pid,rid)
wsSend(1307,{ wsSend(actMap.room_kick,{
playerId=pid,-- Host playerId=pid,-- Host
roomId=rid,-- Admin roomId=rid,-- Admin
}) })
end end
function NET.room.leave() function NET.room_leave()
wsSend(1308) wsSend(actMap.room_leave)
end end
function NET.room.fetch() function NET.room_fetch()
if not TASK.lock('fetchRoom',3) then return end if not TASK.lock('fetchRoom',3) then return end
wsSend(1309,{ wsSend(actMap.room_lock,{
data={ pageIndex=0,
pageIndex=0, pageSize=26,
pageSize=26,
}
}) })
end end
function NET.room.setPW(pw,rid) function NET.room_setPW(pw,rid)
if not TASK.lock('setRoomPW',2) then return end if not TASK.lock('setRoomPW',2) then return end
wsSend(1310,{ wsSend(actMap.room_lock,{
data={ password=pw,
password=pw, roomId=rid,-- Admin
roomId=rid,-- Admin
}
}) })
end end
function NET.room.remove(rid) function NET.room_remove(rid)
wsSend(1311,{ wsSend(actMap.room_remove,{
roomId=rid-- Admin roomId=rid-- Admin
}) })
end end
-- Player -- Player
NET.player={} function NET.player_updateConf()
function NET.player.updateConf() wsSend(actMap.player_updateConf,dumpBasicConfig())
wsSend(1200,dumpBasicConfig())
end end
function NET.player.finish(msg)-- what msg? function NET.player_finish(msg)-- what msg?
wsSend(1201,msg) wsSend(actMap.player_finish,msg)
end end
function NET.player.joinGroup(gid) function NET.player_joinGroup(gid)
wsSend(1202,gid) wsSend(actMap.player_joinGroup,gid)
end end
function NET.player.setReady(bool) function NET.player_setReady(bool)
wsSend(1203,bool) wsSend(actMap.player_setReady,bool)
end end
function NET.player.setHost(pid) function NET.player_setHost(pid)
wsSend(1204,{ wsSend(actMap.player_setHost,{
playerId=pid, playerId=pid,
role='Admin', role='Admin',
}) })
end end
function NET.player.setState(state)-- what state? function NET.player_setState(state)-- what state?
wsSend(1205,state) wsSend(actMap.player_setState,state)
end end
function NET.player.stream(stream) function NET.player_stream(stream)
wsSend(1206,stream) wsSend(actMap.player_stream,stream)
end end
function NET.player.setPlaying(playing) function NET.player_setPlaying(playing)
wsSend(1207,playing and 'Gamer' or 'Spectator') wsSend(actMap.player_setPlaying,playing and 'Gamer' or 'Spectator')
end end
-- Match
-- WS -- WS
NET.ws={} NET.wsCallBack={}
function NET.ws.connect() function NET.wsCallBack.global_getOnlineCount(body)
NET.onlineCount=tonumber(body.data) or "_"
end
function NET.wsCallBack.room_chat(body)-- TODO
end
function NET.wsCallBack.room_create(body)
TASK.unlock('createRoom')
-- NET.roomState=...
-- SCN.go('net_game')
WAIT.interrupt()
end
function NET.wsCallBack.room_getData(body)-- TODO
end
function NET.wsCallBack.room_setData(body)-- TODO
end
function NET.wsCallBack.room_getInfo(body)-- TODO
end
function NET.wsCallBack.room_setInfo(body)-- TODO
end
function NET.wsCallBack.room_enter(body)
TASK.unlock('enterRoom')
-- NET.roomState=...
-- SCN.go('net_game')
WAIT.interrupt()
end
function NET.wsCallBack.room_kick(body)-- TODO
end
function NET.wsCallBack.room_leave(body)-- TODO
end
function NET.wsCallBack.room_fetch(body)
TASK.unlock('fetchRoom')
if body.data then SCN.scenes.net_rooms.widgetList.roomList:setList(body.data) end
end
function NET.wsCallBack.room_setPW(body)-- TODO
end
function NET.wsCallBack.room_remove(body)-- TODO
end
function NET.wsCallBack.player_updateConf(body)-- TODO
end
function NET.wsCallBack.player_finish(body)-- TODO
end
function NET.wsCallBack.player_joinGroup(body)-- TODO
end
function NET.wsCallBack.player_setReady(body)-- TODO
end
function NET.wsCallBack.player_setHost(body)-- TODO
end
function NET.wsCallBack.player_setState(body)-- TODO
end
function NET.wsCallBack.player_stream(body)-- TODO
end
function NET.wsCallBack.player_setPlaying(body)-- TODO
end
function NET.ws_connect()
if WS.status('game')=='dead' then if WS.status('game')=='dead' then
WS.connect('game','',{['x-access-token']=USER.aToken},6) WS.connect('game','',{['x-access-token']=USER.aToken},6)
TASK.new(NET.ws.update) TASK.new(NET.ws_update)
end end
end end
function NET.ws.close() function NET.ws_close()
WS.close('game') WS.close('game')
end end
function NET.ws.update() function NET.ws_update()
local updateOnlineTimer=0
while WS.status('game')~='dead' do while WS.status('game')~='dead' do
coroutine.yield() local dt=coroutine.yield()
updateOnlineTimer=updateOnlineTimer+dt
if updateOnlineTimer>6.26 then
NET.global_getOnlineCount()
updateOnlineTimer=0
end
local msg,op=WS.read('game') local msg,op=WS.read('game')
if msg then if msg then
if op=='ping' then if op=='ping' then
@@ -469,42 +561,16 @@ function NET.ws.update()
else else
local body=JSON.decode(msg) local body=JSON.decode(msg)
if body then if body then
-- print(("RECV ACT: $1 ($2)"):repD(res.action,res.type)) -- print(("RECV ACT: $1 ($2)"):repD(body.action,body.type))
-- print(TABLE.dump(body))
if body.type=='Failed' then if body.type=='Failed' then
parseError(body.message~=nil and body.message or msg) parseError(body.message~=nil and body.message or msg)
elseif body.action==1100 then-- TODO else
elseif body.action==1101 then-- TODO local f=NET.wsCallBack[actMap[body.action]]
elseif body.action==1102 then-- TODO if f then f(body) else print("Wrong action number: "..body.action) end
elseif body.action==1201 then-- Finish
elseif body.action==1202 then-- Join group
elseif body.action==1203 then-- Set ready
elseif body.action==1204 then-- Set host
elseif body.action==1205 then-- Set state
elseif body.action==1206 then-- Stream
elseif body.action==1207 then-- Set playing
elseif body.action==1301 then-- Create room
TASK.unlock('createRoom')
-- NET.roomState=...
-- SCN.go('net_game')
WAIT.interrupt()
elseif body.action==1302 then-- Get room data
elseif body.action==1303 then-- Set room data
elseif body.action==1304 then-- Get room info
elseif body.action==1305 then-- Set room info
elseif body.action==1306 then-- Enter room
TASK.unlock('enterRoom')
-- NET.roomState=...
-- SCN.go('net_game')
WAIT.interrupt()
elseif body.action==1307 then-- Kick room
elseif body.action==1308 then-- Leave room
elseif body.action==1309 then-- Fetch rooms
TASK.unlock('fetchRoom')
if body.data then SCN.scenes.net_rooms.widgetList.roomList:setList(body.data) end
elseif body.action==1310 then-- Set password
elseif body.action==1311 then-- Remove room
end end
else else
MES.new('warn',"Wrong json: "..msg,5)
WS.alert('user') WS.alert('user')
end end
end end
@@ -516,10 +582,8 @@ end
-- Account & User -- Account & User
function NET.getUserInfo(uid) function NET.getUserInfo(uid)
wsSend({ wsSend({
data={ uid=uid,
uid=uid, hash=USERS.getHash(uid),
hash=USERS.getHash(uid),
},
}) })
end end

View File

@@ -2845,7 +2845,7 @@ function Player:lose(force)
gameOver() gameOver()
self:newTask(#PLAYERS>1 and task_lose or task_finish) self:newTask(#PLAYERS>1 and task_lose or task_finish)
if GAME.net and not NET.spectate then if GAME.net and not NET.spectate then
NET.player.finish() NET.player_finish()
else else
TASK.new(task_autoPause) TASK.new(task_autoPause)
end end

View File

@@ -9,7 +9,7 @@ function scene.sceneInit()
sysAndScn=SYSTEM.."-"..VERSION.string.." scene:"..Z.getErr('#').scene 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." 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 errorShot,errorInfo=Z.getErr('#').shot,Z.getErr('#').mes
NET.ws.close() NET.ws_close()
if SETTING then if SETTING then
SFX.fplay('error',SETTING.voc*.8 or 0) SFX.fplay('error',SETTING.voc*.8 or 0)
end end

View File

@@ -28,14 +28,14 @@ local function _hideReadyUI()
end end
local function _setCancel() local function _setCancel()
NET.player.setPlaying(true) NET.player_setPlaying(true)
NET.player.setReady(true) NET.player_setReady(true)
end end
local function _setReady() local function _setReady()
NET.player.setReady(true) NET.player_setReady(true)
end end
local function _setSpectate() local function _setSpectate()
NET.player.setPlaying(false) NET.player_setPlaying(false)
end end
local function _gotoSetting() local function _gotoSetting()
@@ -44,7 +44,7 @@ local function _gotoSetting()
end end
local function _quit() local function _quit()
if tryBack() then if tryBack() then
NET.room.leave() NET.room_leave()
if SCN.stack[#SCN.stack-1]=='net_newRoom' then if SCN.stack[#SCN.stack-1]=='net_newRoom' then
SCN.pop() SCN.pop()
end end
@@ -77,7 +77,7 @@ function scene.sceneInit()
newMessageTimer=0 newMessageTimer=0
if SCN.prev=='setting_game' then if SCN.prev=='setting_game' then
NET.player.updateConf() NET.player_updateConf()
end end
if GAME.prevBG then if GAME.prevBG then
BG.set(GAME.prevBG) BG.set(GAME.prevBG)
@@ -144,8 +144,9 @@ function scene.keyDown(key,isRep)
elseif key=='return' then elseif key=='return' then
local mes=STRING.trim(inputBox:getText()) local mes=STRING.trim(inputBox:getText())
if not inputBox.hide and #mes>0 then if not inputBox.hide and #mes>0 then
NET.room.chat(mes) if NET.room.chat(mes) then
inputBox:clear() inputBox:clear()
end
else else
_switchChat() _switchChat()
end end
@@ -239,7 +240,7 @@ end
function scene.update(dt) function scene.update(dt)
if WS.status('game')~='running' then if WS.status('game')~='running' then
NET.ws.close() NET.ws_close()
SCN.back() SCN.back()
return return
end end
@@ -268,7 +269,7 @@ function scene.update(dt)
elseif #stream%3==2 then elseif #stream%3==2 then
stream=stream.."\0\0\0\0" stream=stream.."\0\0\0\0"
end end
NET.player.stream(stream) NET.player_stream(stream)
lastUpstreamTime=PLAYERS[1].alive and P1.frameRun or 1e99 lastUpstreamTime=PLAYERS[1].alive and P1.frameRun or 1e99
end end
else else

View File

@@ -4,7 +4,7 @@ function scene.sceneInit()
BG.set() BG.set()
end end
function scene.sceneBack() function scene.sceneBack()
NET.ws.close() NET.ws_close()
end end
function scene.draw() function scene.draw()
@@ -24,7 +24,7 @@ scene.widgetList={
USER.__data.password=false USER.__data.password=false
USER.__data.rToken=false USER.__data.rToken=false
USER.__data.aToken=false USER.__data.aToken=false
NET.ws.close() NET.ws_close()
SCN.back() SCN.back()
end end
end}, end},

View File

@@ -36,7 +36,7 @@ local function _createRoom()
if #roomname==0 then if #roomname==0 then
roomname=(USERS.getUsername(USER.uid) or "Anonymous").."'s room" roomname=(USERS.getUsername(USER.uid) or "Anonymous").."'s room"
end end
NET.room.create( NET.room_create(
roomname, roomname,
descriptionBox.value, descriptionBox.value,
ROOMENV.capacity, ROOMENV.capacity,

View File

@@ -67,7 +67,7 @@ local passwordBox=WIDGET.newInputBox{name='password',x=350,y=505,w=500,h=50,secr
}]] }]]
local function _fetchRoom() local function _fetchRoom()
fetchTimer=10 fetchTimer=10
NET.room.fetch() NET.room_fetch()
end end
local scene={} local scene={}
@@ -84,11 +84,12 @@ function scene.keyDown(key)
end end
elseif roomList:getLen()>0 and (key=='join' or key=='return' and love.keyboard.isDown('lctrl','rctrl')) then elseif roomList:getLen()>0 and (key=='join' or key=='return' and love.keyboard.isDown('lctrl','rctrl')) then
local R=roomList:getSel() local R=roomList:getSel()
if TASK.getLock('fetchRoom') or not R then return end if R and not TASK.getLock('fetchRoom') then
if R.info.version==VERSION.room then if R.info.version==VERSION.room then
NET.room.enter(R.roomId,passwordBox.value) NET.room.enter(R.roomId,passwordBox.value)
else else
MES.new('error',text.versionNotMatch) MES.new('error',text.versionNotMatch)
end
end end
else else
return true return true