From 23dd166006c49526d8c91edb854cff146e61f52f Mon Sep 17 00:00:00 2001 From: CornWorld Date: Wed, 7 Feb 2024 18:16:23 +0800 Subject: [PATCH] [Refactor] Rename packages' name --- packages/server/api/internal/receiver/file.go | 4 +-- packages/server/api/provider.go | 2 +- packages/server/game/block/base_block.go | 4 +-- packages/server/game/block/block.go | 4 +-- packages/server/game/block/block_blank.go | 4 +-- packages/server/game/block/block_castle.go | 4 +-- packages/server/game/block/block_king.go | 4 +-- packages/server/game/block/block_mountain.go | 4 +-- packages/server/game/block/block_soldier.go | 4 +-- packages/server/game/block/register.go | 8 ++--- packages/server/game/judge/mode1v1.go | 34 +++++++++++++++++++ packages/server/main.go | 7 ++-- packages/server/main_test.go | 8 ++--- .../utils/pkg/data_source/local/local.go | 2 +- 14 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 packages/server/game/judge/mode1v1.go diff --git a/packages/server/api/internal/receiver/file.go b/packages/server/api/internal/receiver/file.go index 3663423..8285355 100644 --- a/packages/server/api/internal/receiver/file.go +++ b/packages/server/api/internal/receiver/file.go @@ -6,7 +6,7 @@ import ( "os" _command "server/api/internal/command" "server/game" - judge_pool "server/game/judge_pool" + "server/game/judge" "server/game/map" "server/utils/pkg/data_source" "strconv" @@ -24,7 +24,7 @@ func ApplyDataSource(source any) { data = source.(data_source.TempDataSource) } -func NewFileReceiver(pool *judge_pool.Pool) { +func NewFileReceiver(pool *judge.Pool) { f := LoadFile() for index, r := range f { diff --git a/packages/server/api/provider.go b/packages/server/api/provider.go index b0054e6..ce78b6c 100644 --- a/packages/server/api/provider.go +++ b/packages/server/api/provider.go @@ -3,7 +3,7 @@ package api import ( "server/api/internal/command" "server/api/internal/receiver" - judge_pool "server/game/judge_pool" + judge_pool "server/game/judge" "time" ) diff --git a/packages/server/game/block/base_block.go b/packages/server/game/block/base_block.go index 798ee95..0a317db 100644 --- a/packages/server/game/block/base_block.go +++ b/packages/server/game/block/base_block.go @@ -11,9 +11,9 @@ type BaseBlock struct { number uint16 } -func (*BaseBlock) Meta() BlockMeta { +func (*BaseBlock) Meta() Meta { logrus.Panic("no block meta can be provided") - return BlockMeta{} + return Meta{} } func (block *BaseBlock) Number() uint16 { diff --git a/packages/server/game/block/block.go b/packages/server/game/block/block.go index 4b5cf19..5c8a80b 100644 --- a/packages/server/game/block/block.go +++ b/packages/server/game/block/block.go @@ -1,6 +1,6 @@ package block -type BlockMeta struct { +type Meta struct { Name string Description string BlockId uint8 @@ -27,7 +27,7 @@ type Block interface { // MoveTo Ret: a new block to replace this place MoveTo(Val) Block - Meta() BlockMeta + Meta() Meta } type Position struct{ X, Y uint8 } diff --git a/packages/server/game/block/block_blank.go b/packages/server/game/block/block_blank.go index d37abf6..eaea83a 100644 --- a/packages/server/game/block/block_blank.go +++ b/packages/server/game/block/block_blank.go @@ -6,7 +6,7 @@ type Blank struct { BaseBlock } -var BlankMeta = BlockMeta{ +var BlankMeta = Meta{ BlockId: 0, Name: "blank", Description: "", @@ -17,7 +17,7 @@ func init() { Register(BlankMeta, toBlockBlank) } -func (*Blank) Meta() BlockMeta { +func (*Blank) Meta() Meta { return BlankMeta } diff --git a/packages/server/game/block/block_castle.go b/packages/server/game/block/block_castle.go index fcba7e3..eabb215 100644 --- a/packages/server/game/block/block_castle.go +++ b/packages/server/game/block/block_castle.go @@ -10,7 +10,7 @@ type Castle struct { BaseBuilding } -var CastleMeta = BlockMeta{ +var CastleMeta = Meta{ BlockId: 3, Name: "castle", Description: "", @@ -32,6 +32,6 @@ func toBlockCastle(b Block) Block { return Block(&ret) } -func (*Castle) Meta() BlockMeta { +func (*Castle) Meta() Meta { return CastleMeta } diff --git a/packages/server/game/block/block_king.go b/packages/server/game/block/block_king.go index f723c8d..20e79ad 100644 --- a/packages/server/game/block/block_king.go +++ b/packages/server/game/block/block_king.go @@ -7,7 +7,7 @@ type King struct { originalOwnerId uint16 } -var KingMeta = BlockMeta{ +var KingMeta = Meta{ BlockId: 2, Name: "king", Description: "", @@ -30,7 +30,7 @@ func (block *King) IsDied() bool { return block.originalOwnerId != block.ownerId } -func (*King) Meta() BlockMeta { +func (*King) Meta() Meta { return KingMeta } diff --git a/packages/server/game/block/block_mountain.go b/packages/server/game/block/block_mountain.go index a8ead20..9a411e2 100644 --- a/packages/server/game/block/block_mountain.go +++ b/packages/server/game/block/block_mountain.go @@ -4,7 +4,7 @@ type Mountain struct { BaseBlock } -var MountainMeta = BlockMeta{ +var MountainMeta = Meta{ BlockId: 4, Name: "mountain", Description: "", @@ -19,7 +19,7 @@ func toBlockMountain(Block) Block { return Block(&Mountain{}) } -func (*Mountain) Meta() BlockMeta { +func (*Mountain) Meta() Meta { return MountainMeta } diff --git a/packages/server/game/block/block_soldier.go b/packages/server/game/block/block_soldier.go index d3939d5..30815df 100644 --- a/packages/server/game/block/block_soldier.go +++ b/packages/server/game/block/block_soldier.go @@ -6,7 +6,7 @@ type Soldier struct { BaseBlock } -var SoldierMeta = BlockMeta{ +var SoldierMeta = Meta{ BlockId: 1, Name: "soldier", Description: "", @@ -24,7 +24,7 @@ func toBlockSoldier(b Block) Block { return Block(&ret) } -func (*Soldier) Meta() BlockMeta { +func (*Soldier) Meta() Meta { return SoldierMeta } diff --git a/packages/server/game/block/register.go b/packages/server/game/block/register.go index 825f9c4..b9921c5 100644 --- a/packages/server/game/block/register.go +++ b/packages/server/game/block/register.go @@ -8,9 +8,9 @@ type tranFunc func(Block) Block var transBlockTypeFunc map[uint8]tranFunc var GetBlockIdByName map[string]uint8 -var GetMetaById map[uint8]BlockMeta +var GetMetaById map[uint8]Meta -func Register(meta BlockMeta, transFunc tranFunc) { +func Register(meta Meta, transFunc tranFunc) { if transBlockTypeFunc == nil { transBlockTypeFunc = make(map[uint8]tranFunc) } @@ -18,13 +18,13 @@ func Register(meta BlockMeta, transFunc tranFunc) { GetBlockIdByName = make(map[string]uint8) } if GetMetaById == nil { - GetMetaById = make(map[uint8]BlockMeta) + GetMetaById = make(map[uint8]Meta) } GetBlockIdByName[meta.Name] = meta.BlockId GetMetaById[meta.BlockId] = meta transBlockTypeFunc[meta.BlockId] = transFunc - logrus.Println("Registered a block game_def", "id:", meta.BlockId, " name:", meta.Name, " description:", meta.Description) + logrus.Println("Registered a block", "id:", meta.BlockId, "name:", meta.Name, "description:", meta.Description) } func ToBlockByTypeId(typeId uint8, block Block) Block { diff --git a/packages/server/game/judge/mode1v1.go b/packages/server/game/judge/mode1v1.go new file mode 100644 index 0000000..cf434e8 --- /dev/null +++ b/packages/server/game/judge/mode1v1.go @@ -0,0 +1,34 @@ +package judge + +import ( + "server/game" + "server/game/block" +) + +func judgeGameMode1v1(g *game.Game, kingPos []block.Position) game.Status { + flag := true + for _, k := range kingPos { + if g.Map.GetBlock(k).Meta().BlockId != block.KingMeta.BlockId { + flag = false + break + } + } + if !flag { + var w uint16 + for _, k := range kingPos { + if g.Map.GetBlock(k).Meta().BlockId == block.KingMeta.BlockId { + w = g.Map.GetBlock(k).OwnerId() + } + } + var wt uint8 + for _, u := range g.UserList { + if u.UserId == w { + wt = u.TeamId + break + } + } + g.Winner = wt + return game.StatusEnd + } + return game.StatusRunning +} diff --git a/packages/server/main.go b/packages/server/main.go index ed66767..cd30fe3 100644 --- a/packages/server/main.go +++ b/packages/server/main.go @@ -10,7 +10,7 @@ import ( "server/game" _ "server/game/block" "server/game/instruction" - "server/game/judge_pool" + "server/game/judge" "server/utils/pkg/data_source/local" db "server/utils/pkg/pg" "time" @@ -59,8 +59,9 @@ func main() { } data.OriginalMapStrPool[0] = "[\n[0,0,0,0,2],\n[0,2,0,0,0],\n[0,0,0,0,0],\n[0,3,3,0,3],\n[0,3,0,2,0]\n]" - judge_pool.ApplyDataSource(&data) - p := judge_pool.CreatePool([]game.Mode{game.Mode1v1}) + judge.ApplyDataSource(&data) + judge.ApplyDataSource(&data) + p := judge.CreatePool([]game.Mode{game.Mode1v1}) time.Sleep(200 * time.Millisecond) diff --git a/packages/server/main_test.go b/packages/server/main_test.go index 6714488..70b1d56 100644 --- a/packages/server/main_test.go +++ b/packages/server/main_test.go @@ -7,7 +7,7 @@ import ( "server/game" _ "server/game/block" "server/game/instruction" - judge_pool "server/game/judge_pool" + "server/game/judge" "server/utils/pkg/data_source/local" "testing" "time" @@ -26,8 +26,8 @@ func TestServer_main(t *testing.T) { } data.OriginalMapStrPool[0] = "[\n[0,0,0,0,2],\n[0,2,0,0,0],\n[0,0,0,0,0],\n[0,3,3,0,3],\n[0,3,0,2,0]\n]" - judge_pool.ApplyDataSource(&data) - p := judge_pool.CreatePool([]game.Mode{game.Mode1v1}) + judge.ApplyDataSource(&data) + p := judge.CreatePool([]game.Mode{game.Mode1v1}) time.Sleep(200 * time.Millisecond) @@ -54,6 +54,6 @@ func TestServer_main(t *testing.T) { } } if userList[0] != "test2" { - t.Fatalf("game result is unexpected: expected [\"2\"], got %v (team %d)", userList, data.GamePool[id].Winner) + t.Fatalf("game result is unexpected: expected [test2], got %v (team %d)", userList, data.GamePool[id].Winner) } } diff --git a/packages/server/utils/pkg/data_source/local/local.go b/packages/server/utils/pkg/data_source/local/local.go index 2d97d27..56f0f6c 100644 --- a/packages/server/utils/pkg/data_source/local/local.go +++ b/packages/server/utils/pkg/data_source/local/local.go @@ -96,7 +96,7 @@ func (l *Local) GetGameInfo(id game.Id) *game.Game { defer l.unlock() g := *l.GamePool[id] g.UserList = nil - g.Map = _map.CreateMapWithInfo(g.Map.Id(), g.Map.Size()) + g.Map = _map.CreateEmptyMapWithInfo(g.Map.Id(), g.Map.Size()) return &g } else { return nil