收拾掉旧的联网相关函数准备换新

This commit is contained in:
MrZ626
2021-02-17 04:00:26 +08:00
parent cdc57dda38
commit a883c8a6cf
6 changed files with 31 additions and 329 deletions

View File

@@ -3,72 +3,6 @@ local tc=love.touch
local max,min,sin=math.max,math.min,math.sin
local function tick_httpREQ_launch(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 res.message=="OK"and response.code==200 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..response.code..": "..res.message,"warn")
end
return
elseif request_error then
LOG.print(text.getNoticeFail..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.getNoticeFail..": "..text.httpTimeout,"message")
return
end
end
end
local function tick_httpREQ_autoLogin(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
LOGIN=true
LOG.print(text.loginSuccessed)
httpRequest(
TICK_httpREQ_getUserInfo,
PATH.http..PATH.user,
"GET",
{["Content-Type"]="application/json"},
json.encode{
email=USER.email,
auth_token=USER.auth_token,
}
)
else
LOGIN=false
LOG.print(text.loginFailed..": "..text.httpCode..response.code.."-"..res.message,"warn")
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.loginFailed..": "..text.httpTimeout,"message")
return
end
end
end
local scene={}
local time--Animation timer

View File

@@ -1,55 +1,3 @@
local function tick_httpREQ_newLogin(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
LOGIN=true
USER.email=res.email
USER.auth_token=res.auth_token
USER.id=res.id
FILE.save(USER,"conf/user","q")
LOG.print(text.loginSuccessed)
httpRequest(
TICK_httpREQ_getUserInfo,
PATH.http..PATH.user,
"GET",
{["Content-Type"]="application/json"},
json.encode{
email=USER.email,
auth_token=USER.auth_token,
}
)
httpRequest(
TICK_httpREQ_getAccessToken,
PATH.http..PATH.access,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=USER.email,
auth_token=USER.auth_token,
}
)
else
LOG.print(text.httpCode..response.code..": "..res.message,"warn")
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.loginFailed..": "..text.httpTimeout,"message")
return
end
end
end
local function login()
local email= WIDGET.active.email.value
local password= WIDGET.active.password.value
@@ -59,16 +7,10 @@ local function login()
LOG.print(text.noPassword)return
end
--[[TODO
httpRequest(
tick_httpREQ_newLogin,
PATH.http..PATH.auth,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=email,
password=password,
}
)
]]
end

View File

@@ -1,44 +1,5 @@
local gc=love.graphics
local function tick_httpREQ_manualAutoLogin(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
LOG.print(text.accessSuccessed)
SCN.go("net_menu")
elseif response.code==403 or response.code==401 then
httpRequest(
TICK_httpREQ_getAccessToken,
PATH.http..PATH.access,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=USER.email,
auth_token=USER.auth_token,
}
)
else
local err=json.decode(response.body)
if err then
LOG.print(text.httpCode..response.code..": "..err.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.loginFailed..": "..text.httpTimeout,"message")
return
end
end
end
local scene={}
local tip=gc.newText(getFont(30),"")

View File

@@ -1,25 +1,3 @@
local function tick_goChatRoom(task)
local time=0
while true do
coroutine.yield()
local wsconn,connErr=client.poll(task)
if wsconn then
WSCONN=wsconn
SCN.go("net_chat")
LOG.print(text.wsSuccessed,"warn")
return
elseif connErr then
LOG.print(text.wsFailed..": "..connErr,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.wsFailed..": "..text.httpTimeout,"message")
return
end
end
end
local scene={}
function scene.sceneInit()
@@ -30,12 +8,12 @@ scene.widgetList={
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="chat", x=640, y=540,w=350,h=120,font=40,code=function()
wsConnect(
tick_goChatRoom,
PATH.socket..PATH.onlineChat..
"?email="..urlEncode(USER.email)..
"&token="..urlEncode(USER.access_token)
)
--[[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},
}

View File

@@ -6,100 +6,26 @@ local scrollPos,selected
local lastfreshTime
local lastCreateRoomTime=0
local function task_enterRoom(task)
local time=0
while true do
coroutine.yield()
local wsconn,connErr=client.poll(task)
if wsconn then
WSCONN=wsconn
loadGame("netBattle",true,true)
LOG.print(text.wsSuccessed,"warn")
return
elseif connErr then
LOG.print(text.wsFailed..": "..connErr,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.wsFailed..": "..text.httpTimeout,"message")
return
end
end
end
local function enterRoom(roomID)
wsConnect(
task_enterRoom,
PATH.socket..PATH.onlinePlay..
"?email="..urlEncode(USER.email)..
"&token="..urlEncode(USER.access_token)..
"&id="..urlEncode(roomID)..
"&conf="..urlEncode(dumpBasicConfig())
-- "&password="..urlEncode(password),
)
end
local function task_fetchRooms(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
rooms=res.room_list
else
LOG.print(text.httpCode..response.code..": "..res.message,"warn")
end
return
elseif request_error then
LOG.print(text.roomsFetchFailed..": "..request_error,"warn")
return
end
time=time+1
if time>210 then
LOG.print(text.roomsFetchFailed..": "..text.httpTimeout,"warn")
return
end
end
end
local function task_createRoom(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
LOG.print(text.createRoomSuccessed)
enterRoom(res.room.id)
else
LOG.print(text.httpCode..response.code..": "..res.message,"warn")
end
return
elseif request_error then
LOG.print(text.roomsCreateFailed..": "..request_error,"warn")
return
end
time=time+1
if time>210 then
LOG.print(text.roomsCreateFailed..": "..text.httpTimeout,"warn")
return
end
end
--[[TODO
wsWrite("???",json.encode{
email=USER.email,
token=USER.access_token,
id=roomID,
conf=dumpBasicConfig(),
-- password=password,
})
]]
end
local function fresh()
lastfreshTime=TIME()
rooms=nil
httpRequest(
task_fetchRooms,
PATH.http..PATH.onlinePlay,
"GET",
{["Content-Type"]="application/json"},
json.encode{
email=USER.email,
access_token=USER.access_token,
}
)
--[[TODO
wsWrite("???",json.encode{
email=USER.email,
access_token=USER.access_token,
})
]]
end
local scene={}
@@ -121,18 +47,14 @@ function scene.keyDown(k)
end
elseif k=="n"then
if TIME()-lastCreateRoomTime>26 then
httpRequest(
task_createRoom,
PATH.http..PATH.onlinePlay.."/classic",
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=USER.email,
access_token=USER.access_token,
room_name=(USER.name or"???").."'s room",
room_password=nil,
}
)
--[[TODO
wsWrite("???",json.encode{
email=USER.email,
access_token=USER.access_token,
room_name=(USER.name or"???").."'s room",
room_password=nil,
})
]]
lastCreateRoomTime=TIME()
else
LOG.print(text.createRoomTooFast,"warn")

View File

@@ -1,34 +1,3 @@
local function tick_httpREQ_register(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
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
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.loginFailed..": "..text.httpTimeout,"message")
return
end
end
end
local scene={}
function scene.keyDown(key)
@@ -46,17 +15,13 @@ function scene.keyDown(key)
elseif password~=password2 then
LOG.print(text.diffPassword)return
end
httpRequest(
tick_httpREQ_register,
PATH.http..PATH.auth,
"POST",
{["Content-Type"]="application/json"},
--[[
json.encode{
username=username,
email=email,
password=password,
}
)
]]
elseif key=="escape"then
SCN.back()
else