Skip to content

Commit

Permalink
[Feat] Add fog of war generating test units
Browse files Browse the repository at this point in the history
  • Loading branch information
CornWorld committed Feb 8, 2024
1 parent e691cf8 commit 530f5e4
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 111 deletions.
107 changes: 4 additions & 103 deletions packages/server/game/game_opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,105 +8,6 @@ import (
"testing"
)

//func Test_allocateKing(t *testing.T) {
// type args struct {
// ctx *judge.gameContext
// }
//
// tests := []struct {
// name string
// args args
// wantKings int // Expected number of Kings after allocation
// }{
// {
// name: "All users already have a King",
// args: args{
// ctx: &judge.gameContext{
// g: &Game{
// UserList: []user.User{{UserId: 1}, {UserId: 2}},
// Map: _map.CreateMapWithBlocks(0, [][]block.Block{
// {
// block.NewBlock(block.KingMeta.BlockId, 0, 1),
// block.NewBlock(block.KingMeta.BlockId, 0, 2),
// }}),
// },
// kingPos: []block.Position{{X: 1, Y: 1}, {X: 2, Y: 1}},
// },
// },
// wantKings: 2,
// },
// {
// name: "Some users do not have a King",
// args: args{
// ctx: &judge.gameContext{
// g: &Game{
// UserList: []user.User{{UserId: 1}, {UserId: 2}},
// Map: _map.CreateMapWithBlocks(0, [][]block.Block{
// {
// block.NewBlock(block.KingMeta.BlockId, 0, 0),
// block.NewBlock(block.KingMeta.BlockId, 0, 2),
// },
// }),
// },
// kingPos: []block.Position{{X: 1, Y: 1}, {X: 2, Y: 1}},
// },
// },
// wantKings: 2,
// },
// {
// name: "No users have a King",
// args: args{
// ctx: &judge.gameContext{
// g: &Game{
// UserList: []user.User{{UserId: 1}, {UserId: 2}, {UserId: 3}},
// Map: _map.CreateMapWithBlocks(0, [][]block.Block{
// {
// block.NewBlock(block.KingMeta.BlockId, 0, 0),
// block.NewBlock(block.KingMeta.BlockId, 0, 0),
// block.NewBlock(block.KingMeta.BlockId, 0, 0),
// },
// }),
// },
// kingPos: []block.Position{{X: 1, Y: 1}, {X: 2, Y: 1}, {X: 3, Y: 1}},
// },
// },
// wantKings: 3,
// },
// {
// name: "Transform King to Castle",
// args: args{
// ctx: &judge.gameContext{
// g: &Game{
// UserList: []user.User{{UserId: 1}, {UserId: 2}},
// Map: _map.CreateMapWithBlocks(0, [][]block.Block{
// {
// block.NewBlock(block.KingMeta.BlockId, 0, 0),
// block.NewBlock(block.KingMeta.BlockId, 0, 0),
// block.NewBlock(block.KingMeta.BlockId, 0, 0),
// },
// }),
// },
// kingPos: []block.Position{{X: 1, Y: 1}, {X: 2, Y: 1}, {X: 3, Y: 1}},
// },
// },
// wantKings: 2,
// },
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// allocateKing(tt.args.ctx)
// gotKings := len(tt.args.ctx.kingPos)
// if gotKings != tt.wantKings {
// t.Errorf("allocateKing() = %v, want %v", gotKings, tt.wantKings)
// }
// realGetKings := len(getKingPos(tt.args.ctx.g))
// if realGetKings != tt.wantKings {
// t.Errorf("allocateKing() real = %v, want %v", realGetKings, tt.wantKings)
// }
// })
// }
//}

