修改刷新房间相关代码

This commit is contained in:
MrZ626
2021-03-30 14:55:46 +08:00
parent 20f031fcb4
commit 23c0c63c6a
7 changed files with 64 additions and 56 deletions

View File

@@ -252,7 +252,7 @@ return{
chat="Chat",
},
net_rooms={
fresh="Reresh",
refresh="Refresh",
new="New Room",
join="Join",
up="",

View File

@@ -225,7 +225,7 @@ return{
chat="Chat",
},
net_rooms={
fresh="Fresh",
-- refresh="Refresh",
new="Nouveau salon",
join="Rejoindre",
up="",

View File

@@ -252,7 +252,7 @@ return{
chat="Chat",
},
net_rooms={
fresh="Fresh",
-- refresh="Refresh",
-- new="New room",
-- join="Join",
up="",

View File

@@ -231,7 +231,7 @@ return{
-- chat="Chat",
},
net_rooms={
-- fresh="Fresh",
-- refresh="Refresh",
-- new="New room",
-- join="Join",
up="",

View File

@@ -252,7 +252,7 @@ return{
chat="聊天室",
},
net_rooms={
fresh="刷新",
refresh="刷新",
new="创建房间",
join="加入",
up="",

View File

@@ -2,7 +2,7 @@ local data=love.data
local NET={
login=false,
allow_online=false,
roomList=false,
roomList={},
accessToken=false,
}
@@ -26,6 +26,9 @@ end
local function _unlock(name)
locks[name]=false
end
function NET.getLock(name)
return locks[name]
end
--Parse json message
local function _parse(res)
@@ -119,15 +122,17 @@ function NET.signal_quit()
end
--Room
function NET.freshRoom()
WS.send("play",JSON.encode{
action=0,
data={
type=nil,
begin=0,
count=10,
}
})
function NET.fetchRoom()
if _lock("fetchRoom")then
WS.send("play",JSON.encode{
action=0,
data={
type=nil,
begin=0,
count=10,
}
})
end
end
function NET.createRoom()
if _lock("enterRoom")then
@@ -266,6 +271,7 @@ function NET.TICK_WS_play()
SCN.go("net_menu")
elseif res.action==0 then--Fetch rooms
NET.roomList=res.roomList
_unlock("fetchRoom")
elseif res.action==2 then--Join(create) room
-- loadGame("netBattle",true,true)
_unlock("enterRoom")

View File

@@ -3,13 +3,12 @@ local min=math.min
local NET=NET
local scrollPos,selected
local lastfreshTime
local lastFetchTime
local lastCreateRoomTime=0
local function freshRoomList()
NET.roomList=nil
lastfreshTime=TIME()
NET.freshRoom()
local function fetchRoom()
lastFetchTime=TIME()
NET.fetchRoom()
end
local scene={}
@@ -18,7 +17,7 @@ function scene.sceneInit()
BG.set("bg1")
scrollPos=0
selected=1
freshRoomList()
fetchRoom()
end
function scene.wheelMoved(_,y)
@@ -26,8 +25,8 @@ function scene.wheelMoved(_,y)
end
function scene.keyDown(k)
if k=="r"then
if TIME()-lastfreshTime>1 then
freshRoomList()
if TIME()-lastFetchTime>1 then
fetchRoom()
end
elseif k=="n"then
if TIME()-lastCreateRoomTime>26 then
@@ -38,7 +37,7 @@ function scene.keyDown(k)
end
elseif k=="escape"then
SCN.back()
elseif NET.roomList and #NET.roomList>0 then
elseif #NET.roomList>0 then
if k=="down"then
if selected<#NET.roomList then
selected=selected+1
@@ -54,6 +53,7 @@ function scene.keyDown(k)
end
end
elseif k=="return"then
if NET.getLock("fetchRoom")then return end
if NET.roomList[selected].private then
LOG.print("Can't enter private room now")
return
@@ -64,49 +64,51 @@ function scene.keyDown(k)
end
function scene.update()
if TIME()-lastfreshTime>5 then
freshRoomList()
if TIME()-lastFetchTime>5 then
fetchRoom()
end
end
function scene.draw()
--Fetching timer
gc.setColor(1,1,1,.26)
gc.arc("fill","pie",240,620,60,-1.5708,-1.5708+1.2566*(TIME()-lastfreshTime))
if NET.roomList then
gc.setColor(1,1,1)
if #NET.roomList>0 then
gc.setLineWidth(2)
gc.rectangle("line",55,110,1100,400)
gc.setColor(1,1,1,.3)
gc.rectangle("fill",55,40*(1+selected-scrollPos)+30,1100,40)
setFont(35)
for i=1,min(10,#NET.roomList-scrollPos)do
local R=NET.roomList[scrollPos+i]
if R.private then
gc.setColor(1,1,1)
gc.draw(IMG.lock,64,75+40*i)
end
gc.setColor(.9,.9,1)
gc.print(scrollPos+i,100,66+40*i)
gc.setColor(1,1,.7)
gc.print(R.name,200,66+40*i)
gc.setColor(1,1,1)
gc.printf(R.type,500,66+40*i,500,"right")
gc.print(R.count.."/"..R.capacity,1050,66+40*i)
end
else
setFont(60)
mStr(text.noRooms,640,315)
gc.arc("fill","pie",240,620,60,-1.5708,-1.5708+1.2566*(TIME()-lastFetchTime))
--Room list
gc.setColor(1,1,1)
gc.setLineWidth(2)
gc.rectangle("line",55,110,1100,400)
gc.setColor(1,1,1,.3)
gc.rectangle("fill",55,40*(1+selected-scrollPos)+30,1100,40)
setFont(35)
for i=1,min(10,#NET.roomList-scrollPos)do
local R=NET.roomList[scrollPos+i]
if R.private then
gc.setColor(1,1,1)
gc.draw(IMG.lock,64,75+40*i)
end
gc.setColor(.9,.9,1)
gc.print(scrollPos+i,100,66+40*i)
gc.setColor(1,1,.7)
gc.print(R.name,200,66+40*i)
gc.setColor(1,1,1)
gc.printf(R.type,500,66+40*i,500,"right")
gc.print(R.count.."/"..R.capacity,1050,66+40*i)
end
--No room message
if #NET.roomList==0 then
setFont(60)
mStr(text.noRooms,640,315)
end
end
scene.widgetList={
WIDGET.newKey{name="fresh", x=240,y=620,w=140,h=140,font=40,code=freshRoomList,hide=function()return TIME()-lastfreshTime<1.26 end},
WIDGET.newKey{name="refresh", x=240,y=620,w=140,h=140,font=40,code=fetchRoom, hide=function()return TIME()-lastFetchTime<1.26 end},
WIDGET.newKey{name="new", x=440,y=620,w=140,h=140,font=25,code=pressKey"n"},
WIDGET.newKey{name="join", x=640,y=620,w=140,h=140,font=40,code=pressKey"return",hide=function()return not NET.roomList end},
WIDGET.newKey{name="up", x=840,y=585,w=140,h=70,font=40,code=pressKey"up",hide=function()return not NET.roomList end},
WIDGET.newKey{name="down", x=840,y=655,w=140,h=70,font=40,code=pressKey"down",hide=function()return not NET.roomList end},
WIDGET.newKey{name="join", x=640,y=620,w=140,h=140,font=40,code=pressKey"return", hide=function()return #NET.roomList==0 end},
WIDGET.newKey{name="up", x=840,y=585,w=140,h=70,font=40,code=pressKey"up", hide=function()return #NET.roomList==0 end},
WIDGET.newKey{name="down", x=840,y=655,w=140,h=70,font=40,code=pressKey"down", hide=function()return #NET.roomList==0 end},
WIDGET.newButton{name="back", x=1140,y=640,w=170,h=80,font=40,code=backScene},
}