diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-07-30 20:16:49 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-07-30 20:16:49 +0200 |
| commit | f64e8747e287e949ce7cbbb42303ad1cdfabec89 (patch) | |
| tree | c9e0a03b075e24f724bffaa40e015398db10cd1a | |
| parent | aceaf88ba23480919c6921eda14a761476b417ed (diff) | |
| download | muhqs-game-f64e8747e287e949ce7cbbb42303ad1cdfabec89.tar.gz muhqs-game-f64e8747e287e949ce7cbbb42303ad1cdfabec89.zip | |
minor code improvements reported by golangci-lint
| -rw-r--r-- | go/activities/draft.go | 20 | ||||
| -rw-r--r-- | go/game/action.go | 9 | ||||
| -rw-r--r-- | go/game/ai.go | 8 | ||||
| -rw-r--r-- | go/game/ai_test.go | 4 | ||||
| -rw-r--r-- | go/game/areaEffect_test.go | 2 | ||||
| -rw-r--r-- | go/game/cardImplementations_test.go | 4 | ||||
| -rw-r--r-- | go/game/draft.go | 3 | ||||
| -rw-r--r-- | go/game/pileOfCards.go | 4 | ||||
| -rw-r--r-- | go/game/set.go | 2 | ||||
| -rw-r--r-- | go/game/state.go | 37 | ||||
| -rw-r--r-- | go/game/targets.go | 4 | ||||
| -rw-r--r-- | go/game/trigger.go | 2 | ||||
| -rw-r--r-- | go/game/unit_test.go | 7 |
13 files changed, 55 insertions, 51 deletions
diff --git a/go/activities/draft.go b/go/activities/draft.go index f1b3fb58..e2af5f5c 100644 --- a/go/activities/draft.go +++ b/go/activities/draft.go @@ -83,7 +83,9 @@ func NewLocalDraft(width, height int, playerName string) *Draft { 30, "?", func(*ui.SimpleButton) { - ui.OpenUrl("https://muhq.space/muhqs-game/build/en/rules.html#draft-notation") + if err := ui.OpenUrl("https://muhq.space/muhqs-game/build/en/rules.html#draft-notation"); err != nil { + // TODO: present the error + } })) d.AddWidget(ui.NewFixedTextBox( @@ -109,14 +111,14 @@ func NewLocalDraft(width, height int, playerName string) *Draft { DRAFT_BUTTON_HEIGHT, "Start", func(*ui.SimpleButton) { - d.startDraft(d.descInput.TextOrLabel(), d.setInput.TextOrLabel(), aiNInput.GetChoosen()) + d.startDraftSafe(d.descInput.TextOrLabel(), d.setInput.TextOrLabel(), aiNInput.GetChoosen()) })) return d } -// StartNewLocalDraft creates a new draft from its inputs and immediatly starts it. -// The started draft activiry is returned or any occured error. +// StartNewLocalDraft creates a new draft from its inputs and immediately starts it. +// The started draft activity is returned or any occurred error. func StartNewLocalDraft(width, height int, playerName string, desc string, sets string, aiN int) (*Draft, error) { d := &Draft{ Collection: ui.Collection{Width: width, Height: height}, @@ -149,7 +151,7 @@ func (d *Draft) Update() error { } if n.Valid() { - log.Println("Received draft pick: %v\n", n) + log.Printf("Received draft pick: %v\n", n) d.presentPack(n) } @@ -193,7 +195,10 @@ func (d *Draft) presentPack(n game.PlayerNotification) { } card := _card.(*game.Card) - d.ctrl.SendAction(game.NewDraftPick(d.player, pack, card)) + err := d.ctrl.SendAction(game.NewDraftPick(d.player, pack, card)) + if err != nil { + // FIXME: handle error + } d.RemoveWidget(grid) if d.timer != nil { d.timer = nil @@ -229,7 +234,7 @@ func (d *Draft) startDraft(desc string, setList string, aiN int) error { return err } - for _ = range aiN { + for range aiN { d.draft.AddRandomAi() } @@ -276,7 +281,6 @@ func (d *Draft) startDraft(desc string, setList string, aiN int) error { } func (d *Draft) startDraftSafe(desc string, setList string, aiN int) { - err := d.startDraft(desc, setList, aiN) if err == nil { return diff --git a/go/game/action.go b/go/game/action.go index 077f4026..913e1574 100644 --- a/go/game/action.go +++ b/go/game/action.go @@ -777,7 +777,8 @@ func MarshalAction(_a Action) []byte { } var ( - InvalidActionFormat = errors.New("Invalid Action Format") + ErrInvalidActionFormat = errors.New("invalid action format") + ErrUnknownPlayer = errors.New("unknown player") ) var ActionRegex = regexp.MustCompile(`^([^\xA0]*)\xA0([^\xA0]*)\xA0?(.*)?$`) @@ -785,14 +786,14 @@ var ActionRegex = regexp.MustCompile(`^([^\xA0]*)\xA0([^\xA0]*)\xA0?(.*)?$`) func UnmarshalAction(s State, in []byte) (a Action, err error) { m := ActionRegex.FindSubmatch(in) if len(m) < 2 || len(m) > 4 { - return nil, InvalidActionFormat + return nil, ErrInvalidActionFormat } _action := string(m[2]) switch _action { case "pick": p := s.PlayerByName(string(m[1])) if p == nil { - err = errors.New("Unknown player") + err = ErrUnknownPlayer return } _p := strings.Split(string(m[3]), ":") @@ -809,7 +810,7 @@ func UnmarshalAction(s State, in []byte) (a Action, err error) { case "pass": p := s.PlayerByName(string(m[1])) if p == nil { - err = errors.New("Unknown player") + err = ErrUnknownPlayer return } a = NewPassPriority(p) diff --git a/go/game/ai.go b/go/game/ai.go index 928285bc..f24a62a1 100644 --- a/go/game/ai.go +++ b/go/game/ai.go @@ -115,7 +115,7 @@ func selectRandomTargets(rand *rand.Rand, targets *Targets) error { for t.RequireSelection() { options := t.Options() if len(options) == 0 { - return fmt.Errorf("No possible selection") + return errors.New("no possible selection") } idx := rand.Intn(len(options)) @@ -689,7 +689,7 @@ func (ai *SimpleAiControl) handlePriority() Action { // TODO: support "smarter" discarding t := ai.last.Context.(*Targets) for _, c := range ai.p.Hand.Cards() { - t.AddSelection(c) + _ = t.AddSelection(c) } return newTargetSelection(ai.p, t) } @@ -710,12 +710,12 @@ func (ai *SimpleAiControl) handleTargetSelection() Action { case Phases.DiscardStep: // TODO: support "smarter" discarding for _, c := range ai.p.Hand.Cards() { - t.AddSelection(c) + _ = t.AddSelection(c) } return ctx.Action } - selectRandomTargets(s.Rand, t) + _ = selectRandomTargets(s.Rand, t) return ctx.Action } diff --git a/go/game/ai_test.go b/go/game/ai_test.go index 8c3ba0f9..c1d378b6 100644 --- a/go/game/ai_test.go +++ b/go/game/ai_test.go @@ -255,7 +255,7 @@ symbols: } ai.promptAction() - a, ok := ai.NextAction() + a, _ := ai.NextAction() if a == nil { t.Fatal("Nil action received from shy AI") } @@ -267,7 +267,7 @@ symbols: } ai.promptAction() - _, ok = ai.NextAction() + _, ok := ai.NextAction() if ok { t.Fatal("ShyAI did not close its channel") } diff --git a/go/game/areaEffect_test.go b/go/game/areaEffect_test.go index 30bcb760..80cbd4c0 100644 --- a/go/game/areaEffect_test.go +++ b/go/game/areaEffect_test.go @@ -87,7 +87,7 @@ symbols: } a := NewMoveAction(f) - a.Target().AddSelection(m.TileAt(Position{1, 0})) + _ = a.Target().AddSelection(m.TileAt(Position{1, 0})) a.resolve(s) diff --git a/go/game/cardImplementations_test.go b/go/game/cardImplementations_test.go index 1ad7e142..e073a027 100644 --- a/go/game/cardImplementations_test.go +++ b/go/game/cardImplementations_test.go @@ -46,7 +46,7 @@ symbols: t.Fatal("palisade is missing") } - a.Target().AddSelection(m.TileAt(Position{0, 1})) + _ = a.Target().AddSelection(m.TileAt(Position{0, 1})) s.ResolveAction(a) if m.TileAt(Position{0, 1}).Type != TileTypes.Neutral { t.Fatal("tile not neutralized") @@ -57,7 +57,7 @@ symbols: t.Fatal("full action still has target selection") } - a.Target().AddSelection(palisade) + _ = a.Target().AddSelection(palisade) s.ResolveAction(a) if m.TileAt(Position{0, 0}).Permanent != nil { t.Fatal("palisade not destroyed") diff --git a/go/game/draft.go b/go/game/draft.go index 5854d23d..17f64b70 100644 --- a/go/game/draft.go +++ b/go/game/draft.go @@ -62,7 +62,8 @@ func (d *Draft) parseDesc(desc string) error { } func (d *Draft) Valid() bool { - return !(d.packSize < d.cardsPerPack*len(d.players)) + required := d.cardsPerPack*len(d.players) + return d.packSize > required } func (d *Draft) PackSize() int { diff --git a/go/game/pileOfCards.go b/go/game/pileOfCards.go index 0be45c6b..d22e0b6e 100644 --- a/go/game/pileOfCards.go +++ b/go/game/pileOfCards.go @@ -42,7 +42,7 @@ func (poc *PileOfCardsBase) String() string { return fmt.Sprintf("[%s]", strings.Join(cardNames, ", ")) } -var ErrInvalidPocString = errors.New("Invalid PileOfCards format") +var ErrInvalidPocString = errors.New("invalid PileOfCards format") // Initialise a PileOfCard from a string. // Return InvalidPocString on error. @@ -156,7 +156,7 @@ func (poc *PileOfCardsBase) ToList() (list string) { func NewRandomPackFromPool(pool PileOfCards, packSize int) PileOfCards { pack := NewPileOfCards() - for _ = range packSize { + for range packSize { card := pool.Cards()[rand.Intn(len(pool.Cards())-1)] pool.MoveCard(card, pack) } diff --git a/go/game/set.go b/go/game/set.go index 8e20487b..b5fe8b76 100644 --- a/go/game/set.go +++ b/go/game/set.go @@ -24,7 +24,7 @@ const ( Exp1 ) -var ErrUnknownSet = errors.New("Unknown set") +var ErrUnknownSet = errors.New("unknown set") func (si SetIdentifier) String() string { switch si { diff --git a/go/game/state.go b/go/game/state.go index dd3cb007..8901aed7 100644 --- a/go/game/state.go +++ b/go/game/state.go @@ -1,6 +1,7 @@ package game import ( + "errors" "fmt" "log" "math/rand" @@ -272,7 +273,7 @@ func (s *LocalState) stateBasedActions() []*Player { func (s *LocalState) IsValidPlay(a *PlayAction) error { if s.activePhase != Phases.ActionPhase { - return fmt.Errorf("Play actions can only be declared during one's actions phase") + return errors.New("play actions can only be declared during one's actions phase") } if a.Card.IsPermanent() { @@ -283,7 +284,7 @@ func (s *LocalState) IsValidPlay(a *PlayAction) error { spawn := a.Target().sel[0].(*Tile) if !slices.Contains(s.AvailableSpawnTiles(a.Source().(*Player), a.Card), spawn) { - return fmt.Errorf("Spawn %v is not a valid spawn tile for %s", + return fmt.Errorf("spawn %v is not a valid spawn tile for %s", spawn.Position.String(), a.Card.Name) } } @@ -293,19 +294,19 @@ func (s *LocalState) IsValidPlay(a *PlayAction) error { func (s *LocalState) IsValidMove(a *MoveAction) error { if s.activePhase != Phases.ActionPhase { - return fmt.Errorf("Move actions can only be declared during one's actions phase") + return errors.New("move actions can only be declared during one's actions phase") } u := a.Source().(*Unit) tile := relaxedTileTarget(a, a.Target().sel[0]) if !slices.Contains(u.MoveRangeTiles(), tile) { - return fmt.Errorf("Unit %s@%v can not move to %s", + return fmt.Errorf("unit %s@%v can not move to %s", u.card.Name, u.tile.Position, tile.Position.String()) } if !u.IsAvailableTile(tile) { - return fmt.Errorf("Unit %s@%v can not move to not available tile %v", + return fmt.Errorf("unit %s@%v can not move to not available tile %v", u.card.Name, u.tile.Position, tile) } @@ -314,7 +315,7 @@ func (s *LocalState) IsValidMove(a *MoveAction) error { func (s *LocalState) IsValidAttack(a *AttackAction) error { if s.activePhase != Phases.ActionPhase { - return fmt.Errorf("Attack actions can only be declared during one's actions phase") + return errors.New("Attack actions can only be declared during one's actions phase") } unit := a.Source().(*Unit) @@ -324,7 +325,7 @@ func (s *LocalState) IsValidAttack(a *AttackAction) error { target := a.Target().sel[0].(Permanent) if !IsPositionInRange(unit.tile.Position, target.Tile().Position, unit.Attack.MaxRange()) { - return fmt.Errorf("Attack target on %s is not in attack range %d of %s on %s", + return fmt.Errorf("attack target on %s is not in attack range %d of %s on %s", target.Tile().Position.String(), unit.Attack.MaxRange(), unit.card.Name, unit.tile.Position.String()) } @@ -362,7 +363,7 @@ func (s *LocalState) isValidBuy(a *BuyAction) error { } if !a.card().IsBuyable() { - return fmt.Errorf("Card %s is not buyable", card.Name) + return fmt.Errorf("card %s is not buyable", card.Name) } return nil @@ -370,13 +371,13 @@ func (s *LocalState) isValidBuy(a *BuyAction) error { func (s *LocalState) IsValidEquip(a *EquipAction) error { if s.activePhase != Phases.ActionPhase { - return fmt.Errorf("Play actions can only be declared during one's actions phase") + return errors.New("play actions can only be declared during one's actions phase") } uTile := TileOrContainingPermTile(a.Source().(*Unit)) eTile := TileOrContainingPermTile(a.equipment) if !IsPositionInRange(uTile.Position, eTile.Position, 1) { - return fmt.Errorf("Can only equip adjacent equipments") + return errors.New("can only equip adjacent equipments") } return nil @@ -384,7 +385,7 @@ func (s *LocalState) IsValidEquip(a *EquipAction) error { func (s *LocalState) IsValidArtifactSwitch(a *ArtifactSwitchAction) error { if s.activePhase != Phases.ActionPhase { - return fmt.Errorf("Play actions can only be declared during one's actions phase") + return errors.New("play actions can only be declared during one's actions phase") } u := a.Source().(*Unit) @@ -392,12 +393,12 @@ func (s *LocalState) IsValidArtifactSwitch(a *ArtifactSwitchAction) error { artifact := a.Artifact aTile := TileOrContainingPermTile(artifact) if !IsPositionInRange(uTile.Position, aTile.Position, 1) { - return fmt.Errorf("Can only switch position with adjacent equipments") + return errors.New("can only switch position with adjacent equipments") } solid, err := artifact.XEffect("solid") if err != nil && artifact.Controller() != u.Controller() && solid > 0 { - return fmt.Errorf("Can only switch with solid artifacts you control") + return errors.New("can only switch with solid artifacts you control") } return nil @@ -405,7 +406,7 @@ func (s *LocalState) IsValidArtifactSwitch(a *ArtifactSwitchAction) error { func (s *LocalState) IsValidArtifactMove(a *ArtifactMoveAction) error { if s.activePhase != Phases.ActionPhase { - return fmt.Errorf("Play actions can only be declared during one's actions phase") + return errors.New("play actions can only be declared during one's actions phase") } u := a.Source().(*Unit) @@ -413,12 +414,12 @@ func (s *LocalState) IsValidArtifactMove(a *ArtifactMoveAction) error { artifact := a.Artifact aTile := TileOrContainingPermTile(artifact) if !IsPositionInRange(uTile.Position, aTile.Position, 1) { - return fmt.Errorf("Can only switch position with adjacent equipments") + return errors.New("can only switch position with adjacent equipments") } solid, err := artifact.XEffect("solid") if err != nil && artifact.Controller() != u.Controller() && solid > 0 { - return fmt.Errorf("Can only switch with solid artifacts you control") + return errors.New("can only switch with solid artifacts you control") } return nil @@ -466,12 +467,12 @@ func (s *LocalState) declareAction(a Action) { switch a.(type) { case *BuyAction: if s.activePhase != Phases.BuyPhase { - err = fmt.Errorf("Cards can only be bought during one's buy phase") + err = errors.New("cards can only be bought during one's buy phase") } case *FreeAction: default: if a.Speed() == ActionSpeeds.Slow && !s.stack.IsEmpty() { - err = fmt.Errorf("Slow action can not be declared while other actions are on the stack") + err = errors.New("slow action can not be declared while other actions are on the stack") } } } diff --git a/go/game/targets.go b/go/game/targets.go index 473b26b0..3eccfe76 100644 --- a/go/game/targets.go +++ b/go/game/targets.go @@ -234,11 +234,11 @@ func (t *Target) HasOptions() bool { func (t *Target) CheckSelection(s *LocalState) error { if t.RequireSelection() { - return fmt.Errorf("Number of selected targets and required ones does not match") + return errors.New("number of selected targets and required ones does not match") } if t.requirement.max != -1 && len(t.sel) > t.requirement.max { - return fmt.Errorf("To many selection %d (%d allowed)", len(t.sel), t.requirement.max) + return fmt.Errorf("to many selection %d (%d allowed)", len(t.sel), t.requirement.max) } for _, sel := range t.sel { diff --git a/go/game/trigger.go b/go/game/trigger.go index 2619bdd8..b8eb28c0 100644 --- a/go/game/trigger.go +++ b/go/game/trigger.go @@ -132,7 +132,7 @@ func newTargetedTrigger(p Permanent, singleshot bool, resolveFunc ActionResolveF func newDeathTrigger(p Permanent, singleshot bool, resolveFunc ActionResolveFunc, permCond func(Permanent) bool, desc string) Trigger { t := triggerBase{source: p, resolveFunc: resolveFunc, desc: desc} t.condition = func(_ *LocalState, event Event) (bool, bool) { - if !(event.eventType == EventTypes.Sacrifice || event.eventType == EventTypes.Destruction) { + if event.eventType != EventTypes.Sacrifice && event.eventType != EventTypes.Destruction { return false, false } destroyedPerm := event.affected[0].(Permanent) diff --git a/go/game/unit_test.go b/go/game/unit_test.go index 26ef3700..602fa8fb 100644 --- a/go/game/unit_test.go +++ b/go/game/unit_test.go @@ -101,19 +101,16 @@ symbols: o := s.AddNewPlayer("opponent", NewDeck()) a := s.addNewUnit(NewCard("base/archer"), Position{2, 0}, p) - units := []*Unit{} for x := range 3 { for y := range 3 { - var u *Unit pos := Position{x, y} if x == 2 && y == 0 { continue } else if x == 2 && y == 2 { - u = s.addNewUnit(NewCard("nautics/fisher"), pos, o) + s.addNewUnit(NewCard("nautics/fisher"), pos, o) } else { - u = s.addNewUnit(NewCard("misc/farmer"), pos, o) + s.addNewUnit(NewCard("misc/farmer"), pos, o) } - units = append(units, u) } } |
