From cbfb594c83a4cc513a5f560f3ec5f2f826275ccc Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 9 Mar 2015 14:01:30 +0100 Subject: [PATCH] Added JSON marshaller for players. --- cmd/mtwebmapper/players.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/cmd/mtwebmapper/players.go b/cmd/mtwebmapper/players.go index f58d637..227049e 100644 --- a/cmd/mtwebmapper/players.go +++ b/cmd/mtwebmapper/players.go @@ -1,16 +1,47 @@ package main -import "net/http" +import ( + "bytes" + "html/template" + "net/http" +) + +var geoJSONTmpl = template.Must(template.New("geojson").Parse( + `{ "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [{{.X}}, {{.Z}}] + }, + "properties": { + "name": {{.Name || html }} + } +}`)) + +type player struct { + X float64 + Y float64 + Z float64 + Name string +} type players struct { fifo string wsf *websocketForwarder + pls []*player } func newPlayers(fifo string, wsf *websocketForwarder) *players { return &players{fifo: fifo, wsf: wsf} } +func (p *player) MarshalJSON() ([]byte, error) { + var buf bytes.Buffer + if err := geoJSONTmpl.Execute(&buf, p); err != nil { + return nil, err + } + return buf.Bytes(), nil +} + func (ps *players) run() { for { // TODO: Implement me!