整理代码,适配新的玩家信息拉取机制,修改玩家数据保存方式

This commit is contained in:
MrZ626
2021-03-30 12:02:51 +08:00
parent 0efdb7dc33
commit ec63618bea
4 changed files with 189 additions and 211 deletions

View File

@@ -1060,202 +1060,4 @@ do--CUS/SETXXX(k)
function SETrev(k)return function()s[k]=not s[k]end end
function CUSsto(k)return function(i)c[k]=i end end
function SETsto(k)return function(i)s[k]=i end end
end
--Network funcs
do
--[[
register:
if response.message=="OK"then
NET.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
goChatRoom:
if res.message=="OK"then
SCN.go("net_chat")
LOG.print(text.wsSuccessed,"warn")
else
LOG.print(text.wsFailed..": "..connErr,"warn")
end
]]
function TICK_WS_app()
local retryTime=5
while true do
YIELD()
local status=WS.status("app")
if status=="running"then
local message,op=WS.read("app")
if message then
if op=="ping"then
NET.pong("app",message)
elseif op=="pong"then
elseif op=="close"then
NET.wsCloseMessage(message)
return
else
local res=JSON.decode(message)
if res then
if VERSION_CODE>=res.lowest then
NET.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)
else
WS.alert("app")
end
end
end
elseif status=="dead"then
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
NET.pong("user",message)
elseif op=="pong"then
elseif op=="close"then
NET.wsCloseMessage(message)
return
else
local res=JSON.decode(message)
if res then
if res.message=="Connected"then
NET.login=true
if res.id then
USER.id=res.id
USER.authToken=res.authToken
SCN.back()
end
FILE.save(USER,"conf/user","q")
LOG.print(text.loginSuccessed)
--Get self infos
NET.getSelfInfo()
elseif res.action==0 then--Get accessToken
USER.accessToken=res.accessToken
LOG.print(text.accessSuccessed)
NET.wsConnectPlay()
NET.unlock("accessToken")
elseif res.action==1 then--Get userInfo
if res.id==USER.id then--Own
USER.name=res.username
USER.motto=res.motto
USER.avatar=res.avatar
FILE.save(USER,"conf/user")
NET.unlock("getSelfInfo")
else--Others
LOG.print("Get user info: "..USER.id)
end
end
else
WS.alert("user")
end
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
NET.pong("play",message)
elseif op=="pong"then
elseif op=="close"then
NET.wsCloseMessage(message)
return
else
local res=JSON.decode(message)
if res then
if res.message=="Connected"then
NET.unlock("connectPlay")
SCN.go("net_menu")
elseif res.action==0 then--Fetch rooms
NET.roomList=res.roomList
elseif res.action==2 then--Join(create) room
loadGame("netBattle",true,true)
NET.unlock("enterRoom")
elseif res.action==3 then--Leave room
SCN.back()
end
else
WS.alert("play")
end
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
NET.pong("stream",message)
elseif op=="pong"then
elseif op=="close"then
NET.wsCloseMessage(message)
return
else
--TODO
end
end
end
end
end
function TICK_WS_chat()
while true do
YIELD()
local status=WS.status("chat")
if status=="running"then
local message,op=WS.read("chat")
if message then
if op=="ping"then
NET.pong("chat",message)
elseif op=="pong"then
elseif op=="close"then
NET.wsCloseMessage(message)
return
else
local res=JSON.decode(message)
if res then
--TODO
else
WS.alert("chat")
end
end
end
end
end
end
end