联网推进(观战功能测试)

This commit is contained in:
MrZ626
2021-05-18 19:52:29 +08:00
parent d07075ca9c
commit 5b43ff8c45
12 changed files with 123 additions and 62 deletions

View File

@@ -105,10 +105,11 @@ return{
started="Playing",
joinRoom="has joined the room.",
leaveRoom="has left the room.",
ready="READY",
connStream="CONNECTING",
waitStream="WAITING",
ready="Ready",
connStream="Connecting",
waitStream="Waiting",
champion="$1 won",
spectating="Spectating",
chatRemain="Online",
chatStart="------Beginning of log------",
chatHistory="------New messages below------",
@@ -266,6 +267,7 @@ return{
},
net_game={
ready="Ready",
spectate="Spectate",
cancel="Cancel",
},
setting_game={

View File

@@ -109,6 +109,7 @@ return{
-- connStream="CONNECTING",
-- waitStream="WAITING",
champion="$1 a gagné",
-- spectating="Spectating",
chatRemain="En ligne : ",
chatStart="--------Début des logs--------",
chatHistory="-Nouveaux messages en dessous-",
@@ -236,6 +237,7 @@ return{
},
net_game={
-- ready="Ready",
-- spectate="Spectate",
-- cancel="Cancel",
},
setting_game={

View File

@@ -266,6 +266,7 @@ return{
},
net_game={
-- ready="Ready",
-- spectate="Spectate",
-- cancel="Cancel",
},
setting_game={

View File

@@ -109,6 +109,7 @@ return{
-- connStream="CONNECTING",
-- waitStream="WAITING",
champion="$1 ganó!",
-- spectating="Spectating",
chatRemain="Usuarios en línea: ",
chatStart="------Comienzo del historial------",
chatHistory="------Nuevos mensajes------",
@@ -243,6 +244,7 @@ return{
},
net_game={
ready="Estoy Listo",
-- spectate="Spectate",
-- cancel="Cancel",
},
setting_game={

View File

@@ -47,6 +47,7 @@ return{
createRoomSuccessed="创好了",
started="开了",
champion="神仙是 $1",
spectating="看戏中",
stat={
"开了几次:",

View File

@@ -109,6 +109,7 @@ return{
connStream="正在连接",
waitStream="等待其他人连接",
champion="$1 获胜",
spectating="观战中",
chatRemain="人数:",
chatStart="------消息的开头------",
chatHistory="------以上是历史消息------",
@@ -266,6 +267,7 @@ return{
},
net_game={
ready="准备",
spectate="观战",
cancel="取消",
},
setting_game={

View File

@@ -8,8 +8,8 @@ local NET={
connected=false,
allow_online=false,
accessToken=false,
roomList={},
roomState={
roomList={},--Local roomlist, updated frequently
roomState={--A copy of room structure on server
roomInfo={
name=false,
type=false,
@@ -21,10 +21,13 @@ local NET={
private=false,
start=false,
},
watch=false,
allReady=false,
waitingStream=false,
spectate=false,--If player is spectating
streamRoomID=false,
seed=false,
allReady=false,
connectingStream=false,
waitingStream=false,
UserCount="_",
PlayCount="_",
@@ -136,7 +139,7 @@ function NET.wsconn_stream()
WS.connect('stream','/stream',JSON.encode{
uid=USER.uid,
accessToken=NET.accessToken,
rid=NET.streamRoomID,
srid=NET.streamRoomID,
})
TASK.new(NET.updateWS_stream)
end
@@ -266,9 +269,9 @@ end
function NET.changeConfig()
WS.send('play','{"action":5,"data":'..JSON.encode({config=dumpBasicConfig()})..'}')
end
function NET.signal_ready(ready)
function NET.signal_joinMode(ready)
if NET.lock('ready',3)and not NET.roomState.start then
WS.send('play','{"action":6,"data":{"ready":'..tostring(ready)..'}}')
WS.send('play','{"action":6,"data":'..JSON.encode{mode=ready}..'}')
end
end
function NET.signal_die()
@@ -423,7 +426,7 @@ function NET.updateWS_play()
uid=p.uid,
username=p.username,
sid=p.sid,
ready=p.ready,
mode=p.mode,
config=p.config,
}
end
@@ -434,15 +437,27 @@ function NET.updateWS_play()
NET.roomState.capacity=d.capacity
NET.roomState.private=d.private
NET.roomState.start=d.start
NET.srid=d.srid
NET.allReady=false
NET.connectingStream=false
NET.waitingStream=false
NET.spectate=false
NET.streamRoomID=false
loadGame('netBattle',true,true)
if d.srid then
NET.spectate=true
NET.streamRoomID=d.srid
NET.connectingStream=true
end
else
--Load other players
netPLY.add{
uid=d.uid,
username=d.username,
sid=d.sid,
ready=d.ready,
mode=d.mode,
config=d.config,
}
if SCN.socketRead then SCN.socketRead('join',d)end
@@ -464,13 +479,13 @@ function NET.updateWS_play()
if SCN.socketRead then SCN.socketRead('talk',d)end
elseif res.action==5 then--Player change settings
netPLY.setConf(d.uid,d.config)
elseif res.action==6 then--One ready
netPLY.setReady(d.uid,d.ready)
elseif res.action==6 then--Player change join mode
netPLY.setJoinMode(d.uid,d.mode)
elseif res.action==7 then--All Ready
SFX.play('reach',.6)
NET.allReady=true
elseif res.action==8 then--Set
NET.streamRoomID=d.rid
NET.streamRoomID=d.srid
NET.allReady=false
NET.connectingStream=true
NET.wsconn_stream()
@@ -507,25 +522,34 @@ function NET.updateWS_stream()
if res.type=='Connect'then
NET.unlock('wsc_stream')
NET.connectingStream=false
NET.waitingStream=true
elseif res.action==0 then--Game start
NET.waitingStream=false
NET.roomState.start=true
if SCN.socketRead then SCN.socketRead('go',d)end
SCN.socketRead('go')
elseif res.action==1 then--Game finished
--?
elseif res.action==2 then--Player join
if res.type=='Self'then
NET.seed=d.seed
NET.spectate=d.spectate
netPLY.setConnect(d.uid)
for _,p in next,d.connected do
if not p.watch then
if not p.spectate then
netPLY.setConnect(p.uid)
end
end
netPLY.setConnect(d.uid)
NET.spectate=d.watch==true
if d.spectate then
if d.start then
SCN.socketRead('go')
for _,v in next,d.history or{}do
SCN.socketRead('stream',v)
end
end
else
NET.waitingStream=true
end
else
if d.watch then
netPLY.setWatch(d.uid)
if d.spectate then
netPLY.setJoinMode(d.uid,2)
else
netPLY.setConnect(d.uid)
end

View File

@@ -87,7 +87,7 @@ end
function netPLY.clear()for _=1,netPLY.getCount()do rem(PLY)end end
function netPLY.add(p)
p.watch=false
p.mode=0
p.connected=false
ins(PLY,p.uid==USER.uid and 1 or #PLY+1,p)
local a=rnd()*6.2832
@@ -101,21 +101,22 @@ end
function netPLY.getCount()return #PLY end
function netPLY.rawgetPLY(i)return PLY[i]end
function netPLY.getSID(uid)return getPLY(uid).sid end
function netPLY.getSelfReady()return PLY[1].ready end
function netPLY.getSelfJoinMode()return PLY[1].mode end
function netPLY.getSelfReady()return PLY[1].mode>0 end
function netPLY.setPlayerObj(ply,p)ply.p=p end
function netPLY.setConf(uid,config)getPLY(uid).config=config end
function netPLY.setReady(uid,ready)
function netPLY.setJoinMode(uid,ready)
for i,p in next,PLY do
if p.uid==uid then
if p.ready~=ready then
p.ready=ready
if not ready then NET.allReady=false end
if p.mode~=ready then
p.mode=ready
if ready==0 then NET.allReady=false end
SFX.play('spin_0',.6)
if i==1 then
NET.unlock('ready')
elseif not PLY[1].ready then
elseif PLY[1].mode==0 then
for j=2,#PLY do
if not PLY[j].ready then
if PLY[j].mode==0 then
return
end
end
@@ -127,11 +128,9 @@ function netPLY.setReady(uid,ready)
end
end
function netPLY.setConnect(uid)getPLY(uid).connected=true end
function netPLY.setWatch(uid)getPLY(uid).watch=true end
function netPLY.resetState()
for i=1,#PLY do
PLY[i].ready=false
PLY[i].watch=false
PLY[i].mode=0
PLY[i].connected=false
end
end
@@ -169,7 +168,11 @@ function netPLY.draw()
local p=PLY[i]
gc.translate(p.x,p.y)
--Rectangle
gc.setColor(COLOR[p.watch and"L"or p.connected and"N"or p.ready and'G'or'Z'])
gc.setColor(COLOR[
p.mode==0 and'Z'or
p.mode==1 and(p.connected and"N"or"G")or
p.mode==2 and(p.connected and"Y"or"F")
])
gc.setLineWidth(2)
gc.rectangle('line',0,0,p.w,p.h)

View File

@@ -21,7 +21,7 @@ local scene={}
function scene.sceneInit(org)
BG.set()
destroyPlayers()
mapCam.zoomK=org=="main"and 5 or 1
mapCam.zoomK=org=='main'and 5 or 1
end
local function getK()

View File

@@ -1,5 +1,8 @@
local gc,tc=love.graphics,love.touch
local gc_setColor,gc_print=gc.setColor,gc.print
local setFont,mStr=setFont,mStr
local ins=table.insert
local SCR,VK,NET,netPLY=SCR,VK,NET,netPLY
@@ -15,9 +18,9 @@ local lastBackTime=0
local noTouch,noKey=false,false
local touchMoveLastFrame=false
local function _switchReady()
NET.signal_ready(not netPLY.getSelfReady())
end
local function _setReady()NET.signal_joinMode(1)end
local function _setSpectate()NET.signal_joinMode(2)end
local function _setCancel()NET.signal_joinMode(0)end
local function _gotoSetting()
if not(netPLY.getSelfReady()or NET.getlock('ready'))then
SCN.go('setting_game')
@@ -36,8 +39,10 @@ function scene.sceneInit(org)
lastUpstreamTime=0
upstreamProgress=1
if org=="setting_game"then
NET.changeConfig()
if org=='setting_game'then NET.changeConfig()end
if NET.streamRoomID then
NET.wsconn_stream()
NET.streamRoomID=false
end
end
function scene.sceneBack()
@@ -123,7 +128,15 @@ function scene.keyDown(key)
end
else
if key=="space"then
_switchReady()
if netPLY.getSelfJoinMode()==0 then
_setReady()
else
_setCancel()
end
elseif key=="p"then
if netPLY.getSelfJoinMode()==0 then
_setSpectate()
end
elseif key=="s"then
_gotoSetting()
end
@@ -184,7 +197,7 @@ function scene.socketRead(cmd,d)
love.keyboard.setKeyRepeat(false)
lastUpstreamTime=0
upstreamProgress=1
resetGameData('n',d.seed)
resetGameData('n',NET.seed)
netPLY.mouseMove(0,0)
else
LOG.print("Redundant [Go]",'warn')
@@ -204,7 +217,7 @@ function scene.socketRead(cmd,d)
end
netPLY.resetState()
elseif cmd=='stream'then
if d.uid~=USER.uid and playing then
if d.uid~=USER.uid then
for _,P in next,PLAYERS do
if P.uid==d.uid then
local res,stream=pcall(love.data.decode,'string','base64',d.stream)
@@ -213,9 +226,11 @@ function scene.socketRead(cmd,d)
else
LOG.print("Bad stream from "..P.username.."#"..P.uid,30)
end
break
end
end
end
end
end
@@ -271,6 +286,12 @@ function scene.draw()
--Warning
drawWarning()
if NET.spectate then
setFont(30)
gc_setColor(.2,1,0,.8)
gc_print(text.spectating,940,0)
end
else
--Users
netPLY.draw()
@@ -278,24 +299,24 @@ function scene.draw()
--Ready & Set mark
setFont(50)
if NET.allReady then
gc.setColor(0,1,.5,.9)
gc_setColor(0,1,.5,.9)
mStr(text.ready,640,15)
elseif NET.connectingStream then
gc.setColor(.1,1,.8,.9)
gc_setColor(.1,1,.8,.9)
mStr(text.connStream,640,15)
elseif NET.waitingStream then
gc.setColor(0,.8,1,.9)
gc_setColor(0,.8,1,.9)
mStr(text.waitStream,640,15)
end
--Room info.
gc.setColor(1,1,1)
gc_setColor(1,1,1)
setFont(25)
gc.printf(NET.roomState.roomInfo.name,0,685,1270,'right')
setFont(40)
gc.print(netPLY.getCount().."/"..NET.roomState.capacity,70,655)
if NET.roomState.private then gc.draw(IMG.lock,30,668)end
if NET.roomState.start then gc.setColor(0,1,0)gc.print(text.started,230,655)end
if NET.roomState.start then gc_setColor(0,1,0)gc_print(text.started,230,655)end
--Profile
drawSelfProfile()
@@ -307,7 +328,7 @@ function scene.draw()
--New message
if textBox.new then
setFont(40)
gc.setColor(1,1,0)
gc_setColor(1,1,0)
gc.print("M",430,10)
end
end
@@ -315,20 +336,28 @@ scene.widgetList={
textBox,
inputBox,
WIDGET.newKey{name="setting",fText=TEXTURE.setting,x=1200,y=160,w=90,h=90,code=_gotoSetting,hideF=function()return playing or netPLY.getSelfReady()or NET.getlock('ready')end},
WIDGET.newKey{name="ready",x=1060,y=630,w=300,h=80,color='lB',font=40,code=_switchReady,
WIDGET.newKey{name="ready",x=950,y=630,w=190,h=80,color='lG',font=35,code=_setReady,
hideF=function()
return
playing or
NET.roomState.start or
netPLY.getSelfReady()or
netPLY.getSelfReady() or
NET.getlock('ready')
end},
WIDGET.newKey{name="cancel",x=1060,y=630,w=300,h=80,color='H',font=40,code=_switchReady,
WIDGET.newKey{name="spectate",x=1150,y=630,w=190,h=80,color='lO',font=35,code=_setSpectate,
hideF=function()
return
playing or
NET.roomState.start or
not netPLY.getSelfReady()or
netPLY.getSelfReady() or
NET.getlock('ready')
end},
WIDGET.newKey{name="cancel",x=1050,y=630,w=390,h=80,color='lH',font=40,code=_setCancel,
hideF=function()
return
playing or
NET.roomState.start or
not netPLY.getSelfReady() or
NET.getlock('ready')
end},
WIDGET.newKey{name="hideChat",fText="...",x=380,y=35,w=60,font=35,code=pressKey"return"},

View File

@@ -29,10 +29,6 @@ local scene={}
function scene.sceneInit()
BG.set()
NET.spectate=false
NET.allReady=false
NET.connectingStream=false
NET.waitingStream=false
scrollPos=0
selected=1
fetchRoom()
@@ -113,7 +109,6 @@ function scene.mouseClick(x,y)
if NET.roomList[s]then
if selected~=s then
selected=s
print(1)
SFX.play('click',.4)
else
scene.keyDown("return")

View File

@@ -34,7 +34,7 @@ function scene.sceneInit(org)
local P=PLAYERS[1]
local S=P.stat
timer=org=="game"and 0 or 50
timer=org=='game'and 0 or 50
local frameLostRate=(S.frame/S.time/60-1)*100
form={