more cleaning up zug zug

master
Jordan Orelli 4 years ago
parent ad6dc3d6b5
commit 352693b2d3

@ -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() {

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

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

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

Loading…
Cancel
Save