2019-02-15 11:11:07 +03:00
|
|
|
package mapobject
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mapserver/app"
|
2022-01-30 15:29:47 +03:00
|
|
|
"mapserver/coords"
|
2019-02-15 11:11:07 +03:00
|
|
|
"mapserver/eventbus"
|
|
|
|
"mapserver/mapobjectdb"
|
2022-01-30 15:29:47 +03:00
|
|
|
"mapserver/types"
|
2019-02-15 11:11:07 +03:00
|
|
|
|
2022-01-30 15:29:47 +03:00
|
|
|
"github.com/minetest-go/mapparser"
|
2019-02-15 11:11:07 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MapObjectListener interface {
|
2023-12-29 18:00:11 +03:00
|
|
|
onMapObject(mbpos *types.MapBlockCoords, x, y, z int, block *mapparser.MapBlock) *mapobjectdb.MapObject
|
2019-02-15 11:11:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type MapMultiObjectListener interface {
|
2023-12-29 18:00:11 +03:00
|
|
|
onMapObject(mbpos *types.MapBlockCoords, x, y, z int, block *mapparser.MapBlock) []*mapobjectdb.MapObject
|
2019-02-15 11:11:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Listener struct {
|
|
|
|
ctx *app.App
|
|
|
|
objectlisteners map[string]MapObjectListener
|
|
|
|
multiobjectlisteners map[string]MapMultiObjectListener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Listener) AddMapObject(blockname string, ol MapObjectListener) {
|
|
|
|
this.objectlisteners[blockname] = ol
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Listener) AddMapMultiObject(blockname string, ol MapMultiObjectListener) {
|
|
|
|
this.multiobjectlisteners[blockname] = ol
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Listener) OnEvent(eventtype string, o interface{}) {
|
|
|
|
if eventtype != eventbus.MAPBLOCK_RENDERED {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-30 15:29:47 +03:00
|
|
|
pmb := o.(*types.ParsedMapblock)
|
2019-02-15 11:11:07 +03:00
|
|
|
|
2022-01-30 15:29:47 +03:00
|
|
|
err := this.ctx.Objectdb.RemoveMapData(pmb.Pos)
|
2019-02-15 11:11:07 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-01-30 15:29:47 +03:00
|
|
|
this.ctx.WebEventbus.Emit("mapobjects-cleared", pmb.Pos)
|
2019-02-15 11:11:07 +03:00
|
|
|
|
2019-03-22 10:11:32 +03:00
|
|
|
//TODO: refactor into single loop
|
2022-01-30 15:29:47 +03:00
|
|
|
for id, name := range pmb.Mapblock.BlockMapping {
|
2019-02-15 11:11:07 +03:00
|
|
|
|
|
|
|
for k, v := range this.multiobjectlisteners {
|
|
|
|
if k == name {
|
|
|
|
//block matches
|
2022-01-30 15:29:47 +03:00
|
|
|
coords.IterateMapblock(func(x, y, z int) {
|
|
|
|
nodeid := pmb.Mapblock.GetNodeId(x, y, z)
|
2019-02-15 11:11:07 +03:00
|
|
|
if nodeid == id {
|
|
|
|
fields := logrus.Fields{
|
2022-01-30 15:29:47 +03:00
|
|
|
"mbpos": pmb.Pos,
|
2019-02-15 11:11:07 +03:00
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"z": z,
|
|
|
|
"type": name,
|
|
|
|
"nodeid": nodeid,
|
|
|
|
}
|
|
|
|
log.WithFields(fields).Debug("OnEvent()")
|
|
|
|
|
2022-01-30 15:29:47 +03:00
|
|
|
objs := v.onMapObject(pmb.Pos, x, y, z, pmb.Mapblock)
|
2019-02-15 11:11:07 +03:00
|
|
|
|
|
|
|
if len(objs) > 0 {
|
|
|
|
for _, obj := range objs {
|
2019-03-21 17:44:34 +03:00
|
|
|
err := this.ctx.Objectdb.AddMapData(obj)
|
|
|
|
if err != nil {
|
2019-03-22 10:11:32 +03:00
|
|
|
fields = logrus.Fields{
|
2022-01-30 15:29:47 +03:00
|
|
|
"mbpos": pmb.Pos,
|
2019-03-22 10:11:32 +03:00
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"z": z,
|
|
|
|
"type": name,
|
|
|
|
"obj": obj,
|
|
|
|
}
|
|
|
|
log.WithFields(fields).Error("AddMapData()")
|
|
|
|
|
|
|
|
//unrecoverable
|
2019-03-21 17:44:34 +03:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-02-15 11:11:07 +03:00
|
|
|
this.ctx.WebEventbus.Emit("mapobject-created", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} // k==name
|
|
|
|
} //for k,v
|
|
|
|
|
|
|
|
for k, v := range this.objectlisteners {
|
|
|
|
if k == name {
|
|
|
|
//block matches
|
2022-01-30 15:29:47 +03:00
|
|
|
coords.IterateMapblock(func(x, y, z int) {
|
|
|
|
nodeid := pmb.Mapblock.GetNodeId(x, y, z)
|
2019-02-15 11:11:07 +03:00
|
|
|
if nodeid == id {
|
|
|
|
fields := logrus.Fields{
|
2022-01-30 15:29:47 +03:00
|
|
|
"mbpos": pmb.Pos,
|
2019-02-15 11:11:07 +03:00
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"z": z,
|
|
|
|
"type": name,
|
|
|
|
"nodeid": nodeid,
|
|
|
|
}
|
|
|
|
log.WithFields(fields).Debug("OnEvent()")
|
|
|
|
|
2022-01-30 15:29:47 +03:00
|
|
|
obj := v.onMapObject(pmb.Pos, x, y, z, pmb.Mapblock)
|
2019-02-15 11:11:07 +03:00
|
|
|
|
|
|
|
if obj != nil {
|
2019-03-21 17:50:41 +03:00
|
|
|
err := this.ctx.Objectdb.AddMapData(obj)
|
|
|
|
if err != nil {
|
2019-03-22 10:11:32 +03:00
|
|
|
fields = logrus.Fields{
|
2022-01-30 15:29:47 +03:00
|
|
|
"mbpos": pmb.Pos,
|
2019-03-22 10:11:32 +03:00
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"z": z,
|
|
|
|
"type": name,
|
|
|
|
"obj": obj,
|
|
|
|
}
|
|
|
|
log.WithFields(fields).Error("AddMapData()")
|
|
|
|
|
|
|
|
//unrecoverable
|
2019-03-21 17:50:41 +03:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-02-15 11:11:07 +03:00
|
|
|
this.ctx.WebEventbus.Emit("mapobject-created", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} // k==name
|
|
|
|
} //for k,v
|
|
|
|
|
|
|
|
} //for id, name
|
|
|
|
}
|