feat: more validators
This commit is contained in:
@@ -63,42 +63,43 @@ func commandRaceQuit(actor string) (Command, error) {
|
||||
}
|
||||
|
||||
func commandRaceVote(actor string, c json.RawMessage) (Command, error) {
|
||||
var v rest.CommandRaceVote
|
||||
if err := json.Unmarshal(c, &v); err != nil {
|
||||
if v, err := unmarshallCommand(c, new(rest.CommandRaceVote)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.RaceVote(actor, v.Acceptor)
|
||||
}, nil
|
||||
}
|
||||
if err := validateCommand(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.RaceVote(actor, v.Recipient)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func commandRaceRelation(actor string, c json.RawMessage) (Command, error) {
|
||||
var v rest.CommandRaceRelation
|
||||
if err := json.Unmarshal(c, &v); err != nil {
|
||||
if v, err := unmarshallCommand(c, new(rest.CommandRaceRelation)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.RaceRelation(actor, v.Acceptor, v.Relation)
|
||||
}, nil
|
||||
}
|
||||
if err := validateCommand(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.RaceRelation(actor, v.Opponent, v.Relation)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func commandShipClassCreate(actor string, c json.RawMessage) (Command, error) {
|
||||
v := new(rest.CommandShipClassCreate)
|
||||
if v, err := unmarshallCommand(c, new(rest.CommandShipClassCreate)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.ShipClassCreate(actor, v.Name, v.Drive, int(v.Armament), v.Weapons, v.Shields, v.Cargo)
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func unmarshallCommand[T rest.DecodableCommand](c json.RawMessage, v *T) (*T, error) {
|
||||
if err := json.Unmarshal(c, v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateCommand(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.ShipClassCreate(actor, v.Name, v.Drive, int(v.Armament), v.Weapons, v.Shields, v.Cargo)
|
||||
}, nil
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func validateCommand(v any) error {
|
||||
|
||||
Reference in New Issue
Block a user