更新联网相关代码(非最终,但不会运行到)
This commit is contained in:
@@ -427,9 +427,10 @@ function love.errorhandler(msg)
|
|||||||
love.timer.sleep(.26)
|
love.timer.sleep(.26)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local WSnames={"app","chat","play","stream"}
|
local WSnames={"app","user","chat","play","stream"}
|
||||||
local WScolor={
|
local WScolor={
|
||||||
{1,0,0,.26},
|
{1,0,0,.26},
|
||||||
|
{1,.7,0,.26},
|
||||||
{0,.7,1,.26},
|
{0,.7,1,.26},
|
||||||
{0,1,0,.26},
|
{0,1,0,.26},
|
||||||
{1,1,0,.26}
|
{1,1,0,.26}
|
||||||
@@ -540,7 +541,7 @@ function love.run()
|
|||||||
gc_push("transform")
|
gc_push("transform")
|
||||||
gc.translate(SCR.w,0)
|
gc.translate(SCR.w,0)
|
||||||
gc.scale(SCR.k)
|
gc.scale(SCR.k)
|
||||||
for i=1,4 do
|
for i=1,5 do
|
||||||
local status=WS.status(WSnames[i])
|
local status=WS.status(WSnames[i])
|
||||||
gc_setColor(WScolor[i])
|
gc_setColor(WScolor[i])
|
||||||
gc_rectangle("fill",0,20*i,-20,-20)
|
gc_rectangle("fill",0,20*i,-20,-20)
|
||||||
|
|||||||
5
main.lua
5
main.lua
@@ -328,6 +328,11 @@ do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if STAT.version<1301 then
|
||||||
|
fs.remove("conf/user")
|
||||||
|
NOGAME=true
|
||||||
|
end
|
||||||
|
|
||||||
for _,v in next,VK_org do
|
for _,v in next,VK_org do
|
||||||
if not v.color then
|
if not v.color then
|
||||||
NOGAME=true
|
NOGAME=true
|
||||||
|
|||||||
@@ -1063,45 +1063,116 @@ end
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
--Network tick function
|
--Network funcs
|
||||||
function TICK_httpREQ_getAccessToken(task)
|
do
|
||||||
local time=0
|
--[[
|
||||||
while true do
|
launch:
|
||||||
coroutine.yield()
|
local res=json.decode(res.body)
|
||||||
local response,request_error=client.poll(task)
|
if res.message=="OK"then
|
||||||
if response then
|
LOG.print(res.notice,360,COLOR.sky)
|
||||||
local res=json.decode(response.body)
|
if VERSION_CODE>=res.version_code then
|
||||||
if response.code==200 and res.message=="OK"then
|
LATEST_VERSION=true
|
||||||
|
else
|
||||||
|
LOG.print(string.gsub(text.oldVersion,"$1",res.version_name),"warn")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
LOG.print(text.httpCode..res.code..": "..res.message,"warn")
|
||||||
|
end
|
||||||
|
|
||||||
|
register:
|
||||||
|
if response.message=="OK"then
|
||||||
|
LOGIN=true
|
||||||
|
USER.name=res.name
|
||||||
|
USER.id=res.id
|
||||||
|
USER.motto=res.motto
|
||||||
|
USER.avatar=res.avatar
|
||||||
|
FILE.save(USER,"conf/user","q")
|
||||||
|
LOG.print(text.registerSuccessed..": "..res.message)
|
||||||
|
else
|
||||||
|
LOG.print(text.httpCode..response.code..": "..res.message,"warn")
|
||||||
|
end
|
||||||
|
|
||||||
|
autoLogin:
|
||||||
|
if res.message=="OK"then
|
||||||
|
LOGIN=true
|
||||||
|
LOG.print(text.loginSuccessed)
|
||||||
|
httpRequest(
|
||||||
|
TICK_httpREQ_getUserInfo,
|
||||||
|
PATH.http..PATH.user,
|
||||||
|
"GET",
|
||||||
|
{["Content-Type"]="application/json"},
|
||||||
|
json.encode{
|
||||||
|
email=USER.email,
|
||||||
|
authToken=USER.authToken,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
else
|
||||||
|
LOGIN=false
|
||||||
|
LOG.print(text.loginFailed..": "..text.httpCode..res.code.."-"..res.message,"warn")
|
||||||
|
end
|
||||||
|
return
|
||||||
|
|
||||||
|
newLogin:
|
||||||
|
if res.message=="OK"then
|
||||||
|
LOGIN=true
|
||||||
|
USER.email=res.email
|
||||||
|
USER.authToken=res.authToken
|
||||||
|
USER.id=res.id
|
||||||
|
FILE.save(USER,"conf/user","q")
|
||||||
|
LOG.print(text.loginSuccessed)
|
||||||
|
|
||||||
|
--TODO:getUserInfo
|
||||||
|
json.encode{
|
||||||
|
email=USER.email,
|
||||||
|
authToken=USER.authToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
--TODO:getAccessToken
|
||||||
|
json.encode{
|
||||||
|
email=USER.email,
|
||||||
|
authToken=USER.authToken,
|
||||||
|
}
|
||||||
|
else
|
||||||
|
LOG.print(text.httpCode..res.code..": "..res.message,"warn")
|
||||||
|
end
|
||||||
|
|
||||||
|
manualAutoLogin:
|
||||||
|
if res.message=="OK"then
|
||||||
LOG.print(text.accessSuccessed)
|
LOG.print(text.accessSuccessed)
|
||||||
USER.access_token=res.access_token
|
SCN.go("net_menu")
|
||||||
|
elseif res.code==403 or res.code==401 then
|
||||||
|
httpRequest(
|
||||||
|
TICK_httpREQ_getAccessToken,
|
||||||
|
PATH.http..PATH.access,
|
||||||
|
"POST",
|
||||||
|
{["Content-Type"]="application/json"},
|
||||||
|
json.encode{
|
||||||
|
email=USER.email,
|
||||||
|
authToken=USER.authToken,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
else
|
||||||
|
local err=json.decode(res.body)
|
||||||
|
if err then
|
||||||
|
LOG.print(text.httpCode..res.code..": "..err.message,"warn")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
getAccessToken:
|
||||||
|
if res.message=="OK"then
|
||||||
|
LOG.print(text.accessSuccessed)
|
||||||
|
USER.accessToken=res.accessToken
|
||||||
FILE.save(USER,"conf/user")
|
FILE.save(USER,"conf/user")
|
||||||
SCN.swapTo("net_menu")
|
SCN.swapTo("net_menu")
|
||||||
else
|
else
|
||||||
LOGIN=false
|
LOGIN=false
|
||||||
USER.access_token=false
|
USER.accessToken=false
|
||||||
USER.auth_token=false
|
USER.authToken=false
|
||||||
LOG.print(text.loginFailed..": "..text.httpCode..response.code.."-"..res.message,"warn")
|
LOG.print(text.loginFailed..": "..text.httpCode..response.code.."-"..res.message,"warn")
|
||||||
end
|
end
|
||||||
return
|
|
||||||
elseif request_error then
|
getUserInfo:
|
||||||
LOG.print(text.loginFailed..": "..request_error,"warn")
|
if res.message=="OK"then
|
||||||
return
|
|
||||||
end
|
|
||||||
time=time+1
|
|
||||||
if time>360 then
|
|
||||||
LOG.print(text.loginFailed..": "..text.httpTimeout,"message")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function TICK_httpREQ_getUserInfo(task)
|
|
||||||
local time=0
|
|
||||||
while true do
|
|
||||||
coroutine.yield()
|
|
||||||
local response,request_error=client.poll(task)
|
|
||||||
if response then
|
|
||||||
local res=json.decode(response.body)
|
|
||||||
if response.code==200 and res.message=="OK"then
|
|
||||||
USER.name=res.username
|
USER.name=res.username
|
||||||
USER.motto=res.motto
|
USER.motto=res.motto
|
||||||
USER.avatar=res.avatar
|
USER.avatar=res.avatar
|
||||||
@@ -1109,38 +1180,83 @@ function TICK_httpREQ_getUserInfo(task)
|
|||||||
else
|
else
|
||||||
LOG.print("Get user info failed: "..text.httpCode..response.code.."-"..res.message,"warn")
|
LOG.print("Get user info failed: "..text.httpCode..response.code.."-"..res.message,"warn")
|
||||||
end
|
end
|
||||||
return
|
|
||||||
elseif request_error then
|
goChatRoom:
|
||||||
LOG.print(text.loginFailed..": "..request_error,"warn")
|
if res.message=="OK"then
|
||||||
return
|
SCN.go("net_chat")
|
||||||
|
LOG.print(text.wsSuccessed,"warn")
|
||||||
|
else
|
||||||
|
LOG.print(text.wsFailed..": "..connErr,"warn")
|
||||||
end
|
end
|
||||||
time=time+1
|
|
||||||
if time>360 then
|
fetchRooms:
|
||||||
LOG.print(text.loginFailed..": "..text.httpTimeout,"message")
|
if res.message=="OK"then
|
||||||
return
|
rooms=res.room_list
|
||||||
|
else
|
||||||
|
LOG.print(text.httpCode..response.code..": "..res.message,"warn")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
createRoom:
|
||||||
|
if res.message=="OK" then
|
||||||
|
LOG.print(text.createRoomSuccessed)
|
||||||
|
enterRoom(res.room.id)
|
||||||
|
else
|
||||||
|
LOG.print(text.httpCode..res.code..": "..res.message,"warn")
|
||||||
end
|
end
|
||||||
end
|
|
||||||
function TICK_wsRead()
|
|
||||||
|
enterRoom:
|
||||||
|
if res.message=="OK"then
|
||||||
|
loadGame("netBattle",true,true)
|
||||||
|
LOG.print(text.wsSuccessed,"warn")
|
||||||
|
else
|
||||||
|
LOG.print(text.wsFailed..": "..connErr,"warn")
|
||||||
|
end
|
||||||
|
]]
|
||||||
|
|
||||||
|
function TICK_WS_app()
|
||||||
while true do
|
while true do
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
if not WSCONN then return end
|
local status=WS.status("app")
|
||||||
local messages,readErr=client.read(WSCONN)
|
if status=="running"then
|
||||||
if messages then
|
local message,op=WS.read("app")
|
||||||
if SCN.socketRead then
|
if message then
|
||||||
for i=1,#messages do
|
if op=="ping"then
|
||||||
SCN.socketRead(messages[i])
|
WS.send("app",message,"pong")
|
||||||
end
|
--TODO: ping animation
|
||||||
|
--TODO: what to do with res?
|
||||||
|
elseif op=="close"then
|
||||||
|
LOG.print(text.wsClose..message,"warn")
|
||||||
|
return
|
||||||
else
|
else
|
||||||
|
message=json.decode(message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif status~="connecting"then
|
||||||
|
WS.connect("app","/app")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function TICK_WS_chat()
|
||||||
|
while true do
|
||||||
|
coroutine.yield()
|
||||||
|
local status=WS.status("chat")
|
||||||
|
if status=="running"then
|
||||||
|
local message,op=WS.read("chat")
|
||||||
|
if message then
|
||||||
|
if op=="ping"then
|
||||||
|
WS.send("chat",message,"pong")
|
||||||
|
--TODO: ping animation
|
||||||
|
--TODO: what to do with res?
|
||||||
|
elseif op=="close"then
|
||||||
|
LOG.print(text.wsClose..message,"warn")
|
||||||
return
|
return
|
||||||
end
|
else
|
||||||
elseif readErr then
|
message=json.decode(message)
|
||||||
WSCONN=false
|
|
||||||
LOG.print(text.wsError..tostring(readErr),"warn")
|
|
||||||
while #SCN.stack>4 do
|
|
||||||
SCN.pop()
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
--TODO: more WSs
|
||||||
end
|
end
|
||||||
@@ -236,8 +236,8 @@ USER=FILE.load("conf/user")or{--User infomation
|
|||||||
email=false,
|
email=false,
|
||||||
motto=false,
|
motto=false,
|
||||||
avatar=false,
|
avatar=false,
|
||||||
auth_token=false,
|
authToken=false,
|
||||||
access_token=false,
|
accessToken=false,
|
||||||
|
|
||||||
--Local data
|
--Local data
|
||||||
xp=0,lv=1,
|
xp=0,lv=1,
|
||||||
|
|||||||
@@ -132,19 +132,10 @@ function scene.update()
|
|||||||
SFX.play("welcome_sfx")
|
SFX.play("welcome_sfx")
|
||||||
VOC.play("welcome_voc")
|
VOC.play("welcome_voc")
|
||||||
--[[TODO
|
--[[TODO
|
||||||
httpRequest(tick_httpREQ_launch,PATH.http..PATH.appInfo)
|
WS.send("user",json.encode{
|
||||||
if USER.auth_token and USER.email then
|
id=USER.id,
|
||||||
httpRequest(
|
authToken=USER.authToken,
|
||||||
tick_httpREQ_autoLogin,
|
})
|
||||||
PATH.http..PATH.auth,
|
|
||||||
"GET",
|
|
||||||
{["Content-Type"]="application/json"},
|
|
||||||
json.encode{
|
|
||||||
email=USER.email,
|
|
||||||
auth_token=USER.auth_token,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
end
|
|
||||||
]]
|
]]
|
||||||
if FESTIVAL=="Xmas"then
|
if FESTIVAL=="Xmas"then
|
||||||
LOG.print("==============",COLOR.red)
|
LOG.print("==============",COLOR.red)
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ local function login()
|
|||||||
LOG.print(text.noPassword)return
|
LOG.print(text.noPassword)return
|
||||||
end
|
end
|
||||||
--[[TODO
|
--[[TODO
|
||||||
json.encode{
|
WS.send("user",json.encode{
|
||||||
email=email,
|
email=email,
|
||||||
password=password,
|
password=password,
|
||||||
}
|
})
|
||||||
]]
|
]]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -71,28 +71,18 @@ scene.widgetList={
|
|||||||
WIDGET.newButton{name="online", x=370,y=220,w=200,h=140,color="lCyan", font=40,code=function()
|
WIDGET.newButton{name="online", x=370,y=220,w=200,h=140,color="lCyan", font=40,code=function()
|
||||||
if LOGIN then
|
if LOGIN then
|
||||||
--[[TODO
|
--[[TODO
|
||||||
if USER.access_token then
|
if USER.accessToken then
|
||||||
httpRequest(
|
WS.send("app",json.encode{
|
||||||
tick_httpREQ_manualAutoLogin,
|
opration="access",
|
||||||
PATH.http..PATH.access,
|
|
||||||
"GET",
|
|
||||||
{["Content-Type"]="application/json"},
|
|
||||||
json.encode{
|
|
||||||
email=USER.email,
|
email=USER.email,
|
||||||
access_token=USER.access_token,
|
accessToken=USER.accessToken,
|
||||||
}
|
})
|
||||||
)
|
|
||||||
else
|
else
|
||||||
httpRequest(
|
WS.send("app",json.encode{
|
||||||
TICK_httpREQ_getAccessToken,
|
opration="access",
|
||||||
PATH.http..PATH.access,
|
|
||||||
"POST",
|
|
||||||
{["Content-Type"]="application/json"},
|
|
||||||
json.encode{
|
|
||||||
email=USER.email,
|
email=USER.email,
|
||||||
auth_token=USER.auth_token,
|
authToken=USER.authToken,
|
||||||
}
|
})
|
||||||
)
|
|
||||||
end
|
end
|
||||||
]]
|
]]
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -7,14 +7,7 @@ end
|
|||||||
scene.widgetList={
|
scene.widgetList={
|
||||||
WIDGET.newButton{name="ffa", x=640, y=200,w=350,h=120,font=40,code=NULL},
|
WIDGET.newButton{name="ffa", x=640, y=200,w=350,h=120,font=40,code=NULL},
|
||||||
WIDGET.newButton{name="rooms", x=640, y=360,w=350,h=120,font=40,code=goScene"net_rooms"},
|
WIDGET.newButton{name="rooms", x=640, y=360,w=350,h=120,font=40,code=goScene"net_rooms"},
|
||||||
WIDGET.newButton{name="chat", x=640, y=540,w=350,h=120,font=40,code=function()
|
WIDGET.newButton{name="chat", x=640, y=540,w=350,h=120,font=40,code=goScene"net_chat",hide=function()return WS.status("chat")~="running"end},
|
||||||
--[[TODO
|
|
||||||
wsConnect("chat","/chat",json.encode{
|
|
||||||
email=USER.email,
|
|
||||||
token=USER.access_token,
|
|
||||||
})
|
|
||||||
]]
|
|
||||||
end},
|
|
||||||
WIDGET.newButton{name="back", x=1140, y=640,w=170,h=80,font=40,code=backScene},
|
WIDGET.newButton{name="back", x=1140, y=640,w=170,h=80,font=40,code=backScene},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ local function enterRoom(roomID)
|
|||||||
--[[TODO
|
--[[TODO
|
||||||
WS.connect("play","/play",json.encode{
|
WS.connect("play","/play",json.encode{
|
||||||
email=USER.email,
|
email=USER.email,
|
||||||
token=USER.access_token,
|
token=USER.accessToken,
|
||||||
id=roomID,
|
id=roomID,
|
||||||
conf=dumpBasicConfig(),
|
conf=dumpBasicConfig(),
|
||||||
-- password=password,
|
-- password=password,
|
||||||
@@ -23,7 +23,7 @@ local function fresh()
|
|||||||
--[[TODO
|
--[[TODO
|
||||||
WS.connect("play","/play",json.encode{
|
WS.connect("play","/play",json.encode{
|
||||||
email=USER.email,
|
email=USER.email,
|
||||||
access_token=USER.access_token,
|
accessToken=USER.accessToken,
|
||||||
})
|
})
|
||||||
]]
|
]]
|
||||||
end
|
end
|
||||||
@@ -50,7 +50,7 @@ function scene.keyDown(k)
|
|||||||
--[[TODO
|
--[[TODO
|
||||||
WS.send("room",json.encode{
|
WS.send("room",json.encode{
|
||||||
email=USER.email,
|
email=USER.email,
|
||||||
access_token=USER.access_token,
|
accessToken=USER.accessToken,
|
||||||
room_name=(USER.name or"???").."'s room",
|
room_name=(USER.name or"???").."'s room",
|
||||||
room_password=nil,
|
room_password=nil,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user