2023-04-30 18:51:05 +08: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,
|
2023-07-01 04:12:19 +08:00
|
|
|
observee._splayer:getScreenName(),
|
|
|
|
observee._splayer:getAvatar(),
|
2023-04-30 18:51:05 +08:00
|
|
|
})
|
|
|
|
player:doNotify("EnterRoom", json.encode{
|
|
|
|
#self.players, self.timeout, self.settings
|
|
|
|
})
|
2023-05-19 16:23:24 +08:00
|
|
|
player:doNotify("StartGame", "")
|
2023-04-30 18:51:05 +08:00
|
|
|
|
|
|
|
-- send player data
|
2023-07-02 22:39:01 +08:00
|
|
|
for _, p in ipairs(self:getOtherPlayers(observee, false, true)) do
|
2023-04-30 18:51:05 +08:00
|
|
|
player:doNotify("AddPlayer", json.encode{
|
|
|
|
p.id,
|
2023-07-01 04:12:19 +08:00
|
|
|
p._splayer:getScreenName(),
|
|
|
|
p._splayer:getAvatar(),
|
2023-04-30 18:51:05 +08:00
|
|
|
})
|
|
|
|
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")
|
2023-08-03 20:34:43 +08:00
|
|
|
p:marshal(player, true)
|
2023-04-30 18:51:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
player:doNotify("UpdateDrawPile", #self.draw_pile)
|
|
|
|
player:doNotify("UpdateRoundNum", self:getTag("RoundCount") or 0)
|
|
|
|
|
2023-07-12 20:35:05 +08:00
|
|
|
table.insert(self.observers, {observee.id, player, player:getId()})
|
2023-04-30 18:51:05 +08:00
|
|
|
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
|
2023-07-12 20:35:05 +08:00
|
|
|
local pid = t[3]
|
|
|
|
if pid == id then
|
2023-04-30 18:51:05 +08:00
|
|
|
table.removeOne(self.observers, t)
|
2023-07-12 20:35:05 +08:00
|
|
|
self:doBroadcastNotify("RemoveObserver", json.encode{ pid })
|
2023-04-30 18:51:05 +08:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local request_handlers = {}
|
|
|
|
request_handlers["reconnect"] = function(room, id, reqlist)
|
|
|
|
local p = room:getPlayerById(id)
|
2023-06-04 19:31:44 +08:00
|
|
|
if p then
|
|
|
|
p:reconnect()
|
|
|
|
end
|
2023-04-30 18:51:05 +08: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)
|
2023-07-02 22:39:01 +08:00
|
|
|
if p then
|
|
|
|
p:prelightSkill(reqlist[3], reqlist[4] == "true")
|
|
|
|
end
|
2023-04-30 18:51:05 +08:00
|
|
|
end
|
|
|
|
|
2023-05-19 07:45:21 +08: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 20:53:26 +08:00
|
|
|
if not (p and luck_data) then return end
|
2023-05-19 07:45:21 +08: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 10:56:33 +08:00
|
|
|
else
|
|
|
|
p.serverplayer:setThinking(false)
|
2023-05-19 07:45:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
room:setTag("LuckCardData", luck_data)
|
|
|
|
end
|
|
|
|
|
2023-04-30 18:51:05 +08: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 13:26:02 +08:00
|
|
|
special_cards = to.special_cards,
|
2023-04-30 18:51:05 +08:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-07-02 20:39:42 +08:00
|
|
|
request_handlers["surrender"] = function(room, id, reqlist)
|
2023-07-02 22:39:01 +08:00
|
|
|
local player = room:getPlayerById(id)
|
|
|
|
if not player then return end
|
2023-07-02 20:39:42 +08:00
|
|
|
local logic = room.logic
|
|
|
|
local curEvent = logic:getCurrentEvent()
|
|
|
|
if curEvent then
|
|
|
|
curEvent:addExitFunc(
|
|
|
|
function()
|
|
|
|
player.surrendered = true
|
|
|
|
room:broadcastProperty(player, "surrendered")
|
|
|
|
room:gameOver(Fk.game_modes[room.settings.gameMode]:getWinner(player))
|
|
|
|
end
|
|
|
|
)
|
|
|
|
room.hasSurrendered = true
|
|
|
|
room:doBroadcastNotify("CancelRequest", "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-16 10:56:33 +08:00
|
|
|
request_handlers["newroom"] = function(s, id)
|
|
|
|
s:registerRoom(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- 处理异步请求的协程,本身也是个死循环就是了。
|
|
|
|
-- 为了适应调度器,目前又暂且将请求分为“耗时请求”和不耗时请求。
|
|
|
|
-- 耗时请求处理后会立刻挂起。不耗时的请求则会不断处理直到请求队列空后再挂起。
|
2023-05-19 07:45:21 +08:00
|
|
|
local function requestLoop(self)
|
2023-04-30 18:51:05 +08:00
|
|
|
while true do
|
|
|
|
local ret = false
|
2023-06-16 10:56:33 +08:00
|
|
|
local request = self.thread:fetchRequest()
|
2023-04-30 18:51:05 +08:00
|
|
|
if request ~= "" then
|
|
|
|
ret = true
|
|
|
|
local reqlist = request:split(",")
|
2023-06-16 10:56:33 +08: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 18:51:05 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return requestLoop
|