Skip to content

Commit

Permalink
[Refactor] Extract block management code into a separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
CornWorld committed Jan 27, 2024
1 parent 857cc52 commit 644b073
Show file tree
Hide file tree
Showing 23 changed files with 405 additions and 321 deletions.
6 changes: 3 additions & 3 deletions packages/server/api/internal/command/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 11 additions & 9 deletions packages/server/judgepool/internal/judge/judge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -116,29 +118,29 @@ 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
}
}
}
}

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})
}
}
}
return kingPos
}

// 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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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--
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()

Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions packages/server/utils/pkg/datasource/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/server/utils/pkg/datasource/pg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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(),
Expand Down
39 changes: 0 additions & 39 deletions packages/server/utils/pkg/map/block/baseBuilding.go

This file was deleted.

34 changes: 0 additions & 34 deletions packages/server/utils/pkg/map/block/blockBlank.go

This file was deleted.

37 changes: 0 additions & 37 deletions packages/server/utils/pkg/map/block/blockCastle.go

This file was deleted.

45 changes: 0 additions & 45 deletions packages/server/utils/pkg/map/block/blockKing.go

This file was deleted.

28 changes: 0 additions & 28 deletions packages/server/utils/pkg/map/block/blockMountain.go

This file was deleted.

Loading

0 comments on commit 644b073

Please sign in to comment.