This commit is contained in:
NatureFreshMilk 2019-02-08 16:02:24 +01:00
parent 3f531d65c0
commit 582c7b06b8
18 changed files with 201 additions and 209 deletions

View File

@ -7,8 +7,8 @@ import (
"mapserver/mapblockaccessor"
"mapserver/mapblockrenderer"
"mapserver/mapobjectdb"
"mapserver/settings"
"mapserver/params"
"mapserver/settings"
"mapserver/tilerenderer"
)

View File

@ -11,7 +11,7 @@ import (
type Config struct {
Port int `json:"port"`
EnablePrometheus bool `json:"enableprometheus"`
EnablePrometheus bool `json:"enableprometheus"`
EnableRendering bool `json:"enablerendering"`
Webdev bool `json:"webdev"`
WebApi *WebApiConfig `json:"webapi"`
@ -76,7 +76,7 @@ func ParseConfig(filename string) (*Config, error) {
cfg := Config{
Port: 8080,
EnableRendering: true,
EnablePrometheus: true,
EnablePrometheus: true,
Webdev: false,
WebApi: &webapi,
Layers: layers,

View File

@ -8,14 +8,14 @@ import (
"mapserver/mapblockrenderer"
sqliteobjdb "mapserver/mapobjectdb/sqlite"
"mapserver/params"
"mapserver/settings"
"mapserver/tilerenderer"
"mapserver/worldconfig"
"mapserver/settings"
"github.com/sirupsen/logrus"
"os"
"io/ioutil"
"os"
"errors"
)
@ -61,7 +61,6 @@ func Setup(p params.ParamsType, cfg *Config) *App {
}
logrus.WithFields(logrus.Fields{"count": count}).Info("Loaded default colors")
//load provided colors, if available
info, err := os.Stat("colors.txt")
if info != nil && err == nil {

View File

@ -1,58 +1,58 @@
package luaparser
import (
"github.com/yuin/gopher-lua"
"errors"
"strconv"
"errors"
"github.com/yuin/gopher-lua"
"strconv"
)
func New() *LuaParser {
p := LuaParser{
state: lua.NewState(lua.Options{SkipOpenLibs: true}),
}
p := LuaParser{
state: lua.NewState(lua.Options{SkipOpenLibs: true}),
}
return &p
return &p
}
type LuaParser struct {
state *lua.LState
state *lua.LState
}
func (this *LuaParser) ParseMap(expr string) (map[string]interface{}, error) {
result := make(map[string]interface{})
result := make(map[string]interface{})
err := this.state.DoString(expr)
if err != nil {
return result, err
}
err := this.state.DoString(expr)
if err != nil {
return result, err
}
lv := this.state.Get(-1)
lv := this.state.Get(-1)
tbl, ok := lv.(*lua.LTable)
if !ok {
return result, errors.New("parsing failed")
}
tbl, ok := lv.(*lua.LTable)
if !ok {
return result, errors.New("parsing failed")
}
tbl.ForEach(func(k, v lua.LValue){
key, ok := k.(lua.LString)
tbl.ForEach(func(k, v lua.LValue) {
key, ok := k.(lua.LString)
if !ok {
return
}
if !ok {
return
}
boolValue, ok := v.(lua.LBool)
if ok {
result[key.String()] = boolValue == lua.LTrue
}
intValue, ok := v.(lua.LNumber)
if ok {
result[key.String()], _ = strconv.Atoi(intValue.String())
}
strValue, ok := v.(lua.LString)
if ok {
result[key.String()] = strValue.String()
}
})
boolValue, ok := v.(lua.LBool)
if ok {
result[key.String()] = boolValue == lua.LTrue
}
intValue, ok := v.(lua.LNumber)
if ok {
result[key.String()], _ = strconv.Atoi(intValue.String())
}
strValue, ok := v.(lua.LString)
if ok {
result[key.String()] = strValue.String()
}
})
return result, nil
return result, nil
}

View File

@ -1,30 +1,30 @@
package luaparser
import (
"testing"
"fmt"
"fmt"
"testing"
)
func TestParse(t *testing.T){
p := New()
m, err := p.ParseMap(`return {a=1, b=true, c="abc"}`)
func TestParse(t *testing.T) {
p := New()
m, err := p.ParseMap(`return {a=1, b=true, c="abc"}`)
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
fmt.Println(m)
fmt.Println(m)
if m["a"].(int) != 1 {
t.Fatal("parsing error")
}
if m["a"].(int) != 1 {
t.Fatal("parsing error")
}
if !m["b"].(bool) {
t.Fatal("parsing error")
}
if !m["b"].(bool) {
t.Fatal("parsing error")
}
if m["c"].(string) != "abc" {
t.Fatal("parsing error")
}
if m["c"].(string) != "abc" {
t.Fatal("parsing error")
}
}

View File

@ -178,14 +178,13 @@ func (a *MapBlockAccessor) FindMapBlocksByPos(lastpos *coords.MapBlockCoords, li
result.List = mblist
fields = logrus.Fields{
"len(List)": len(result.List),
"len(List)": len(result.List),
"unfilteredCount": result.UnfilteredCount,
"hasMore": result.HasMore,
"limit": limit,
"hasMore": result.HasMore,
"limit": limit,
}
logrus.WithFields(fields).Debug("FindMapBlocksByPos:Result")
return &result, nil
}

View File

@ -1,12 +1,11 @@
package mapblockparser
func IterateMapblock(cb func(x,y,z int)){
for x := 0; x < 16; x++ {
for y := 0; y < 16; y++ {
for z := 0; z < 16; z++ {
cb(x,y,z)
}
}
}
func IterateMapblock(cb func(x, y, z int)) {
for x := 0; x < 16; x++ {
for y := 0; y < 16; y++ {
for z := 0; z < 16; z++ {
cb(x, y, z)
}
}
}
}

View File

@ -18,8 +18,8 @@ type MapMultiObjectListener interface {
}
type Listener struct {
ctx *app.App
objectlisteners map[string]MapObjectListener
ctx *app.App
objectlisteners map[string]MapObjectListener
multiobjectlisteners map[string]MapMultiObjectListener
}
@ -50,7 +50,7 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
for k, v := range this.multiobjectlisteners {
if k == name {
//block matches
mapblockparser.IterateMapblock(func(x,y,z int){
mapblockparser.IterateMapblock(func(x, y, z int) {
nodeid := block.GetNodeId(x, y, z)
if nodeid == id {
fields := logrus.Fields{
@ -79,7 +79,7 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
for k, v := range this.objectlisteners {
if k == name {
//block matches
mapblockparser.IterateMapblock(func(x,y,z int){
mapblockparser.IterateMapblock(func(x, y, z int) {
nodeid := block.GetNodeId(x, y, z)
if nodeid == id {
fields := logrus.Fields{
@ -108,8 +108,8 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
func Setup(ctx *app.App) {
l := Listener{
ctx: ctx,
objectlisteners: make(map[string]MapObjectListener),
ctx: ctx,
objectlisteners: make(map[string]MapObjectListener),
multiobjectlisteners: make(map[string]MapMultiObjectListener),
}

View File

@ -3,8 +3,8 @@ package mapobject
import (
"mapserver/mapblockparser"
"mapserver/mapobjectdb"
"strconv"
"math"
"strconv"
)
type SmartShopBlock struct{}
@ -44,13 +44,13 @@ func (this *SmartShopBlock) onMapObject(x, y, z int, block *mapblockparser.MapBl
stock := 0
for _, item := range mainInv.Items {
if item.Name == out_item {
stock += item.Count
}
if item.Name == out_item {
stock += item.Count
}
}
//multiples of out_count
stock_factor := math.Floor( float64(stock) / float64(out_count) )
stock_factor := math.Floor(float64(stock) / float64(out_count))
o.Attributes["in_item"] = in_item
o.Attributes["in_count"] = strconv.Itoa(int(in_count))

View File

@ -7,7 +7,6 @@ import (
"github.com/sirupsen/logrus"
)
/*
sqlite perf: https://stackoverflow.com/questions/1711631/improve-insert-per-second-performance-of-sqlite?rq=1
PRAGMA synchronous = OFF

View File

@ -1,16 +1,14 @@
package sqlite
func (db *Sqlite3Accessor) GetSetting(key string, defaultvalue string) (string, error) {
rows, err := db.db.Query(getSettingQuery, key)
rows, err := db.db.Query(getSettingQuery, key)
if err != nil {
return "", err
}
defer rows.Close()
value := defaultvalue
value := defaultvalue
if rows.Next() {
@ -25,6 +23,6 @@ func (db *Sqlite3Accessor) GetSetting(key string, defaultvalue string) (string,
}
func (db *Sqlite3Accessor) SetSetting(key string, value string) error {
_, err := db.db.Exec(setSettingQuery, key, value)
_, err := db.db.Exec(setSettingQuery, key, value)
return err
}

View File

@ -1,110 +1,109 @@
package settings
import (
"mapserver/mapobjectdb"
"strconv"
"mapserver/mapobjectdb"
"strconv"
)
const (
SETTING_LAST_MTIME = "last_mtime"
SETTING_LASTX = "last_x"
SETTING_LASTY = "last_y"
SETTING_LASTZ = "last_z"
SETTING_INITIAL_RUN = "initial_run"
SETTING_LEGACY_PROCESSED = "legacy_processed"
SETTING_LAST_MTIME = "last_mtime"
SETTING_LASTX = "last_x"
SETTING_LASTY = "last_y"
SETTING_LASTZ = "last_z"
SETTING_INITIAL_RUN = "initial_run"
SETTING_LEGACY_PROCESSED = "legacy_processed"
)
type Settings struct {
db mapobjectdb.DBAccessor
db mapobjectdb.DBAccessor
}
func New(db mapobjectdb.DBAccessor) *Settings{
return &Settings{
db: db,
}
func New(db mapobjectdb.DBAccessor) *Settings {
return &Settings{
db: db,
}
}
func (this *Settings) GetString(key string, defaultValue string) string {
str, err := this.db.GetSetting(key, defaultValue)
if err != nil {
panic(err)
}
str, err := this.db.GetSetting(key, defaultValue)
if err != nil {
panic(err)
}
return str
return str
}
func (this *Settings) SetString(key string, value string) {
err := this.db.SetSetting(key, value)
if err != nil {
panic(err)
}
err := this.db.SetSetting(key, value)
if err != nil {
panic(err)
}
}
func (this *Settings) GetInt(key string, defaultValue int) int {
str, err := this.db.GetSetting(key, strconv.Itoa(defaultValue))
if err != nil {
panic(err)
}
str, err := this.db.GetSetting(key, strconv.Itoa(defaultValue))
if err != nil {
panic(err)
}
value, err := strconv.Atoi(str)
if err != nil {
panic(err)
}
return value
return value
}
func (this *Settings) SetInt(key string, value int) {
err := this.db.SetSetting(key, strconv.Itoa(value))
if err != nil {
err := this.db.SetSetting(key, strconv.Itoa(value))
if err != nil {
panic(err)
}
}
func (this *Settings) GetInt64(key string, defaultValue int64) int64 {
str, err := this.db.GetSetting(key, strconv.FormatInt(defaultValue, 10))
if err != nil {
panic(err)
}
str, err := this.db.GetSetting(key, strconv.FormatInt(defaultValue, 10))
if err != nil {
panic(err)
}
value, err := strconv.ParseInt(str, 10, 64)
if err != nil {
panic(err)
}
return value
return value
}
func (this *Settings) SetInt64(key string, value int64) {
err := this.db.SetSetting(key, strconv.FormatInt(value, 10))
if err != nil {
err := this.db.SetSetting(key, strconv.FormatInt(value, 10))
if err != nil {
panic(err)
}
}
func (this *Settings) GetBool(key string, defaultValue bool) bool {
defStr := "false"
if defaultValue {
defStr = "true"
}
defStr := "false"
if defaultValue {
defStr = "true"
}
str, err := this.db.GetSetting(key, defStr)
if err != nil {
panic(err)
}
str, err := this.db.GetSetting(key, defStr)
if err != nil {
panic(err)
}
return str == "true"
return str == "true"
}
func (this *Settings) SetBool(key string, value bool) {
defStr := "false"
if value {
defStr = "true"
}
defStr := "false"
if value {
defStr = "true"
}
err := this.db.SetSetting(key, defStr)
if err != nil {
err := this.db.SetSetting(key, defStr)
if err != nil {
panic(err)
}
}

View File

@ -1,87 +1,87 @@
package settings
import (
"testing"
"os"
"io/ioutil"
"mapserver/mapobjectdb/sqlite"
"io/ioutil"
"mapserver/mapobjectdb/sqlite"
"os"
"testing"
)
func TestStrings(t *testing.T){
tmpfile, err := ioutil.TempFile("", "TileDBTest.*.sqlite")
func TestStrings(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TileDBTest.*.sqlite")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
db, err := sqlite.New(tmpfile.Name())
if err != nil {
panic(err)
}
db, err := sqlite.New(tmpfile.Name())
if err != nil {
panic(err)
}
err = db.Migrate()
if err != nil {
panic(err)
}
err = db.Migrate()
if err != nil {
panic(err)
}
s := New(db)
s := New(db)
//string
//string
s.SetString("k", "v")
str := s.GetString("k", "v2")
if str != "v" {
t.Fatal("getstring failed: " + str)
}
s.SetString("k", "v")
str := s.GetString("k", "v2")
if str != "v" {
t.Fatal("getstring failed: " + str)
}
if s.GetString("k2", "v3") != "v3" {
t.Fatal("getstring with default failed")
}
if s.GetString("k2", "v3") != "v3" {
t.Fatal("getstring with default failed")
}
//int
//int
s.SetInt("i", 123)
i := s.GetInt("i", 456)
if i != 123 {
t.Fatal("getint failed")
}
s.SetInt("i", 123)
i := s.GetInt("i", 456)
if i != 123 {
t.Fatal("getint failed")
}
s.SetInt("i3", -123)
i = s.GetInt("i3", 456)
if i != -123 {
t.Fatal("getint negative failed")
}
s.SetInt("i3", -123)
i = s.GetInt("i3", 456)
if i != -123 {
t.Fatal("getint negative failed")
}
if s.GetInt("i2", 111) != 111 {
t.Fatal("getint with default failed")
}
if s.GetInt("i2", 111) != 111 {
t.Fatal("getint with default failed")
}
//int64
//int64
s.SetInt64("i", 1230000012300056)
i2 := s.GetInt64("i", 456)
if i2 != 1230000012300056 {
t.Fatal("getint64 failed")
}
s.SetInt64("i", 1230000012300056)
i2 := s.GetInt64("i", 456)
if i2 != 1230000012300056 {
t.Fatal("getint64 failed")
}
if s.GetInt64("i2", 12300000123000564) != 12300000123000564 {
t.Fatal("getint with default failed")
}
if s.GetInt64("i2", 12300000123000564) != 12300000123000564 {
t.Fatal("getint with default failed")
}
//bool
//bool
s.SetBool("b", false)
b2 := s.GetBool("b", true)
if b2 {
t.Fatal("getbool failed")
}
s.SetBool("b", false)
b2 := s.GetBool("b", true)
if b2 {
t.Fatal("getbool failed")
}
if s.GetBool("b2", false) {
t.Fatal("getbool with default failed")
}
if s.GetBool("b2", false) {
t.Fatal("getbool with default failed")
}
if !s.GetBool("b2", true) {
t.Fatal("getbool with default failed")
}
if !s.GetBool("b2", true) {
t.Fatal("getbool with default failed")
}
}

View File

@ -8,7 +8,7 @@ import (
func Job(ctx *app.App) {
initMetrics()
jobs := make(chan *coords.TileCoords, ctx.Config.RenderingQueue)
for i := 0; i < ctx.Config.RenderingJobs; i++ {

View File

@ -4,7 +4,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
)
var (
totalRenderedMapblocks = prometheus.NewCounter(
prometheus.CounterOpts{
@ -14,6 +13,6 @@ var (
)
)
func initMetrics(){
prometheus.MustRegister(totalRenderedMapblocks)
func initMetrics() {
prometheus.MustRegister(totalRenderedMapblocks)
}

View File

@ -6,8 +6,8 @@ import (
"net/http"
"strconv"
"github.com/sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
func Serve(ctx *app.App) {

View File

@ -1,6 +1,7 @@
package web
import (
"github.com/prometheus/client_golang/prometheus"
"image/color"
"mapserver/app"
"mapserver/coords"
@ -8,7 +9,6 @@ import (
"net/http"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
var (

View File

@ -23,7 +23,7 @@ type WS struct {
ctx *app.App
channels map[int]chan []byte
mutex *sync.RWMutex
clients int
clients int
}
func NewWS(ctx *app.App) *WS {