From a4c60c2fd856b8f4e28ce152b26c60143a02f0af Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 27 Oct 2020 04:14:10 +0000 Subject: [PATCH] tagging was bad --- internal/sim/player.go | 3 +-- internal/sim/world.go | 5 ++-- internal/wire/error.go | 10 +++++--- internal/wire/ok.go | 9 +++++-- internal/wire/request.go | 2 +- internal/wire/response.go | 2 +- internal/wire/self.go | 2 +- internal/wire/tag.go | 51 --------------------------------------- internal/wire/value.go | 23 +++++++++++++++++- 9 files changed, 42 insertions(+), 65 deletions(-) delete mode 100644 internal/wire/tag.go diff --git a/internal/sim/player.go b/internal/sim/player.go index 33db8bb..3d6b1c9 100644 --- a/internal/sim/player.go +++ b/internal/sim/player.go @@ -9,8 +9,7 @@ import ( // player represents a player character in the simulation type player struct { *blammo.Log - sessionID int - entityID int + entityID int } func (p *player) update(dt time.Duration) { diff --git a/internal/sim/world.go b/internal/sim/world.go index d8ba618..7f3e987 100644 --- a/internal/sim/world.go +++ b/internal/sim/world.go @@ -61,9 +61,8 @@ func (w *World) SpawnPlayer(id int) int { w.Info("spawning player with id: %d into room %q", id, r.name) t := &r.tiles[0] p := player{ - Log: w.Child("players").Child(strconv.Itoa(id)), - sessionID: id, - entityID: w.lastEntityID, + Log: w.Child("players").Child(strconv.Itoa(id)), + entityID: w.lastEntityID, } t.addEntity(&p) return p.entityID diff --git a/internal/wire/error.go b/internal/wire/error.go index 03dd6f5..6ce0865 100644 --- a/internal/wire/error.go +++ b/internal/wire/error.go @@ -8,10 +8,14 @@ type Error struct { val error } -func (e Error) Error() string { return e.val.Error() } -func (e Error) NetTag() Tag { return T_Error } -func (e Error) Unwrap() error { return e.val } +func (e Error) Error() string { return e.val.Error() } +func (e Error) NetTag() string { return "error" } +func (e Error) Unwrap() error { return e.val } func Errorf(t string, args ...interface{}) Error { return Error{val: fmt.Errorf(t, args...)} } + +func init() { + Register(func() Value { return new(Error) }) +} diff --git a/internal/wire/ok.go b/internal/wire/ok.go index 8cb6adc..f416341 100644 --- a/internal/wire/ok.go +++ b/internal/wire/ok.go @@ -1,4 +1,9 @@ package wire -type OK struct {} -func (OK) NetTag() Tag { return T_OK } +type OK struct{} + +func (OK) NetTag() string { return "ok" } + +func init() { + Register(func() Value { return new(OK) }) +} diff --git a/internal/wire/request.go b/internal/wire/request.go index f23a6d4..b7b519c 100644 --- a/internal/wire/request.go +++ b/internal/wire/request.go @@ -2,7 +2,7 @@ package wire type Request struct { Seq int `json:"seq"` - Type Tag `json:"type"` + Type string `json:"type"` Body interface{} `json:"body"` } diff --git a/internal/wire/response.go b/internal/wire/response.go index 29c19e6..5cbc28e 100644 --- a/internal/wire/response.go +++ b/internal/wire/response.go @@ -2,7 +2,7 @@ package wire type Response struct { Re int `json:"re,omitempty"` - Type Tag `json:"type"` + Type string `json:"type"` Body interface{} `json:"body"` } diff --git a/internal/wire/self.go b/internal/wire/self.go index dc5d3f4..c3e3533 100644 --- a/internal/wire/self.go +++ b/internal/wire/self.go @@ -6,4 +6,4 @@ type Self_Move struct { Y int `json:"y"` } -func (Self_Move) NetTag() Tag { return T_Client_Move } +func (Self_Move) NetTag() string { return "self/move" } diff --git a/internal/wire/tag.go b/internal/wire/tag.go deleted file mode 100644 index 2bacf7a..0000000 --- a/internal/wire/tag.go +++ /dev/null @@ -1,51 +0,0 @@ -package wire - -import ( - "encoding/json" - "fmt" -) - -type Tag uint - -const ( - T_None Tag = iota - T_Error - T_OK - T_Client_Move -) - -func (t Tag) String() string { - switch t { - case T_Error: - return "error" - case T_OK: - return "ok" - case T_Client_Move: - return "self/move" - default: - panic("unknown type tag") - } -} - -func (t *Tag) UnmarshalJSON(b []byte) error { - var name string - if err := json.Unmarshal(b, &name); err != nil { - return err - } - - switch name { - case "error": - *t = T_Error - return nil - 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) - } -} - -func (t Tag) MarshalJSON() ([]byte, error) { return json.Marshal(t.String()) } diff --git a/internal/wire/value.go b/internal/wire/value.go index 8ea5050..2cf53e4 100644 --- a/internal/wire/value.go +++ b/internal/wire/value.go @@ -1,3 +1,24 @@ package wire -type Value interface{ NetTag() Tag } +type Value interface { + NetTag() string +} + +var registry = make(map[string]func() Value) + +func Register(f func() Value) { + v := f() + t := v.NetTag() + if _, exists := registry[t]; exists { + panic("cannot register type: a type already exists with tag " + t) + } + registry[t] = f +} + +func New(name string) Value { + f, ok := registry[name] + if !ok { + return nil + } + return f() +}