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