Gamecore (#3)

* create some class

* create some base classes

* fixup

Co-authored-by: Ho-spair <linyuy@163.com>
This commit is contained in:
Notify-ctrl 2022-03-25 12:28:07 +08:00 committed by Notify-ctrl
parent dea1c40ad8
commit 3dc95ebc49
13 changed files with 276 additions and 47 deletions

View File

@ -0,0 +1 @@

View File

@ -1,52 +1,37 @@
-- class Card : public Object
local Card = class('Card')
-- public:
-- enum Suit
Card.Suit = {
Spade = 0,
Club = 1,
Heart = 2,
Diamond = 3,
NoSuitBlack = 4,
NoSuitRed = 5,
NoSuit = 6,
SuitToBeDecided = -1,
local SUITS = {
"Spade",
"Club",
"Diamond",
"Heart",
"NonSuit",
}
-- enum Color
Card.Color = {
Red = 0,
Black = 1,
Colorless = 2,
CardSuit = Util:createEnum(SUITS)
local COLOR = {
"Red",
"Black",
"NonColor",
}
-- enum HandlingMethod
Card.HandlingMethod = {
MethodNone = 0,
MethodUse = 1,
MethodResponse = 2,
MethodDiscard = 3,
MethodRecast = 4,
MethodPindian = 5,
}
function Card:initialize(suit, number)
CardColor = Util:createEnum(COLOR)
local Card = class("Card")
function Card:initialize(name, suit, cardNumber)
self.name = name
self.suit = suit
self.cardNumber = cardNumber
end
-- private:
local subcards = {} -- array of cards
local target_fixed
local mute
local will_throw
local has_preact
local can_recast
local m_suit
local m_number
local id
local skill_name
local handling_method
local flags = {}
function Card:getColor()
if self.suit == CardSuit.Spade or self.suit == CardSuit.Club then
return CardColor.Red
elseif self.suit == CardSuit.Diamond or self.suit == CardSuit.Heart then
return CardColor.Black
else
return CardColor.NonColor
end
end
return Card

65
lua/core/engine.lua Normal file
View File

@ -0,0 +1,65 @@
local Sanguosha = class("Engine")
function Sanguosha:initialize()
self.skills = {}
self.generals = {}
self.cards = {}
end
function Sanguosha:addSkill(skill)
table.insert(self.skills, skill)
end
function Sanguosha:addGeneral(general)
table.insert(self.generals, general)
end
function Sanguosha:addCard(card)
table.insert(self.cards, cards)
end
function Sanguosha:getGeneralsRandomly(num, generalPool, except, filter)
if filter then
assert(type(filter) == "function")
end
generalPool = generalPool or self.generals
except = except or {}
local availableGenerals = {}
for _, general in ipairs(generalPool) do
if not table.contains(except, general) and not (filter and filter(general)) then
table.insert(availableGenerals, general)
end
end
if #availableGenerals == 0 then
return {}
end
local result = {}
while num > 0 do
local randomGeneral = math.random(1, #availableGenerals)
table.insert(result, randomGeneral)
table.remove(availableGenerals, randomGeneral)
if #availableGenerals == 0 then
break
end
end
return result
end
function Sanguosha:getAllGenerals(except)
local result = {}
for _, general in ipairs(self.generals) do
if not (except and table.contains(except, general)) then
table.insert(result, general)
end
end
return result
end
return Sanguosha

19
lua/core/general.lua Normal file
View File

@ -0,0 +1,19 @@
General = class("General")
function General:initialize(package, name, kingdom, hp, maxHp, gender, initialHp)
self.package = package
self.name = name
self.kingdom = kingdom
self.hp = hp
self.maxHp = maxHp
self.gender = gender
self.initialHp = initialHp or maxHp
self.skills = {}
end
function General:addSkill(skill)
table.insert(self.skills, skill)
end
return General

27
lua/core/player.lua Normal file
View File

@ -0,0 +1,27 @@
local Player = class("Skill")
function Player:initialize()
self.hp = nil
self.maxHp = nil
self.general = nil
self.dying = false
self.dead = false
self.playerSkills = {}
end
function Player:setGeneral(general, setHp, addSkills)
self.general = general
if setHp then
self.maxHp = general.maxHp
self.hp = general.initialHp
end
if addSkills then
table.insertTable(self.playerSkills, general.skills)
end
end
function Player:setHp(maxHp, initialHp)
self.maxHp = maxHp
self.hp = initialHp or maxHp
end

32
lua/core/skill.lua Normal file
View File

@ -0,0 +1,32 @@
local SKILL_TYPE = {
"Common",
"Frequent",
"Compulsory",
"Awaken",
"Limit",
"Lord",
}
SkillType = Util:createEnum(SKILL_TYPE)
local Skill = class("Skill")
function Skill:initialize(name, skillType)
self.name = name
self.description = ":" .. name
self.skillType = skillType
end
local TriggerSkill = class("TriggerSkill", Skill)
function TriggerSkill:initialize(spec)
Skill.initialize(self, spec.name, spec.skillType)
self.isRefreshAt = spec.isRefreshAt
self.isTriggerable = spec.isTriggerable
self.targetFilter = spec.targetFilter
self.cardFilter = spec.cardFilter
self.beforeTrigger = spec.beforeTrigger
self.onTrigger = spec.onTrigger
end
return Skill

View File

@ -1,11 +1,13 @@
-- Fundemental script for FreeKill
-- Load mods, init the engine, etc.
package.path = package.path .. ';./lua/lib/?.lua'
package.path = package.path .. ";./lua/lib/?.lua"
.. ";./lua/core/?.lua"
-- load libraries
class = require 'middleclass'
json = require 'json'
class = require "middleclass"
json = require "json"
Util = require "util"
DebugMode = true
@ -16,3 +18,9 @@ function pt(t)
end
-- load core classes
Sanguosha = require "engine"
General = require "general"
Card = require "card"
Skill = require "skill"
-- load packages

16
lua/server/event.lua Normal file
View File

@ -0,0 +1,16 @@
local EVENTS = {
"GameStart",
"PhaseChanging",
"PhaseStart",
"PhaseProceeding",
"PhaseEnd",
"PreCardUse",
"AfterCardUseDeclared",
"AfterCardTargetDeclared",
"CardUsing",
"CardUseFinished",
}
GameEvent = Util:createEnum(EVENTS)

23
lua/server/gamelogic.lua Normal file
View File

@ -0,0 +1,23 @@
function logic()
chooseGeneral()
initSkillList()
actionNormal()
end
function chooseGeneral()
for _, p in ipairs(room:getPlayers()) do
local g = p:askForGeneral()
room:changeHero(p, g)
end
end
function actionNormal()
local p = room:getLord()
while true do
room:setCurrent(p)
act(room:getCurrent)
p = p:getNextAlive()
end
end
function trigger() end

18
lua/server/room.lua Normal file
View File

@ -0,0 +1,18 @@
Room = class("Room")
-- Just same as "static int roomId" in cpp
-- However id 0 is for lobby, so we start at 1
local roomId = 1
function Room:initialize()
self.id = roomId
roomId = roomId + 1
self.room = ServerInstace:findRoom(self.id)
end
function Room:getCProperties()
self.name = self.room:getName()
self.capacity = self.room:getCapacity()
end
return Room

View File

@ -1,4 +1,4 @@
local Server = class('Server')
local Server = class("Server")
freekill.server_callback = {}
@ -12,6 +12,13 @@ function Server:initialize()
print("Server error: Unknown command " .. command);
end
end
self.rooms = {} -- hashtable: uid --> room
self.players = {} -- hashtable: uid --> splayer
end
function Server:createRoom(owner, roomName, capacity)
end
freekill.server_callback["CreateRoom"] = function(jsonData)
@ -21,6 +28,7 @@ freekill.server_callback["CreateRoom"] = function(jsonData)
local roomName = data[2]
local capacity = data[3]
freekill.ServerInstance:createRoom(owner, roomName, capacity)
ServerInstance:createRoom()
end
freekill.server_callback["EnterRoom"] = function(jsonData)

View File

@ -0,0 +1 @@

26
lua/util.lua Normal file
View File

@ -0,0 +1,26 @@
function table:contains(element)
if #self == 0 or type(self[1]) ~= type(element) then return false end
for _, e in ipairs(self) do
if e == element then return true end
end
end
function table:insertTable(list)
for _, e in ipairs(list) do
table.insert(self, e)
end
end
local Util = class("Util")
function Util.static:createEnum(tbl, index)
assert(type(tbl) == "table")
local enumtbl = {}
local enumindex = index or 0
for i, v in ipairs(tbl) do
enumtbl[v] = enumindex + i
end
return enumtbl
end
return Util