diff --git a/server/app/app.go b/server/app/app.go index 602abdc..eb68804 100644 --- a/server/app/app.go +++ b/server/app/app.go @@ -3,6 +3,7 @@ package app import ( "mapserver/colormapping" "mapserver/db" + "mapserver/eventbus" "mapserver/mapblockaccessor" "mapserver/mapblockrenderer" "mapserver/mapobjectdb" @@ -27,4 +28,6 @@ type App struct { Colormapping *colormapping.ColorMapping Mapblockrenderer *mapblockrenderer.MapBlockRenderer Tilerenderer *tilerenderer.TileRenderer + + WebEventbus *eventbus.Eventbus } diff --git a/server/app/setup.go b/server/app/setup.go index c8639c6..6f72698 100644 --- a/server/app/setup.go +++ b/server/app/setup.go @@ -3,6 +3,7 @@ package app import ( "mapserver/colormapping" "mapserver/db" + "mapserver/eventbus" "mapserver/mapblockaccessor" "mapserver/mapblockrenderer" "mapserver/mapobjectdb" @@ -19,6 +20,7 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) { a := App{} a.Params = p a.Config = cfg + a.WebEventbus = eventbus.New() //Parse world config a.Worldconfig = worldconfig.Parse("world.mt") @@ -37,7 +39,6 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) { } //migrate block db - err = a.Blockdb.Migrate() if err != nil { return nil, err @@ -47,7 +48,6 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) { a.BlockAccessor = mapblockaccessor.NewMapBlockAccessor(a.Blockdb) //color mapping - a.Colormapping = colormapping.NewColorMapping() err = a.Colormapping.LoadVFSColors(false, "/colors.txt") if err != nil { @@ -58,7 +58,6 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) { a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping) //tile database - a.Objectdb, err = mapobjectdb.NewSqliteAccessor("mapserver.sqlite") if err != nil { @@ -66,7 +65,6 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) { } //migrate tile database - err = a.Objectdb.Migrate() if err != nil { diff --git a/server/eventbus/eventbus.go b/server/eventbus/eventbus.go index 4c07f88..0cb1501 100644 --- a/server/eventbus/eventbus.go +++ b/server/eventbus/eventbus.go @@ -5,6 +5,7 @@ import ( ) const ( + //TODO: move to separate package MAPBLOCK_RENDERED = "mapblock-rendered" TILE_RENDERED = "rendered-tile" ) diff --git a/server/mapblockparser/mapblock.go b/server/mapblockparser/mapblock.go index b46c1bd..bbac670 100644 --- a/server/mapblockparser/mapblock.go +++ b/server/mapblockparser/mapblock.go @@ -47,7 +47,7 @@ func (mb *MapBlock) GetNodeId(x, y, z int) int { } 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] } diff --git a/server/mapobject/poi.go b/server/mapobject/poi.go index 4addfab..603fa99 100644 --- a/server/mapobject/poi.go +++ b/server/mapobject/poi.go @@ -5,9 +5,9 @@ import ( "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) 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["owner"] = md["owner"] - odb.AddMapData(o) + return o } diff --git a/server/mapobject/protector.go b/server/mapobject/protector.go index f687cf4..a876681 100644 --- a/server/mapobject/protector.go +++ b/server/mapobject/protector.go @@ -5,13 +5,13 @@ import ( "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) o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "protector") o.Attributes["owner"] = md["owner"] - odb.AddMapData(o) + return o } diff --git a/server/mapobject/setup.go b/server/mapobject/setup.go index 9706068..17d6749 100644 --- a/server/mapobject/setup.go +++ b/server/mapobject/setup.go @@ -8,15 +8,15 @@ import ( ) 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 { - ctx *app.App + ctx *app.App objectlisteners map[string]MapObjectListener } -func (this *Listener) AddMapObject(blockname string, ol MapObjectListener){ +func (this *Listener) AddMapObject(blockname string, ol MapObjectListener) { this.objectlisteners[blockname] = ol } @@ -42,20 +42,25 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) { for z := 0; z < 16; z++ { nodeid := block.GetNodeId(x, y, z) 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 - }//x + } //z + } //y + } //x } - }//for k,v - }//for id, name + } //for k,v + } //for id, name } func Setup(ctx *app.App) { l := Listener{ - ctx: ctx, + ctx: ctx, objectlisteners: make(map[string]MapObjectListener), } diff --git a/server/mapobject/train.go b/server/mapobject/train.go index 00f67e3..49de508 100644 --- a/server/mapobject/train.go +++ b/server/mapobject/train.go @@ -5,9 +5,9 @@ import ( "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) 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["owner"] = md["owner"] - odb.AddMapData(o) + return o } diff --git a/server/mapobject/travelnet.go b/server/mapobject/travelnet.go index eb3a37b..4ddbcbf 100644 --- a/server/mapobject/travelnet.go +++ b/server/mapobject/travelnet.go @@ -5,14 +5,14 @@ import ( "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) if md["station_name"] == "" || md["owner"] == "" { //station not set up - return + return nil } 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_network"] = md["station_network"] - odb.AddMapData(o) + return o } diff --git a/server/web/serve.go b/server/web/serve.go index 1495ab7..1d64781 100644 --- a/server/web/serve.go +++ b/server/web/serve.go @@ -26,6 +26,7 @@ func Serve(ctx *app.App) { mux.Handle("/api/ws", ws) ctx.Tilerenderer.Eventbus.AddListener(ws) + ctx.WebEventbus.AddListener(ws) if ctx.Config.WebApi.EnableMapblock { //mapblock endpoint