tagging was bad

master
Jordan Orelli 4 years ago
parent 69902a8009
commit a4c60c2fd8

@ -9,8 +9,7 @@ import (
// player represents a player character in the simulation // player represents a player character in the simulation
type player struct { type player struct {
*blammo.Log *blammo.Log
sessionID int entityID int
entityID int
} }
func (p *player) update(dt time.Duration) { func (p *player) update(dt time.Duration) {

@ -61,9 +61,8 @@ func (w *World) SpawnPlayer(id int) int {
w.Info("spawning player with id: %d into room %q", id, r.name) w.Info("spawning player with id: %d into room %q", id, r.name)
t := &r.tiles[0] t := &r.tiles[0]
p := player{ p := player{
Log: w.Child("players").Child(strconv.Itoa(id)), Log: w.Child("players").Child(strconv.Itoa(id)),
sessionID: id, entityID: w.lastEntityID,
entityID: w.lastEntityID,
} }
t.addEntity(&p) t.addEntity(&p)
return p.entityID return p.entityID

@ -8,10 +8,14 @@ type Error struct {
val error val error
} }
func (e Error) Error() string { return e.val.Error() } 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 (e Error) Unwrap() error { return e.val }
func Errorf(t string, args ...interface{}) Error { func Errorf(t string, args ...interface{}) Error {
return Error{val: fmt.Errorf(t, args...)} return Error{val: fmt.Errorf(t, args...)}
} }
func init() {
Register(func() Value { return new(Error) })
}

@ -1,4 +1,9 @@
package wire package wire
type OK struct {} 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 { type Request struct {
Seq int `json:"seq"` Seq int `json:"seq"`
Type Tag `json:"type"` Type string `json:"type"`
Body interface{} `json:"body"` Body interface{} `json:"body"`
} }

@ -2,7 +2,7 @@ package wire
type Response struct { type Response struct {
Re int `json:"re,omitempty"` Re int `json:"re,omitempty"`
Type Tag `json:"type"` Type string `json:"type"`
Body interface{} `json:"body"` Body interface{} `json:"body"`
} }

@ -6,4 +6,4 @@ type Self_Move struct {
Y int `json:"y"` 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 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