Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
fix: check if commander has the ship before updating the fleet
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmolly committed Jul 14, 2024
1 parent 237e233 commit 9ffcaa2
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion orm/fleet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package orm

import "github.com/lib/pq"
import (
"errors"

"github.com/lib/pq"
)

var (
ErrInvalidShipID = errors.New("invalid ship id")
)

type Fleet struct {
ID uint32 `gorm:"primary_key"` // uniquely identifies the fleet
Expand All @@ -17,6 +25,10 @@ func CreateFleet(owner *Commander, id uint32, name string, ships []uint32) error
fleet.CommanderID = owner.CommanderID
fleet.GameID = id
for _, shipID := range ships {
// check if the commander has this ship
if _, ok := owner.OwnedShipsMap[shipID]; !ok {
return ErrInvalidShipID
}
fleet.ShipList = append(fleet.ShipList, int64(shipID))
}
if err := GormDB.Create(&fleet).Error; err != nil {
Expand All @@ -35,6 +47,10 @@ func (f *Fleet) RenameFleet(name string) error {
func (f *Fleet) UpdateShipList(owner *Commander, ships []uint32) error {
f.ShipList = make([]int64, len(ships))
for i, shipID := range ships {
// check if the commander has this ship
if _, ok := owner.OwnedShipsMap[shipID]; !ok {
return ErrInvalidShipID
}
f.ShipList[i] = int64(shipID)
}
if err := GormDB.Save(f).Error; err != nil {
Expand Down

0 comments on commit 9ffcaa2

Please sign in to comment.