Skip to content

Commit

Permalink
[Change] Reorganize the package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
CornWorld committed Feb 7, 2024
1 parent 5295b31 commit 44e2969
Show file tree
Hide file tree
Showing 33 changed files with 388 additions and 464 deletions.
22 changes: 11 additions & 11 deletions packages/server/api/internal/command/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package command

import (
"encoding/json"
"server/game_logic"
gameDef "server/game_logic/game_def"
"server/game_logic/map"
"server/game"
"server/game/block"
"server/game/map"
"server/utils/pkg/data_source"
game_temp_pool "server/utils/pkg/game_temp_pool"
)
Expand All @@ -17,7 +17,7 @@ func ApplyDataSource(source any) {

type visibilityArr [][]bool

func getVisibility(id game_logic.Id, userId uint16) *visibilityArr {
func getVisibility(id game.Id, userId uint16) *visibilityArr {
m := data.GetCurrentMap(id)
ul := data.GetCurrentUserList(id)

Expand Down Expand Up @@ -62,7 +62,7 @@ func getVisibility(id game_logic.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(gameDef.Position{X: colNum + 1, Y: rowNum + 1}).OwnerId()
o := m.GetBlock(block.Position{X: colNum + 1, Y: rowNum + 1}).OwnerId()
if o == 0 {
continue
}
Expand All @@ -79,7 +79,7 @@ func getVisibility(id game_logic.Id, userId uint16) *visibilityArr {

type processedMap [][][]uint16

func getProcessedMap(id game_logic.Id, userId uint16, m *_map.Map) *processedMap {
func getProcessedMap(id game.Id, userId uint16, m *_map.Map) *processedMap {
vis := getVisibility(id, userId)
var ret *processedMap
if r, ok := game_temp_pool.Get(id, "processedMap"); ok {
Expand All @@ -93,7 +93,7 @@ func getProcessedMap(id game_logic.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(gameDef.Position{X: colNum + 1, Y: rowNum + 1})
b := m.GetBlock(block.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 All @@ -113,12 +113,12 @@ type playerInfo struct {
Status string `json:"status"`
}

func getUserList(id game_logic.Id) []playerInfo {
func getUserList(id game.Id) []playerInfo {
l := data.GetCurrentUserList(id)
ret := make([]playerInfo, len(l))
var status string
for i, u := range l {
if u.Status == gameDef.UserStatusConnected {
if u.Status == game.UserStatusConnected {
status = "connected"
} else {
status = "disconnect"
Expand All @@ -134,7 +134,7 @@ func getUserList(id game_logic.Id) []playerInfo {
return ret
}

func GenerateMessage(_type string, id game_logic.Id, userId uint16) string {
func GenerateMessage(_type string, id game.Id, userId uint16) string {
switch _type {
case "start":
{
Expand Down Expand Up @@ -172,7 +172,7 @@ func GenerateMessage(_type string, id game_logic.Id, userId uint16) string {
res := struct {
Action string `json:"action"`
Players []playerInfo `json:"players"`
Mode gameDef.Mode `json:"mode"`
Mode game.Mode `json:"mode"`
}{"info", getUserList(id), g.Mode}
ret, _ := json.Marshal(res)
return string(ret)
Expand Down
17 changes: 9 additions & 8 deletions packages/server/api/internal/command/pauser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package command
import (
"fmt"
"github.com/go-playground/validator/v10"
"server/game_logic/game_def"
"server/game/block"
"server/game/instruction"
"strconv"
"strings"
)

func PauseCommandStr(userId uint16, str string) (game_def.Instruction, error) {
func PauseCommandStr(userId uint16, str string) (instruction.Instruction, error) {
var err error = nil
ret := game_def.Instruction(nil)
ret := instruction.Instruction(nil)
args := strings.Split(str, " ")
v := validator.New()
switch args[0] {
Expand All @@ -28,9 +29,9 @@ func PauseCommandStr(userId uint16, str string) (game_def.Instruction, error) {
n, _ := strconv.Atoi(c.Number)
x, _ := strconv.Atoi(c.X)
y, _ := strconv.Atoi(c.Y)
ret = game_def.Move{
Position: game_def.Position{X: uint8(x), Y: uint8(y)},
Towards: game_def.MoveTowardsType(c.Towards),
ret = instruction.Move{
Position: block.Position{X: uint8(x), Y: uint8(y)},
Towards: instruction.MoveTowardsType(c.Towards),
Number: uint16(n),
}
} else {
Expand All @@ -51,7 +52,7 @@ func PauseCommandStr(userId uint16, str string) (game_def.Instruction, error) {
if c.Status == "true" {
s = true
}
ret = game_def.ForceStart{
ret = instruction.ForceStart{
UserId: userId,
Status: s,
}
Expand All @@ -66,7 +67,7 @@ func PauseCommandStr(userId uint16, str string) (game_def.Instruction, error) {
case "Surrender":
{
if len(args)-1 == 0 {
ret = game_def.Surrender{UserId: userId}
ret = instruction.Surrender{UserId: userId}
} else {
err = fmt.Errorf("argument number not right")
}
Expand Down
7 changes: 3 additions & 4 deletions packages/server/api/internal/receiver/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package receiver

import (
"context"
"server/game_logic"
"server/game_logic/game_def"
"server/game"
)

type Context struct {
context.Context
Game *game_logic.Game
User game_def.User
Game *game.Game
User game.User
Command chan string
Message chan string
}
Expand Down
45 changes: 22 additions & 23 deletions packages/server/api/internal/receiver/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"github.com/sirupsen/logrus"
"os"
_command "server/api/internal/command"
"server/game_logic"
"server/game_logic/game_def"
"server/game_logic/map"
judge_pool "server/judge_pool"
"server/game"
judge_pool "server/game/judge_pool"
"server/game/map"
"server/utils/pkg/data_source"
"strconv"
"strings"
Expand All @@ -31,13 +30,13 @@ func NewFileReceiver(pool *judge_pool.Pool) {
for index, r := range f {
time.Sleep(time.Millisecond * 200)
logrus.Infof("start game by reply file #%d", r.Id)
g := &game_logic.Game{
g := &game.Game{
Map: r.Map,
Mode: game_def.Mode1v1,
Id: game_logic.Id(index + 1e3),
UserList: []game_def.User{},
Mode: game.Mode1v1,
Id: game.Id(index + 1e3),
UserList: []game.User{},
CreateTime: time.Now().UnixMicro(),
Status: game_logic.StatusWaiting,
Status: game.StatusWaiting,
RoundNum: 0,
}
pool.DebugNewGame(g)
Expand All @@ -58,12 +57,12 @@ func NewFileReceiver(pool *judge_pool.Pool) {
}

type command struct {
User game_def.User
User game.User
Ins []string
}

type reply struct {
Id game_logic.Id
Id game.Id
UserPack []command
Map *_map.Map
}
Expand All @@ -88,7 +87,7 @@ func LoadFile() []reply {

id, _ := strconv.Atoi(s[0])
r := reply{
Id: game_logic.Id(id),
Id: game.Id(id),
UserPack: []command{},
}

Expand All @@ -112,10 +111,10 @@ func LoadFile() []reply {
cmdStr := strings.Split(t[1], "\n")

cmd := command{
User: game_def.User{
User: game.User{
Name: name,
UserId: uint16(userId),
Status: game_def.UserStatusConnected,
Status: game.UserStatusConnected,
TeamId: uint8(userId) - 1,
ForceStartStatus: false,
},
Expand Down Expand Up @@ -143,15 +142,15 @@ func fakePlayer(ctx *Context, c []string) {
case <-ticker.C:
{
g := data.GetGameInfo(ctx.Game.Id)
if g.Status == game_logic.StatusEnd {
if g.Status == game.StatusEnd {
return
}

if currentRound >= uint16(len(c)) {
playerLogger.Infof("Command(tot: %d) runs out, quit", len(c))
ticker.Stop()
player := ctx.User
player.Status = game_def.UserStatusDisconnected
player.Status = game.UserStatusDisconnected
data.SetUserStatus(ctx.Game.Id, player)
}
if ctx.Game.RoundNum > currentRound {
Expand Down Expand Up @@ -180,7 +179,7 @@ func fakePlayer(ctx *Context, c []string) {

func receiver(ctx *Context) {
//ctx.User.Name = strconv.Itoa(int(ctx.User.UserId)) // DEBUG ONLY, avoiding strange username from `gioreply` file
ctx.User.Status = game_def.UserStatusConnected
ctx.User.Status = game.UserStatusConnected
data.SetUserStatus(ctx.Game.Id, ctx.User)

receiverLogger := logrus.WithFields(logrus.Fields{
Expand All @@ -189,7 +188,7 @@ func receiver(ctx *Context) {
receiverLogger.Infof("user join")

defer func() {
ctx.User.Status = game_def.UserStatusDisconnected
ctx.User.Status = game.UserStatusDisconnected
data.SetUserStatus(ctx.Game.Id, ctx.User)
receiverLogger.Infof("user quit")
}()
Expand Down Expand Up @@ -218,7 +217,7 @@ func receiver(ctx *Context) {
}
case <-ticker.C:
{
done := func(g *game_logic.Game, d string) {
done := func(g *game.Game, d string) {
res := _command.GenerateMessage(d, ctx.Game.Id, ctx.User.UserId)
ctx.Message <- res
ctx.Game = g
Expand All @@ -227,11 +226,11 @@ func receiver(ctx *Context) {
// Check game status
g := data.GetGameInfo(ctx.Game.Id)
if g.Status != ctx.Game.Status {
if ctx.Game.Status == game_logic.StatusWaiting && g.Status == game_logic.StatusRunning {
if ctx.Game.Status == game.StatusWaiting && g.Status == game.StatusRunning {
done(g, "info")
done(g, "start")
continue
} else if g.Status == game_logic.StatusEnd {
} else if g.Status == game.StatusEnd {
done(g, "end")
ticker.Stop()
flag = false
Expand All @@ -240,12 +239,12 @@ func receiver(ctx *Context) {
} else {
if i%20 == 0 {
done(g, "info")
if ctx.Game.Status == game_logic.StatusWaiting {
if ctx.Game.Status == game.StatusWaiting {
done(g, "wait")
}
}

if ctx.Game.Status == game_logic.StatusRunning && ctx.Game.RoundNum != g.RoundNum {
if ctx.Game.Status == game.StatusRunning && ctx.Game.RoundNum != g.RoundNum {
done(g, "newTurn")
continue
}
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/judge_pool"
judge_pool "server/game/judge_pool"
"time"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package block

import (
"github.com/sirupsen/logrus"
"server/game_logic/game_def"
)

var _ game_def.Block = (*BaseBlock)(nil)
var _ Block = (*BaseBlock)(nil)

type BaseBlock struct {
ownerId uint16
number uint16
}

func (*BaseBlock) Meta() game_def.BlockMeta {
func (*BaseBlock) Meta() BlockMeta {
logrus.Panic("no block meta can be provided")
return game_def.BlockMeta{}
return BlockMeta{}
}

func (block *BaseBlock) Number() uint16 {
Expand All @@ -29,14 +28,14 @@ func (*BaseBlock) RoundStart(_ uint16) {}

func (*BaseBlock) RoundEnd(_ uint16) {}

func (*BaseBlock) GetMoveStatus() game_def.MoveStatus {
return game_def.MoveStatus{}
func (*BaseBlock) GetMoveStatus() MoveStatus {
return MoveStatus{}
}

func (*BaseBlock) MoveFrom(_ uint16) uint16 {
return 0
}

func (*BaseBlock) MoveTo(game_def.BlockVal) game_def.Block {
func (*BaseBlock) MoveTo(BlockVal) Block {
return nil
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package block

import (
"server/game_logic/game_def"
)

var _ game_def.Block = (*BaseBuilding)(nil)
var _ Block = (*BaseBuilding)(nil)

type BaseBuilding struct {
BaseBlock
Expand All @@ -20,8 +16,8 @@ func (block *BaseBuilding) RoundStart(_ uint16) {
}
}

func (*BaseBuilding) GetMoveStatus() game_def.MoveStatus {
return game_def.MoveStatus{AllowMoveFrom: true, AllowMoveTo: true}
func (*BaseBuilding) GetMoveStatus() MoveStatus {
return MoveStatus{AllowMoveFrom: true, AllowMoveTo: true}
}

func (block *BaseBuilding) MoveFrom(number uint16) uint16 {
Expand All @@ -36,7 +32,7 @@ func (block *BaseBuilding) MoveFrom(number uint16) uint16 {
return ret
}

func (block *BaseBuilding) MoveTo(info game_def.BlockVal) game_def.Block {
func (block *BaseBuilding) MoveTo(info BlockVal) Block {
if block.ownerId != info.OwnerId {
if block.number < info.Number {
block.ownerId = info.OwnerId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package game_def
package block

type BlockMeta struct {
Name string
Expand Down
Loading

0 comments on commit 44e2969

Please sign in to comment.