Update api path, add global table (#16)

This commit is contained in:
Particle_G
2020-12-02 23:51:30 +08:00
committed by MrZ626
parent cd3eab103c
commit ac701c4759
13 changed files with 132 additions and 10 deletions

View File

@@ -171,7 +171,6 @@ do--httpRequest
httpRequest=
client and function(tick,path,method,header,body)
local task,err=client.httpraw{
-- url="http://47.103.200.40/"..path,
url="http://krakens.tpddns.cn:10026"..path,
method=method or"GET",
header=header,
@@ -187,6 +186,25 @@ do--httpRequest
function()
LOG.print("[NO NETlib]",5,COLOR.yellow)
end
wsConnect=
client and function(tick,path,header)
local task,err=client.wsraw{
-- url="http://krakens.tpddns.cn:10026"..path,
url="ws://127.0.0.1:10026"..path,
origin = "127.0.0.1",
header=header,
}
if task then
TASK.new(tick,{wsconntask=task,time=0,net=true})
else
LOG.print("NETlib error: "..err,"warn")
end
TASK.netTaskCount=TASK.netTaskCount+1
end or
function()
LOG.print("[NO NETlib]",5,COLOR.yellow)
end
end
do--json
--

View File

@@ -23,6 +23,7 @@ LOADED=false
NOGAME=false
LOGIN=false
EDITING=""
WSCONN=nil
math.randomseed(os.time()*626)
love.keyboard.setKeyRepeat(true)

View File

@@ -309,6 +309,15 @@ ACCOUNT={
avatar=nil,
}
PATH={
api="/tech/api/v1",
appInfo="/app/info",
users="/users",
auth="/auth",
access="/auth/access",
versus="/online/versus"
}
keyMap={
{"left","right","x","z","c","up","down","space","tab","r"},{},
--Keyboard

View File

@@ -81,6 +81,8 @@ return{
registerFailed="Registration failed",
loginSuccessed="Successfully logged in!",
loginFailed="Login failed",
wsSuccessed="Websocket connected!",
wsFailed="Websocket failed to connect",
errorMsg="An error has occurred and Techmino needs to restart.\nError info has been created, and you can send it to the author.",
@@ -483,6 +485,10 @@ return{
password="Password",
password2="Reenter password",
},
netgame={
title="Online game",
test="Test",
},
account={
title="Account",
},

View File

@@ -84,6 +84,8 @@ return{
registerFailed="Erreur d'enregistrement",
-- loginSuccessed="Successfully logged in!",
-- loginFailed="Login failed",
-- wsSuccessed="Websocket connected!",
-- wsFailed="Websocket failed to connect",
errorMsg="Une erreur est survenue et Techmino doit redémarrer.\nDes informations concernant l'erreur ont été créées, et vous pouvez les envoyer au créateur.",
@@ -480,6 +482,10 @@ return{
password="Mot de passe",
password2="Confirmer le mot de passe",
},
netgame={ -- TODO: translation
title="Online game",
test="Test",
},
account={
title="Compte",
},

View File

@@ -85,6 +85,8 @@ return{
registerFailed="Registro fallido.",
-- loginSuccessed="Successfully logged in!",
-- loginFailed="Login failed",
-- wsSuccessed="Websocket connected!",
-- wsFailed="Websocket failed to connect",
errorMsg="Ha ocurrido un error y Techmino necesita reiniciarse.\nSe creó un registro de error, puedes enviarlo al autor.",
@@ -485,6 +487,10 @@ return{
password="Contraseña",
password2="Repetir Contr.",
},
netgame={ -- TODO: translation
title="Online game",
test="Test",
},
account={
title="Cuenta",
},

View File

@@ -407,6 +407,10 @@ return{
password="*",
password2="*",
},
netgame={
title="Online game",
test="Test",
},
account={
title="@_@",
},

View File

@@ -82,6 +82,8 @@ return{
registerFailed="注册失败",
loginSuccessed="登录成功!",
loginFailed="登录失败",
wsSuccessed="连接成功!",
wsFailed="连接失败",
errorMsg="Techmino遭受了雷击,需要重新启动.\n我们已收集了一些错误信息,你可以向作者进行反馈.",
@@ -506,6 +508,10 @@ return{
password="密码",
password2="确认密码",
},
netgame={
title="在线游戏",
test="测试",
},
account={
title="账户",
},

View File

@@ -8,6 +8,27 @@ function sceneInit.chat()
BG.set("none")
end
local function socketConnect()
wsConnect(
TICK.wsCONN_connect,
"/solo?room_id=114",
{}
)
end
local function socketWrite()
if not WSCONN then
LOG.print("尚未连接到服务器","warn")
return
end
local message = WIDGET.active.message.value
print("TextBox: "..message)
local writeErr = client.write(WSCONN, message)
if writeErr then
print(writeErr, "warn")
end
return true
end
WIDGET.init("chat",{
WIDGET.newTextBox{name="text", x=40, y=500,w=980,h=180,font=40},
WIDGET.newButton{name="send", x=1140, y=540,w=170,h=80,font=40,code=send},

View File

@@ -116,8 +116,8 @@ function Tmr.load()
LOADED=true
SFX.play("welcome_sfx")
VOC.play("welcome_voc")
httpRequest(TICK.httpREQ_launch,"/tech/api/v1/app/info")
if ACCOUNT.auth_token then
httpRequest(TICK.httpREQ_launch,PATH.api..PATH.appInfo)
if ACCOUNT.auth_token and ACCOUNT.email then
local res=json.encode{
email=ACCOUNT.email,
auth_token=ACCOUNT.auth_token,
@@ -125,7 +125,7 @@ function Tmr.load()
if res then
httpRequest(
TICK.httpREQ_autoLogin,
"/tech/api/v1/users",
PATH.api..PATH.auth,
"GET",
{["Content-Type"]="application/json"},
res

View File

@@ -14,7 +14,7 @@ function keyDown.login(key)
if res then
httpRequest(
TICK.httpREQ_newLogin,
"/tech/api/v1/users",
PATH.api..PATH.auth,
"GET",
{["Content-Type"]="application/json"},
res

View File

@@ -21,7 +21,7 @@ function keyDown.register(key)
if res then
httpRequest(
TICK.httpREQ_register,
"/tech/api/v1/users",
PATH.api..PATH.auth,
"POST",
{["Content-Type"]="application/json"},
res

View File

@@ -129,7 +129,7 @@ function Tick.httpREQ_register(data)
end
return true
elseif request_error then
LOG.print(text.registerFailed..": "..request_error,"warn")
LOG.print(text.loginFailed..": "..request_error,"warn")
return true
end
return checkTimeout(data,360)
@@ -151,7 +151,7 @@ function Tick.httpREQ_newLogin(data)
end
return true
elseif request_error then
LOG.print(text.registerFailed..": "..request_error,"warn")
LOG.print(text.loginFailed..": "..request_error,"warn")
return true
end
return checkTimeout(data,360)
@@ -163,7 +163,7 @@ function Tick.httpREQ_autoLogin(data)
LOGIN=true
local res=json.decode(response.body)
if res then
LOG.print(text.loginSuccessed..": "..res.message)
LOG.print(text.loginSuccessed)
end
else
LOGIN=false
@@ -174,9 +174,54 @@ function Tick.httpREQ_autoLogin(data)
end
return true
elseif request_error then
LOG.print(text.registerFailed..": "..request_error,"warn")
LOG.print(text.loginFailed..": "..request_error,"warn")
return true
end
return checkTimeout(data,360)
end
function Tick.wsCONN_connect(data)
print("Running wsconntask...")
if data.wsconntask then
local wsconn,connErr=client.poll(data.wsconntask)
if wsconn then
WSCONN = wsconn
TASK.new(Tick.wsCONN_read,{net=true})
return true
elseif connErr then
LOG.print(text.wsFailed..": "..connErr,"warn")
return true
end
end
return checkTimeout(data,360)
end
function Tick.wsCONN_read(data)
if not data.net then
return true
end
local messages,readErr=client.read(WSCONN)
if messages then
if messages[1] then
print(messages[1])
LOG.print("Message: "..messages[1])
end
elseif readErr then
print("Read error: "..readErr)
if readErr == "EOF" then
LOG.print("Socket closed!","warn")
end
WSCONN = nil
return true
end
end
-- function Tick.wsCONN_write(data)
-- if not data.net then
-- return true
-- end
-- local writeErr=client.write(WSCONN,data.message)
-- if writeErr then
-- print(writeErr, "warn")
-- end
-- return true
-- end
return Tick