From 751214e5d0a023eaa77ed4fc807ba096df992f6e Mon Sep 17 00:00:00 2001 From: IliaDenisov Date: Wed, 11 Feb 2026 15:40:58 +0300 Subject: [PATCH] feat: command api --- internal/model/rest/command.go | 11 +- internal/router/handler/command.go | 227 +++++++++++++++++++++++++++++ 2 files changed, 233 insertions(+), 5 deletions(-) diff --git a/internal/model/rest/command.go b/internal/model/rest/command.go index 78a1916..e29b010 100644 --- a/internal/model/rest/command.go +++ b/internal/model/rest/command.go @@ -12,15 +12,15 @@ type CommandType string const ( CommandTypeRaceQuit CommandType = "raceQuit" CommandTypeRaceVote CommandType = "raceVote" - CommandTypeRaceRelation CommandType = "declarePeace" + CommandTypeRaceRelation CommandType = "raceRelation" CommandTypeShipClassCreate CommandType = "shipClassCreate" CommandTypeShipClassMerge CommandType = "shipClassMerge" CommandTypeShipClassRemove CommandType = "shipClassRemove" + CommandTypeShipGroupBreak CommandType = "shipGroupBreak" CommandTypeShipGroupLoad CommandType = "shipGroupLoad" CommandTypeShipGroupUnload CommandType = "shipGroupUnload" CommandTypeShipGroupSend CommandType = "shipGroupSend" CommandTypeShipGroupUpgrade CommandType = "shipGroupUpgrade" - CommandTypeShipGroupBreak CommandType = "shipGroupBreak" CommandTypeShipGroupMerge CommandType = "shipGroupMerge" CommandTypeShipGroupDismantle CommandType = "shipGroupDismantle" CommandTypeShipGroupTransfer CommandType = "shipGroupTransfer" @@ -104,9 +104,9 @@ type CommandShipGroupSend struct { type CommandShipGroupUpgrade struct { CommandMeta - ID string `json:"id" binding:"required,uuid_rfc4122"` - Tech string `json:"tech" binding:"oneof=ALL DRIVE WEAPONS SHIELDS CARGO"` - Level int `json:"level" binding:"gte=1"` + ID string `json:"id" binding:"required,uuid_rfc4122"` + Tech string `json:"tech" binding:"oneof=ALL DRIVE WEAPONS SHIELDS CARGO"` + Level float64 `json:"level" binding:"gte=1"` } type CommandShipGroupMerge struct { @@ -116,6 +116,7 @@ type CommandShipGroupMerge struct { type CommandShipGroupBreak struct { CommandMeta ID string `json:"id" binding:"required,uuid_rfc4122"` + NewID string `json:"newId" binding:"required,uuid_rfc4122"` Quantity int `json:"quantity" binding:"gte=0"` } diff --git a/internal/router/handler/command.go b/internal/router/handler/command.go index a72f545..f117006 100644 --- a/internal/router/handler/command.go +++ b/internal/router/handler/command.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/go-playground/validator/v10" + "github.com/google/uuid" "github.com/iliadenisov/galaxy/internal/controller" "github.com/gin-gonic/gin" @@ -53,6 +54,44 @@ func parseCommand(actor string, c json.RawMessage) (Command, error) { return commandRaceRelation(actor, c) case rest.CommandTypeShipClassCreate: return commandShipClassCreate(actor, c) + case rest.CommandTypeShipClassMerge: + return commandShipClassMerge(actor, c) + case rest.CommandTypeShipClassRemove: + return commandShipClassRemove(actor, c) + case rest.CommandTypeShipGroupBreak: + return commandShipGroupBreak(actor, c) + case rest.CommandTypeShipGroupLoad: + return commandShipGroupLoad(actor, c) + case rest.CommandTypeShipGroupUnload: + return commandShipGroupUnload(actor, c) + case rest.CommandTypeShipGroupSend: + return commandShipGroupSend(actor, c) + case rest.CommandTypeShipGroupUpgrade: + return commandShipGroupUpgrade(actor, c) + case rest.CommandTypeShipGroupMerge: + return commandShipGroupMerge(actor, c) + case rest.CommandTypeShipGroupDismantle: + return commandShipGroupDismantle(actor, c) + case rest.CommandTypeShipGroupTransfer: + return commandShipGroupTransfer(actor, c) + case rest.CommandTypeShipGroupJoinFleet: + return commandShipGroupJoinFleet(actor, c) + case rest.CommandTypeFleetMerge: + return commandFleetMerge(actor, c) + case rest.CommandTypeFleetSend: + return commandFleetSend(actor, c) + case rest.CommandTypeScienceCreate: + return commandScienceCreate(actor, c) + case rest.CommandTypeScienceRemove: + return commandScienceRemove(actor, c) + case rest.CommandTypePlanetRename: + return commandPlanetRename(actor, c) + case rest.CommandTypePlanetProduce: + return commandPlanetProduce(actor, c) + case rest.CommandTypePlanetRouteSet: + return commandPlanetRouteSet(actor, c) + case rest.CommandTypePlanetRouteRemove: + return commandPlanetRouteRemove(actor, c) default: return nil, fmt.Errorf("unknown comman type: %s", t) } @@ -92,6 +131,194 @@ func commandShipClassCreate(actor string, c json.RawMessage) (Command, error) { } } +func commandShipClassMerge(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandShipClassMerge)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ShipClassMerge(actor, v.Name, v.Target) + }, nil + } +} + +func commandShipClassRemove(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandShipClassRemove)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ShipClassRemove(actor, v.Name) + }, nil + } +} + +func commandShipGroupBreak(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandShipGroupBreak)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ShipGroupBreak(actor, uuid.MustParse(v.ID), uuid.MustParse(v.NewID), uint(v.Quantity)) + }, nil + } +} + +func commandShipGroupLoad(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandShipGroupLoad)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ShipGroupLoad(actor, uuid.MustParse(v.ID), v.Cargo, v.Quantity) + }, nil + } +} + +func commandShipGroupUnload(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandShipGroupUnload)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ShipGroupUnload(actor, uuid.MustParse(v.ID), v.Quantity) + }, nil + } +} + +func commandShipGroupSend(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandShipGroupSend)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ShipGroupSend(actor, uuid.MustParse(v.ID), uint(v.Destination)) + }, nil + } +} + +func commandShipGroupUpgrade(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandShipGroupUpgrade)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ShipGroupUpgrade(actor, uuid.MustParse(v.ID), v.Tech, v.Level) + }, nil + } +} + +func commandShipGroupMerge(actor string, c json.RawMessage) (Command, error) { + return func(c controller.Ctrl) error { + return c.ShipGroupMerge(actor) + }, nil +} + +func commandShipGroupDismantle(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandShipGroupDismantle)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ShipGroupDismantle(actor, uuid.MustParse(v.ID)) + }, nil + } +} + +func commandShipGroupTransfer(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandShipGroupTransfer)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ShipGroupTransfer(actor, v.Acceptor, uuid.MustParse(v.ID)) + }, nil + } +} + +func commandShipGroupJoinFleet(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandShipGroupJoinFleet)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ShipGroupJoinFleet(actor, v.Name, uuid.MustParse(v.ID)) + }, nil + } +} + +func commandFleetMerge(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandFleetMerge)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.FleetMerge(actor, v.Name, v.Target) + }, nil + } +} + +func commandFleetSend(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandFleetSend)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.FleetSend(actor, v.Name, uint(v.Destination)) + }, nil + } +} + +func commandScienceCreate(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandScienceCreate)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ScienceCreate(actor, v.Name, v.Drive, v.Weapons, v.Shields, v.Cargo) + }, nil + } +} + +func commandScienceRemove(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandScienceRemove)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.ScienceRemove(actor, v.Name) + }, nil + } +} + +func commandPlanetRename(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandPlanetRename)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.PlanetRename(actor, v.Number, v.Name) + }, nil + } +} + +func commandPlanetProduce(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandPlanetProduce)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.PlanetProduce(actor, v.Number, v.Production, v.Subject) + }, nil + } +} + +func commandPlanetRouteSet(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandPlanetRouteSet)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.PlanetRouteSet(actor, v.LoadType, uint(v.Origin), uint(v.Destination)) + }, nil + } +} + +func commandPlanetRouteRemove(actor string, c json.RawMessage) (Command, error) { + if v, err := unmarshallCommand(c, new(rest.CommandPlanetRouteRemove)); err != nil { + return nil, err + } else { + return func(c controller.Ctrl) error { + return c.PlanetRouteRemove(actor, v.LoadType, uint(v.Origin)) + }, nil + } +} + +// Helpers + func unmarshallCommand[T rest.DecodableCommand](c json.RawMessage, v *T) (*T, error) { if err := json.Unmarshal(c, v); err != nil { return nil, err