add new mapblock+pos type / refactor listeners

This commit is contained in:
BuckarooBanzay 2022-01-30 13:29:47 +01:00
parent 73c93af328
commit 571825ef88
7 changed files with 62 additions and 28 deletions

View File

@ -0,0 +1,13 @@
package coords
type MapblockIterator func(x, y, z int)
func IterateMapblock(it MapblockIterator) {
for x := 0; x < 16; x++ {
for y := 0; y < 16; y++ {
for z := 0; z < 16; z++ {
it(x, y, z)
}
}
}
}

View File

@ -3,6 +3,7 @@ package mapblockaccessor
import ( import (
"mapserver/coords" "mapserver/coords"
"mapserver/eventbus" "mapserver/eventbus"
"mapserver/types"
"sync" "sync"
"github.com/minetest-go/mapparser" "github.com/minetest-go/mapparser"
@ -84,7 +85,7 @@ func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.M
return nil, err return nil, err
} }
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock) a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, pos))
cacheBlockCount.Inc() cacheBlockCount.Inc()
a.blockcache.Set(key, mapblock, cache.DefaultExpiration) a.blockcache.Set(key, mapblock, cache.DefaultExpiration)

View File

@ -4,6 +4,7 @@ import (
"mapserver/eventbus" "mapserver/eventbus"
"mapserver/layer" "mapserver/layer"
"mapserver/settings" "mapserver/settings"
"mapserver/types"
"github.com/minetest-go/mapparser" "github.com/minetest-go/mapparser"
cache "github.com/patrickmn/go-cache" cache "github.com/patrickmn/go-cache"
@ -12,7 +13,7 @@ import (
type FindNextLegacyBlocksResult struct { type FindNextLegacyBlocksResult struct {
HasMore bool HasMore bool
List []*mapparser.MapBlock List []*types.ParsedMapblock
UnfilteredCount int UnfilteredCount int
Progress float64 Progress float64
LastMtime int64 LastMtime int64
@ -29,7 +30,7 @@ func (a *MapBlockAccessor) FindNextLegacyBlocks(s settings.Settings, layers []*l
blocks := nextResult.List blocks := nextResult.List
result := FindNextLegacyBlocksResult{} result := FindNextLegacyBlocksResult{}
mblist := make([]*mapparser.MapBlock, 0) mblist := make([]*types.ParsedMapblock, 0)
result.HasMore = nextResult.HasMore result.HasMore = nextResult.HasMore
result.UnfilteredCount = nextResult.UnfilteredCount result.UnfilteredCount = nextResult.UnfilteredCount
result.Progress = nextResult.Progress result.Progress = nextResult.Progress
@ -59,11 +60,11 @@ func (a *MapBlockAccessor) FindNextLegacyBlocks(s settings.Settings, layers []*l
return nil, err return nil, err
} }
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock) a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, block.Pos))
a.blockcache.Set(key, mapblock, cache.DefaultExpiration) a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
cacheBlockCount.Inc() cacheBlockCount.Inc()
mblist = append(mblist, mapblock) mblist = append(mblist, types.NewParsedMapblock(mapblock, block.Pos))
} }

View File

@ -4,6 +4,7 @@ import (
"mapserver/coords" "mapserver/coords"
"mapserver/eventbus" "mapserver/eventbus"
"mapserver/layer" "mapserver/layer"
"mapserver/types"
"github.com/minetest-go/mapparser" "github.com/minetest-go/mapparser"
cache "github.com/patrickmn/go-cache" cache "github.com/patrickmn/go-cache"
@ -15,7 +16,7 @@ type FindMapBlocksByMtimeResult struct {
HasMore bool HasMore bool
LastPos *coords.MapBlockCoords LastPos *coords.MapBlockCoords
LastMtime int64 LastMtime int64
List []*mapparser.MapBlock List []*types.ParsedMapblock
UnfilteredCount int UnfilteredCount int
} }
@ -39,7 +40,7 @@ func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, laye
result := FindMapBlocksByMtimeResult{} result := FindMapBlocksByMtimeResult{}
mblist := make([]*mapparser.MapBlock, 0) mblist := make([]*types.ParsedMapblock, 0)
var newlastpos *coords.MapBlockCoords var newlastpos *coords.MapBlockCoords
result.HasMore = len(blocks) == limit result.HasMore = len(blocks) == limit
result.UnfilteredCount = len(blocks) result.UnfilteredCount = len(blocks)
@ -77,11 +78,11 @@ func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, laye
continue continue
} }
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock) a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, block.Pos))
a.blockcache.Set(key, mapblock, cache.DefaultExpiration) a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
cacheBlockCount.Inc() cacheBlockCount.Inc()
mblist = append(mblist, mapblock) mblist = append(mblist, types.NewParsedMapblock(mapblock, block.Pos))
} }

View File