func TestGame_allocateKing(t *testing.T) {
tests := []struct {
name string
Expand All @@ -117,7 +18,7 @@ func TestGame_allocateKing(t *testing.T) {
name: "All users already have a King",
g: &Game{
UserList: []user.User{{UserId: 1}, {UserId: 2}},
Map: _map.CreateMapWithBlocks(0, [][]block.Block{
Map: _map.New(0, [][]block.Block{
{
block.NewBlock(block.KingMeta.BlockId, 0, 1),
block.NewBlock(block.KingMeta.BlockId, 0, 2),
Expand All @@ -130,7 +31,7 @@ func TestGame_allocateKing(t *testing.T) {
name: "Some users do not have a King",
g: &Game{
UserList: []user.User{{UserId: 1}, {UserId: 2}},
Map: _map.CreateMapWithBlocks(0, [][]block.Block{
Map: _map.New(0, [][]block.Block{
{
block.NewBlock(block.KingMeta.BlockId, 0, 0),
block.NewBlock(block.KingMeta.BlockId, 0, 2),
Expand All @@ -144,7 +45,7 @@ func TestGame_allocateKing(t *testing.T) {
name: "No users have a King",
g: &Game{
UserList: []user.User{{UserId: 1}, {UserId: 2}, {UserId: 3}},
Map: _map.CreateMapWithBlocks(0, [][]block.Block{
Map: _map.New(0, [][]block.Block{
{
block.NewBlock(block.KingMeta.BlockId, 0, 0),
block.NewBlock(block.KingMeta.BlockId, 0, 0),
Expand All @@ -158,7 +59,7 @@ func TestGame_allocateKing(t *testing.T) {
name: "Transform King to Castle",
g: &Game{
UserList: []user.User{{UserId: 1}, {UserId: 2}},
Map: _map.CreateMapWithBlocks(0, [][]block.Block{
Map: _map.New(0, [][]block.Block{
{
block.NewBlock(block.KingMeta.BlockId, 0, 0),
block.NewBlock(block.KingMeta.BlockId, 0, 0),
Expand Down
2 changes: 1 addition & 1 deletion packages/server/game/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (l *Local) GetGameInfo(id Id) *Game {
defer l.unlock()
g := *l.GamePool[id]
g.UserList = nil
g.Map = _map.CreateEmptyMapWithInfo(g.Map.Id(), g.Map.Size())
g.Map = _map.NewWithoutBlocks(g.Map.Id(), g.Map.Size())
return &g
} else {
return nil
Expand Down
5 changes: 3 additions & 2 deletions packages/server/game/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (m *Map) RoundEnd(roundNum uint16) {
}
}

func CreateEmptyMapWithInfo(mapId uint32, size MapSize) *Map {
func NewWithoutBlocks(mapId uint32, size MapSize) *Map {
return &Map{
Blocks: nil,
mapInfo: mapInfo{
Expand All @@ -70,7 +70,7 @@ func CreateEmptyMapWithInfo(mapId uint32, size MapSize) *Map {
}
}

func CreateMapWithBlocks(mapId uint32, blocks [][]block.Block) *Map {
func New(mapId uint32, blocks [][]block.Block) *Map {
return &Map{
Blocks: blocks,
mapInfo: mapInfo{
Expand Down Expand Up @@ -136,6 +136,7 @@ func (m *Map) getVisibilityInFog(gameId uint16, teamId uint8, userList []user.Us
lightOffset := []struct {
x, y int
}{{0, 1}, {0, -1}, {-1, 0}, {1, 0}}
(*ret)[y][x] = true
for _, r := range lightOffset {
ly := y + r.y
lx := x + r.x
Expand Down
125 changes: 120 additions & 5 deletions packages/server/game/map/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"server/game/block"
_ "server/game/block"
"server/game/user"
"testing"
)

Expand All @@ -22,7 +23,7 @@ func TestConvJsonStrMap(t *testing.T) {
args: args{
jsonStr: `{"mappings":{"block":["blank"]},"game_def":[[0,0,0]]}`,
},
want: CreateMapWithBlocks(0, [][]block.Block{
want: New(0, [][]block.Block{
{
&block.Blank{},
&block.Blank{},
Expand All @@ -35,7 +36,7 @@ func TestConvJsonStrMap(t *testing.T) {
args: args{
jsonStr: `{"mappings":{"block":["blank"],"owner":[1,2]},"game_def":[[0,0,0]]}`,
},
want: CreateMapWithBlocks(0, [][]block.Block{
want: New(0, [][]block.Block{
{
&block.Blank{},
&block.Blank{},
Expand All @@ -48,7 +49,7 @@ func TestConvJsonStrMap(t *testing.T) {
args: args{
jsonStr: `{"mappings":{"block":["blank"]},"game_def":[[0,0,0]],"owner":[[1,1,1]]}`,
},
want: CreateMapWithBlocks(0, [][]block.Block{
want: New(0, [][]block.Block{
{
&block.Blank{},
&block.Blank{},
Expand All @@ -61,7 +62,7 @@ func TestConvJsonStrMap(t *testing.T) {
args: args{
jsonStr: `{"mappings":{"block":["soldier"],"owner":[1]},"game_def":[[0,0,0]],"owner":[[1,1,1]]}`,
},
want: CreateMapWithBlocks(0, [][]block.Block{
want: New(0, [][]block.Block{
{
block.NewBlock(1, 0, 1),
block.NewBlock(1, 0, 1),
Expand All @@ -74,7 +75,7 @@ func TestConvJsonStrMap(t *testing.T) {
args: args{
jsonStr: `{"mappings":{"block":["soldier"],"owner":[1]},"game_def":[[0,0,0]],"owner":[[1,1,1]],"number":[[1,2,255]]}`,
},
want: CreateMapWithBlocks(0, [][]block.Block{
want: New(0, [][]block.Block{
{
block.NewBlock(1, 1, 1),
block.NewBlock(1, 2, 1),
Expand Down Expand Up @@ -102,3 +103,117 @@ func TestConvJsonStrMap(t *testing.T) {
})
}
}

func TestMap_WarFog(t *testing.T) {
type args struct {
gameId uint16
teamId uint8
userList []user.User
}
otherBlock := block.NewBlock(block.SoldierMeta.BlockId, 111, 3)
soldierBlock := block.NewBlock(block.SoldierMeta.BlockId, 10, 1)
otherBlockData := []uint16{1, 3, 111}
soldierBlockData := []uint16{1, 1, 10}
blockDisallowedData := []uint16{0, 0, 0}
map9x9center := New(0, [][]block.Block{
{otherBlock, otherBlock, otherBlock},
{otherBlock, soldierBlock, otherBlock},
{otherBlock, otherBlock, otherBlock},
})
tests1 := []struct {
name string
m *Map
args args
want *visibilityInFog
}{
{name: "9x9(center) block owner", m: map9x9center, args: args{gameId: 0, teamId: 1, userList: []user.User{{UserId: 1, TeamId: 1}}},
want: &visibilityInFog{
{false, true, false},
{true, true, true},
{false, true, false},
},
},
{name: "9x9(center) team member", m: map9x9center, args: args{gameId: 0, teamId: 1, userList: []user.User{{UserId: 1, TeamId: 1}, {UserId: 2, TeamId: 1}}},
want: &visibilityInFog{
{false, true, false},
{true, true, true},
{false, true, false},
},
},
{name: "9x9(center) others", m: map9x9center, args: args{gameId: 0, teamId: 2, userList: []user.User{{UserId: 1, TeamId: 1}, {UserId: 2, TeamId: 2}}},
want: &visibilityInFog{
{false, false, false},
{false, false, false},
{false, false, false},
},
},
{name: "9x9(corner) block owner",
m: New(0, [][]block.Block{
{soldierBlock, otherBlock, otherBlock},
{otherBlock, otherBlock, otherBlock},
{otherBlock, otherBlock, soldierBlock},
}),
args: args{gameId: 0, teamId: 1, userList: []user.User{{UserId: 1, TeamId: 1}}},
want: &visibilityInFog{
{true, true, false},
{true, false, true},
{false, true, true},
},
},
}
for _, tt := range tests1 {
t.Run("visibilityInFog "+tt.name, func(t *testing.T) {
if got := tt.m.getVisibilityInFog(tt.args.gameId, tt.args.teamId, tt.args.userList); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getVisibilityInFog() = %v, want %v", got, tt.want)
}
})
}

tests2 := []struct {
name string
m *Map
args args
want *mapWithFog
}{
{name: "9x9(center) block owner", m: map9x9center, args: args{gameId: 0, teamId: 1, userList: []user.User{{UserId: 1, TeamId: 1}}},
want: &mapWithFog{
{blockDisallowedData, otherBlockData, blockDisallowedData},
{otherBlockData, soldierBlockData, otherBlockData},
{blockDisallowedData, otherBlockData, blockDisallowedData},
},
},
{name: "9x9(center) team member", m: map9x9center, args: args{gameId: 0, teamId: 1, userList: []user.User{{UserId: 1, TeamId: 1}, {UserId: 2, TeamId: 1}}},
want: &mapWithFog{
{blockDisallowedData, otherBlockData, blockDisallowedData},
{otherBlockData, soldierBlockData, otherBlockData},
{blockDisallowedData, otherBlockData, blockDisallowedData},
},
},
{name: "9x9(center) others", m: map9x9center, args: args{gameId: 0, teamId: 2, userList: []user.User{{UserId: 1, TeamId: 1}, {UserId: 2, TeamId: 2}}},
want: &mapWithFog{
{blockDisallowedData, blockDisallowedData, blockDisallowedData},
{blockDisallowedData, blockDisallowedData, blockDisallowedData},
{blockDisallowedData, blockDisallowedData, blockDisallowedData},
},
},
{name: "9x9(corner) block owner", args: args{gameId: 0, teamId: 1, userList: []user.User{{UserId: 1, TeamId: 1}}},
m: New(0, [][]block.Block{
{soldierBlock, otherBlock, otherBlock},
{otherBlock, otherBlock, otherBlock},
{otherBlock, otherBlock, soldierBlock},
}),
want: &mapWithFog{
{soldierBlockData, otherBlockData, blockDisallowedData},
{otherBlockData, blockDisallowedData, otherBlockData},
{blockDisallowedData, otherBlockData, soldierBlockData},
},
},
}
for _, tt := range tests2 {
t.Run("GetProcessedMap "+tt.name, func(t *testing.T) {
if got := tt.m.GetProcessedMap(tt.args.gameId, tt.args.teamId, tt.args.userList); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetProcessedMap() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 530f5e4

Please sign in to comment.