2022-03-27 06:49:41 +00:00
|
|
|
Server = class('Server')
|
|
|
|
package.path = package.path .. ';./lua/server/?.lua'
|
|
|
|
Room = require "room"
|
|
|
|
ServerPlayer = require "serverplayer"
|
2022-01-24 02:23:08 +00:00
|
|
|
|
|
|
|
freekill.server_callback = {}
|
|
|
|
|
|
|
|
function Server:initialize()
|
|
|
|
self.server = freekill.ServerInstance
|
2022-03-27 12:00:29 +00:00
|
|
|
self.db = freekill.ServerInstance:getDatabase()
|
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
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-03-27 06:49:41 +00:00
|
|
|
self.server.startRoom = function(_self, _room)
|
|
|
|
local room = Room:new(_room)
|
|
|
|
room.server = self
|
|
|
|
table.insert(self.rooms, room)
|
|
|
|
|
|
|
|
room:run()
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-03-27 06:49:41 +00:00
|
|
|
-- If room.run returns, the game is over and lua room
|
|
|
|
-- should be destoried now.
|
|
|
|
-- This behavior does not affect C++ Room.
|
|
|
|
table.removeOne(self.rooms, room)
|
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-03-27 06:49:41 +00:00
|
|
|
self.rooms = {}
|
|
|
|
self.players = {}
|
2022-01-24 02:23:08 +00:00
|
|
|
end
|
|
|
|
|
2022-03-27 12:00:29 +00:00
|
|
|
freekill.server_callback["UpdateAvatar"] = function(jsonData)
|
|
|
|
-- jsonData: [ int uid, string newavatar ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, avatar = data[1], data[2]
|
|
|
|
local sql = "UPDATE userinfo SET avatar='%s' WHERE id=%d;"
|
|
|
|
Sql.exec(ServerInstance.db, string.format(sql, avatar, id))
|
|
|
|
local player = freekill.ServerInstance:findPlayer(id)
|
|
|
|
player:doNotify("UpdateAvatar", "[]")
|
|
|
|
end
|
|
|
|
|
|
|
|
freekill.server_callback["UpdatePassword"] = function(jsonData)
|
|
|
|
-- jsonData: [ int uid, string oldpassword, int newpassword ]
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id, old, new = data[1], data[2], data[3]
|
|
|
|
local sql_find = "SELECT password FROM userinfo WHERE id=%d;"
|
|
|
|
local sql_update = "UPDATE userinfo SET password='%s' WHERE id=%d;"
|
|
|
|
|
|
|
|
local db = ServerInstance.db
|
|
|
|
local passed = false
|
|
|
|
local result = Sql.exec_select(db, string.format(sql_find, id))
|
|
|
|
passed = (result["password"][1] == sha256(old))
|
|
|
|
if passed then
|
|
|
|
Sql.exec(db, string.format(sql_update, sha256(new), id))
|
|
|
|
end
|
|
|
|
|
|
|
|
local player = freekill.ServerInstance:findPlayer(tonumber(id))
|
|
|
|
player:doNotify("UpdatePassword", passed and "1" or "0")
|
|
|
|
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-03-23 11:40:28 +00:00
|
|
|
freekill.server_callback["DoLuaScript"] = function(jsonData)
|
|
|
|
-- jsonData: [ int uid, string luaScript ]
|
|
|
|
-- warning: only use this in debugging mode.
|
|
|
|
if not DebugMode then return end
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
assert(load(data[2]))()
|
|
|
|
end
|
|
|
|
|
2022-03-27 06:49:41 +00:00
|
|
|
freekill.server_callback["PlayerStateChanged"] = function(jsonData)
|
|
|
|
-- jsonData: [ int uid, string stateString ]
|
|
|
|
-- note: this function is not called by Router.
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local id = data[1]
|
|
|
|
local stateString = data[2]
|
|
|
|
ServerInstance.players[id].state = stateString
|
|
|
|
end
|
|
|
|
|
2022-01-24 02:23:08 +00:00
|
|
|
ServerInstance = Server:new()
|