2022-03-30 16:33:56 +08:00
|
|
|
---@class ServerPlayer : Player
|
2022-03-31 13:29:23 +08:00
|
|
|
---@field serverplayer fk.ServerPlayer
|
|
|
|
---@field room Room
|
|
|
|
---@field next ServerPlayer
|
|
|
|
---@field request_data string
|
|
|
|
---@field client_reply string
|
|
|
|
---@field default_reply string
|
|
|
|
---@field reply_ready boolean
|
2022-03-27 14:49:41 +08:00
|
|
|
local ServerPlayer = Player:subclass("ServerPlayer")
|
2022-03-25 12:28:07 +08:00
|
|
|
|
2022-03-27 14:49:41 +08:00
|
|
|
function ServerPlayer:initialize(_self)
|
|
|
|
Player.initialize(self)
|
|
|
|
self.serverplayer = _self
|
2022-04-03 21:43:35 +08:00
|
|
|
self.id = _self:getId()
|
2022-03-30 16:33:56 +08:00
|
|
|
self.room = nil
|
2022-03-30 14:14:40 +08:00
|
|
|
|
|
|
|
self.next = nil
|
|
|
|
|
|
|
|
-- Below are for doBroadcastRequest
|
|
|
|
self.request_data = ""
|
|
|
|
self.client_reply = ""
|
|
|
|
self.default_reply = ""
|
|
|
|
self.reply_ready = false
|
2022-03-27 14:49:41 +08:00
|
|
|
end
|
|
|
|
|
2022-04-01 20:51:01 +08:00
|
|
|
---@return integer
|
2022-03-27 14:49:41 +08:00
|
|
|
function ServerPlayer:getId()
|
2022-04-03 21:43:35 +08:00
|
|
|
return self.id
|
2022-03-27 14:49:41 +08:00
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
---@param command string
|
|
|
|
---@param jsonData string
|
2022-03-28 22:24:30 +08:00
|
|
|
function ServerPlayer:doNotify(command, jsonData)
|
|
|
|
self.serverplayer:doNotify(command, jsonData)
|
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
--- Send a request to client, and allow client to reply within *timeout* seconds.
|
|
|
|
---
|
|
|
|
--- *timeout* must not be negative. If nil, room.timeout is used.
|
|
|
|
---@param command string
|
|
|
|
---@param jsonData string
|
2022-04-01 20:51:01 +08:00
|
|
|
---@param timeout integer
|
2022-03-28 22:24:30 +08:00
|
|
|
function ServerPlayer:doRequest(command, jsonData, timeout)
|
2022-03-30 14:14:40 +08:00
|
|
|
timeout = timeout or self.room.timeout
|
|
|
|
self.client_reply = ""
|
|
|
|
self.reply_ready = false
|
2022-03-28 22:24:30 +08:00
|
|
|
self.serverplayer:doRequest(command, jsonData, timeout)
|
|
|
|
end
|
|
|
|
|
2022-03-31 13:29:23 +08:00
|
|
|
--- Wait for at most *timeout* seconds for reply from client.
|
|
|
|
---
|
|
|
|
--- If *timeout* is negative or **nil**, the function will wait forever until get reply.
|
2022-04-02 21:39:44 +08:00
|
|
|
---@param timeout integer @ seconds to wait
|
|
|
|
---@return string @ JSON data
|
2022-03-30 14:14:40 +08:00
|
|
|
function ServerPlayer:waitForReply(timeout)
|
|
|
|
local result = ""
|
|
|
|
if timeout == nil then
|
|
|
|
result = self.serverplayer:waitForReply()
|
|
|
|
else
|
|
|
|
result = self.serverplayer:waitForReply(timeout)
|
|
|
|
end
|
|
|
|
self.request_data = ""
|
|
|
|
self.client_reply = result
|
|
|
|
if result ~= "" then self.reply_ready = true end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2022-04-01 20:51:01 +08:00
|
|
|
---@param skill Skill
|
|
|
|
function ServerPlayer:hasSkill(skill)
|
|
|
|
return table.contains(self.player_skills, skill)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerPlayer:isAlive()
|
|
|
|
return self.dead == false
|
|
|
|
end
|
|
|
|
|
|
|
|
function ServerPlayer:getNextAlive()
|
|
|
|
if #self.room.alive_players == 0 then
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
local ret = self.next
|
|
|
|
while ret.dead do
|
|
|
|
ret = ret.next
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2022-04-02 21:39:44 +08:00
|
|
|
function ServerPlayer:turnOver()
|
|
|
|
self.faceup = not self.faceup
|
|
|
|
self.room:broadcastProperty(self, "faceup")
|
|
|
|
|
|
|
|
-- TODO: log
|
|
|
|
self.room.logic:trigger(fk.TurnedOver, self)
|
|
|
|
end
|
|
|
|
|
2022-03-27 14:49:41 +08:00
|
|
|
return ServerPlayer
|