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 5, 2024
1 parent 3cad58b commit fc185fa
Show file tree
Hide file tree
Showing 34 changed files with 380 additions and 341 deletions.
30 changes: 15 additions & 15 deletions packages/server/api/internal/command/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package command

import (
"encoding/json"
"server/utils/pkg/datasource"
"server/utils/pkg/game"
game_temp_pool "server/utils/pkg/gametemppool"
"server/utils/pkg/map"
"server/utils/pkg/map/type"
"server/game_logic"
gameDef "server/game_logic/game_def"
"server/game_logic/map"
"server/utils/pkg/data_source"
game_temp_pool "server/utils/pkg/game_temp_pool"
)

var data datasource.TempDataSource
var data data_source.TempDataSource

func ApplyDataSource(source any) {
data = source.(datasource.TempDataSource)
data = source.(data_source.TempDataSource)
}

type visibilityArr [][]bool

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

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

type processedMap [][][]uint16

func getProcessedMap(id game.Id, userId uint16, m *_map.Map) *processedMap {
func getProcessedMap(id game_logic.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.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(_type.Position{X: colNum + 1, Y: rowNum + 1})
b := m.GetBlock(gameDef.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.Id) []playerInfo {
func getUserList(id game_logic.Id) []playerInfo {
l := data.GetCurrentUserList(id)
ret := make([]playerInfo, len(l))
var status string
for i, u := range l {
if u.Status == game.UserStatusConnected {
if u.Status == gameDef.UserStatusConnected {
status = "connected"
} else {
status = "disconnect"
Expand All @@ -134,7 +134,7 @@ func getUserList(id game.Id) []playerInfo {
return ret
}

func GenerateMessage(_type string, id game.Id, userId uint16) string {
func GenerateMessage(_type string, id game_logic.Id, userId uint16) string {
switch _type {
case "start":
{
Expand Down Expand Up @@ -172,7 +172,7 @@ func GenerateMessage(_type string, id game.Id, userId uint16) string {
res := struct {
Action string `json:"action"`
Players []playerInfo `json:"players"`
Mode game.Mode `json:"mode"`
Mode gameDef.Mode `json:"mode"`
}{"info", getUserList(id), g.Mode}
ret, _ := json.Marshal(res)
return string(ret)
Expand Down
16 changes: 8 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,14 @@ package command
import (
"fmt"
"github.com/go-playground/validator/v10"
"server/utils/pkg/instruction"
"server/game_logic/game_def"
"strconv"
"strings"
)

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

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

type Context struct {
context.Context
Game *game.Game
User game.User
Game *game_logic.Game
User _type.User
Command chan string
Message chan string
}
Expand Down
59 changes: 32 additions & 27 deletions packages/server/api/internal/receiver/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"github.com/sirupsen/logrus"
"os"
_command "server/api/internal/command"
judge_pool "server/judgepool"
"server/utils/pkg/datasource"
"server/utils/pkg/game"
"server/utils/pkg/map"
"server/game_logic"
"server/game_logic/game_def"
"server/game_logic/map"
judge_pool "server/judge_pool"
"server/utils/pkg/data_source"
"strconv"
"strings"
"time"
Expand All @@ -17,11 +18,11 @@ import (
// NOTE: DEBUG ONLY
// Use to receive instructions from local file, in order to test the game functions

var data datasource.TempDataSource
var data data_source.TempDataSource
var fileDir = "./test/replay"

func ApplyDataSource(source any) {
data = source.(datasource.TempDataSource)
data = source.(data_source.TempDataSource)
}

func NewFileReceiver(pool *judge_pool.Pool) {
Expand All @@ -30,13 +31,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.Game{
g := &game_logic.Game{
Map: r.Map,
Mode: game.Mode1v1,
Id: game.Id(index + 1e3),
UserList: []game.User{},
Mode: _type.Mode1v1,
Id: game_logic.Id(index + 1e3),
UserList: []_type.User{},
CreateTime: time.Now().UnixMicro(),
Status: game.StatusWaiting,
Status: game_logic.StatusWaiting,
RoundNum: 0,
}
pool.DebugNewGame(g)
Expand All @@ -57,12 +58,12 @@ func NewFileReceiver(pool *judge_pool.Pool) {
}

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

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

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

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

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

if currentRound >= uint16(len(c)) {
playerLogger.Infof("Command(tot: %d) runs out, quit", len(c))
ticker.Stop()
player := ctx.User
player.Status = _type.UserStatusDisconnected
data.SetUserStatus(ctx.Game.Id, player)
}
if ctx.Game.RoundNum > currentRound {
playerLogger.Printf("Discover new round %d", g.RoundNum)
Expand All @@ -175,8 +179,8 @@ 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.UserStatusConnected
//ctx.User.Name = strconv.Itoa(int(ctx.User.UserId)) // DEBUG ONLY, avoiding strange username from `gioreply` file
ctx.User.Status = _type.UserStatusConnected
data.SetUserStatus(ctx.Game.Id, ctx.User)

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

defer func() {
ctx.User.Status = game.UserStatusDisconnected
ctx.User.Status = _type.UserStatusDisconnected
data.SetUserStatus(ctx.Game.Id, ctx.User)
receiverLogger.Infof("user quit")
}()
Expand All @@ -202,18 +206,19 @@ func receiver(ctx *Context) {
}
case cmd := <-ctx.Command:
{
if cmd == "" {

if strings.TrimSpace(cmd) == "" {
continue
}
ins, err := _command.PauseCommandStr(ctx.User.UserId, cmd)
if err != nil {
receiverLogger.Panicf("cannot parse command: %s", cmd)
receiverLogger.Panicf("cannot parse command: |%s|", cmd)
}
data.UpdateInstruction(ctx.Game.Id, ctx.User, ins)
}
case <-ticker.C:
{
done := func(g *game.Game, d string) {
done := func(g *game_logic.Game, d string) {
res := _command.GenerateMessage(d, ctx.Game.Id, ctx.User.UserId)
ctx.Message <- res
ctx.Game = g
Expand All @@ -222,11 +227,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.StatusWaiting && g.Status == game.StatusRunning {
if ctx.Game.Status == game_logic.StatusWaiting && g.Status == game_logic.StatusRunning {
done(g, "info")
done(g, "start")
continue
} else if g.Status == game.StatusEnd {
} else if g.Status == game_logic.StatusEnd {
done(g, "end")
ticker.Stop()
flag = false
Expand All @@ -235,12 +240,12 @@ func receiver(ctx *Context) {
} else {
if i%20 == 0 {
done(g, "info")
if ctx.Game.Status == game.StatusWaiting {
if ctx.Game.Status == game_logic.StatusWaiting {
done(g, "wait")
}
}

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

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

import (
"github.com/sirupsen/logrus"
"server/utils/pkg/map/type"
"server/game_logic/game_def"
)

var _ _type.Block = (*BaseBlock)(nil)
Expand Down Expand Up @@ -33,7 +33,9 @@ func (*BaseBlock) GetMoveStatus() _type.MoveStatus {
return _type.MoveStatus{}
}

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

func (*BaseBlock) MoveTo(_type.BlockVal) _type.Block {
return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package block

import "server/utils/pkg/map/type"
import (
"server/game_logic/game_def"
)

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

Expand All @@ -22,8 +24,16 @@ func (*BaseBuilding) GetMoveStatus() _type.MoveStatus {
return _type.MoveStatus{AllowMoveFrom: true, AllowMoveTo: true}
}

func (block *BaseBuilding) MoveFrom(number uint16) {
block.number -= number
func (block *BaseBuilding) MoveFrom(number uint16) uint16 {
var ret uint16
if block.number <= number {
ret = block.number - 1
block.number = 1
} else {
ret = number
block.number -= number
}
return ret
}

func (block *BaseBuilding) MoveTo(info _type.BlockVal) _type.Block {
Expand Down
Loading

0 comments on commit fc185fa

Please sign in to comment.