Skip to content

Commit

Permalink
[Refactor] Rename packages' name
Browse files Browse the repository at this point in the history
  • Loading branch information
CornWorld committed Feb 7, 2024
1 parent eaccb28 commit 23dd166
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 29 deletions.
4 changes: 2 additions & 2 deletions packages/server/api/internal/receiver/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/api/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
4 changes: 2 additions & 2 deletions packages/server/game/block/base_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions packages/server/game/block/block.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package block

type BlockMeta struct {
type Meta struct {
Name string
Description string
BlockId uint8
Expand All @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions packages/server/game/block/block_blank.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Blank struct {
BaseBlock
}

var BlankMeta = BlockMeta{
var BlankMeta = Meta{
BlockId: 0,
Name: "blank",
Description: "",
Expand All @@ -17,7 +17,7 @@ func init() {
Register(BlankMeta, toBlockBlank)
}

func (*Blank) Meta() BlockMeta {
func (*Blank) Meta() Meta {
return BlankMeta
}

Expand Down
4 changes: 2 additions & 2 deletions packages/server/game/block/block_castle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Castle struct {
BaseBuilding
}

var CastleMeta = BlockMeta{
var CastleMeta = Meta{
BlockId: 3,
Name: "castle",
Description: "",
Expand All @@ -32,6 +32,6 @@ func toBlockCastle(b Block) Block {
return Block(&ret)
}

func (*Castle) Meta() BlockMeta {
func (*Castle) Meta() Meta {
return CastleMeta
}
4 changes: 2 additions & 2 deletions packages/server/game/block/block_king.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type King struct {
originalOwnerId uint16
}

var KingMeta = BlockMeta{
var KingMeta = Meta{
BlockId: 2,
Name: "king",
Description: "",
Expand All @@ -30,7 +30,7 @@ func (block *King) IsDied() bool {
return block.originalOwnerId != block.ownerId
}

func (*King) Meta() BlockMeta {
func (*King) Meta() Meta {
return KingMeta
}

Expand Down
4 changes: 2 additions & 2 deletions packages/server/game/block/block_mountain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Mountain struct {
BaseBlock
}

var MountainMeta = BlockMeta{
var MountainMeta = Meta{
BlockId: 4,
Name: "mountain",
Description: "",
Expand All @@ -19,7 +19,7 @@ func toBlockMountain(Block) Block {
return Block(&Mountain{})
}

func (*Mountain) Meta() BlockMeta {
func (*Mountain) Meta() Meta {
return MountainMeta
}

Expand Down
4 changes: 2 additions & 2 deletions packages/server/game/block/block_soldier.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Soldier struct {
BaseBlock
}

var SoldierMeta = BlockMeta{
var SoldierMeta = Meta{
BlockId: 1,
Name: "soldier",
Description: "",
Expand All @@ -24,7 +24,7 @@ func toBlockSoldier(b Block) Block {
return Block(&ret)
}

func (*Soldier) Meta() BlockMeta {
func (*Soldier) Meta() Meta {
return SoldierMeta
}

Expand Down
8 changes: 4 additions & 4 deletions packages/server/game/block/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ 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)
}
if GetBlockIdByName == nil {
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 {
Expand Down
34 changes: 34 additions & 0 deletions packages/server/game/judge/mode1v1.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 4 additions & 3 deletions packages/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions packages/server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion packages/server/utils/pkg/data_source/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 23dd166

Please sign in to comment.