forked from MTSR/mapserver
cleaner eventbus
This commit is contained in:
parent
f00016e5e6
commit
d03d120df6
@ -3,6 +3,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"mapserver/colormapping"
|
"mapserver/colormapping"
|
||||||
"mapserver/db"
|
"mapserver/db"
|
||||||
|
"mapserver/eventbus"
|
||||||
"mapserver/mapblockaccessor"
|
"mapserver/mapblockaccessor"
|
||||||
"mapserver/mapblockrenderer"
|
"mapserver/mapblockrenderer"
|
||||||
"mapserver/mapobjectdb"
|
"mapserver/mapobjectdb"
|
||||||
@ -27,4 +28,6 @@ type App struct {
|
|||||||
Colormapping *colormapping.ColorMapping
|
Colormapping *colormapping.ColorMapping
|
||||||
Mapblockrenderer *mapblockrenderer.MapBlockRenderer
|
Mapblockrenderer *mapblockrenderer.MapBlockRenderer
|
||||||
Tilerenderer *tilerenderer.TileRenderer
|
Tilerenderer *tilerenderer.TileRenderer
|
||||||
|
|
||||||
|
WebEventbus *eventbus.Eventbus
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"mapserver/colormapping"
|
"mapserver/colormapping"
|
||||||
"mapserver/db"
|
"mapserver/db"
|
||||||
|
"mapserver/eventbus"
|
||||||
"mapserver/mapblockaccessor"
|
"mapserver/mapblockaccessor"
|
||||||
"mapserver/mapblockrenderer"
|
"mapserver/mapblockrenderer"
|
||||||
"mapserver/mapobjectdb"
|
"mapserver/mapobjectdb"
|
||||||
@ -19,6 +20,7 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
|
|||||||
a := App{}
|
a := App{}
|
||||||
a.Params = p
|
a.Params = p
|
||||||
a.Config = cfg
|
a.Config = cfg
|
||||||
|
a.WebEventbus = eventbus.New()
|
||||||
|
|
||||||
//Parse world config
|
//Parse world config
|
||||||
a.Worldconfig = worldconfig.Parse("world.mt")
|
a.Worldconfig = worldconfig.Parse("world.mt")
|
||||||
@ -37,7 +39,6 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//migrate block db
|
//migrate block db
|
||||||
|
|
||||||
err = a.Blockdb.Migrate()
|
err = a.Blockdb.Migrate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -47,7 +48,6 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
|
|||||||
a.BlockAccessor = mapblockaccessor.NewMapBlockAccessor(a.Blockdb)
|
a.BlockAccessor = mapblockaccessor.NewMapBlockAccessor(a.Blockdb)
|
||||||
|
|
||||||
//color mapping
|
//color mapping
|
||||||
|
|
||||||
a.Colormapping = colormapping.NewColorMapping()
|
a.Colormapping = colormapping.NewColorMapping()
|
||||||
err = a.Colormapping.LoadVFSColors(false, "/colors.txt")
|
err = a.Colormapping.LoadVFSColors(false, "/colors.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -58,7 +58,6 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
|
|||||||
a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping)
|
a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping)
|
||||||
|
|
||||||
//tile database
|
//tile database
|
||||||
|
|
||||||
a.Objectdb, err = mapobjectdb.NewSqliteAccessor("mapserver.sqlite")
|
a.Objectdb, err = mapobjectdb.NewSqliteAccessor("mapserver.sqlite")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -66,7 +65,6 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//migrate tile database
|
//migrate tile database
|
||||||
|
|
||||||
err = a.Objectdb.Migrate()
|
err = a.Objectdb.Migrate()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
//TODO: move to separate package
|
||||||
MAPBLOCK_RENDERED = "mapblock-rendered"
|
MAPBLOCK_RENDERED = "mapblock-rendered"
|
||||||
TILE_RENDERED = "rendered-tile"
|
TILE_RENDERED = "rendered-tile"
|
||||||
)
|
)
|
||||||
|
@ -47,7 +47,7 @@ func (mb *MapBlock) GetNodeId(x, y, z int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (mb *MapBlock) GetNodeName(x, y, z int) string {
|
func (mb *MapBlock) GetNodeName(x, y, z int) string {
|
||||||
id := mb.GetNodeId(x,y,z)
|
id := mb.GetNodeId(x, y, z)
|
||||||
return mb.BlockMapping[id]
|
return mb.BlockMapping[id]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,9 +5,9 @@ import (
|
|||||||
"mapserver/mapobjectdb"
|
"mapserver/mapobjectdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PoiBlock struct {}
|
type PoiBlock struct{}
|
||||||
|
|
||||||
func (this *PoiBlock) onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor) {
|
func (this *PoiBlock) onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject {
|
||||||
md := block.Metadata.GetMetadata(x, y, z)
|
md := block.Metadata.GetMetadata(x, y, z)
|
||||||
|
|
||||||
o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "poi")
|
o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "poi")
|
||||||
@ -17,5 +17,5 @@ func (this *PoiBlock) onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb
|
|||||||
o.Attributes["active"] = md["active"]
|
o.Attributes["active"] = md["active"]
|
||||||
o.Attributes["owner"] = md["owner"]
|
o.Attributes["owner"] = md["owner"]
|
||||||
|
|
||||||
odb.AddMapData(o)
|
return o
|
||||||
}
|
}
|
||||||
|
@ -5,13 +5,13 @@ import (
|
|||||||
"mapserver/mapobjectdb"
|
"mapserver/mapobjectdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProtectorBlock struct {}
|
type ProtectorBlock struct{}
|
||||||
|
|
||||||
func (this *ProtectorBlock) onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor) {
|
func (this *ProtectorBlock) onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject {
|
||||||
md := block.Metadata.GetMetadata(x, y, z)
|
md := block.Metadata.GetMetadata(x, y, z)
|
||||||
|
|
||||||
o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "protector")
|
o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "protector")
|
||||||
o.Attributes["owner"] = md["owner"]
|
o.Attributes["owner"] = md["owner"]
|
||||||
|
|
||||||
odb.AddMapData(o)
|
return o
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MapObjectListener interface {
|
type MapObjectListener interface {
|
||||||
onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor)
|
onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject
|
||||||
}
|
}
|
||||||
|
|
||||||
type Listener struct {
|
type Listener struct {
|
||||||
@ -16,7 +16,7 @@ type Listener struct {
|
|||||||
objectlisteners map[string]MapObjectListener
|
objectlisteners map[string]MapObjectListener
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Listener) AddMapObject(blockname string, ol MapObjectListener){
|
func (this *Listener) AddMapObject(blockname string, ol MapObjectListener) {
|
||||||
this.objectlisteners[blockname] = ol
|
this.objectlisteners[blockname] = ol
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,15 +42,20 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
|
|||||||
for z := 0; z < 16; z++ {
|
for z := 0; z < 16; z++ {
|
||||||
nodeid := block.GetNodeId(x, y, z)
|
nodeid := block.GetNodeId(x, y, z)
|
||||||
if nodeid == id {
|
if nodeid == id {
|
||||||
v.onMapObject(x, y, z, block, this.ctx.Objectdb)
|
obj := v.onMapObject(x, y, z, block)
|
||||||
|
|
||||||
|
if obj != nil {
|
||||||
|
this.ctx.Objectdb.AddMapData(obj)
|
||||||
|
this.ctx.WebEventbus.Emit("mapobject", obj)
|
||||||
}
|
}
|
||||||
}//z
|
}
|
||||||
}//y
|
} //z
|
||||||
}//x
|
} //y
|
||||||
|
} //x
|
||||||
|
|
||||||
}
|
}
|
||||||
}//for k,v
|
} //for k,v
|
||||||
}//for id, name
|
} //for id, name
|
||||||
}
|
}
|
||||||
|
|
||||||
func Setup(ctx *app.App) {
|
func Setup(ctx *app.App) {
|
||||||
|
@ -5,9 +5,9 @@ import (
|
|||||||
"mapserver/mapobjectdb"
|
"mapserver/mapobjectdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TrainBlock struct {}
|
type TrainBlock struct{}
|
||||||
|
|
||||||
func (this *TrainBlock) onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor) {
|
func (this *TrainBlock) onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject {
|
||||||
md := block.Metadata.GetMetadata(x, y, z)
|
md := block.Metadata.GetMetadata(x, y, z)
|
||||||
|
|
||||||
o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "train")
|
o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "train")
|
||||||
@ -16,5 +16,5 @@ func (this *TrainBlock) onMapObject(x,y,z int, block *mapblockparser.MapBlock, o
|
|||||||
o.Attributes["index"] = md["index"]
|
o.Attributes["index"] = md["index"]
|
||||||
o.Attributes["owner"] = md["owner"]
|
o.Attributes["owner"] = md["owner"]
|
||||||
|
|
||||||
odb.AddMapData(o)
|
return o
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,14 @@ import (
|
|||||||
"mapserver/mapobjectdb"
|
"mapserver/mapobjectdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TravelnetBlock struct {}
|
type TravelnetBlock struct{}
|
||||||
|
|
||||||
func (this *TravelnetBlock) onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor) {
|
func (this *TravelnetBlock) onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject {
|
||||||
md := block.Metadata.GetMetadata(x, y, z)
|
md := block.Metadata.GetMetadata(x, y, z)
|
||||||
|
|
||||||
if md["station_name"] == "" || md["owner"] == "" {
|
if md["station_name"] == "" || md["owner"] == "" {
|
||||||
//station not set up
|
//station not set up
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "travelnet")
|
o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "travelnet")
|
||||||
@ -20,5 +20,5 @@ func (this *TravelnetBlock) onMapObject(x,y,z int, block *mapblockparser.MapBloc
|
|||||||
o.Attributes["station_name"] = md["station_name"]
|
o.Attributes["station_name"] = md["station_name"]
|
||||||
o.Attributes["station_network"] = md["station_network"]
|
o.Attributes["station_network"] = md["station_network"]
|
||||||
|
|
||||||
odb.AddMapData(o)
|
return o
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ func Serve(ctx *app.App) {
|
|||||||
mux.Handle("/api/ws", ws)
|
mux.Handle("/api/ws", ws)
|
||||||
|
|
||||||
ctx.Tilerenderer.Eventbus.AddListener(ws)
|
ctx.Tilerenderer.Eventbus.AddListener(ws)
|
||||||
|
ctx.WebEventbus.AddListener(ws)
|
||||||
|
|
||||||
if ctx.Config.WebApi.EnableMapblock {
|
if ctx.Config.WebApi.EnableMapblock {
|
||||||
//mapblock endpoint
|
//mapblock endpoint
|
||||||
|
Loading…
Reference in New Issue
Block a user