diff --git a/packages/server/api/internal/command/message.go b/packages/server/api/internal/command/message.go index 514b5de..030149c 100644 --- a/packages/server/api/internal/command/message.go +++ b/packages/server/api/internal/command/message.go @@ -6,7 +6,7 @@ import ( "server/utils/pkg/game" game_temp_pool "server/utils/pkg/gametemppool" "server/utils/pkg/map" - "server/utils/pkg/map/block" + "server/utils/pkg/map/type" ) var data datasource.TempDataSource @@ -62,7 +62,7 @@ func getVisibility(id game.Id, userId uint16) *visibilityArr { for rowNum := uint8(0); rowNum <= m.Size().H-1; rowNum++ { for colNum := uint8(0); colNum <= m.Size().W-1; colNum++ { - o := m.GetBlock(block.Position{X: colNum + 1, Y: rowNum + 1}).OwnerId() + o := m.GetBlock(_type.Position{X: colNum + 1, Y: rowNum + 1}).OwnerId() if o == 0 { continue } @@ -93,7 +93,7 @@ func getProcessedMap(id game.Id, userId uint16, m *_map.Map) *processedMap { for rowNum := uint8(0); rowNum <= m.Size().H-1; rowNum++ { (*ret)[rowNum] = make([][]uint16, m.Size().W) for colNum := uint8(0); colNum <= m.Size().W-1; colNum++ { - b := m.GetBlock(block.Position{X: colNum + 1, Y: rowNum + 1}) + b := m.GetBlock(_type.Position{X: colNum + 1, Y: rowNum + 1}) if (*vis)[rowNum][colNum] { (*ret)[rowNum][colNum] = []uint16{uint16(b.Meta().BlockId), b.OwnerId(), b.Number()} } else { diff --git a/packages/server/judgepool/internal/judge/judge.go b/packages/server/judgepool/internal/judge/judge.go index 3255259..ef9f98d 100644 --- a/packages/server/judgepool/internal/judge/judge.go +++ b/packages/server/judgepool/internal/judge/judge.go @@ -7,7 +7,9 @@ import ( game_temp_pool "server/utils/pkg/gametemppool" "server/utils/pkg/instruction" "server/utils/pkg/map" - "server/utils/pkg/map/block" + "server/utils/pkg/map/blockManager" + "server/utils/pkg/map/blockManager/block" + "server/utils/pkg/map/type" "time" ) @@ -116,7 +118,7 @@ func judgeWorking(j *GameJudge) { g.Map.RoundStart(g.RoundNum) data.SetGameMap(j.gameId, g.Map) - _map.DebugOutput(g.Map, func(block block.Block) uint16 { + _map.DebugOutput(g.Map, func(block _type.Block) uint16 { return uint16(block.Meta().BlockId) }) // TODO } @@ -124,13 +126,13 @@ func judgeWorking(j *GameJudge) { } } -func getKingPos(g *game.Game) []block.Position { - var kingPos []block.Position +func getKingPos(g *game.Game) []_type.Position { + var kingPos []_type.Position for y := uint8(1); y <= g.Map.Size().H; y++ { for x := uint8(1); x <= g.Map.Size().W; x++ { - b := g.Map.GetBlock(block.Position{X: x, Y: y}) + b := g.Map.GetBlock(_type.Position{X: x, Y: y}) if b.Meta().BlockId == block.KingMeta.BlockId { - kingPos = append(kingPos, block.Position{X: x, Y: y}) + kingPos = append(kingPos, _type.Position{X: x, Y: y}) } } } @@ -138,7 +140,7 @@ func getKingPos(g *game.Game) []block.Position { } // judgeGame TODO: Add unit test -func judgeGame(g *game.Game, kingPos []block.Position) game.Status { +func judgeGame(g *game.Game, kingPos []_type.Position) game.Status { // Check online player number onlinePlayerNum := uint8(0) for _, u := range g.UserList { @@ -191,7 +193,7 @@ func judgeGame(g *game.Game, kingPos []block.Position) game.Status { return game.StatusRunning } -func allocateKing(g *game.Game, kingPos []block.Position) { +func allocateKing(g *game.Game, kingPos []_type.Position) { allocatableKingNum := 0 for _, k := range kingPos { if g.Map.GetBlock(k).OwnerId() == 0 { @@ -205,7 +207,7 @@ func allocateKing(g *game.Game, kingPos []block.Position) { break } g.Map.SetBlock(kingPos[i], - block.NewBlock(block.KingMeta.BlockId, g.Map.GetBlock(kingPos[i]).Number(), u.UserId)) + blockManager.NewBlock(block.KingMeta.BlockId, g.Map.GetBlock(kingPos[i]).Number(), u.UserId)) allocatableKingNum-- } } diff --git a/packages/server/main.go b/packages/server/main.go index 4e0b45b..029bec7 100644 --- a/packages/server/main.go +++ b/packages/server/main.go @@ -11,7 +11,7 @@ import ( "server/utils/pkg/datasource/local" "server/utils/pkg/game" "server/utils/pkg/instruction" - _ "server/utils/pkg/map/block" + _ "server/utils/pkg/map/blockManager/block" db "server/utils/pkg/pg" "time" ) @@ -30,6 +30,11 @@ const defaultConfigOptions = ` ` func main() { + logrus.SetLevel(logrus.TraceLevel) + logrus.SetFormatter(&nested.Formatter{ + TimestampFormat: time.RFC3339, + }) + flag.StringVar(&configFile, "config", defaultConfigPath, "config file path") flag.Parse() @@ -47,11 +52,6 @@ func main() { ctx, exit := context.WithCancel(context.Background()) defer exit() - logrus.SetLevel(logrus.TraceLevel) - logrus.SetFormatter(&nested.Formatter{ - TimestampFormat: time.RFC3339, - }) - data := local.Local{ GamePool: make(map[game.Id]*game.Game), OriginalMapStrPool: make(map[uint32]string), diff --git a/packages/server/utils/pkg/datasource/local/local_test.go b/packages/server/utils/pkg/datasource/local/local_test.go index c4f2a52..e8192b8 100644 --- a/packages/server/utils/pkg/datasource/local/local_test.go +++ b/packages/server/utils/pkg/datasource/local/local_test.go @@ -4,7 +4,7 @@ import ( "server/utils/pkg/game" "server/utils/pkg/instruction" "server/utils/pkg/map" - "server/utils/pkg/map/block" + "server/utils/pkg/map/type" "strconv" "testing" ) @@ -185,8 +185,8 @@ func TestLocal_Map(t *testing.T) { if a.Size() == b.Size() && a.Id() == b.Id() { for y := uint8(1); y <= a.Size().H; y++ { for x := uint8(1); x <= a.Size().W; x++ { - blockA := a.GetBlock(block.Position{x, y}) - blockB := b.GetBlock(block.Position{x, y}) + blockA := a.GetBlock(_type.Position{x, y}) + blockB := b.GetBlock(_type.Position{x, y}) if blockA != blockB { ret = false break diff --git a/packages/server/utils/pkg/datasource/pg/pg.go b/packages/server/utils/pkg/datasource/pg/pg.go index 30bfc98..1b95b9b 100644 --- a/packages/server/utils/pkg/datasource/pg/pg.go +++ b/packages/server/utils/pkg/datasource/pg/pg.go @@ -8,7 +8,7 @@ import ( "server/utils/pkg/game" "server/utils/pkg/instruction" _map "server/utils/pkg/map" - "server/utils/pkg/map/block" + "server/utils/pkg/map/type" db "server/utils/pkg/pg" ) @@ -45,7 +45,7 @@ func generatorMapJson(m *_map.Map) string { for y := uint8(1); y <= m.Size().H; y++ { ret.Blocks[y] = make([]b, m.Size().W) for x := uint8(1); x <= m.Size().W; x++ { - ob := m.GetBlock(block.Position{X: x, Y: y}) + ob := m.GetBlock(_type.Position{X: x, Y: y}) ret.Blocks[y][x] = b{ TypeId: ob.Meta().BlockId, OwnerId: ob.OwnerId(), diff --git a/packages/server/utils/pkg/map/block/baseBuilding.go b/packages/server/utils/pkg/map/block/baseBuilding.go deleted file mode 100644 index 09ba693..0000000 --- a/packages/server/utils/pkg/map/block/baseBuilding.go +++ /dev/null @@ -1,39 +0,0 @@ -package block - -var _ Block = (*BaseBuilding)(nil) - -type BaseBuilding struct { - BaseBlock -} - -func (block *BaseBuilding) Number() uint16 { - return block.number -} - -func (block *BaseBuilding) RoundStart(_ uint16) { - if block.OwnerId() != 0 { - block.number += 1 - } -} - -func (*BaseBuilding) GetMoveStatus() MoveStatus { - return MoveStatus{true, true} -} - -func (block *BaseBuilding) MoveFrom(number uint16) { - block.number -= number -} - -func (block *BaseBuilding) MoveTo(ownerId uint16, number uint16) Block { - if block.ownerId != ownerId { - if block.number < number { - block.ownerId = ownerId - block.number = number - block.number - } else { - block.number -= number - } - } else { - block.number += number - } - return nil -} diff --git a/packages/server/utils/pkg/map/block/blockBlank.go b/packages/server/utils/pkg/map/block/blockBlank.go deleted file mode 100644 index 81ad9b4..0000000 --- a/packages/server/utils/pkg/map/block/blockBlank.go +++ /dev/null @@ -1,34 +0,0 @@ -package block - -var _ Block = (*Blank)(nil) - -type Blank struct { - BaseBlock -} - -var BlankMeta = Meta{ - BlockId: 0, - Name: "blank", - Description: "", - VisitFallBackType: 0, -} - -func init() { - RegisterBlockType(BlankMeta, toBlockBlank) -} - -func (*Blank) Meta() Meta { - return BlankMeta -} - -func (*Blank) GetMoveStatus() MoveStatus { - return MoveStatus{false, true} -} - -func toBlockBlank(_ uint16, _ uint16) Block { - return Block(&Blank{}) -} - -func (*Blank) MoveTo(ownerId uint16, number uint16) Block { - return toBlockSoldier(number, ownerId) -} diff --git a/packages/server/utils/pkg/map/block/blockCastle.go b/packages/server/utils/pkg/map/block/blockCastle.go deleted file mode 100644 index b1bfffc..0000000 --- a/packages/server/utils/pkg/map/block/blockCastle.go +++ /dev/null @@ -1,37 +0,0 @@ -package block - -import ( - "math/rand" -) - -var _ Block = (*Castle)(nil) - -type Castle struct { - BaseBuilding -} - -var CastleMeta = Meta{ - BlockId: 3, - Name: "castle", - Description: "", - VisitFallBackType: 3, -} - -func init() { - RegisterBlockType(CastleMeta, toBlockCastle) -} - -func toBlockCastle(number uint16, ownerId uint16) Block { - var ret Castle - if number == 0 { - ret.number = uint16(30) + uint16(rand.Intn(30)) - } else { - ret.number = number - } - ret.ownerId = ownerId - return Block(&ret) -} - -func (*Castle) Meta() Meta { - return CastleMeta -} diff --git a/packages/server/utils/pkg/map/block/blockKing.go b/packages/server/utils/pkg/map/block/blockKing.go deleted file mode 100644 index 45ba862..0000000 --- a/packages/server/utils/pkg/map/block/blockKing.go +++ /dev/null @@ -1,45 +0,0 @@ -package block - -var _ Block = (*King)(nil) - -type King struct { - BaseBuilding - originalOwnerId uint16 -} - -var KingMeta = Meta{ - BlockId: 2, - Name: "king", - Description: "", - VisitFallBackType: CastleMeta.BlockId, -} - -func init() { - RegisterBlockType(KingMeta, toBlockKing) -} - -func toBlockKing(number uint16, ownerId uint16) Block { - var ret King - ret.number = number - ret.ownerId = ownerId - ret.originalOwnerId = ownerId - return Block(&ret) -} - -func (block *King) IsDied() bool { - return block.originalOwnerId != block.ownerId -} - -func (*King) Meta() Meta { - return KingMeta -} - -func (block *King) MoveTo(ownerId uint16, number uint16) Block { - if !block.IsDied() { - block.BaseBuilding.MoveTo(ownerId, number) - } - if block.IsDied() { - return toBlockCastle(block.number, ownerId) - } - return nil -} diff --git a/packages/server/utils/pkg/map/block/blockMountain.go b/packages/server/utils/pkg/map/block/blockMountain.go deleted file mode 100644 index 3d0a0f5..0000000 --- a/packages/server/utils/pkg/map/block/blockMountain.go +++ /dev/null @@ -1,28 +0,0 @@ -package block - -type Mountain struct { - BaseBlock -} - -var MountainMeta = Meta{ - BlockId: 4, - Name: "mountain", - Description: "", - VisitFallBackType: 4, -} - -func init() { - RegisterBlockType(MountainMeta, toBlockMountain) -} - -func toBlockMountain(number uint16, ownerId uint16) Block { - return Block(&Mountain{}) -} - -func (*Mountain) Meta() Meta { - return MountainMeta -} - -func (*Mountain) GetMoveStatus() MoveStatus { // same as `BaseBlock`'s, in order to remain the function - return MoveStatus{false, false} -} diff --git a/packages/server/utils/pkg/map/block/blockSoldier.go b/packages/server/utils/pkg/map/block/blockSoldier.go deleted file mode 100644 index 3e837f4..0000000 --- a/packages/server/utils/pkg/map/block/blockSoldier.go +++ /dev/null @@ -1,61 +0,0 @@ -package block - -type Soldier struct { - BaseBlock -} - -var SoldierMeta = Meta{ - BlockId: 1, - Name: "soldier", - Description: "", - VisitFallBackType: BlankMeta.BlockId, -} - -func init() { - RegisterBlockType(SoldierMeta, toBlockSoldier) -} - -func toBlockSoldier(number uint16, ownerId uint16) Block { - var ret Soldier - ret.number = number - ret.ownerId = ownerId - return Block(&ret) -} - -func (*Soldier) Meta() Meta { - return SoldierMeta -} - -func (block *Soldier) Number() uint16 { - return block.number -} - -func (block *Soldier) RoundStart(roundNum uint16) { - if (roundNum%25)-1 == 0 && roundNum != 1 { - block.number += 1 - } -} - -func (*Soldier) GetMoveStatus() MoveStatus { - return MoveStatus{true, true} -} - -func (block *Soldier) MoveFrom(number uint16) { - block.number -= number -} - -func (block *Soldier) MoveTo(ownerId uint16, number uint16) Block { - - if block.ownerId != ownerId { - if block.number < number { - block.ownerId = ownerId - block.number = number - block.number - } else { - block.number -= number - } - } else { - block.number += number - } - return nil - -} diff --git a/packages/server/utils/pkg/map/block/register.go b/packages/server/utils/pkg/map/block/register.go deleted file mode 100644 index e52066c..0000000 --- a/packages/server/utils/pkg/map/block/register.go +++ /dev/null @@ -1,30 +0,0 @@ -package block - -import ( - "log" -) - -type tranFunc func(number uint16, ownerId uint16) Block - -var transBlockTypeFunc map[uint8]tranFunc - -func RegisterBlockType(meta Meta, transFunc tranFunc) { - if transBlockTypeFunc == nil { - transBlockTypeFunc = make(map[uint8]tranFunc) - } - transBlockTypeFunc[meta.BlockId] = transFunc - log.Println("[Info] Registered a block type", "id:", meta.BlockId, " name:", meta.Name, " description:", meta.Description) -} - -func ToBlockByTypeId(typeId uint8, block Block) Block { - transFunc, err := transBlockTypeFunc[typeId] - if !err { - log.Println("[Warn] Get an unknown blockTypeId", typeId) - transFunc = transBlockTypeFunc[0] // Note: Must ensure blocks.BlockBlankMeta.BlockId=0 - } - return transFunc(block.Number(), block.OwnerId()) -} - -func NewBlock(typeId uint8, number uint16, ownerId uint16) Block { - return ToBlockByTypeId(typeId, &BaseBlock{ownerId, number}) -} diff --git a/packages/server/utils/pkg/map/blockManager/baseBlock.go b/packages/server/utils/pkg/map/blockManager/baseBlock.go new file mode 100644 index 0000000..36e91d1 --- /dev/null +++ b/packages/server/utils/pkg/map/blockManager/baseBlock.go @@ -0,0 +1,44 @@ +/* + Create block only +*/ + +package blockManager + +import ( + "github.com/sirupsen/logrus" + _type "server/utils/pkg/map/type" +) + +var _ _type.Block = (*BaseBlock)(nil) + +type BaseBlock struct { + ownerId uint16 + number uint16 +} + +func (*BaseBlock) Meta() _type.Meta { + logrus.Panic("no block meta can be provided") + return _type.Meta{} +} + +func (block *BaseBlock) Number() uint16 { + return block.number +} + +func (block *BaseBlock) OwnerId() uint16 { + return block.ownerId +} + +func (*BaseBlock) RoundStart(_ uint16) {} + +func (*BaseBlock) RoundEnd(_ uint16) {} + +func (*BaseBlock) GetMoveStatus() _type.MoveStatus { + return _type.MoveStatus{} +} + +func (*BaseBlock) MoveFrom(_ uint16) {} + +func (*BaseBlock) MoveTo(_type.BlockVal) _type.Block { + return nil +} diff --git a/packages/server/utils/pkg/map/block/baseBlock.go b/packages/server/utils/pkg/map/blockManager/block/baseBlock.go similarity index 50% rename from packages/server/utils/pkg/map/block/baseBlock.go rename to packages/server/utils/pkg/map/blockManager/block/baseBlock.go index 05e4ff6..ab1ea18 100644 --- a/packages/server/utils/pkg/map/block/baseBlock.go +++ b/packages/server/utils/pkg/map/blockManager/block/baseBlock.go @@ -1,14 +1,20 @@ package block -var _ Block = (*BaseBlock)(nil) +import ( + "github.com/sirupsen/logrus" + "server/utils/pkg/map/type" +) + +var _ _type.Block = (*BaseBlock)(nil) type BaseBlock struct { ownerId uint16 number uint16 } -func (block *BaseBlock) Meta() Meta { - panic("no block meta can be provided") +func (*BaseBlock) Meta() _type.Meta { + logrus.Panic("no block meta can be provided") + return _type.Meta{} } func (block *BaseBlock) Number() uint16 { @@ -23,12 +29,12 @@ func (*BaseBlock) RoundStart(_ uint16) {} func (*BaseBlock) RoundEnd(_ uint16) {} -func (*BaseBlock) GetMoveStatus() MoveStatus { - return MoveStatus{false, false} +func (*BaseBlock) GetMoveStatus() _type.MoveStatus { + return _type.MoveStatus{} } func (*BaseBlock) MoveFrom(_ uint16) {} -func (*BaseBlock) MoveTo(_ uint16, _ uint16) Block { +func (*BaseBlock) MoveTo(_type.BlockVal) _type.Block { return nil } diff --git a/packages/server/utils/pkg/map/blockManager/block/baseBuilding.go b/packages/server/utils/pkg/map/blockManager/block/baseBuilding.go new file mode 100644 index 0000000..5ca1350 --- /dev/null +++ b/packages/server/utils/pkg/map/blockManager/block/baseBuilding.go @@ -0,0 +1,41 @@ +package block + +import "server/utils/pkg/map/type" + +var _ _type.Block = (*BaseBuilding)(nil) + +type BaseBuilding struct { + BaseBlock +} + +func (block *BaseBuilding) Number() uint16 { + return block.number +} + +func (block *BaseBuilding) RoundStart(_ uint16) { + if block.OwnerId() != 0 { + block.number += 1 + } +} + +func (*BaseBuilding) GetMoveStatus() _type.MoveStatus { + return _type.MoveStatus{AllowMoveFrom: true, AllowMoveTo: true} +} + +func (block *BaseBuilding) MoveFrom(number uint16) { + block.number -= number +} + +func (block *BaseBuilding) MoveTo(info _type.BlockVal) _type.Block { + if block.ownerId != info.OwnerId { + if block.number < info.Number { + block.ownerId = info.OwnerId + block.number = info.Number - block.number + } else { + block.number -= info.Number + } + } else { + block.number += info.Number + } + return nil +} diff --git a/packages/server/utils/pkg/map/blockManager/block/blockBlank.go b/packages/server/utils/pkg/map/blockManager/block/blockBlank.go new file mode 100644 index 0000000..5ac4896 --- /dev/null +++ b/packages/server/utils/pkg/map/blockManager/block/blockBlank.go @@ -0,0 +1,39 @@ +package block + +import ( + "server/utils/pkg/map/blockManager" + "server/utils/pkg/map/type" +) + +var _ _type.Block = (*Blank)(nil) + +type Blank struct { + BaseBlock +} + +var BlankMeta = _type.Meta{ + BlockId: 0, + Name: "blank", + Description: "", + VisitFallBackType: 0, +} + +func init() { + blockManager.Register(BlankMeta, toBlockBlank) +} + +func (*Blank) Meta() _type.Meta { + return BlankMeta +} + +func (*Blank) GetMoveStatus() _type.MoveStatus { + return _type.MoveStatus{false, true} +} + +func toBlockBlank(_type.Block) _type.Block { + return _type.Block(&Blank{}) +} + +func (b *Blank) MoveTo(_type.BlockVal) _type.Block { + return toBlockSoldier(b) +} diff --git a/packages/server/utils/pkg/map/blockManager/block/blockCastle.go b/packages/server/utils/pkg/map/blockManager/block/blockCastle.go new file mode 100644 index 0000000..ddd0bea --- /dev/null +++ b/packages/server/utils/pkg/map/blockManager/block/blockCastle.go @@ -0,0 +1,39 @@ +package block + +import ( + "math/rand" + "server/utils/pkg/map/blockManager" + "server/utils/pkg/map/type" +) + +var _ _type.Block = (*Castle)(nil) + +type Castle struct { + BaseBuilding +} + +var CastleMeta = _type.Meta{ + BlockId: 3, + Name: "castle", + Description: "", + VisitFallBackType: 3, +} + +func init() { + blockManager.Register(CastleMeta, toBlockCastle) +} + +func toBlockCastle(b _type.Block) _type.Block { + var ret Castle + if b.Number() == 0 { + ret.number = uint16(30) + uint16(rand.Intn(30)) + } else { + ret.number = b.Number() + } + ret.ownerId = b.OwnerId() + return _type.Block(&ret) +} + +func (*Castle) Meta() _type.Meta { + return CastleMeta +} diff --git a/packages/server/utils/pkg/map/blockManager/block/blockKing.go b/packages/server/utils/pkg/map/blockManager/block/blockKing.go new file mode 100644 index 0000000..ab68990 --- /dev/null +++ b/packages/server/utils/pkg/map/blockManager/block/blockKing.go @@ -0,0 +1,50 @@ +package block + +import ( + "server/utils/pkg/map/blockManager" + "server/utils/pkg/map/type" +) + +var _ _type.Block = (*King)(nil) + +type King struct { + BaseBuilding + originalOwnerId uint16 +} + +var KingMeta = _type.Meta{ + BlockId: 2, + Name: "king", + Description: "", + VisitFallBackType: CastleMeta.BlockId, +} + +func init() { + blockManager.Register(KingMeta, toBlockKing) +} + +func toBlockKing(b _type.Block) _type.Block { + var ret King + ret.number = b.Number() + ret.ownerId = b.OwnerId() + ret.originalOwnerId = b.OwnerId() + return _type.Block(&ret) +} + +func (block *King) IsDied() bool { + return block.originalOwnerId != block.ownerId +} + +func (*King) Meta() _type.Meta { + return KingMeta +} + +func (block *King) MoveTo(info _type.BlockVal) _type.Block { + if !block.IsDied() { + block.BaseBuilding.MoveTo(info) + } + if block.IsDied() { + return toBlockCastle(block) + } + return nil +} diff --git a/packages/server/utils/pkg/map/blockManager/block/blockMountain.go b/packages/server/utils/pkg/map/blockManager/block/blockMountain.go new file mode 100644 index 0000000..60973e9 --- /dev/null +++ b/packages/server/utils/pkg/map/blockManager/block/blockMountain.go @@ -0,0 +1,33 @@ +package block + +import ( + "server/utils/pkg/map/blockManager" + "server/utils/pkg/map/type" +) + +type Mountain struct { + BaseBlock +} + +var MountainMeta = _type.Meta{ + BlockId: 4, + Name: "mountain", + Description: "", + VisitFallBackType: 4, +} + +func init() { + blockManager.Register(MountainMeta, toBlockMountain) +} + +func toBlockMountain(_type.Block) _type.Block { + return _type.Block(&Mountain{}) +} + +func (*Mountain) Meta() _type.Meta { + return MountainMeta +} + +func (*Mountain) GetMoveStatus() _type.MoveStatus { // same as `BaseBlock`'s, in order to remain the function + return _type.MoveStatus{false, false} +} diff --git a/packages/server/utils/pkg/map/blockManager/block/blockSoldier.go b/packages/server/utils/pkg/map/blockManager/block/blockSoldier.go new file mode 100644 index 0000000..3abab48 --- /dev/null +++ b/packages/server/utils/pkg/map/blockManager/block/blockSoldier.go @@ -0,0 +1,66 @@ +package block + +import ( + "server/utils/pkg/map/blockManager" + "server/utils/pkg/map/type" +) + +type Soldier struct { + BaseBlock +} + +var SoldierMeta = _type.Meta{ + BlockId: 1, + Name: "soldier", + Description: "", + VisitFallBackType: BlankMeta.BlockId, +} + +func init() { + blockManager.Register(SoldierMeta, toBlockSoldier) +} + +func toBlockSoldier(b _type.Block) _type.Block { + var ret Soldier + ret.number = b.Number() + ret.ownerId = b.OwnerId() + return _type.Block(&ret) +} + +func (*Soldier) Meta() _type.Meta { + return SoldierMeta +} + +func (block *Soldier) Number() uint16 { + return block.number +} + +func (block *Soldier) RoundStart(roundNum uint16) { + if (roundNum%25)-1 == 0 && roundNum != 1 { + block.number += 1 + } +} + +func (*Soldier) GetMoveStatus() _type.MoveStatus { + return _type.MoveStatus{true, true} +} + +func (block *Soldier) MoveFrom(number uint16) { + block.number -= number +} + +func (block *Soldier) MoveTo(info _type.BlockVal) _type.Block { + + if block.ownerId != info.OwnerId { + if block.number < info.Number { + block.ownerId = info.OwnerId + block.number = info.Number - block.number + } else { + block.number -= info.Number + } + } else { + block.number += info.Number + } + return nil + +} diff --git a/packages/server/utils/pkg/map/blockManager/register.go b/packages/server/utils/pkg/map/blockManager/register.go new file mode 100644 index 0000000..980d9e9 --- /dev/null +++ b/packages/server/utils/pkg/map/blockManager/register.go @@ -0,0 +1,32 @@ +package blockManager + +import ( + "github.com/sirupsen/logrus" + "server/utils/pkg/map/type" +) + +type tranFunc func(_type.Block) _type.Block + +var transBlockTypeFunc map[uint8]tranFunc + +func Register(meta _type.Meta, transFunc tranFunc) { + if transBlockTypeFunc == nil { + transBlockTypeFunc = make(map[uint8]tranFunc) + } + + transBlockTypeFunc[meta.BlockId] = transFunc + logrus.Println("Registered a block type", "id:", meta.BlockId, " name:", meta.Name, " description:", meta.Description) +} + +func ToBlockByTypeId(typeId uint8, block _type.Block) _type.Block { + transFunc, err := transBlockTypeFunc[typeId] + if !err { + logrus.Warningln("Get an unknown blockTypeId", typeId) + transFunc = transBlockTypeFunc[0] // Note: Must ensure blocks.BlockBlankMeta.BlockId=0 + } + return transFunc(block) +} + +func NewBlock(typeId uint8, number uint16, ownerId uint16) _type.Block { + return ToBlockByTypeId(typeId, &BaseBlock{number: number, ownerId: ownerId}) +} diff --git a/packages/server/utils/pkg/map/map.go b/packages/server/utils/pkg/map/map.go index b74ef7c..05698b0 100644 --- a/packages/server/utils/pkg/map/map.go +++ b/packages/server/utils/pkg/map/map.go @@ -4,7 +4,8 @@ import ( "encoding/json" "github.com/sirupsen/logrus" "server/utils/pkg/instruction" - "server/utils/pkg/map/block" + "server/utils/pkg/map/blockManager" + "server/utils/pkg/map/type" "strconv" ) @@ -16,7 +17,7 @@ type mapInfo struct { } type Map struct { - blocks [][]block.Block + blocks [][]_type.Block mapInfo } @@ -28,11 +29,11 @@ func (p *Map) Id() uint32 { return p.id } -func (p *Map) GetBlock(position block.Position) block.Block { +func (p *Map) GetBlock(position _type.Position) _type.Block { return p.blocks[position.Y-1][position.X-1] } -func (p *Map) SetBlock(position block.Position, block block.Block) { +func (p *Map) SetBlock(position _type.Position, block _type.Block) { p.blocks[position.Y-1][position.X-1] = block } @@ -70,7 +71,7 @@ func CreateMapWithInfo(mapId uint32, size MapSize) *Map { } } -func DebugOutput(p *Map, f func(block.Block) uint16) { // Only for debugging +func DebugOutput(p *Map, f func(_type.Block) uint16) { // Only for debugging tmp := "" ex := func(i uint16) string { ex := "" @@ -95,7 +96,7 @@ func DebugOutput(p *Map, f func(block.Block) uint16) { // Only for debugging logrus.Tracef("\n%s\n", tmp) } -func isPositionLegal(position block.Position, size MapSize) bool { +func isPositionLegal(position _type.Position, size MapSize) bool { return 1 <= position.X && position.X <= size.W && 1 <= position.Y && position.Y <= size.H } @@ -124,12 +125,12 @@ func (p *Map) Move(ins instruction.Move) bool { } } - instructionPosition := block.Position{X: ins.Position.X, Y: ins.Position.Y} + instructionPosition := _type.Position{X: ins.Position.X, Y: ins.Position.Y} if !isPositionLegal(instructionPosition, p.size) { return false } - newPosition := block.Position{X: uint8(int(ins.Position.X) + offsetX), Y: uint8(int(ins.Position.Y) + offsetY)} + newPosition := _type.Position{X: uint8(int(ins.Position.X) + offsetX), Y: uint8(int(ins.Position.Y) + offsetY)} // It won't overflow 'cause the min value is 0 if !isPositionLegal(newPosition, p.size) { return false @@ -157,9 +158,9 @@ func (p *Map) Move(ins instruction.Move) bool { return false } - var toBlockNew block.Block + var toBlockNew _type.Block thisBlock.MoveFrom(ins.Number) - toBlockNew = toBlock.MoveTo(thisBlock.OwnerId(), ins.Number) + toBlockNew = toBlock.MoveTo(_type.BlockVal{Number: thisBlock.OwnerId(), OwnerId: ins.Number}) if toBlockNew != nil { p.SetBlock(newPosition, toBlockNew) } @@ -174,11 +175,11 @@ func Str2GameMap(mapId uint32, originalMapStr string) *Map { return nil } size := MapSize{W: uint8(len(result[0])), H: uint8(len(result))} - ret := make([][]block.Block, size.H) + ret := make([][]_type.Block, size.H) for rowNum, row := range result { - ret[rowNum] = make([]block.Block, size.W) + ret[rowNum] = make([]_type.Block, size.W) for colNum, typeId := range row { - ret[rowNum][colNum] = block.NewBlock(typeId, 0, 0) + ret[rowNum][colNum] = blockManager.NewBlock(typeId, 0, 0) } } return &Map{ @@ -194,15 +195,15 @@ func FullStr2GameMap(mapId uint32, originalMapStr string) *Map { return nil } size := MapSize{W: uint8(len(result[0])), H: uint8(len(result))} - ret := make([][]block.Block, size.H) + ret := make([][]_type.Block, size.H) for rowNum, row := range result { - ret[rowNum] = make([]block.Block, size.W) + ret[rowNum] = make([]_type.Block, size.W) for colNum, blockInfo := range row { blockId := blockInfo[0] ownerId := blockInfo[1] number := blockInfo[2] - newBlock := block.NewBlock(uint8(blockId), number, ownerId) + newBlock := blockManager.NewBlock(uint8(blockId), number, ownerId) ret[rowNum][colNum] = newBlock } diff --git a/packages/server/utils/pkg/map/block/block.go b/packages/server/utils/pkg/map/type/block.go similarity index 84% rename from packages/server/utils/pkg/map/block/block.go rename to packages/server/utils/pkg/map/type/block.go index b3237a9..0b2ad93 100644 --- a/packages/server/utils/pkg/map/block/block.go +++ b/packages/server/utils/pkg/map/type/block.go @@ -1,4 +1,4 @@ -package block +package _type type Meta struct { Name string @@ -25,9 +25,14 @@ type Block interface { GetMoveStatus() MoveStatus MoveFrom(number uint16) // MoveTo Ret: a new block to replace this place - MoveTo(ownerId uint16, number uint16) Block + MoveTo(BlockVal) Block Meta() Meta } type Position struct{ X, Y uint8 } + +type BlockVal struct { + Number uint16 + OwnerId uint16 +}