diff --git a/Zframework/init.lua b/Zframework/init.lua index b414284e..3e4ced56 100644 --- a/Zframework/init.lua +++ b/Zframework/init.lua @@ -427,9 +427,10 @@ function love.errorhandler(msg) love.timer.sleep(.26) end end -local WSnames={"app","chat","play","stream"} +local WSnames={"app","user","chat","play","stream"} local WScolor={ {1,0,0,.26}, + {1,.7,0,.26}, {0,.7,1,.26}, {0,1,0,.26}, {1,1,0,.26} @@ -540,7 +541,7 @@ function love.run() gc_push("transform") gc.translate(SCR.w,0) gc.scale(SCR.k) - for i=1,4 do + for i=1,5 do local status=WS.status(WSnames[i]) gc_setColor(WScolor[i]) gc_rectangle("fill",0,20*i,-20,-20) diff --git a/main.lua b/main.lua index c717ebf8..432d2223 100644 --- a/main.lua +++ b/main.lua @@ -328,6 +328,11 @@ do end end + if STAT.version<1301 then + fs.remove("conf/user") + NOGAME=true + end + for _,v in next,VK_org do if not v.color then NOGAME=true diff --git a/parts/gametoolfunc.lua b/parts/gametoolfunc.lua index a8df27fe..d4129114 100644 --- a/parts/gametoolfunc.lua +++ b/parts/gametoolfunc.lua @@ -1063,84 +1063,200 @@ end ---Network tick function -function TICK_httpREQ_getAccessToken(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.accessSuccessed) - USER.access_token=res.access_token - FILE.save(USER,"conf/user") - SCN.swapTo("net_menu") +--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 - LOGIN=false - USER.access_token=false - USER.auth_token=false - LOG.print(text.loginFailed..": "..text.httpCode..response.code.."-"..res.message,"warn") + LOG.print(string.gsub(text.oldVersion,"$1",res.version_name),"warn") end - return - elseif request_error then - LOG.print(text.loginFailed..": "..request_error,"warn") - return + else + LOG.print(text.httpCode..res.code..": "..res.message,"warn") end - time=time+1 - if time>360 then - LOG.print(text.loginFailed..": "..text.httpTimeout,"message") - return + + register: + if response.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 - end -end -function TICK_httpREQ_getUserInfo(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 - 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") + + 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 - 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 + + 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 - end -end -function TICK_wsRead() - while true do - coroutine.yield() - if not WSCONN then return end - local messages,readErr=client.read(WSCONN) - if messages then - if SCN.socketRead then - for i=1,#messages do - SCN.socketRead(messages[i]) + + 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") + LOG.print(text.wsSuccessed,"warn") + else + LOG.print(text.wsFailed..": "..connErr,"warn") + end + + fetchRooms: + if res.message=="OK"then + rooms=res.room_list + else + LOG.print(text.httpCode..response.code..": "..res.message,"warn") + end + + createRoom: + if res.message=="OK" then + LOG.print(text.createRoomSuccessed) + enterRoom(res.room.id) + else + LOG.print(text.httpCode..res.code..": "..res.message,"warn") + end + + + enterRoom: + if res.message=="OK"then + loadGame("netBattle",true,true) + LOG.print(text.wsSuccessed,"warn") + else + LOG.print(text.wsFailed..": "..connErr,"warn") + end + ]] + + function TICK_WS_app() + while true do + coroutine.yield() + local status=WS.status("app") + if status=="running"then + local message,op=WS.read("app") + 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") + return + else + message=json.decode(message) + end end - else - return + elseif status~="connecting"then + WS.connect("app","/app") end - elseif readErr then - WSCONN=false - LOG.print(text.wsError..tostring(readErr),"warn") - while #SCN.stack>4 do - SCN.pop() - end - return end end + function TICK_WS_chat() + while true do + coroutine.yield() + local status=WS.status("chat") + if status=="running"then + local message,op=WS.read("chat") + 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") + return + else + message=json.decode(message) + end + end + end + end + end + --TODO: more WSs end \ No newline at end of file diff --git a/parts/globalTables.lua b/parts/globalTables.lua index 29b037ad..a8383094 100644 --- a/parts/globalTables.lua +++ b/parts/globalTables.lua @@ -236,8 +236,8 @@ USER=FILE.load("conf/user")or{--User infomation email=false, motto=false, avatar=false, - auth_token=false, - access_token=false, + authToken=false, + accessToken=false, --Local data xp=0,lv=1, diff --git a/parts/scenes/load.lua b/parts/scenes/load.lua index bb6385dd..9f6c82f9 100644 --- a/parts/scenes/load.lua +++ b/parts/scenes/load.lua @@ -132,19 +132,10 @@ function scene.update() SFX.play("welcome_sfx") VOC.play("welcome_voc") --[[TODO - httpRequest(tick_httpREQ_launch,PATH.http..PATH.appInfo) - if USER.auth_token and USER.email then - httpRequest( - tick_httpREQ_autoLogin, - PATH.http..PATH.auth, - "GET", - {["Content-Type"]="application/json"}, - json.encode{ - email=USER.email, - auth_token=USER.auth_token, - } - ) - end + WS.send("user",json.encode{ + id=USER.id, + authToken=USER.authToken, + }) ]] if FESTIVAL=="Xmas"then LOG.print("==============",COLOR.red) diff --git a/parts/scenes/login.lua b/parts/scenes/login.lua index 114fb634..5cbb2e9f 100644 --- a/parts/scenes/login.lua +++ b/parts/scenes/login.lua @@ -7,10 +7,10 @@ local function login() LOG.print(text.noPassword)return end --[[TODO - json.encode{ + WS.send("user",json.encode{ email=email, password=password, - } + }) ]] end diff --git a/parts/scenes/main.lua b/parts/scenes/main.lua index da8b27a4..813eae1a 100644 --- a/parts/scenes/main.lua +++ b/parts/scenes/main.lua @@ -71,28 +71,18 @@ scene.widgetList={ WIDGET.newButton{name="online", x=370,y=220,w=200,h=140,color="lCyan", font=40,code=function() if LOGIN then --[[TODO - if USER.access_token then - httpRequest( - tick_httpREQ_manualAutoLogin, - PATH.http..PATH.access, - "GET", - {["Content-Type"]="application/json"}, - json.encode{ - email=USER.email, - access_token=USER.access_token, - } - ) + if USER.accessToken then + WS.send("app",json.encode{ + opration="access", + email=USER.email, + accessToken=USER.accessToken, + }) else - httpRequest( - TICK_httpREQ_getAccessToken, - PATH.http..PATH.access, - "POST", - {["Content-Type"]="application/json"}, - json.encode{ - email=USER.email, - auth_token=USER.auth_token, - } - ) + WS.send("app",json.encode{ + opration="access", + email=USER.email, + authToken=USER.authToken, + }) end ]] else diff --git a/parts/scenes/net_menu.lua b/parts/scenes/net_menu.lua index a81025d2..8f1597fb 100644 --- a/parts/scenes/net_menu.lua +++ b/parts/scenes/net_menu.lua @@ -7,14 +7,7 @@ end 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() - --[[TODO - wsConnect("chat","/chat",json.encode{ - email=USER.email, - token=USER.access_token, - }) - ]] - end}, + WIDGET.newButton{name="chat", x=640, y=540,w=350,h=120,font=40,code=goScene"net_chat",hide=function()return WS.status("chat")~="running"end}, WIDGET.newButton{name="back", x=1140, y=640,w=170,h=80,font=40,code=backScene}, } diff --git a/parts/scenes/net_rooms.lua b/parts/scenes/net_rooms.lua index 7f475b1d..5bdfa21c 100644 --- a/parts/scenes/net_rooms.lua +++ b/parts/scenes/net_rooms.lua @@ -8,23 +8,23 @@ local lastCreateRoomTime=0 local function enterRoom(roomID) --[[TODO - WS.connect("play","/play",json.encode{ - email=USER.email, - token=USER.access_token, - id=roomID, - conf=dumpBasicConfig(), - -- password=password, - }) + WS.connect("play","/play",json.encode{ + email=USER.email, + token=USER.accessToken, + id=roomID, + conf=dumpBasicConfig(), + -- password=password, + }) ]] end local function fresh() lastfreshTime=TIME() rooms=nil --[[TODO - WS.connect("play","/play",json.encode{ - email=USER.email, - access_token=USER.access_token, - }) + WS.connect("play","/play",json.encode{ + email=USER.email, + accessToken=USER.accessToken, + }) ]] end @@ -48,12 +48,12 @@ function scene.keyDown(k) elseif k=="n"then if TIME()-lastCreateRoomTime>26 then --[[TODO - WS.send("room",json.encode{ - email=USER.email, - access_token=USER.access_token, - room_name=(USER.name or"???").."'s room", - room_password=nil, - }) + WS.send("room",json.encode{ + email=USER.email, + accessToken=USER.accessToken, + room_name=(USER.name or"???").."'s room", + room_password=nil, + }) ]] lastCreateRoomTime=TIME() else