This commit is contained in:
Notify-ctrl 2022-03-30 16:33:56 +08:00
parent 7fd127b849
commit a85f510c98
16 changed files with 268 additions and 2 deletions

View File

@ -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") local Engine = class("Engine")
function Engine:initialize() function Engine:initialize()
@ -20,7 +28,7 @@ function Engine:initialize()
self:loadPackages() self:loadPackages()
end end
-- Package pack ---@param pack Package
function Engine:loadPackage(pack) function Engine:loadPackage(pack)
assert(pack:isInstanceOf(Package)) assert(pack:isInstanceOf(Package))
if self.packages[pack.name] ~= nil then if self.packages[pack.name] ~= nil then
@ -48,6 +56,7 @@ function Engine:loadPackages()
FileIO.cd("..") FileIO.cd("..")
end end
---@param t table
function Engine:loadTranslationTable(t) function Engine:loadTranslationTable(t)
assert(type(t) == "table") assert(type(t) == "table")
for k, v in pairs(t) do for k, v in pairs(t) do
@ -55,6 +64,7 @@ function Engine:loadTranslationTable(t)
end end
end end
---@param skill any
function Engine:addSkill(skill) function Engine:addSkill(skill)
assert(skill.class:isSubclassOf(Skill)) assert(skill.class:isSubclassOf(Skill))
if self.skills[skill.name] ~= nil then if self.skills[skill.name] ~= nil then
@ -63,6 +73,7 @@ function Engine:addSkill(skill)
self.skills[skill.name] = skill self.skills[skill.name] = skill
end end
---@param skills any[]
function Engine:addSkills(skills) function Engine:addSkills(skills)
assert(type(skills) == "table") assert(type(skills) == "table")
for _, skill in ipairs(skills) do for _, skill in ipairs(skills) do
@ -70,6 +81,7 @@ function Engine:addSkills(skills)
end end
end end
---@param general General
function Engine:addGeneral(general) function Engine:addGeneral(general)
assert(general:isInstanceOf(General)) assert(general:isInstanceOf(General))
if self.generals[general.name] ~= nil then if self.generals[general.name] ~= nil then
@ -78,6 +90,7 @@ function Engine:addGeneral(general)
self.generals[general.name] = general self.generals[general.name] = general
end end
---@param generals General[]
function Engine:addGenerals(generals) function Engine:addGenerals(generals)
assert(type(generals) == "table") assert(type(generals) == "table")
for _, general in ipairs(generals) do for _, general in ipairs(generals) do
@ -97,6 +110,11 @@ function Engine:addCards(cards)
end end
end end
---@param num number
---@param generalPool General[]
---@param except string[]
---@param filter function
---@return General[] generals
function Engine:getGeneralsRandomly(num, generalPool, except, filter) function Engine:getGeneralsRandomly(num, generalPool, except, filter)
if filter then if filter then
assert(type(filter) == "function") assert(type(filter) == "function")
@ -130,6 +148,8 @@ function Engine:getGeneralsRandomly(num, generalPool, except, filter)
return result return result
end end
---@param except General[]
---@return General[]
function Engine:getAllGenerals(except) function Engine:getAllGenerals(except)
local result = {} local result = {}
for _, general in ipairs(self.generals) do for _, general in ipairs(self.generals) do

View File

