2019-02-10 22:27:53 +03:00
|
|
|
package mapblockaccessor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mapserver/eventbus"
|
2022-01-30 15:29:47 +03:00
|
|
|
"mapserver/types"
|
2019-02-10 22:27:53 +03:00
|
|
|
|
2021-10-12 08:44:07 +03:00
|
|
|
"github.com/minetest-go/mapparser"
|
2019-02-10 22:27:53 +03:00
|
|
|
cache "github.com/patrickmn/go-cache"
|
2019-02-15 10:36:48 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-02-15 11:08:22 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-02-10 22:27:53 +03:00
|
|
|
)
|
|
|
|
|
2019-02-10 22:34:11 +03:00
|
|
|
type FindMapBlocksByMtimeResult struct {
|
|
|
|
HasMore bool
|
2023-12-29 18:00:11 +03:00
|
|
|
LastPos *types.MapBlockCoords
|
2019-02-10 22:34:11 +03:00
|
|
|
LastMtime int64
|
2022-01-30 15:29:47 +03:00
|
|
|
List []*types.ParsedMapblock
|
2019-02-10 22:34:11 +03:00
|
|
|
UnfilteredCount int
|
|
|
|
}
|
|
|
|
|
2023-12-29 18:00:11 +03:00
|
|
|
func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, layerfilter []*types.Layer) (*FindMapBlocksByMtimeResult, error) {
|
2019-02-10 22:27:53 +03:00
|
|
|
|
|
|
|
fields := logrus.Fields{
|
|
|
|
"lastmtime": lastmtime,
|
|
|
|
"limit": limit,
|
|
|
|
}
|
|
|
|
logrus.WithFields(fields).Debug("FindMapBlocksByMtime")
|
|
|
|
|
2019-02-15 10:36:48 +03:00
|
|
|
timer := prometheus.NewTimer(dbGetMtimeDuration)
|
2019-02-10 22:27:53 +03:00
|
|
|
blocks, err := a.accessor.FindBlocksByMtime(lastmtime, limit)
|
2019-02-15 10:36:48 +03:00
|
|
|
timer.ObserveDuration()
|
2019-02-10 22:27:53 +03:00
|
|
|
|
2019-03-22 14:57:04 +03:00
|
|
|
changedBlockCount.Add(float64(len(blocks)))
|
|
|
|
|
2019-02-10 22:27:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-10 22:34:11 +03:00
|
|
|
result := FindMapBlocksByMtimeResult{}
|
2019-02-10 22:27:53 +03:00
|
|
|
|
2022-01-30 15:29:47 +03:00
|
|
|
mblist := make([]*types.ParsedMapblock, 0)
|
2023-12-29 18:00:11 +03:00
|
|
|
var newlastpos *types.MapBlockCoords
|
2019-02-10 22:27:53 +03:00
|
|
|
result.HasMore = len(blocks) == limit
|
|
|
|
result.UnfilteredCount = len(blocks)
|
|
|
|
|
|
|
|
for _, block := range blocks {
|
|
|
|
newlastpos = block.Pos
|
|
|
|
if result.LastMtime < block.Mtime {
|
|
|
|
result.LastMtime = block.Mtime
|
|
|
|
}
|
|
|
|
|
2023-12-29 18:00:11 +03:00
|
|
|
currentLayer := types.FindLayerByY(layerfilter, block.Pos.Y)
|
2019-02-10 22:27:53 +03:00
|
|
|
|
2019-04-15 09:08:28 +03:00
|
|
|
if currentLayer == nil {
|
2019-02-10 22:27:53 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := logrus.Fields{
|
|
|
|
"x": block.Pos.X,
|
|
|
|
"y": block.Pos.Y,
|
|
|
|
"z": block.Pos.Z,
|
|
|
|
}
|
|
|
|
logrus.WithFields(fields).Debug("mapblock")
|
|
|
|
|
|
|
|
key := getKey(block.Pos)
|
|
|
|
|
2021-10-12 08:44:07 +03:00
|
|
|
mapblock, err := mapparser.Parse(block.Data)
|
2019-02-10 22:27:53 +03:00
|
|
|
if err != nil {
|
2019-03-18 22:01:57 +03:00
|
|
|
fields := logrus.Fields{
|
|
|
|
"x": block.Pos.X,
|
|
|
|
"y": block.Pos.Y,
|
|
|
|
"z": block.Pos.Z,
|
|
|
|
"err": err,
|
|
|
|
}
|
|
|
|
logrus.WithFields(fields).Error("parse error")
|
|
|
|
continue
|
2019-02-10 22:27:53 +03:00
|
|
|
}
|
|
|
|
|
2022-01-30 15:29:47 +03:00
|
|
|
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, block.Pos))
|
2019-02-10 22:27:53 +03:00
|
|
|
|
2019-03-13 12:41:37 +03:00
|
|
|
a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
|
|
|
|
cacheBlockCount.Inc()
|
2022-01-30 15:29:47 +03:00
|
|
|
mblist = append(mblist, types.NewParsedMapblock(mapblock, block.Pos))
|
2019-02-10 22:27:53 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
result.LastPos = newlastpos
|
|
|
|
result.List = mblist
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|