aboutsummaryrefslogtreecommitdiff
path: root/go/game/playerControl.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-02-08 17:50:10 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-08-20 15:50:10 +0200
commit45df9bf319d38db916ca983b8c92bb912f5ce9ee (patch)
treed7966002dad4a20bc3ef385bd152554fe278a6df /go/game/playerControl.go
parenteb10f6f4940ecd09fd7d881fe5e1735ff146f919 (diff)
downloadmuhqs-game-45df9bf319d38db916ca983b8c92bb912f5ce9ee.tar.gz
muhqs-game-45df9bf319d38db916ca983b8c92bb912f5ce9ee.zip
intermediate commit
Diffstat (limited to 'go/game/playerControl.go')
-rw-r--r--go/game/playerControl.go97
1 files changed, 68 insertions, 29 deletions
diff --git a/go/game/playerControl.go b/go/game/playerControl.go
index e62d91b3..eea6a19b 100644
--- a/go/game/playerControl.go
+++ b/go/game/playerControl.go
@@ -1,16 +1,35 @@
package game
+import (
+ "fmt"
+ "log"
+)
+
type PlayerNotificationType int
const (
- DeclaredActionNotification = iota
+ DeclaredActionNotification PlayerNotificationType = iota
ResolvedActionNotification
PriorityNotification
- UpkeepPrompt
- BuyPrompt
- HandCardSelectionPrompt
+ TargetSelectionPrompt
)
+func (n PlayerNotificationType) String() string {
+ switch n {
+ case DeclaredActionNotification:
+ return "DeclaredActionNotification"
+ case ResolvedActionNotification:
+ return "ResolvedActionNotification"
+ case PriorityNotification:
+ return "PriorityNotification"
+ case TargetSelectionPrompt:
+ return "TargetSelectionPrompt"
+ default:
+ log.Panicf("Unhandled notification %d", n)
+ return ""
+ }
+}
+
func (n PlayerNotification) IsPriorityNotification() bool {
return n.Notification == PriorityNotification
}
@@ -21,72 +40,92 @@ type PlayerNotification struct {
Error error
}
-func NewPriorityNotification() PlayerNotification {
+func (n *PlayerNotification) String() string {
+ if n.Error != nil {
+ return fmt.Sprintf("error %v: %v", n.Notification, n.Error)
+ }
+
+ return fmt.Sprintf("%v: %v", n.Notification, n.Context)
+}
+
+type TargetSelectionCtx struct {
+ Action Action
+ Prompt string
+}
+
+func newPriorityNotification() PlayerNotification {
return PlayerNotification{PriorityNotification, nil, nil}
}
-func NewDeclaredActionNotification(a Action, err error) PlayerNotification {
+func newDeclaredActionNotification(a Action, err error) PlayerNotification {
return PlayerNotification{DeclaredActionNotification, a, err}
}
-func NewResolvedActionNotification(a Action, err error) PlayerNotification {
+func newResolvedActionNotification(a Action, err error) PlayerNotification {
return PlayerNotification{ResolvedActionNotification, a, err}
}
-func NewUpkeepPrompt(u *Unit) PlayerNotification {
- return PlayerNotification{ResolvedActionNotification, u, nil}
+func newTargetSelectionPrompt(a Action, desc string) PlayerNotification {
+ return PlayerNotification{TargetSelectionPrompt, TargetSelectionCtx{a, desc}, nil}
}
-func NewBuyPrompt() PlayerNotification {
- return PlayerNotification{BuyPrompt, nil, nil}
+func newUpkeepPrompt(p *Player) PlayerNotification {
+ a := newUpkeepAction(p)
+ return newTargetSelectionPrompt(a, "Select units to disband")
}
-func NewHandCardSelectionPrompt(min, max int) PlayerNotification {
- return PlayerNotification{HandCardSelectionPrompt, []int{min, max}, nil}
+func newBuyPrompt(p *Player) PlayerNotification {
+ a := newBuyAction(p)
+ prompt := newTargetSelectionPrompt(a, "Select a card to buy")
+ return prompt
+}
+
+func newHandCardSelectionPrompt(p *Player, min, max int) PlayerNotification {
+ a := newHandCardSelection(p, min, max)
+ desc := fmt.Sprintf("Select between %d and %d hand cards", min, max)
+ return newTargetSelectionPrompt(a, desc)
}
type PlayerControl interface {
+ Player() *Player
RecvAction() Action
SendNotification(PlayerNotification)
}
-type DummyPlayerControl struct{ P *Player }
-
-func (c *DummyPlayerControl) RecvAction() Action { return NewPassPriority(c.P) }
-func (*DummyPlayerControl) SendNotification(PlayerNotification) {}
-
type ChanPlayerControl struct {
- Actions chan Action
- Notifications chan PlayerNotification
+ player *Player
+ actions chan Action
+ notifications chan PlayerNotification
}
-func (c *ChanPlayerControl) SendAction(a Action) { c.Actions <- a }
-func (c *ChanPlayerControl) RecvAction() Action { return <-c.Actions }
-func (c *ChanPlayerControl) SendNotification(n PlayerNotification) { c.Notifications <- n }
+func (c *ChanPlayerControl) Player() *Player { return c.player }
+func (c *ChanPlayerControl) SendAction(a Action) { c.actions <- a }
+func (c *ChanPlayerControl) RecvAction() Action { return <-c.actions }
+func (c *ChanPlayerControl) SendNotification(n PlayerNotification) { c.notifications <- n }
func (c *ChanPlayerControl) RecvNotification() *PlayerNotification {
select {
- case n := <-c.Notifications:
+ case n := <-c.notifications:
return &n
default:
return nil
}
}
-func NewChanPlayerControl() *ChanPlayerControl {
+func NewChanPlayerControl(p *Player) *ChanPlayerControl {
a := make(chan Action)
n := make(chan PlayerNotification)
- return &ChanPlayerControl{a, n}
+ return &ChanPlayerControl{p, a, n}
}
func prompt(ctrl PlayerControl, notification PlayerNotification) Action {
- ctrl.SendNotification(NewPriorityNotification())
+ ctrl.SendNotification(notification)
return ctrl.RecvAction()
}
func promptBuy(ctrl PlayerControl) Action {
- return prompt(ctrl, NewBuyPrompt())
+ return prompt(ctrl, newBuyPrompt(ctrl.Player()))
}
func promptAction(ctrl PlayerControl) Action {
- return prompt(ctrl, NewPriorityNotification())
+ return prompt(ctrl, newPriorityNotification())
}