@ -1,3 +1,4 @@
--- @class General : Object
General = class("General") General = class("General")
-- enum Gender -- enum Gender
@ -18,6 +19,7 @@ function General:initialize(package, name, kingdom, hp, maxHp, gender, initialHp
self.other_skills = {} -- string[] self.other_skills = {} -- string[]
end end
---@param skill any
function General:addSkill(skill) function General:addSkill(skill)
if (type(skill) == "string") then if (type(skill) == "string") then
table.insert(self.other_skills, skill) table.insert(self.other_skills, skill)

View File

@ -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") local Package = class("Package")
-- enum Type -- enum Type
@ -5,7 +12,6 @@ Package.GeneralPack = 0
Package.CardPack = 1 Package.CardPack = 1
Package.SpecialPack = 2 Package.SpecialPack = 2
-- string name, Type type
function Package:initialize(name, _type) function Package:initialize(name, _type)
assert(type(name) == "string") assert(type(name) == "string")
assert(type(_type) == "nil" or type(_type) == "number") assert(type(_type) == "nil" or type(_type) == "number")
@ -20,6 +26,7 @@ function Package:initialize(name, _type)
self.cards = {} --> Card[] self.cards = {} --> Card[]
end end
---@return table skills
function Package:getSkills() function Package:getSkills()
local ret = {table.unpack(self.related_skills)} local ret = {table.unpack(self.related_skills)}
if self.type == Package.GeneralPack then if self.type == Package.GeneralPack then
@ -32,6 +39,7 @@ function Package:getSkills()
return ret return ret
end end
---@param general General
function Package:addGeneral(general) function Package:addGeneral(general)
assert(general.class and general:isInstanceOf(General)) assert(general.class and general:isInstanceOf(General))
table.insert(self.generals, general) table.insert(self.generals, general)

View File

@ -1,3 +1,4 @@
---@class Player : Object
local Player = class("Player") local Player = class("Player")
function Player:initialize() function Player:initialize()

View File

@ -29,16 +29,29 @@ function table:insertTable(list)
end end
end end
---@class Sql
Sql = { Sql = {
---@param filename string
open = function(filename) open = function(filename)
return freekill.OpenDatabase(filename) return freekill.OpenDatabase(filename)
end, end,
---@param db freekill.SQLite3
close = function(db) close = function(db)
freekill.CloseDatabase(db) freekill.CloseDatabase(db)
end, end,
--- Execute an SQL statement.
---@param db freekill.SQLite3
---@param sql string
exec = function(db, sql) exec = function(db, sql)
freekill.ExecSQL(db, sql) freekill.ExecSQL(db, sql)
end, end,
--- Execute a `SELECT` SQL statement.
---@param db freekill.SQLite3
---@param sql string
---@return table data # { [columnName] --> result : string[] }
exec_select = function(db, sql) exec_select = function(db, sql)
return json.decode(freekill.SelectFromDb(db, sql)) return json.decode(freekill.SelectFromDb(db, sql))
end, end,

View File

@ -5,8 +5,13 @@ package.path = package.path .. ";./lua/lib/?.lua"
.. ";./lua/?.lua" .. ";./lua/?.lua"
-- load libraries -- load libraries
---@type class
class = require "middleclass" class = require "middleclass"
---@type json
json = require "json" json = require "json"
require "sha256" require "sha256"
Util = require "core.util" Util = require "core.util"
math.randomseed(os.time()) math.randomseed(os.time())

View File

@ -1,3 +1,4 @@
---@class GameLogic: Object
local GameLogic = class("GameLogic") local GameLogic = class("GameLogic")
function GameLogic:initialize(room) function GameLogic:initialize(room)

View File

@ -1,3 +1,4 @@
---@class Room : Object
local Room = class("Room") local Room = class("Room")
function Room:initialize(_room) function Room:initialize(_room)
@ -13,6 +14,7 @@ function Room:run()
for _, p in freekill.qlist(self.room:getPlayers()) do for _, p in freekill.qlist(self.room:getPlayers()) do
local player = ServerPlayer:new(p) local player = ServerPlayer:new(p)
player.state = p:getStateString() player.state = p:getStateString()
player.room = self
table.insert(self.players, player) table.insert(self.players, player)
self.server.players[player:getId()] = player self.server.players[player:getId()] = player
end end

View File

@ -1,3 +1,4 @@
---@class Server : Object
Server = class('Server') Server = class('Server')
-- load server classes -- load server classes

View File

@ -1,8 +1,10 @@
---@class ServerPlayer : Player
local ServerPlayer = Player:subclass("ServerPlayer") local ServerPlayer = Player:subclass("ServerPlayer")
function ServerPlayer:initialize(_self) function ServerPlayer:initialize(_self)
Player.initialize(self) Player.initialize(self)
self.serverplayer = _self self.serverplayer = _self
self.room = nil
self.next = nil self.next = nil

25
lua/vscode/freekill.lua Normal file
View File

@ -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<ServerPlayer *>.
---@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

33
lua/vscode/lib.lua Normal file
View File

@ -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

64
lua/vscode/player.lua Normal file
View File

@ -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

22
lua/vscode/qt.lua Normal file
View File

@ -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

47
lua/vscode/server.lua Normal file
View File

@ -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

20
lua/vscode/sqlite.lua Normal file
View File

@ -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