退出前会主动断开一下ws连接

WAIT放到网络相关TASK函数内部(会在第一帧触发,不影响)
可以从语言文件翻译服务器的错误消息并显示了
框架跟进
This commit is contained in:
MrZ_26
2022-10-06 03:16:18 +08:00
parent f8d17b23b6
commit 8e99565a9d
15 changed files with 228 additions and 122 deletions

View File

@@ -228,7 +228,16 @@ do-- Z.setOnFocus
end
end)
end
Z.setOnQuit(destroyPlayers)
Z.setOnBeforeQuit(function()
NET.ws.close()
TASK.new(function()
TEST.yieldT(.26)
love.event.quit()
end)
end)
Z.setOnQuit(function()
destroyPlayers()
end)
-- Load settings and statistics
if

View File

@@ -104,6 +104,12 @@ return{
dictNote="==Copied from TetroDictionary==",
Techrater={-- Server's warn/error messages
-- TODO
},
getNoticeFail="Failed to fetch announcements",
oldVersion="Version $1 is now available",
needUpdate="Newer version required!",
@@ -138,6 +144,8 @@ return{
chatStart="------Beginning of log------",
chatHistory="------New messages below------",
keySettingInstruction="Press to bind key\nescape: cancel\nbackspace: delete",
customBGhelp="Drop image file here to apply custom background",
customBGloadFailed="Unsupport image format for custom background",

View File

@@ -103,6 +103,12 @@ return{
dictNote="==Copia de TetroDictionary==",
Techrater={-- Server's warn/error messages
-- TODO
},
getNoticeFail="Error al buscar novedades.",
oldVersion="¡Está disponible la nueva versión $1!",
needUpdate="¡Nueva versión requerida!",
@@ -137,6 +143,8 @@ return{
chatStart="------Comienzo del historial------",
chatHistory="------Nuevos mensajes------",
keySettingInstruction="Pulsa la tecla a mapear\nEsc.: Cancelar\nBackspace: Borrar",
customBGhelp="Suelta una imagen aquí para aplicarla de fondo",
customBGloadFailed="Formato de imagen no soportado",

View File

@@ -94,6 +94,12 @@ return{
-- dictNote="==Copied from TetroDictionary==",
Techrater={-- Server's warn/error messages
-- TODO
},
getNoticeFail="Echec de l'obtention de la notice",
oldVersion="La version $1 est disponible !",
-- versionNotMatch="Version do not match!",
@@ -128,6 +134,8 @@ return{
chatStart="--------Début des logs--------",
chatHistory="-Nouveaux messages en dessous-",
-- keySettingInstruction="Press to bind key\nescape: cancel\nbackspace: delete",
-- customBGhelp="Drop image file here to apply custom background",
-- customBGloadFailed="Unsupport image format for custom background",

View File

@@ -105,6 +105,12 @@ return{
dictNote="==Tersalin dari TetroDictionary==",
Techrater={-- Server's warn/error messages
-- TODO
},
getNoticeFail="Gagal mengambil pengumuman-pengumuman",
oldVersion="Versi $1 sekarang tersedia",
needUpdate="Butuh versi lebih baru!",
@@ -139,6 +145,8 @@ return{
chatStart="------Awal percakapan------",
chatHistory="------Pesan-pesan baru di bawah ini------",
keySettingInstruction="Tekan untuk menghubung tombol ke aksi tertentu\nescape: batal\nbackspace: hapus",
customBGhelp="Seret file gambar di sini untuk memasangkan background",
customBGloadFailed="Format file gambar tidak didukung untuk background",

View File

@@ -105,6 +105,12 @@ return{
dictNote="==TetroDictionaryからコピーしました==",
Techrater={-- Server's warn/error messages
-- TODO
},
getNoticeFail="お知らせ情報を取得できませんでした",
oldVersion="バージョン$1のダウンロードが可能になりました",
needUpdate="最新バージョンに更新してください",
@@ -139,6 +145,8 @@ return{
chatStart="------チャットの先頭------",
chatHistory="------新しいメッセージ------",
keySettingInstruction="選択してキーを入力\nEscape: キャンセル\nBackspace: キーを削除",
customBGhelp="カスタム背景にする画像ファイルをドロップ",
customBGloadFailed="サポートされていないフォーマットのファイルです",

View File

@@ -92,6 +92,12 @@ return{
-- dictNote="==Copied from TetroDictionary==",
Techrater={-- Server's warn/error messages
-- TODO
},
getNoticeFail="Não conseguiu ter anúncios",
oldVersion="Versão $1 esta disponível agora!",
-- versionNotMatch="Version do not match!",
@@ -126,6 +132,8 @@ return{
chatStart="------Começo do log------",
chatHistory="------Novas mensagens abaixo------",
-- keySettingInstruction="Press to bind key\nescape: cancel\nbackspace: delete",
-- customBGhelp="Drop image file here to apply custom background",
-- customBGloadFailed="Unsupport image format for custom background",

View File

@@ -104,6 +104,12 @@ return{
dictNote="==复制于小z词典==",
Techrater={-- Server's warn/error messages
-- TODO
},
getNoticeFail="拉取公告失败",
oldVersion="最新版本$1可以下载了",
needUpdate="请更新游戏!",
@@ -138,6 +144,8 @@ return{
chatStart="------消息的开头------",
chatHistory="------以上是历史消息------",
keySettingInstruction="点击添加键位绑定\nesc取消选中\n退格键清空选中",
customBGhelp="把图片文件拖到这个窗口里使用自定义背景",
customBGloadFailed="自定义背景的图片文件格式不支持",

View File

@@ -104,6 +104,12 @@ return{
dictNote="#!CopySource=Zictionary",
Techrater={-- Server's warn/error messages
-- TODO
},
getNoticeFail="Error.FetchAnnouncement();",
oldVersion="Version.New($1);",
needUpdate="Version.NeedUpdate()",
@@ -138,6 +144,8 @@ return{
chatStart="#!Log.Beginning();",
chatHistory="#!NewMassagesBelow();",
keySettingInstruction="Key.Bind(Press);\nKey.Bind.Cancel(Escspe);\nKey.Bind.Delete(Backspace);",
customBGhelp="Ctm.BG(DropHere);",
customBGloadFailed="Error.Ctm.BG(UnsupportFormat);",

View File

@@ -104,6 +104,12 @@ return{
dictNote="==拷貝自小z辭典==",
Techrater={-- Server's warn/error messages
-- TODO
},
getNoticeFail="無法獲取公告",
oldVersion="版本 $1 現已推出",
needUpdate="請更新遊戲!",
@@ -138,6 +144,8 @@ return{
chatStart="------訊息開始------",
chatHistory="------以上為歷史訊息------",
keySettingInstruction="點擊來設置鍵位\n按esc來取消選中\n按退格鍵來清除選中",
customBGhelp="把圖片檔案拖到這個視窗裏使用自定義背景",
customBGloadFailed="自定義背景的圖片檔案格式不支持",

View File

@@ -29,14 +29,54 @@ local NET={
--------------------------<NEW HTTP API>
local availableErrorTextType={info=1,warn=1,error=1}
local function parseError(pathStr)
LOG(pathStr)
if type(pathStr)~='string' then
MES.new('error',"<"..tostring(pathStr)..">",5)
elseif pathStr:find("[^0-9a-z.]") then
MES.new('error',"["..pathStr.."]",5)
else
local mesPath=STRING.split(pathStr,'.')
if mesPath[1]~='Techrater' then
MES.new('error',"["..pathStr.."]",5)
return
end
local curText=text.Techrater
for i=2,#mesPath do
if type(curText)~='table' then break end
curText=curText[mesPath[i]]
end
if type(curText)=='table' then
if availableErrorTextType[curText[1]] and type(curText[2])=='string' and type(curText[3])=='number' then
MES.new(curText[1],curText[2],math.min(curText[3],5))
else
MES.new('error',"["..pathStr.."]",5)
end
elseif type(curText)=='string' then
if #curText>0 then
MES.new('warn',curText,5)
end
else
MES.new('error',"["..pathStr.."]",5)
end
end
end
local function getMsg(request,timeout)
HTTP(request)
local totalTime=0
while true do
local mes=HTTP.pollMsg(request.pool)
if mes then
if type(mes.body)=='string' and #mes.body>0 then
return JSON.decode(mes.body)
local msg=HTTP.pollMsg(request.pool)
if msg then
if type(msg.body)=='string' and #msg.body>0 then
local body=JSON.decode(msg.body)
if body then
if tostring(body.code):sub(1,1)~='2' then
parseError(body.message~=nil and body.message or msg)
end
return body
end
else
MES.new('info',text.serverDown)
return
@@ -52,6 +92,14 @@ end
function NET.getCode(email)
if not TASK.lock('getCode') then return end
TASK.new(function()
WAIT{
quit=function()
TASK.unlock('getCode')
HTTP.deletePool('getCode')
end,
timeout=12.6,
}
local res=getMsg({
pool='getCode',
path='/techmino/api/v1/auth/verify/email',
@@ -63,26 +111,23 @@ function NET.getCode(email)
USER.email=email
SCN.fileDropped(2)
MES.new('info',text.checkEmail,5)
else
MES.new('error',res.message,5)
end
else
MES.new('warn',text.requestFailed,5)
end
WAIT.interrupt()
end)
WAIT{
quit=function()
TASK.unlock('getCode')
HTTP.deletePool('getCode')
end,
timeout=12.6,
}
end
function NET.codeLogin(code)
if not TASK.lock('codeLogin') then return end
TASK.new(function()
WAIT{
quit=function()
TASK.unlock('codeLogin')
HTTP.deletePool('codeLogin')
end,
timeout=6.26,
}
local res=getMsg({
pool='codeLogin',
path='/techmino/api/v1/auth/login/email',
@@ -96,34 +141,31 @@ function NET.codeLogin(code)
if res.code==200 then
USER.rToken=res.data.refreshToken
USER.aToken=res.data.accessToken
NET.connectWS()
NET.ws.connect()
SCN.pop()SCN.go('net_menu')
elseif res.code==201 then
USER.rToken=res.data.refreshToken
USER.aToken=res.data.accessToken
SCN.pop()SCN.push('net_menu')
SCN.fileDropped(3)
else
MES.new('error',res.message,5)
end
else
MES.new('warn',text.requestFailed,5)
end
WAIT.interrupt()
end)
WAIT{
quit=function()
TASK.unlock('codeLogin')
HTTP.deletePool('codeLogin')
end,
timeout=6.26,
}
end
function NET.setPW(code,pw)
if not TASK.lock('setPW') then return end
TASK.new(function()
pw=HASH.pbkdf2(HASH.sha3_256,pw,"salt",26000)
WAIT{
quit=function()
TASK.unlock('setPW')
HTTP.deletePool('setPW')
end,
timeout=6.26,
}
pw=HASH.pbkdf2(HASH.sha3_256,pw,'salt',26000)
local res=getMsg({
pool='setPW',
@@ -140,22 +182,11 @@ function NET.setPW(code,pw)
if res.code==200 then
USER.password=pw
SCN.back()
else
MES.new('error',res.message,5)
end
else
MES.new('warn',text.requestFailed,5)
end
WAIT.interrupt()
end)
WAIT{
quit=function()
TASK.unlock('setPW')
HTTP.deletePool('setPW')
end,
timeout=6.26,
}
end
function NET.autoLogin()
if not USER.password then
@@ -164,6 +195,14 @@ function NET.autoLogin()
end
if not TASK.lock('autoLogin') then return end
TASK.new(function()
WAIT{
quit=function()
TASK.unlock('autoLogin')
HTTP.deletePool('autoLogin')
end,
timeout=12.6,
}
if USER.aToken then
local res=getMsg({
pool='autoLogin',
@@ -173,12 +212,10 @@ function NET.autoLogin()
if res then
if res.code==200 then
NET.connectWS()
NET.ws.connect()
SCN.go('net_menu')
WAIT.interrupt()
return
else
LOG("Access token expired")
end
else
WAIT.interrupt()
@@ -196,12 +233,10 @@ function NET.autoLogin()
if res.code==200 then
USER.rToken=res.data.refreshToken
USER.aToken=res.data.accessToken
NET.connectWS()
NET.ws.connect()
SCN.go('net_menu')
WAIT.interrupt()
return
else
LOG("Refresh token expired")
end
else
WAIT.interrupt()
@@ -221,12 +256,10 @@ function NET.autoLogin()
if res.code==200 then
USER.rToken=res.data.refreshToken
USER.aToken=res.data.accessToken
NET.connectWS()
NET.ws.connect()
SCN.go('net_menu')
WAIT.interrupt()
return
else
MES.new('warn',text.requestFailed)
end
else
WAIT.interrupt()
@@ -236,17 +269,19 @@ function NET.autoLogin()
SCN.go('login')
WAIT.interrupt()
end)
WAIT{
quit=function()
TASK.unlock('autoLogin')
HTTP.deletePool('autoLogin')
end,
timeout=12.6,
}
end
function NET.pwLogin(email,pw)
if not TASK.lock('pwLogin') then return end
TASK.new(function()
WAIT{
quit=function()
TASK.unlock('pwLogin')
HTTP.deletePool('pwLogin')
end,
timeout=12.6,
}
TEST.yieldT(.26)
pw=HASH.pbkdf2(HASH.sha3_256,pw,"salt",26000)
local res=getMsg({
@@ -264,24 +299,13 @@ function NET.pwLogin(email,pw)
USER.password=pw
USER.rToken=res.data.refreshToken
USER.aToken=res.data.accessToken
NET.connectWS()
NET.ws.connect()
SCN.go('net_menu')
else
MES.new('error',res.message,5)
end
else
MES.new('warn',text.requestFailed,5)
end
WAIT.interrupt()
end)
WAIT{
quit=function()
TASK.unlock('pwLogin')
HTTP.deletePool('pwLogin')
end,
timeout=12.6,
}
end
--------------------------<NEW WS API>
@@ -295,14 +319,20 @@ end
-- Room
NET.room={}
function NET.room.chat(mes,rid)
function NET.room.chat(msg,rid)
wsSend(1300,{
message=mes,
message=msg,
roomId=rid,-- Admin
})
end
function NET.room.create(roomName,description,capacity,roomType,roomData,password)
if not TASK.lock('enterRoom',6) then return end
if not TASK.lock('createRoom',6) then return end
WAIT{
quit=function()
TASK.unlock('createRoom')
end,
timeout=1e99,
}
wsSend(1301,{
capacity=capacity,
info={
@@ -314,12 +344,6 @@ function NET.room.create(roomName,description,capacity,roomType,roomData,passwor
data=roomData,
password=password,
})
WAIT{
quit=function()
TASK.unlock('enterRoom')
end,
timeout=1e99,
}
end
function NET.room.getData(rid)
wsSend(1302,{
@@ -391,8 +415,8 @@ NET.player={}
function NET.player.updateConf()
wsSend(1200,dumpBasicConfig())
end
function NET.player.finish(mes)-- what mes?
wsSend(1201,mes)
function NET.player.finish(msg)-- what msg?
wsSend(1201,msg)
end
function NET.player.joinGroup(gid)
wsSend(1202,gid)
@@ -417,67 +441,68 @@ function NET.player.setPlaying(playing)
end
-- WS
function NET.connectWS()
NET.ws={}
function NET.ws.connect()
if WS.status('game')=='dead' then
WS.connect('game','',{['x-access-token']=USER.aToken},6)
TASK.new(NET.updateWS)
TASK.new(NET.ws.update)
end
end
function NET.closeWS()
function NET.ws.close()
WS.close('game')
end
function NET.updateWS()
function NET.ws.update()
while WS.status('game')~='dead' do
coroutine.yield()
local message,op=WS.read('game')
if message then
local msg,op=WS.read('game')
if msg then
if op=='ping' then
elseif op=='pong' then
elseif op=='close' then
local res=JSON.decode(message)
MES.new('info',("$1 $2"):repD(text.wsClose,res and res.message or message))
local res=JSON.decode(msg)
MES.new('info',("$1 $2"):repD(text.wsClose,res and res.message or msg))
if res and res.message then LOG(res.message) end
TEST.yieldUntilNextScene()
while SCN.stack[#SCN.stack-1]~='main' do SCN.pop() end
SCN.back()
return
else
local res=JSON.decode(message)
if res then
local body=JSON.decode(msg)
if body then
-- print(("RECV ACT: $1 ($2)"):repD(res.action,res.type))
if res.type=='Failed' then
MES.new('warn',text.requestFailed..": "..(res.reason or "/"))
elseif res.action==1100 then-- TODO
elseif res.action==1101 then-- TODO
elseif res.action==1102 then-- TODO
elseif res.action==1201 then-- Finish
elseif res.action==1202 then-- Join group
elseif res.action==1203 then-- Set ready
elseif res.action==1204 then-- Set host
elseif res.action==1205 then-- Set state
elseif res.action==1206 then-- Stream
elseif res.action==1207 then-- Set playing
elseif res.action==1301 then-- Create room
if body.type=='Failed' then
parseError(body.message~=nil and body.message or msg)
elseif body.action==1100 then-- TODO
elseif body.action==1101 then-- TODO
elseif body.action==1102 then-- TODO
elseif body.action==1201 then-- Finish
elseif body.action==1202 then-- Join group
elseif body.action==1203 then-- Set ready
elseif body.action==1204 then-- Set host
elseif body.action==1205 then-- Set state
elseif body.action==1206 then-- Stream
elseif body.action==1207 then-- Set playing
elseif body.action==1301 then-- Create room
TASK.unlock('createRoom')
-- NET.roomState=...
-- SCN.go('net_game')
WAIT.interrupt()
elseif body.action==1302 then-- Get room data
elseif body.action==1303 then-- Set room data
elseif body.action==1304 then-- Get room info
elseif body.action==1305 then-- Set room info
elseif body.action==1306 then-- Enter room
TASK.unlock('enterRoom')
-- NET.roomState=...
-- SCN.go('net_game')
WAIT.interrupt()
elseif res.action==1302 then-- Get room data
elseif res.action==1303 then-- Set room data
elseif res.action==1304 then-- Get room info
elseif res.action==1305 then-- Set room info
elseif res.action==1306 then-- Enter room
TASK.unlock('enterRoom')
-- NET.roomState=...
-- SCN.go('net_game')
WAIT.interrupt()
elseif res.action==1307 then-- Kick room
elseif res.action==1308 then-- Leave room
elseif res.action==1309 then-- Fetch rooms
elseif body.action==1307 then-- Kick room
elseif body.action==1308 then-- Leave room
elseif body.action==1309 then-- Fetch rooms
TASK.unlock('fetchRoom')
if res.data then SCN.scenes.net_rooms.widgetList.roomList:setList(res.data) end
elseif res.action==1310 then-- Set password
elseif res.action==1311 then-- Remove room
if body.data then SCN.scenes.net_rooms.widgetList.roomList:setList(body.data) end
elseif body.action==1310 then-- Set password
elseif body.action==1311 then-- Remove room
end
else
WS.alert('user')

View File

@@ -9,7 +9,7 @@ function scene.sceneInit()
sysAndScn=SYSTEM.."-"..VERSION.string.." scene:"..Z.getErr('#').scene
errorText=LOADED and text.errorMsg or "An error has occurred while the game was loading.\nAn error log has been created so you can send it to the author."
errorShot,errorInfo=Z.getErr('#').shot,Z.getErr('#').mes
NET.closeWS()
NET.ws.close()
if SETTING then
SFX.fplay('error',SETTING.voc*.8 or 0)
end

View File

@@ -239,7 +239,7 @@ end
function scene.update(dt)
if WS.status('game')~='running' then
NET.closeWS()
NET.ws.close()
SCN.back()
return
end

View File

@@ -4,7 +4,7 @@ function scene.sceneInit()
BG.set()
end
function scene.sceneBack()
NET.closeWS()
NET.ws.close()
end
function scene.draw()
@@ -24,7 +24,7 @@ scene.widgetList={
USER.__data.password=false
USER.__data.rToken=false
USER.__data.aToken=false
NET.closeWS()
NET.ws.close()
SCN.back()
end
end},