more cleaning up zug zug

master
Jordan Orelli 4 years ago
parent ad6dc3d6b5
commit 352693b2d3

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/jordanorelli/astro-domu/internal/wire"
"github.com/jordanorelli/blammo" "github.com/jordanorelli/blammo"
) )
@ -19,11 +20,11 @@ type client struct {
port int port int
lastSeq int lastSeq int
conn *websocket.Conn conn *websocket.Conn
outbox chan requestBody outbox chan wire.Request
} }
func (c *client) run(ctx context.Context) { func (c *client) run(ctx context.Context) {
c.outbox = make(chan requestBody) c.outbox = make(chan wire.Request)
dialer := websocket.Dialer{ dialer := websocket.Dialer{
HandshakeTimeout: 3 * time.Second, 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{}) { func (c *client) send(v wire.Value) {
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)
}
c.lastSeq++ c.lastSeq++
req := requestBody{ c.outbox <- wire.NewRequest(c.lastSeq, v)
Seq: c.lastSeq,
Command: cmd,
Args: eargs,
}
c.outbox <- req
} }
func (c *client) readLoop() { func (c *client) readLoop() {

@ -2,6 +2,14 @@ package wire
type Request struct { type Request struct {
Seq int `json:"seq"` Seq int `json:"seq"`
Type string `json:"type"` Type Tag `json:"type"`
Body interface{} `json:"body"` Body interface{} `json:"body"`
} }
func NewRequest(seq int, v Value) Request {
return Request{
Seq: seq,
Type: v.NetTag(),
Body: v,
}
}

@ -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 }

@ -11,6 +11,7 @@ const (
T_None Tag = iota T_None Tag = iota
T_Error T_Error
T_OK T_OK
T_Client_Move
) )
func (t Tag) String() string { func (t Tag) String() string {
@ -19,6 +20,8 @@ func (t Tag) String() string {
return "error" return "error"
case T_OK: case T_OK:
return "ok" return "ok"
case T_Client_Move:
return "self/move"
default: default:
panic("unknown type tag") panic("unknown type tag")
} }
@ -37,6 +40,9 @@ func (t *Tag) UnmarshalJSON(b []byte) error {
case "ok": case "ok":
*t = T_OK *t = T_OK
return nil return nil
case "self/move":
*t = T_Client_Move
return nil
default: default:
return fmt.Errorf("unknown type tag: %q", name) return fmt.Errorf("unknown type tag: %q", name)
} }

@ -2,6 +2,7 @@ package main
import ( import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/wire"
) )
type uiMode interface { type uiMode interface {
@ -22,16 +23,16 @@ func (m *boxWalker) handleEvent(ui *ui, e tcell.Event) bool {
if key == tcell.KeyRune { if key == tcell.KeyRune {
switch v.Rune() { switch v.Rune() {
case 'w': 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) m.move(0, -1)
case 'a': 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) m.move(-1, 0)
case 's': 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) m.move(0, 1)
case 'd': 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) m.move(1, 0)
} }
} }

Loading…
Cancel
Save