diff --git a/lua/core/engine.lua b/lua/core/engine.lua index 660742dd..eb88a399 100644 --- a/lua/core/engine.lua +++ b/lua/core/engine.lua @@ -1,3 +1,11 @@ +---@class Engine : Object +---@field packages table +---@field skills table +---@field related_skills table +---@field generals table +---@field lords table +---@field cards table +---@field translations table local Engine = class("Engine") function Engine:initialize() @@ -20,7 +28,7 @@ function Engine:initialize() self:loadPackages() end --- Package pack +---@param pack Package function Engine:loadPackage(pack) assert(pack:isInstanceOf(Package)) if self.packages[pack.name] ~= nil then @@ -48,6 +56,7 @@ function Engine:loadPackages() FileIO.cd("..") end +---@param t table function Engine:loadTranslationTable(t) assert(type(t) == "table") for k, v in pairs(t) do @@ -55,6 +64,7 @@ function Engine:loadTranslationTable(t) end end +---@param skill any function Engine:addSkill(skill) assert(skill.class:isSubclassOf(Skill)) if self.skills[skill.name] ~= nil then @@ -63,6 +73,7 @@ function Engine:addSkill(skill) self.skills[skill.name] = skill end +---@param skills any[] function Engine:addSkills(skills) assert(type(skills) == "table") for _, skill in ipairs(skills) do @@ -70,6 +81,7 @@ function Engine:addSkills(skills) end end +---@param general General function Engine:addGeneral(general) assert(general:isInstanceOf(General)) if self.generals[general.name] ~= nil then @@ -78,6 +90,7 @@ function Engine:addGeneral(general) self.generals[general.name] = general end +---@param generals General[] function Engine:addGenerals(generals) assert(type(generals) == "table") for _, general in ipairs(generals) do @@ -97,6 +110,11 @@ function Engine:addCards(cards) end end +---@param num number +---@param generalPool General[] +---@param except string[] +---@param filter function +---@return General[] generals function Engine:getGeneralsRandomly(num, generalPool, except, filter) if filter then assert(type(filter) == "function") @@ -130,6 +148,8 @@ function Engine:getGeneralsRandomly(num, generalPool, except, filter) return result end +---@param except General[] +---@return General[] function Engine:getAllGenerals(except) local result = {} for _, general in ipairs(self.generals) do diff --git a/lua/core/general.lua b/lua/core/general.lua index 496de06f..4c3f68b7 100644 --- a/lua/core/general.lua +++ b/lua/core/general.lua @@ -1,3 +1,4 @@ +--- @class General : Object General = class("General") -- enum Gender @@ -18,6 +19,7 @@ function General:initialize(package, name, kingdom, hp, maxHp, gender, initialHp self.other_skills = {} -- string[] end +---@param skill any function General:addSkill(skill) if (type(skill) == "string") then table.insert(self.other_skills, skill) diff --git a/lua/core/package.lua b/lua/core/package.lua index 0d713634..aa1c5700 100644 --- a/lua/core/package.lua +++ b/lua/core/package.lua @@ -1,3 +1,10 @@ +---@class Package : Object +---@field name string +---@field type number +---@field generals table +---@field extra_skills table +---@field related_skills table +---@field cards table local Package = class("Package") -- enum Type @@ -5,7 +12,6 @@ Package.GeneralPack = 0 Package.CardPack = 1 Package.SpecialPack = 2 --- string name, Type type function Package:initialize(name, _type) assert(type(name) == "string") assert(type(_type) == "nil" or type(_type) == "number") @@ -20,6 +26,7 @@ function Package:initialize(name, _type) self.cards = {} --> Card[] end +---@return table skills function Package:getSkills() local ret = {table.unpack(self.related_skills)} if self.type == Package.GeneralPack then @@ -32,6 +39,7 @@ function Package:getSkills() return ret end +---@param general General function Package:addGeneral(general) assert(general.class and general:isInstanceOf(General)) table.insert(self.generals, general) diff --git a/lua/core/player.lua b/lua/core/player.lua index 92c591af..d6c188fb 100644 --- a/lua/core/player.lua +++ b/lua/core/player.lua @@ -1,3 +1,4 @@ +---@class Player : Object local Player = class("Player") function Player:initialize() diff --git a/lua/core/util.lua b/lua/core/util.lua index 73ed643a..fa9e27d3 100644 --- a/lua/core/util.lua +++ b/lua/core/util.lua @@ -29,16 +29,29 @@ function table:insertTable(list) end end +---@class Sql Sql = { + ---@param filename string open = function(filename) return freekill.OpenDatabase(filename) end, + + ---@param db freekill.SQLite3 close = function(db) freekill.CloseDatabase(db) end, + + --- Execute an SQL statement. + ---@param db freekill.SQLite3 + ---@param sql string exec = function(db, sql) freekill.ExecSQL(db, sql) end, + + --- Execute a `SELECT` SQL statement. + ---@param db freekill.SQLite3 + ---@param sql string + ---@return table data # { [columnName] --> result : string[] } exec_select = function(db, sql) return json.decode(freekill.SelectFromDb(db, sql)) end, diff --git a/lua/freekill.lua b/lua/freekill.lua index 3899c84e..faa7a3f9 100644 --- a/lua/freekill.lua +++ b/lua/freekill.lua @@ -5,8 +5,13 @@ package.path = package.path .. ";./lua/lib/?.lua" .. ";./lua/?.lua" -- load libraries + +---@type class class = require "middleclass" + +---@type json json = require "json" + require "sha256" Util = require "core.util" math.randomseed(os.time()) diff --git a/lua/server/gamelogic.lua b/lua/server/gamelogic.lua index 6f569a6e..551e6af2 100644 --- a/lua/server/gamelogic.lua +++ b/lua/server/gamelogic.lua @@ -1,3 +1,4 @@ +---@class GameLogic: Object local GameLogic = class("GameLogic") function GameLogic:initialize(room) diff --git a/lua/server/room.lua b/lua/server/room.lua index 6e6f00e9..00718af8 100644 --- a/lua/server/room.lua +++ b/lua/server/room.lua @@ -1,3 +1,4 @@ +---@class Room : Object local Room = class("Room") function Room:initialize(_room) @@ -13,6 +14,7 @@ function Room:run() for _, p in freekill.qlist(self.room:getPlayers()) do local player = ServerPlayer:new(p) player.state = p:getStateString() + player.room = self table.insert(self.players, player) self.server.players[player:getId()] = player end diff --git a/lua/server/server.lua b/lua/server/server.lua index 3c5abbce..6c953b7d 100644 --- a/lua/server/server.lua +++ b/lua/server/server.lua @@ -1,3 +1,4 @@ +---@class Server : Object Server = class('Server') -- load server classes diff --git a/lua/server/serverplayer.lua b/lua/server/serverplayer.lua index 4590f79c..3bf61cad 100644 --- a/lua/server/serverplayer.lua +++ b/lua/server/serverplayer.lua @@ -1,8 +1,10 @@ +---@class ServerPlayer : Player local ServerPlayer = Player:subclass("ServerPlayer") function ServerPlayer:initialize(_self) Player.initialize(self) self.serverplayer = _self + self.room = nil self.next = nil diff --git a/lua/vscode/freekill.lua b/lua/vscode/freekill.lua new file mode 100644 index 00000000..b2dd0aa2 --- /dev/null +++ b/lua/vscode/freekill.lua @@ -0,0 +1,25 @@ +---@meta + +-- Note: these files are not used by FreeKill. +-- Just for convenience when using sumneko.lua + +---@class freekill +---FreeKill's lua API +freekill = {} + +---@class freekill.SPlayerList +SPlayerList = {} + +--- * get microsecond from Epoch +---@return number microsecond +function freekill:GetMicroSecond()end + +--- construct a QList. +---@return freekill.SPlayerList +function freekill:SPlayerList()end + +function freekill.QmlBackend_pwd()end +function freekill.QmlBackend_ls(filename)end +function freekill.QmlBackend_cd(dir)end +function freekill.QmlBackend_exists(file)end +function freekill.QmlBackend_isDir(file)end diff --git a/lua/vscode/lib.lua b/lua/vscode/lib.lua new file mode 100644 index 00000000..78d0549b --- /dev/null +++ b/lua/vscode/lib.lua @@ -0,0 +1,33 @@ +---@meta + +---@class class +---@field static any +--- middleclass +class = {} + +---@class Object +---@field class class +Object = {} + +function Object:initialize(...) end + +function Object.new(...)end + +---@param name string +function Object:subclass(name)end + +---@param class class +---@return boolean +function Object:isInstanceOf(class) end + +---@class json +json = {} + +--- convert obj to JSON string +---@return string json +function json.encode(obj)end + +--- convert JSON string to lua types +---@param str string # JSON string to decode +---@return table|number|string +function json.decode(str)end diff --git a/lua/vscode/player.lua b/lua/vscode/player.lua new file mode 100644 index 00000000..fd99c6d0 --- /dev/null +++ b/lua/vscode/player.lua @@ -0,0 +1,64 @@ +---@meta + +---@class freekill.Player +FPlayer = {} + +---@return number id +function FPlayer:getId()end + +---@param id number +function FPlayer:setId(id)end + +---@return string name +function FPlayer:getScreenName()end + +---@param name string +function FPlayer:setScreenName(name)end + +---@return string avatar +function FPlayer:getAvatar()end + +---@param avatar string +function FPlayer:setAvatar(avatar)end + +---@return string state +function FPlayer:getStateString()end + +---@param state string +function FPlayer:setStateString(state)end + +---@class freekill.ServerPlayer : freekill.Player +FServerPlayer = {} + +---@return freekill.Server +function FServerPlayer:getServer()end + +---@return freekill.Room +function FServerPlayer:getRoom()end + +---@param room freekill.Room +function FServerPlayer:setRoom(room)end + +---@param msg string +function FServerPlayer:speak(msg)end + +--- Send a request to client, and allow client to reply within *timeout* seconds. +--- +--- *timeout* must not be negative or **nil**. +---@param command string +---@param jsonData string +---@param timeout number +function FServerPlayer:doRequest(command,jsonData,timeout)end + +--- Wait for at most *timeout* seconds for reply from client. +--- +--- If *timeout* is negative or **nil**, the function will wait forever until get reply. +---@param timeout number # seconds to wait +---@return string reply # JSON data +---@overload fun() +function FServerPlayer:waitForReply(timeout)end + +--- Notice the client. +---@param command string +---@param jsonData string +function FServerPlayer:doNotify(command,jsonData)end diff --git a/lua/vscode/qt.lua b/lua/vscode/qt.lua new file mode 100644 index 00000000..3cc3b3ef --- /dev/null +++ b/lua/vscode/qt.lua @@ -0,0 +1,22 @@ +---@meta + +---@return number length +function SPlayerList:length()end + +---@param e freekill.ServerPlayer +function SPlayerList:append(e)end + +---@param e freekill.ServerPlayer +---@return boolean +function SPlayerList:contains(e)end + +---@param index number +---@return freekill.ServerPlayer | nil +function SPlayerList:at(index)end + +function SPlayerList:first()end +function SPlayerList:last()end +function SPlayerList:isEmpty()end +function SPlayerList:removeAt(index)end +function SPlayerList:removeAll()end +function SPlayerList:indexOf(e)end diff --git a/lua/vscode/server.lua b/lua/vscode/server.lua new file mode 100644 index 00000000..be029926 --- /dev/null +++ b/lua/vscode/server.lua @@ -0,0 +1,47 @@ +---@meta + +---@class freekill.Server +FServer = {} + +---@type freekill.Server +freekill.ServerInstance = {} + +---@class freekill.Room +--- Room (C++) +FRoom = {} + +---@param owner freekill.ServerPlayer +---@param name string +---@param capacity number +function FServer:createRoom(owner,name,capacity)end + +---@param id number +---@return freekill.Room room +function FServer:findRoom(id)end + +---@param id number +---@return freekill.ServerPlayer player +function FServer:findPlayer(id)end + +---@return freekill.SQLite3 db +function FServer:getDatabase()end + +function FRoom:getServer()end +function FRoom:getId()end +function FRoom:isLobby()end +function FRoom:getName()end +function FRoom:setName(name)end +function FRoom:getCapacity()end +function FRoom:setCapacity(capacity)end +function FRoom:isFull()end +function FRoom:isAbandoned()end +function FRoom:addPlayer(player)end +function FRoom:removePlayer(player)end +function FRoom:getOwner()end +function FRoom:setOwner(owner)end +function FRoom:getPlayers()end +function FRoom:findPlayer(id)end +function FRoom:getTimeout()end +function FRoom:isStarted()end +function FRoom:doBroadcastNotify(targets,command,jsonData)end +function FRoom:gameOver()end diff --git a/lua/vscode/sqlite.lua b/lua/vscode/sqlite.lua new file mode 100644 index 00000000..a53edab7 --- /dev/null +++ b/lua/vscode/sqlite.lua @@ -0,0 +1,20 @@ +---@meta + +---@class freekill.SQLite3 +SQLite3 = {} + +---@param filename string +---@return freekill.SQLite3 +function freekill.OpenDatabase(filename)end + +---@param db freekill.SQLite3 +---@param sql string +---@return string jsonData +function freekill.SelectFromDb(db, sql)end + +---@param db freekill.SQLite3 +---@param sql string +function freekill.ExecSQL(db, sql)end + +---@param db freekill.SQLite3 +function freekill.CloseDatabase(db)end