aboutsummaryrefslogtreecommitdiff
path: root/go/game/action.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/game/action.go')
-rw-r--r--go/game/action.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/go/game/action.go b/go/game/action.go
index 373c814f..1d34a843 100644
--- a/go/game/action.go
+++ b/go/game/action.go
@@ -1,8 +1,11 @@
package game
import (
+ "errors"
"fmt"
"log"
+ "regexp"
+ "strings"
)
type ActionSpeed int
@@ -713,3 +716,61 @@ func UseAttackAction(a Action) {
a.costFunc = genAttackActionCost(a.Source().(*Unit))
}
}
+
+// Marshal an action to plain text.
+// The format of the action is `SOURCE ACTION CONTEXT`.
+// PassPriority: PLAYER pass
+// DraftPick: PLAYER pick CARD:PACK
+func MarshalAction(_a Action) []byte {
+ out := []byte{}
+ switch a := _a.(type) {
+ case *PassPriority:
+ out = fmt.Appendf(out, "%s\u00A0pass", a.player.Name)
+ case *DraftPick:
+ out = fmt.Appendf(out, "%s\u00A0pick\u00A0%s:%s", a.player.Name, a.pick.Path(), a.pack)
+ default:
+ log.Fatalf("MarshalAction(%s) not implemented\n", a)
+ }
+ return out
+}
+
+var (
+ InvalidActionFormat = errors.New("Invalid Action Format")
+)
+
+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 {
+ log.Println(m)
+ return nil, InvalidActionFormat
+ }
+ _action := string(m[2])
+ if _action == "pick" {
+ p := s.PlayerByName(string(m[1]))
+ if p == nil {
+ err = errors.New("Unknown player")
+ return
+ }
+ _p := strings.Split(string(m[3]), ":")
+ pick, err := NewCardSafe(_p[0])
+ if err != nil {
+ return nil, err
+ }
+ pack := NewPileOfCards()
+ err = pack.FromString(_p[1])
+ if err != nil {
+ return nil, err
+ }
+ a = NewDraftPick(p, pack, pick)
+ } else if _action == "pass" {
+ p := s.PlayerByName(string(m[1]))
+ if p == nil {
+ err = errors.New("Unknown player")
+ return
+ }
+ a = NewPassPriority(p)
+ }
+ return
+}