2023-04-30 18:51:05 +08:00
|
|
|
-- SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2024-10-22 00:10:53 +08:00
|
|
|
-- 本文件是用来处理各种异步请求的
|
|
|
|
-- 与游戏中常见的请求-答复没有什么联系
|
|
|
|
|
2023-04-30 18:51:05 +08:00
|
|
|
local function tellRoomToObserver(self, player)
|
|
|
|
local observee = self.players[1]
|
2024-06-10 15:19:47 +08:00
|
|
|
local start_time = os.getms()
|
2024-10-22 00:10:53 +08:00
|
|
|
local summary = self:toJsonObject(observee)
|
2024-04-19 20:53:19 +08:00
|
|
|
player:doNotify("Observe", json.encode(summary))
|
2023-09-06 22:33:42 +08:00
|
|
|
|
2024-06-10 15:19:47 +08:00
|
|
|
fk.qInfo(string.format("[Observe] %d, %s, in %.3fms",
|
|
|
|
self.id, player:getScreenName(), (os.getms() - start_time) / 1000))
|
|
|
|
|
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-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
|
2024-02-27 02:28:13 +08:00
|
|
|
|
|
|
|
room.hasSurrendered = true
|
|
|
|
player.surrendered = true
|
|
|
|
room:doBroadcastNotify("CancelRequest", "")
|
2024-06-10 15:19:47 +08:00
|
|
|
ResumeRoom(room.id)
|
2023-07-02 20:39:42 +08:00
|
|
|
end
|
|
|
|
|
2024-01-11 18:36:05 +08:00
|
|
|
request_handlers["updatemini"] = function(room, pid, reqlist)
|
|
|
|
local player = room:getPlayerById(pid)
|
|
|
|
local data = player.mini_game_data
|
|
|
|
if not data then return end
|
|
|
|
local game = Fk.mini_games[data.type]
|
|
|
|
if not (game and game.update_func) then return end
|
|
|
|
local dat = table.simpleClone(reqlist)
|
|
|
|
table.remove(dat, 1)
|
|
|
|
table.remove(dat, 1)
|
|
|
|
game.update_func(player, dat)
|
|
|
|
end
|
|
|
|
|
2023-06-16 10:56:33 +08:00
|
|
|
request_handlers["newroom"] = function(s, id)
|
|
|
|
s:registerRoom(id)
|
2024-06-10 15:19:47 +08:00
|
|
|
ResumeRoom(id)
|
2023-06-16 10:56:33 +08:00
|
|
|
end
|
|
|
|
|
2024-04-07 00:35:57 +08:00
|
|
|
request_handlers["reloadpackage"] = function(_, _, reqlist)
|
2024-04-02 01:00:10 +08:00
|
|
|
if not IsConsoleStart() then return end
|
|
|
|
local path = reqlist[3]
|
|
|
|
Fk:reloadPackage(path)
|
|
|
|
end
|
|
|
|
|
2024-06-10 15:19:47 +08:00
|
|
|
return function(self, request)
|
|
|
|
local reqlist = request:split(",")
|
|
|
|
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]
|
|
|
|
Pcall(request_handlers[command], room, id, reqlist)
|
|
|
|
RoomInstance = nil
|
2023-04-30 18:51:05 +08:00
|
|
|
end
|
|
|
|
end
|