2022-01-24 02:23:08 +00:00
|
|
|
local Server = class('Server')
|
|
|
|
|
|
|
|
freekill.server_callback = {}
|
|
|
|
|
|
|
|
function Server:initialize()
|
|
|
|
self.server = freekill.ServerInstance
|
2022-03-02 12:56:37 +00:00
|
|
|
self.server.callback = function(_self, command, jsonData)
|
2022-01-24 02:23:08 +00:00
|
|
|
local cb = freekill.server_callback[command]
|
|
|
|
if (type(cb) == "function") then
|
2022-03-02 12:56:37 +00:00
|
|
|
cb(jsonData)
|
2022-01-24 02:23:08 +00:00
|
|
|
else
|
|
|
|
print("Server error: Unknown command " .. command);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 12:56:37 +00:00
|
|
|
freekill.server_callback["CreateRoom"] = function(jsonData)
|
|
|
|
-- jsonData: [ int uid, string name, int capacity ]
|
|
|
|
local data = json.decode(jsonData)
|
2022-03-01 05:18:00 +00:00
|
|
|
local owner = freekill.ServerInstance:findPlayer(tonumber(data[1]))
|
2022-01-24 02:23:08 +00:00
|
|
|
local roomName = data[2]
|
|
|
|
local capacity = data[3]
|
|
|
|
freekill.ServerInstance:createRoom(owner, roomName, capacity)
|
|
|
|
end
|
|
|
|
|
2022-03-02 12:56:37 +00:00
|
|
|
freekill.server_callback["EnterRoom"] = function(jsonData)
|
|
|
|
-- jsonData: [ int uid, int roomId ]
|
|
|
|
local data = json.decode(jsonData)
|
2022-03-01 05:18:00 +00:00
|
|
|
local player = freekill.ServerInstance:findPlayer(tonumber(data[1]))
|
|
|
|
local room = freekill.ServerInstance:findRoom(tonumber(data[2]))
|
|
|
|
room:addPlayer(player)
|
|
|
|
end
|
|
|
|
|
2022-03-02 12:56:37 +00:00
|
|
|
freekill.server_callback["QuitRoom"] = function(jsonData)
|
|
|
|
-- jsonData: [ int uid ]
|
|
|
|
local data = json.decode(jsonData)
|
2022-03-01 05:18:00 +00:00
|
|
|
local player = freekill.ServerInstance:findPlayer(tonumber(data[1]))
|
|
|
|
local room = player:getRoom()
|
|
|
|
if not room:isLobby() then
|
|
|
|
room:removePlayer(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-24 02:23:08 +00:00
|
|
|
ServerInstance = Server:new()
|