Merge branch 'ws_test' into main
This commit is contained in:
@@ -1066,19 +1066,6 @@ end
|
||||
--Network funcs
|
||||
do
|
||||
--[[
|
||||
launch:
|
||||
local res=JSON.decode(res.body)
|
||||
if res.message=="OK"then
|
||||
LOG.print(res.notice,360,COLOR.sky)
|
||||
if VERSION_CODE>=res.version_code 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
|
||||
@@ -1092,95 +1079,6 @@ do
|
||||
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)
|
||||
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")
|
||||
SCN.swapTo("net_menu")
|
||||
else
|
||||
LOGIN=false
|
||||
USER.accessToken=false
|
||||
USER.authToken=false
|
||||
LOG.print(text.loginFailed..": "..text.httpCode..response.code.."-"..res.message,"warn")
|
||||
end
|
||||
|
||||
getUserInfo:
|
||||
if res.message=="OK"then
|
||||
USER.name=res.username
|
||||
USER.motto=res.motto
|
||||
USER.avatar=res.avatar
|
||||
FILE.save(USER,"conf/user")
|
||||
else
|
||||
LOG.print("Get user info failed: "..text.httpCode..response.code.."-"..res.message,"warn")
|
||||
end
|
||||
|
||||
goChatRoom:
|
||||
if res.message=="OK"then
|
||||
SCN.go("net_chat")
|
||||
@@ -1204,7 +1102,6 @@ do
|
||||
LOG.print(text.httpCode..res.code..": "..res.message,"warn")
|
||||
end
|
||||
|
||||
|
||||
enterRoom:
|
||||
if res.message=="OK"then
|
||||
loadGame("netBattle",true,true)
|
||||
@@ -1215,6 +1112,7 @@ do
|
||||
]]
|
||||
|
||||
function TICK_WS_app()
|
||||
local retryTime=5
|
||||
while true do
|
||||
YIELD()
|
||||
local status=WS.status("app")
|
||||
@@ -1223,20 +1121,76 @@ do
|
||||
if message then
|
||||
if op=="ping"then
|
||||
WS.send("app",message,"pong")
|
||||
--TODO: ping animation
|
||||
--TODO: what to do with res?
|
||||
elseif op=="close"then
|
||||
LOG.print(text.wsClose..message,"warn")
|
||||
message=JSON.decode(message)
|
||||
if message then
|
||||
LOG.print(text.wsClose..message.message,"warn")
|
||||
end
|
||||
return
|
||||
else
|
||||
message=JSON.decode(message)
|
||||
local res=JSON.decode(message)
|
||||
if VERSION_CODE>=res.lowest then
|
||||
ALLOW_ONLINE=true
|
||||
end
|
||||
if VERSION_CODE<res.newestCode then
|
||||
LOG.print(text.oldVersion:gsub("$1",res.newestName),180,COLOR.sky)
|
||||
end
|
||||
LOG.print(res.notice,300,COLOR.sky)
|
||||
end
|
||||
end
|
||||
elseif status=="dead"then
|
||||
for _=1,60 do YIELD()end
|
||||
WS.connect("app","/app",JSON.encode{
|
||||
version=VERSION_CODE,
|
||||
})
|
||||
retryTime=retryTime-1
|
||||
if retryTime==0 then return end
|
||||
for _=1,120 do YIELD()end
|
||||
WS.connect("app","/app")
|
||||
end
|
||||
end
|
||||
end
|
||||
function TICK_WS_user()
|
||||
while true do
|
||||
YIELD()
|
||||
local status=WS.status("user")
|
||||
if status=="running"then
|
||||
local message,op=WS.read("user")
|
||||
if message then
|
||||
if op=="ping"then
|
||||
WS.send("user",message,"pong")
|
||||
elseif op=="close"then
|
||||
message=JSON.decode(message)
|
||||
if message then
|
||||
LOG.print(text.wsClose..message.message,"warn")
|
||||
end
|
||||
return
|
||||
else
|
||||
local res=JSON.decode(message)
|
||||
if res.message=="Connected"then
|
||||
LOGIN=true
|
||||
if res.id then
|
||||
USER.id=res.id
|
||||
USER.authToken=res.authToken
|
||||
SCN.go("net_menu")
|
||||
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,
|
||||
},
|
||||
})
|
||||
elseif res.action==0 then
|
||||
USER.accessToken=res.accessToken
|
||||
LOG.print(text.accessSuccessed)
|
||||
elseif res.action==1 then
|
||||
USER.name=res.username
|
||||
USER.motto=res.motto
|
||||
USER.avatar=res.avatar
|
||||
FILE.save(USER,"conf/user")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1249,22 +1203,64 @@ do
|
||||
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")
|
||||
message=JSON.decode(message)
|
||||
if message then
|
||||
LOG.print(text.wsClose..message.message,"warn")
|
||||
end
|
||||
return
|
||||
else
|
||||
message=JSON.decode(message)
|
||||
local res=JSON.decode(message)
|
||||
--TODO
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function TICK_WS_play()
|
||||
while true do
|
||||
YIELD()
|
||||
local status=WS.status("play")
|
||||
if status=="running"then
|
||||
local message,op=WS.read("play")
|
||||
if message then
|
||||
if op=="ping"then
|
||||
WS.send("play",message,"pong")
|
||||
elseif op=="close"then
|
||||
message=JSON.decode(message)
|
||||
if message then
|
||||
LOG.print(text.wsClose..message.message,"warn")
|
||||
end
|
||||
return
|
||||
else
|
||||
local res=JSON.decode(message)
|
||||
--TODO
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function TICK_WS_stream()
|
||||
while true do
|
||||
YIELD()
|
||||
local status=WS.status("stream")
|
||||
if status=="running"then
|
||||
local message,op=WS.read("stream")
|
||||
if message then
|
||||
if op=="ping"then
|
||||
WS.send("stream",message,"pong")
|
||||
elseif op=="close"then
|
||||
message=JSON.decode(message)
|
||||
if message then
|
||||
LOG.print(text.wsClose..message.message,"warn")
|
||||
end
|
||||
return
|
||||
else
|
||||
local res=JSON.decode(message)
|
||||
--TODO
|
||||
end
|
||||
end
|
||||
elseif status=="dead"then
|
||||
for _=1,60 do YIELD()end
|
||||
WS.connect("chat","/chat",JSON.encode{
|
||||
version=VERSION_CODE,
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
--TODO: more WSs
|
||||
end
|
||||
@@ -176,12 +176,18 @@ local loadingThread=coroutine.create(function()
|
||||
end
|
||||
STAT.run=STAT.run+1
|
||||
LOADED=true
|
||||
--[[TODO
|
||||
WS.send("user",JSON.encode{
|
||||
|
||||
--Connect to server
|
||||
TASK.new(TICK_WS_app)
|
||||
TASK.new(TICK_WS_user)
|
||||
WS.connect("app","/app")
|
||||
if USER.authToken then
|
||||
WS.connect("user","/user",JSON.encode{
|
||||
id=USER.id,
|
||||
authToken=USER.authToken,
|
||||
})
|
||||
]]
|
||||
end
|
||||
|
||||
while true do
|
||||
if math.random()<.126 then
|
||||
upFloor()
|
||||
|
||||
@@ -6,20 +6,19 @@ local function login()
|
||||
elseif #password==0 then
|
||||
LOG.print(text.noPassword)return
|
||||
end
|
||||
--[[TODO
|
||||
WS.send("user",JSON.encode{
|
||||
email=email,
|
||||
password=password,
|
||||
})
|
||||
]]
|
||||
USER.email=email
|
||||
WS.connect("user","/user",JSON.encode{
|
||||
email=email,
|
||||
password=password,
|
||||
})
|
||||
end
|
||||
|
||||
local scene={}
|
||||
|
||||
scene.widgetList={
|
||||
WIDGET.newText{name="title", x=80, y=50,font=70,align="L"},
|
||||
-- WIDGET.newButton{name="register", x=1140, y=100,w=170,h=80,color="green",code=function()SCN.swapTo("register","swipeR")end},
|
||||
WIDGET.newInputBox{name="email", x=380, y=200,w=500,h=60,regex="[0-9A-Za-z@._-]"},
|
||||
-- WIDGET.newButton{name="register",x=1140, y=100,w=170,h=80,color="green",code=function()SCN.swapTo("register","swipeR")end},
|
||||
WIDGET.newInputBox{name="email", x=380, y=200,w=500,h=60,regex="[0-9A-Za-z@._-]"},
|
||||
WIDGET.newInputBox{name="password", x=380, y=300,w=626,h=60,secret=true,regex="[ -~]"},
|
||||
WIDGET.newKey{name="login", x=1140, y=540,w=170,h=80,font=40,code=login},
|
||||
WIDGET.newButton{name="back", x=1140, y=640,w=170,h=80,font=40,code=backScene},
|
||||
|
||||
@@ -77,25 +77,14 @@ function scene.keyDown(key)
|
||||
end
|
||||
elseif key=="a"then
|
||||
if testButton(3)then
|
||||
if not LATEST_VERSION then
|
||||
if not ALLOW_ONLINE then
|
||||
TEXT.show(text.notFinished,640,450,60,"flicker")
|
||||
SFX.play("finesseError")
|
||||
elseif LOGIN then
|
||||
--[[TODO
|
||||
if USER.accessToken then
|
||||
WS.send("app",JSON.encode{
|
||||
opration="access",
|
||||
email=USER.email,
|
||||
accessToken=USER.accessToken,
|
||||
})
|
||||
else
|
||||
WS.send("app",JSON.encode{
|
||||
opration="access",
|
||||
email=USER.email,
|
||||
authToken=USER.authToken,
|
||||
})
|
||||
end
|
||||
]]
|
||||
SCN.go("net_menu")
|
||||
WS.send("user",JSON.encode{
|
||||
action=0,
|
||||
})
|
||||
else
|
||||
SCN.go("login")
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user