diff --git a/client.go b/client.go index 9b252eb..a1bcfe7 100644 --- a/client.go +++ b/client.go @@ -10,6 +10,7 @@ import ( "time" "github.com/gorilla/websocket" + "github.com/jordanorelli/astro-domu/internal/wire" "github.com/jordanorelli/blammo" ) @@ -19,11 +20,11 @@ type client struct { port int lastSeq int conn *websocket.Conn - outbox chan requestBody + outbox chan wire.Request } func (c *client) run(ctx context.Context) { - c.outbox = make(chan requestBody) + c.outbox = make(chan wire.Request) dialer := websocket.Dialer{ HandshakeTimeout: 3 * time.Second, @@ -92,23 +93,9 @@ func (c *client) run(ctx context.Context) { } } -func (c *client) send(cmd string, args map[string]interface{}) { - eargs := make(map[string]json.RawMessage, len(args)) - for k, v := range args { - b, err := json.Marshal(v) - if err != nil { - c.Error("failed to marshal an argument: %v", err) - return - } - eargs[k] = json.RawMessage(b) - } +func (c *client) send(v wire.Value) { c.lastSeq++ - req := requestBody{ - Seq: c.lastSeq, - Command: cmd, - Args: eargs, - } - c.outbox <- req + c.outbox <- wire.NewRequest(c.lastSeq, v) } func (c *client) readLoop() { diff --git a/internal/wire/request.go b/internal/wire/request.go index 9064657..f23a6d4 100644 --- a/internal/wire/request.go +++ b/internal/wire/request.go @@ -2,6 +2,14 @@ package wire type Request struct { Seq int `json:"seq"` - Type string `json:"type"` + Type Tag `json:"type"` Body interface{} `json:"body"` } + +func NewRequest(seq int, v Value) Request { + return Request{ + Seq: seq, + Type: v.NetTag(), + Body: v, + } +} diff --git a/internal/wire/self.go b/internal/wire/self.go new file mode 100644 index 0000000..dc5d3f4 --- /dev/null +++ b/internal/wire/self.go @@ -0,0 +1,9 @@ +package wire + +type Self_Move struct { + Delta bool `json:"delta"` + X int `json:"x"` + Y int `json:"y"` +} + +func (Self_Move) NetTag() Tag { return T_Client_Move } diff --git a/internal/wire/tag.go b/internal/wire/tag.go index 2b41c65..2bacf7a 100644 --- a/internal/wire/tag.go +++ b/internal/wire/tag.go @@ -11,6 +11,7 @@ const ( T_None Tag = iota T_Error T_OK + T_Client_Move ) func (t Tag) String() string { @@ -19,6 +20,8 @@ func (t Tag) String() string { return "error" case T_OK: return "ok" + case T_Client_Move: + return "self/move" default: panic("unknown type tag") } @@ -37,6 +40,9 @@ func (t *Tag) UnmarshalJSON(b []byte) error { case "ok": *t = T_OK return nil + case "self/move": + *t = T_Client_Move + return nil default: return fmt.Errorf("unknown type tag: %q", name) } diff --git a/ui_mode.go b/ui_mode.go index 2cda515..7675c1f 100644 --- a/ui_mode.go +++ b/ui_mode.go @@ -2,6 +2,7 @@ package main import ( "github.com/gdamore/tcell/v2" + "github.com/jordanorelli/astro-domu/internal/wire" ) type uiMode interface { @@ -22,16 +23,16 @@ func (m *boxWalker) handleEvent(ui *ui, e tcell.Event) bool { if key == tcell.KeyRune { switch v.Rune() { case 'w': - ui.client.send("self/move", map[string]interface{}{"delta": []int{0, -1}}) + ui.client.send(wire.Self_Move{Delta: true, X: 0, Y: -1}) m.move(0, -1) case 'a': - ui.client.send("self/move", map[string]interface{}{"delta": []int{-1, 0}}) + ui.client.send(wire.Self_Move{Delta: true, X: -1, Y: 0}) m.move(-1, 0) case 's': - ui.client.send("self/move", map[string]interface{}{"delta": []int{0, 1}}) + ui.client.send(wire.Self_Move{Delta: true, X: 0, Y: 1}) m.move(0, 1) case 'd': - ui.client.send("self/move", map[string]interface{}{"delta": []int{1, 0}}) + ui.client.send(wire.Self_Move{Delta: true, X: 1, Y: 0}) m.move(1, 0) } }