@ -2,19 +2,21 @@ package mapobject
import ( import (
"mapserver/app" "mapserver/app"
"mapserver/coords"
"mapserver/eventbus" "mapserver/eventbus"
"mapserver/mapblockparser"
"mapserver/mapobjectdb" "mapserver/mapobjectdb"
"mapserver/types"
"github.com/minetest-go/mapparser"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type MapObjectListener interface { type MapObjectListener interface {
onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject onMapObject(mbpos *coords.MapBlockCoords, x, y, z int, block *mapparser.MapBlock) *mapobjectdb.MapObject
} }
type MapMultiObjectListener interface { type MapMultiObjectListener interface {
onMapObject(x, y, z int, block *mapblockparser.MapBlock) []*mapobjectdb.MapObject onMapObject(mbpos *coords.MapBlockCoords, x, y, z int, block *mapparser.MapBlock) []*mapobjectdb.MapObject
} }
type Listener struct { type Listener struct {
@ -36,26 +38,26 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
return return
} }
block := o.(*mapblockparser.MapBlock) pmb := o.(*types.ParsedMapblock)
err := this.ctx.Objectdb.RemoveMapData(block.Pos) err := this.ctx.Objectdb.RemoveMapData(pmb.Pos)
if err != nil { if err != nil {
panic(err) panic(err)
} }
this.ctx.WebEventbus.Emit("mapobjects-cleared", block.Pos) this.ctx.WebEventbus.Emit("mapobjects-cleared", pmb.Pos)
//TODO: refactor into single loop //TODO: refactor into single loop
for id, name := range block.BlockMapping { for id, name := range pmb.Mapblock.BlockMapping {
for k, v := range this.multiobjectlisteners { for k, v := range this.multiobjectlisteners {
if k == name { if k == name {
//block matches //block matches
mapblockparser.IterateMapblock(func(x, y, z int) { coords.IterateMapblock(func(x, y, z int) {
nodeid := block.GetNodeId(x, y, z) nodeid := pmb.Mapblock.GetNodeId(x, y, z)
if nodeid == id { if nodeid == id {
fields := logrus.Fields{ fields := logrus.Fields{
"mbpos": block.Pos, "mbpos": pmb.Pos,
"x": x, "x": x,
"y": y, "y": y,
"z": z, "z": z,
@ -64,14 +66,14 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
} }
log.WithFields(fields).Debug("OnEvent()") log.WithFields(fields).Debug("OnEvent()")
objs := v.onMapObject(x, y, z, block) objs := v.onMapObject(pmb.Pos, x, y, z, pmb.Mapblock)
if len(objs) > 0 { if len(objs) > 0 {
for _, obj := range objs { for _, obj := range objs {
err := this.ctx.Objectdb.AddMapData(obj) err := this.ctx.Objectdb.AddMapData(obj)
if err != nil { if err != nil {
fields = logrus.Fields{ fields = logrus.Fields{
"mbpos": block.Pos, "mbpos": pmb.Pos,
"x": x, "x": x,
"y": y, "y": y,
"z": z, "z": z,
@ -95,11 +97,11 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
for k, v := range this.objectlisteners { for k, v := range this.objectlisteners {
if k == name { if k == name {
//block matches //block matches
mapblockparser.IterateMapblock(func(x, y, z int) { coords.IterateMapblock(func(x, y, z int) {
nodeid := block.GetNodeId(x, y, z) nodeid := pmb.Mapblock.GetNodeId(x, y, z)
if nodeid == id { if nodeid == id {
fields := logrus.Fields{ fields := logrus.Fields{
"mbpos": block.Pos, "mbpos": pmb.Pos,
"x": x, "x": x,
"y": y, "y": y,
"z": z, "z": z,
@ -108,13 +110,13 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
} }
log.WithFields(fields).Debug("OnEvent()") log.WithFields(fields).Debug("OnEvent()")
obj := v.onMapObject(x, y, z, block) obj := v.onMapObject(pmb.Pos, x, y, z, pmb.Mapblock)
if obj != nil { if obj != nil {
err := this.ctx.Objectdb.AddMapData(obj) err := this.ctx.Objectdb.AddMapData(obj)
if err != nil { if err != nil {
fields = logrus.Fields{ fields = logrus.Fields{
"mbpos": block.Pos, "mbpos": pmb.Pos,
"x": x, "x": x,
"y": y, "y": y,
"z": z, "z": z,

View File

@ -3,9 +3,9 @@ package tilerendererjob
import ( import (
"mapserver/app" "mapserver/app"
"mapserver/coords" "mapserver/coords"
"mapserver/types"
"strconv" "strconv"
"github.com/minetest-go/mapparser"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -14,7 +14,7 @@ func getTileKey(tc *coords.TileCoords) string {
strconv.Itoa(tc.Zoom) + "/" + strconv.Itoa(tc.LayerId) strconv.Itoa(tc.Zoom) + "/" + strconv.Itoa(tc.LayerId)
} }
func renderMapblocks(ctx *app.App, mblist []*mapparser.MapBlock) int { func renderMapblocks(ctx *app.App, mblist []*types.ParsedMapblock) int {
tileRenderedMap := make(map[string]bool) tileRenderedMap := make(map[string]bool)
tilecount := 0 tilecount := 0
totalRenderedMapblocks.Add(float64(len(mblist))) totalRenderedMapblocks.Add(float64(len(mblist)))

16
types/parsedmapblock.go Normal file
View File

@ -0,0 +1,16 @@
package types
import (
"mapserver/coords"
"github.com/minetest-go/mapparser"
)
type ParsedMapblock struct {
Mapblock *mapparser.MapBlock
Pos *coords.MapBlockCoords
}
func NewParsedMapblock(mb *mapparser.MapBlock, pos *coords.MapBlockCoords) *ParsedMapblock {
return &ParsedMapblock{Mapblock: mb, Pos: pos}
}