tagging was bad

master
Jordan Orelli 4 years ago
parent 69902a8009
commit a4c60c2fd8

@ -9,7 +9,6 @@ import (
// player represents a player character in the simulation
type player struct {
*blammo.Log
sessionID int
entityID int
}

@ -62,7 +62,6 @@ func (w *World) SpawnPlayer(id int) int {
t := &r.tiles[0]
p := player{
Log: w.Child("players").Child(strconv.Itoa(id)),
sessionID: id,
entityID: w.lastEntityID,
}
t.addEntity(&p)

@ -9,9 +9,13 @@ type Error struct {
}
func (e Error) Error() string { return e.val.Error() }
func (e Error) NetTag() Tag { return T_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) })
}

@ -1,4 +1,9 @@
package wire
type OK struct{}
func (OK) NetTag() Tag { return T_OK }
func (OK) NetTag() string { return "ok" }
func init() {
Register(func() Value { return new(OK) })
}

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

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

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

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

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

Loading…
Cancel
Save