mapserver/server/mapobject/smartshop.go

66 lines
1.5 KiB
Go
Raw Normal View History

2019-02-08 13:23:04 +03:00
package mapobject
import (
"mapserver/mapblockparser"
"mapserver/mapobjectdb"
2019-02-08 13:45:56 +03:00
"math"
2019-02-08 18:02:24 +03:00
"strconv"
2019-02-08 13:23:04 +03:00
)
type SmartShopBlock struct{}
2019-02-08 13:45:56 +03:00
func (this *SmartShopBlock) onMapObject(x, y, z int, block *mapblockparser.MapBlock) []*mapobjectdb.MapObject {
2019-02-12 15:34:15 +03:00
list := make([]*mapobjectdb.MapObject, 0)
2019-02-08 13:45:56 +03:00
2019-02-08 13:23:04 +03:00
md := block.Metadata.GetMetadata(x, y, z)
2019-02-08 13:45:56 +03:00
invMap := block.Metadata.GetInventoryMapAtPos(x, y, z)
mainInv := invMap["main"]
if mainInv.IsEmpty() {
return list
}
for i := 1; i <= 4; i++ {
payInvName := "pay" + strconv.Itoa(i)
giveInvName := "give" + strconv.Itoa(i)
pay := invMap[payInvName]
give := invMap[giveInvName]
if pay.IsEmpty() || give.IsEmpty() {
continue
}
o := mapobjectdb.NewMapObject(block.Pos, x, y, z, "shop")
o.Attributes["type"] = "smartshop"
o.Attributes["owner"] = md["owner"]
in_item := pay.Items[0].Name
in_count := math.Max(1, float64(pay.Items[0].Count))
out_item := give.Items[0].Name
out_count := math.Max(1, float64(give.Items[0].Count))
stock := 0
2019-02-08 13:23:04 +03:00
2019-02-08 13:45:56 +03:00
for _, item := range mainInv.Items {
2019-02-08 18:02:24 +03:00
if item.Name == out_item {
stock += item.Count
}
2019-02-08 13:45:56 +03:00
}
2019-02-08 13:23:04 +03:00
2019-02-08 13:45:56 +03:00
//multiples of out_count
2019-02-08 18:02:24 +03:00
stock_factor := math.Floor(float64(stock) / float64(out_count))
2019-02-08 13:23:04 +03:00
2019-02-08 13:45:56 +03:00
o.Attributes["in_item"] = in_item
o.Attributes["in_count"] = strconv.Itoa(int(in_count))
o.Attributes["out_item"] = out_item
o.Attributes["out_count"] = strconv.Itoa(int(out_count))
o.Attributes["stock"] = strconv.Itoa(int(stock_factor))
2019-02-08 13:23:04 +03:00
2019-02-08 13:45:56 +03:00
list = append(list, o)
}
2019-02-08 13:23:04 +03:00
2019-02-08 13:45:56 +03:00
return list
2019-02-08 13:23:04 +03:00
}