2023-04-09 05:35:35 +00:00
|
|
|
-- SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-02-26 07:01:14 +00:00
|
|
|
-- AI base class.
|
|
|
|
-- Do nothing.
|
|
|
|
|
|
|
|
---@class AI: Object
|
2023-03-26 09:32:45 +00:00
|
|
|
---@field public room Room
|
|
|
|
---@field public player ServerPlayer
|
|
|
|
---@field public command string
|
|
|
|
---@field public jsonData string
|
2023-07-14 15:12:46 +00:00
|
|
|
---@field public cb_table table<string, fun(self: AI, jsonData: string)>
|
2023-02-26 07:01:14 +00:00
|
|
|
local AI = class("AI")
|
|
|
|
|
|
|
|
function AI:initialize(player)
|
|
|
|
self.room = RoomInstance
|
|
|
|
self.player = player
|
|
|
|
local cb_t = {}
|
|
|
|
-- default strategy: print command and data, then return ""
|
|
|
|
setmetatable(cb_t, {
|
|
|
|
__index = function()
|
|
|
|
return function()
|
|
|
|
print(self.command, self.jsonData)
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
self.cb_table = cb_t
|
|
|
|
end
|
|
|
|
|
|
|
|
function AI:readRequestData()
|
|
|
|
self.command = self.player.ai_data.command
|
|
|
|
self.jsonData = self.player.ai_data.jsonData
|
|
|
|
end
|
|
|
|
|
|
|
|
function AI:makeReply()
|
|
|
|
Self = self.player
|
|
|
|
local start = os.getms()
|
|
|
|
local ret = self.cb_table[self.command] and self.cb_table[self.command](self, self.jsonData) or "__cancel"
|
|
|
|
local to_delay = 500 - (os.getms() - start) / 1000
|
2023-03-04 17:28:59 +00:00
|
|
|
-- print(to_delay)
|
2023-02-26 07:01:14 +00:00
|
|
|
self.room:delay(to_delay)
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
return AI
|