Simplified player forwarding. Forward empty player list

This commit is contained in:
Sascha L. Teichmann 2022-02-28 15:49:14 +01:00
parent 1ec9d8916b
commit db45013f4e
2 changed files with 8 additions and 29 deletions

View File

@ -27,8 +27,8 @@ type connection struct {
} }
type msg struct { type msg struct {
tiles []xz Tiles []xz `json:"tiles,omitempty"`
pls []*player Pls []*player `json:"pls,omitempty"`
} }
func newWebsocketForwarder() *websocketForwarder { func newWebsocketForwarder() *websocketForwarder {
@ -55,19 +55,10 @@ func (wsf *websocketForwarder) run() {
if len(wsf.connections) == 0 { if len(wsf.connections) == 0 {
continue continue
} }
encMsg := map[string]interface{}{}
if message.tiles != nil {
encMsg["tiles"] = message.tiles
}
if message.pls != nil {
encMsg["players"] = message.pls
}
var buf bytes.Buffer var buf bytes.Buffer
encoder := json.NewEncoder(&buf) encoder := json.NewEncoder(&buf)
if err := encoder.Encode(encMsg); err != nil { if err := encoder.Encode(message); err != nil {
log.Printf("encoding changes failed: %s\n", err) log.Printf("encoding changes failed: %s\n", err)
continue continue
} }
@ -98,11 +89,11 @@ func (wsf *websocketForwarder) ServeHTTP(rw http.ResponseWriter, r *http.Request
} }
func (wsf *websocketForwarder) BaseTilesUpdated(changes []xz) { func (wsf *websocketForwarder) BaseTilesUpdated(changes []xz) {
wsf.broadcast <- msg{tiles: changes} wsf.broadcast <- msg{Tiles: changes}
} }
func (wsf *websocketForwarder) BroadcastPlayers(pls []*player) { func (wsf *websocketForwarder) BroadcastPlayers(pls []*player) {
wsf.broadcast <- msg{pls: pls} wsf.broadcast <- msg{Pls: pls}
} }
func (c *connection) writer() { func (c *connection) writer() {

View File

@ -71,20 +71,6 @@ func (p *player) same(o *player) bool {
math.Abs(p.Z-o.Z) < 0.000001 math.Abs(p.Z-o.Z) < 0.000001
} }
type sortPlayersByName []*player
func (pls sortPlayersByName) Len() int {
return len(pls)
}
func (pls sortPlayersByName) Less(i, j int) bool {
return pls[i].Name < pls[j].Name
}
func (pls sortPlayersByName) Swap(i, j int) {
pls[i], pls[j] = pls[j], pls[i]
}
const selectPlayersSQL = ` const selectPlayersSQL = `
SELECT posx/10.0, posy/10.0, posz/10.0, name SELECT posx/10.0, posy/10.0, posz/10.0, name
FROM player FROM player
@ -158,7 +144,9 @@ func (ps *players) run() {
continue continue
} }
//log.Printf("%+q\n", pls) //log.Printf("%+q\n", pls)
sort.Sort(sortPlayersByName(pls)) sort.Slice(pls, func(i, j int) bool {
return pls[i].Name < pls[j].Name
})
var change bool var change bool
ps.mu.Lock() ps.mu.Lock()
//log.Printf("%+q\n", pls) //log.Printf("%+q\n", pls)