把所有网络操作抽象到NET中
This commit is contained in:
5
main.lua
5
main.lua
@@ -24,10 +24,6 @@ SAVEDIR=fs.getSaveDirectory()
|
||||
LOADED=false
|
||||
DAILYLAUNCH=false
|
||||
EDITING=""
|
||||
NET={
|
||||
login=false,
|
||||
allow_online=false,
|
||||
}
|
||||
ERRDATA={}
|
||||
|
||||
--System setting
|
||||
@@ -80,6 +76,7 @@ require"parts.gametoolfunc"
|
||||
SCR.setSize(1280,720)--Initialize Screen size
|
||||
FIELD[1]=newBoard()--Initialize field[1]
|
||||
|
||||
NET= require"parts.net"
|
||||
AIBUILDER= require"parts.AITemplate"
|
||||
FREEROW= require"parts.freeRow"
|
||||
|
||||
|
||||
@@ -1120,7 +1120,7 @@ do
|
||||
local message,op=WS.read("app")
|
||||
if message then
|
||||
if op=="ping"then
|
||||
WS.send("app",message,"pong")
|
||||
NET.pong("app",message)
|
||||
elseif op=="pong"then
|
||||
elseif op=="close"then
|
||||
message=JSON.decode(message)
|
||||
@@ -1155,7 +1155,7 @@ do
|
||||
local message,op=WS.read("user")
|
||||
if message then
|
||||
if op=="ping"then
|
||||
WS.send("user",message,"pong")
|
||||
NET.pong("user",message)
|
||||
elseif op=="pong"then
|
||||
elseif op=="close"then
|
||||
message=JSON.decode(message)
|
||||
@@ -1171,25 +1171,17 @@ do
|
||||
if res.id then
|
||||
USER.id=res.id
|
||||
USER.authToken=res.authToken
|
||||
WS.send("user",JSON.encode{action=0})
|
||||
NET.getAccessToken()
|
||||
end
|
||||
FILE.save(USER,"conf/user","q")
|
||||
LOG.print(text.loginSuccessed)
|
||||
|
||||
--Get self infos
|
||||
WS.send("user",JSON.encode{
|
||||
action=1,
|
||||
data={
|
||||
id=USER.id,
|
||||
},
|
||||
})
|
||||
NET.getSelfInfo()
|
||||
elseif res.action==0 then
|
||||
USER.accessToken=res.accessToken
|
||||
LOG.print(text.accessSuccessed)
|
||||
WS.connect("play","/play",JSON.encode{
|
||||
id=USER.id,
|
||||
accessToken=USER.accessToken,
|
||||
})
|
||||
NET.wsConnectPlay()
|
||||
elseif res.action==1 then
|
||||
USER.name=res.username
|
||||
USER.motto=res.motto
|
||||
@@ -1209,7 +1201,7 @@ do
|
||||
local message,op=WS.read("chat")
|
||||
if message then
|
||||
if op=="ping"then
|
||||
WS.send("chat",message,"pong")
|
||||
NET.pong("chat",message)
|
||||
elseif op=="pong"then
|
||||
elseif op=="close"then
|
||||
message=JSON.decode(message)
|
||||
@@ -1233,7 +1225,7 @@ do
|
||||
local message,op=WS.read("play")
|
||||
if message then
|
||||
if op=="ping"then
|
||||
WS.send("play",message,"pong")
|
||||
NET.pong("play",message)
|
||||
elseif op=="pong"then
|
||||
elseif op=="close"then
|
||||
message=JSON.decode(message)
|
||||
@@ -1259,7 +1251,7 @@ do
|
||||
local message,op=WS.read("stream")
|
||||
if message then
|
||||
if op=="ping"then
|
||||
WS.send("stream",message,"pong")
|
||||
NET.pong("stream",message)
|
||||
elseif op=="pong"then
|
||||
elseif op=="close"then
|
||||
message=JSON.decode(message)
|
||||
|
||||
84
parts/net.lua
Normal file
84
parts/net.lua
Normal file
@@ -0,0 +1,84 @@
|
||||
local data=love.data
|
||||
local NET={
|
||||
login=false,
|
||||
allow_online=false,
|
||||
}
|
||||
|
||||
--Account
|
||||
function NET.pong(wsName,message)
|
||||
WS.send(wsName,message,"pong")
|
||||
end
|
||||
function NET.getAccessToken()
|
||||
WS.send("user",JSON.encode{action=0})
|
||||
end
|
||||
function NET.getSelfInfo()
|
||||
WS.send("user",JSON.encode{
|
||||
action=1,
|
||||
data={
|
||||
id=USER.id,
|
||||
},
|
||||
})
|
||||
end
|
||||
|
||||
--Play
|
||||
function NET.wsConnectPlay()
|
||||
WS.connect("play","/play",JSON.encode{
|
||||
id=USER.id,
|
||||
accessToken=USER.accessToken,
|
||||
})
|
||||
end
|
||||
function NET.signal_ready()
|
||||
WS.send("play","R")
|
||||
end
|
||||
function NET.uploadRecStream(stream)
|
||||
WS.send("stream",data.encode("string","base64",stream))
|
||||
end
|
||||
function NET.signal_die()
|
||||
WS.send("play","D")
|
||||
end
|
||||
function NET.signal_quit()
|
||||
WS.send("play","Q")
|
||||
end
|
||||
|
||||
--Room
|
||||
function NET.freshRoom()
|
||||
WS.send("play","/play",JSON.encode{
|
||||
action=0,
|
||||
data={
|
||||
type=nil,
|
||||
begin=0,
|
||||
count=10,
|
||||
}
|
||||
})
|
||||
end
|
||||
function NET.createRoom()
|
||||
WS.send("play",JSON.encode{
|
||||
action=1,
|
||||
data={
|
||||
type=nil,
|
||||
name=(USER.name or"???").."'s room",
|
||||
password=nil,
|
||||
conf=dumpBasicConfig(),
|
||||
}
|
||||
})
|
||||
end
|
||||
function NET.enterRoom(roomID,password)
|
||||
WS.send("play","/play",JSON.encode{
|
||||
action=2,
|
||||
data={
|
||||
rid=roomID,
|
||||
conf=dumpBasicConfig(),
|
||||
password=password,
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
--Chat
|
||||
function NET.sendChatMes(mes)
|
||||
WS.send("chat","T"..data.encode("string","base64",mes))
|
||||
end
|
||||
function NET.quitChat()
|
||||
WS.send("chat","Q")
|
||||
end
|
||||
|
||||
return NET
|
||||
@@ -1813,7 +1813,7 @@ function Player.lose(P,force)
|
||||
gameOver()
|
||||
P:newTask(#PLAYERS>1 and tick_lose or tick_finish)
|
||||
if GAME.net then
|
||||
WS.send("play","D")
|
||||
NET.signal_die()
|
||||
else
|
||||
TASK.new(tick_autoPause)
|
||||
end
|
||||
|
||||
@@ -82,7 +82,7 @@ function scene.keyDown(key)
|
||||
TEXT.show(text.needUpdate,640,450,60,"flicker")
|
||||
SFX.play("finesseError")
|
||||
else
|
||||
WS.send("user",JSON.encode{action=0})
|
||||
NET.getAccessToken()
|
||||
end
|
||||
else
|
||||
SCN.go("login")
|
||||
|
||||
@@ -3,12 +3,12 @@ local data=love.data
|
||||
|
||||
local textBox=WIDGET.newTextBox{name="texts",x=40,y=50,w=1200,h=430}
|
||||
local remain--People in chat room
|
||||
local heartBeatTimer
|
||||
local escapeTimer=0
|
||||
|
||||
local function sendMessage()
|
||||
local W=WIDGET.active.input
|
||||
if #W.value>0 and WS.send("chat","T"..data.encode("string","base64",W.value))then
|
||||
if #W.value>0 then
|
||||
NET.sendChatMes(W.value)
|
||||
W.value=""
|
||||
end
|
||||
end
|
||||
@@ -16,7 +16,6 @@ end
|
||||
local scene={}
|
||||
|
||||
function scene.sceneInit()
|
||||
heartBeatTimer=0
|
||||
remain=false
|
||||
|
||||
local texts=textBox.texts
|
||||
@@ -30,7 +29,7 @@ function scene.sceneInit()
|
||||
BG.set("none")
|
||||
end
|
||||
function scene.sceneBack()
|
||||
WS.send("chat","Q")
|
||||
NET.quitChat()
|
||||
LOG.print(text.wsDisconnected,"warn")
|
||||
end
|
||||
|
||||
@@ -80,13 +79,6 @@ function scene.socketRead(mes)
|
||||
end
|
||||
end
|
||||
|
||||
function scene.update(dt)
|
||||
heartBeatTimer=heartBeatTimer+dt
|
||||
if heartBeatTimer>42 then
|
||||
heartBeatTimer=0
|
||||
WS.send("chat","P")
|
||||
end
|
||||
end
|
||||
function scene.draw()
|
||||
setFont(25)
|
||||
gc.setColor(1,1,1)
|
||||
|
||||
@@ -20,7 +20,6 @@ end
|
||||
|
||||
local playerInitialized
|
||||
local playing
|
||||
local heartBeatTimer
|
||||
local lastUpstreamTime
|
||||
local upstreamProgress
|
||||
local lastBackTime=0
|
||||
@@ -30,7 +29,7 @@ local touchMoveLastFrame=false
|
||||
local scene={}
|
||||
|
||||
function scene.sceneBack()
|
||||
WS.send("play","Q")
|
||||
NET.signal_quit()
|
||||
LOG.print(text.wsDisconnected,"warn")
|
||||
love.keyboard.setKeyRepeat(true)
|
||||
end
|
||||
@@ -107,7 +106,7 @@ function scene.keyDown(key)
|
||||
end
|
||||
elseif key=="space"then
|
||||
if not PLAYERS[1].ready then
|
||||
WS.send("play","R")
|
||||
NET.signal_ready()
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -262,14 +261,7 @@ function scene.update(dt)
|
||||
local GAME=GAME
|
||||
|
||||
if WS.status("play")~="running"and not SCN.swapping then SCN.back()end
|
||||
if not playing then
|
||||
heartBeatTimer=heartBeatTimer+dt
|
||||
if heartBeatTimer>42 then
|
||||
heartBeatTimer=0
|
||||
WS.send("play","P")
|
||||
end
|
||||
return
|
||||
end
|
||||
if not playing then return end
|
||||
|
||||
touchMoveLastFrame=false
|
||||
updateVirtualkey()
|
||||
@@ -289,7 +281,7 @@ function scene.update(dt)
|
||||
local stream
|
||||
stream,upstreamProgress=dumpRecording(GAME.rep,upstreamProgress)
|
||||
if #stream>0 then
|
||||
WS.send("stream",data.encode("string","base64",stream))
|
||||
NET.uploadRecStream(stream)
|
||||
else
|
||||
ins(GAME.rep,GAME.frame)
|
||||
ins(GAME.rep,0)
|
||||
|
||||
@@ -9,24 +9,7 @@ local lastCreateRoomTime=0
|
||||
local function fresh()
|
||||
lastfreshTime=TIME()
|
||||
rooms=nil
|
||||
WS.send("play","/play",JSON.encode{
|
||||
action=0,
|
||||
data={
|
||||
type=nil,
|
||||
begin=0,
|
||||
count=10,
|
||||
}
|
||||
})
|
||||
end
|
||||
local function enterRoom(roomID,password)
|
||||
WS.send("play","/play",JSON.encode{
|
||||
action=2,
|
||||
data={
|
||||
rid=roomID,
|
||||
conf=dumpBasicConfig(),
|
||||
password=password,
|
||||
}
|
||||
})
|
||||
NET.freshRoom()
|
||||
end
|
||||
|
||||
local scene={}
|
||||
@@ -48,15 +31,7 @@ function scene.keyDown(k)
|
||||
end
|
||||
elseif k=="n"then
|
||||
if TIME()-lastCreateRoomTime>26 then
|
||||
WS.send("play",JSON.encode{
|
||||
action=1,
|
||||
data={
|
||||
type=nil,
|
||||
name=(USER.name or"???").."'s room",
|
||||
password=nil,
|
||||
conf=dumpBasicConfig(),
|
||||
}
|
||||
})
|
||||
NET.createRoom()
|
||||
lastCreateRoomTime=TIME()
|
||||
else
|
||||
LOG.print(text.createRoomTooFast,"warn")
|
||||
@@ -83,7 +58,7 @@ function scene.keyDown(k)
|
||||
LOG.print("Can't enter private room now")
|
||||
return
|
||||
end
|
||||
enterRoom(rooms[selected].id)
|
||||
NET.enterRoom(rooms[selected].id)--,password
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user