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,
|
2023-06-30 20:12:19 +00:00
|
|
|
observee._splayer:getScreenName(),
|
|
|
|
observee._splayer:getAvatar(),
|
2023-04-30 10:51:05 +00:00
|
|
|
})
|
|
|
|
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
|
2023-07-02 14:39:01 +00:00
|
|
|
for _, p in ipairs(self:getOtherPlayers(observee, false, true)) do
|
2023-04-30 10:51:05 +00:00
|
|
|
player:doNotify("AddPlayer", json.encode{
|
|
|
|
p.id,
|
2023-06-30 20:12:19 +00:00
|
|
|
p._splayer:getScreenName(),
|
|
|
|
p._splayer:getAvatar(),
|
2023-04-30 10:51:05 +00: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))
|
|
|
|
|
2023-09-06 14:16:09 +00:00
|
|
|
-- send printed_cards
|
|
|
|
for i = -2, -math.huge, -1 do
|
|
|
|
local c = Fk.printed_cards[i]
|
|
|
|
if not c then break end
|
|
|
|
player:doNotify("PrintCard", json.encode{ c.name, c.suit, c.number })
|
|
|
|
end
|
|
|
|
|
|
|
|
-- send card marks
|
2023-09-06 14:33:42 +00:00
|
|
|
for id, marks in pairs(self.card_marks) do
|
2023-09-06 14:16:09 +00:00
|
|
|
for k, v in pairs(marks) do
|
|
|
|
player:doNotify("SetCardMark", json.encode{ id, k, v })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-06 14:33:42 +00:00
|
|
|
for _, p in ipairs(self.players) do
|
|
|
|
self:notifyProperty(player, p, "general")
|
|
|
|
self:notifyProperty(player, p, "deputyGeneral")
|
|
|
|
p:marshal(player, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
player:doNotify("UpdateDrawPile", #self.draw_pile)
|
|
|
|
player:doNotify("UpdateRoundNum", self:getTag("RoundCount") or 0)
|
|
|
|
|
2023-07-12 12:35:05 +00:00
|
|
|
table.insert(self.observers, {observee.id, player, player:getId()})
|
2023-04-30 10:51:05 +00: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 12:35:05 +00:00
|
|
|
local pid = t[3]
|
|
|
|
if pid == id then
|
2023-04-30 10:51:05 +00:00
|
|
|
table.removeOne(self.observers, t)
|
2023-07-12 12:35:05 +00:00
|
|
|
self:doBroadcastNotify("RemoveObserver", json.encode{ pid })
|
2023-04-30 10:51:05 +00:00
|
|
|
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)
|
2023-07-02 14:39:01 +00:00
|
|
|
if p then
|
|
|
|
p:prelightSkill(reqlist[3], reqlist[4] == "true")
|
|
|
|
end
|
2023-04-30 10:51:05 +00:00
|
|
|
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-07-02 12:39:42 +00:00
|
|
|
request_handlers["surrender"] = function(room, id, reqlist)
|
2023-07-02 14:39:01 +00:00
|
|
|
local player = room:getPlayerById(id)
|
|
|
|
if not player then return end
|
2023-07-02 12:39:42 +00: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 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]
|
2023-10-02 16:19:12 +00:00
|
|
|
Pcall(request_handlers[command], room, id, reqlist)
|
2023-06-16 02:56:33 +00:00
|
|
|
RoomInstance = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not ret then
|
|
|
|
coroutine.yield()
|
2023-04-30 10:51:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return requestLoop
|