2023-04-30 10:51:05 +00:00
|
|
|
-- SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
local function tellRoomToObserver(self, player)
|
|
|
|
local observee = self.players[1]
|
|
|
|
player:doNotify("Setup", json.encode{
|
|
|
|
observee.id,
|
|
|
|
observee.serverplayer:getScreenName(),
|
|
|
|
observee.serverplayer:getAvatar(),
|
|
|
|
})
|
|
|
|
player:doNotify("EnterRoom", json.encode{
|
|
|
|
#self.players, self.timeout, self.settings
|
|
|
|
})
|
2023-05-19 08:23:24 +00:00
|
|
|
player:doNotify("StartGame", "")
|
2023-04-30 10:51:05 +00:00
|
|
|
|
|
|
|
-- send player data
|
|
|
|
for _, p in ipairs(self:getOtherPlayers(observee, true, true)) do
|
|
|
|
player:doNotify("AddPlayer", json.encode{
|
|
|
|
p.id,
|
|
|
|
p.serverplayer:getScreenName(),
|
|
|
|
p.serverplayer:getAvatar(),
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
local player_circle = {}
|
|
|
|
for i = 1, #self.players do
|
|
|
|
table.insert(player_circle, self.players[i].id)
|
|
|
|
end
|
|
|
|
player:doNotify("ArrangeSeats", json.encode(player_circle))
|
|
|
|
|
|
|
|
for _, p in ipairs(self.players) do
|
|
|
|
self:notifyProperty(player, p, "general")
|
|
|
|
self:notifyProperty(player, p, "deputyGeneral")
|
|
|
|
p:marshal(player)
|
|
|
|
end
|
|
|
|
|
|
|
|
player:doNotify("UpdateDrawPile", #self.draw_pile)
|
|
|
|
player:doNotify("UpdateRoundNum", self:getTag("RoundCount") or 0)
|
|
|
|
|
|
|
|
table.insert(self.observers, {observee.id, player})
|
|
|
|
end
|
|
|
|
|
|
|
|
local function addObserver(self, id)
|
|
|
|
local all_observers = self.room:getObservers()
|
|
|
|
for _, p in fk.qlist(all_observers) do
|
|
|
|
if p:getId() == id then
|
|
|
|
tellRoomToObserver(self, p)
|
|
|
|
self:doBroadcastNotify("AddObserver", json.encode{
|
|
|
|
p:getId(),
|
|
|
|
p:getScreenName(),
|
|
|
|
p:getAvatar()
|
|
|
|
})
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function removeObserver(self, id)
|
|
|
|
for _, t in ipairs(self.observers) do
|
|
|
|
local __, p = table.unpack(t)
|
|
|
|
if p:getId() == id then
|
|
|
|
table.removeOne(self.observers, t)
|
|
|
|
self:doBroadcastNotify("RemoveObserver", json.encode{
|
|
|
|
p:getId(),
|
|
|
|
})
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local request_handlers = {}
|
|
|
|
request_handlers["reconnect"] = function(room, id, reqlist)
|
|
|
|
local p = room:getPlayerById(id)
|
2023-06-04 11:31:44 +00:00
|
|
|
if p then
|
|
|
|
p:reconnect()
|
|
|
|
end
|
2023-04-30 10:51:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
request_handlers["observe"] = function(room, id, reqlist)
|
|
|
|
addObserver(room, id)
|
|
|
|
end
|
|
|
|
|
|
|
|
request_handlers["leave"] = function(room, id, reqlist)
|
|
|
|
removeObserver(room, id)
|
|
|
|
end
|
|
|
|
|
|
|
|
request_handlers["prelight"] = function(room, id, reqlist)
|
|
|
|
local p = room:getPlayerById(id)
|
|
|
|
p:prelightSkill(reqlist[3], reqlist[4] == "true")
|
|
|
|
end
|
|
|
|
|
2023-05-18 23:45:21 +00:00
|
|
|
request_handlers["luckcard"] = function(room, id, reqlist)
|
|
|
|
local p = room:getPlayerById(id)
|
|
|
|
local cancel = reqlist[3] == "false"
|
|
|
|
local luck_data = room:getTag("LuckCardData")
|
2023-05-26 12:53:26 +00:00
|
|
|
if not (p and luck_data) then return end
|
2023-05-18 23:45:21 +00:00
|
|
|
local pdata = luck_data[id]
|
|
|
|
|
|
|
|
if not cancel then
|
|
|
|
pdata.luckTime = pdata.luckTime - 1
|
|
|
|
luck_data.discardInit(room, p)
|
|
|
|
luck_data.drawInit(room, p, pdata.num)
|
|
|
|
else
|
|
|
|
pdata.luckTime = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if pdata.luckTime > 0 then
|
|
|
|
p:doNotify("AskForLuckCard", pdata.luckTime)
|
2023-06-16 02:56:33 +00:00
|
|
|
else
|
|
|
|
p.serverplayer:setThinking(false)
|
2023-05-18 23:45:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
room:setTag("LuckCardData", luck_data)
|
|
|
|
end
|
|
|
|
|
2023-04-30 10:51:05 +00:00
|
|
|
request_handlers["changeself"] = function(room, id, reqlist)
|
|
|
|
local p = room:getPlayerById(id)
|
|
|
|
local toId = tonumber(reqlist[3])
|
|
|
|
local from = p
|
|
|
|
local to = room:getPlayerById(toId)
|
|
|
|
local from_sp = from._splayer
|
|
|
|
|
|
|
|
-- 注意发来信息的玩家的主视角可能已经不是自己了
|
|
|
|
-- 先换成正确的玩家
|
|
|
|
from = table.find(room.players, function(p)
|
|
|
|
return table.contains(p._observers, from_sp)
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- 切换视角
|
|
|
|
table.removeOne(from._observers, from_sp)
|
|
|
|
table.insert(to._observers, from_sp)
|
|
|
|
from_sp:doNotify("ChangeSelf", json.encode {
|
|
|
|
id = toId,
|
|
|
|
handcards = to:getCardIds(Player.Hand),
|
2023-06-16 05:26:02 +00:00
|
|
|
special_cards = to.special_cards,
|
2023-04-30 10:51:05 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-06-16 02:56:33 +00:00
|
|
|
request_handlers["newroom"] = function(s, id)
|
|
|
|
s:registerRoom(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- 处理异步请求的协程,本身也是个死循环就是了。
|
|
|
|
-- 为了适应调度器,目前又暂且将请求分为“耗时请求”和不耗时请求。
|
|
|
|
-- 耗时请求处理后会立刻挂起。不耗时的请求则会不断处理直到请求队列空后再挂起。
|
2023-05-18 23:45:21 +00:00
|
|
|
local function requestLoop(self)
|
2023-04-30 10:51:05 +00:00
|
|
|
while true do
|
|
|
|
local ret = false
|
2023-06-16 02:56:33 +00:00
|
|
|
local request = self.thread:fetchRequest()
|
2023-04-30 10:51:05 +00:00
|
|
|
if request ~= "" then
|
|
|
|
ret = true
|
|
|
|
local reqlist = request:split(",")
|
2023-06-16 02:56:33 +00:00
|
|
|
local roomId = tonumber(table.remove(reqlist, 1))
|
|
|
|
local room = self:getRoom(roomId)
|
|
|
|
|
|
|
|
if room then
|
|
|
|
RoomInstance = room
|
|
|
|
local id = tonumber(reqlist[1])
|
|
|
|
local command = reqlist[2]
|
|
|
|
request_handlers[command](room, id, reqlist)
|
|
|
|
RoomInstance = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not ret then
|
|
|
|
coroutine.yield()
|
2023-04-30 10:51:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return requestLoop
|