atm/fancyvend

This commit is contained in:
NatureFreshMilk 2019-02-15 11:25:49 +01:00
parent 70ade4e437
commit 9904b0fd95
5 changed files with 103 additions and 4 deletions

View File

@ -30,11 +30,16 @@ type Item struct {
Name string `json:"name"`
Count int `json:"count"`
Wear int `json:"wear"`
//TODO: metadata
}
func (this *Item) IsEmpty() bool {
return this.Name == "" && this.Count == 0
}
type Inventory struct {
Size int `json:"size"`
Items []Item `json:"items"`
Items []*Item `json:"items"`
}
func getNodePos(x, y, z int) int {

View File

@ -148,7 +148,7 @@ func parseMetadata(mapblock *MapBlock, data []byte) (int, error) {
item.Count = int(val)
}
currentInventory.Items = append(currentInventory.Items, item)
currentInventory.Items = append(currentInventory.Items, &item)
}

12
server/mapobject/atm.go Normal file
View File

@ -0,0 +1,12 @@
package mapobject
import (
"mapserver/mapblockparser"
"mapserver/mapobjectdb"
)
type ATM struct{}
func (this *ATM) onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject {
return mapobjectdb.NewMapObject(block.Pos, x, y, z, "atm")
}

View File

@ -0,0 +1,78 @@
package mapobject
import (
"mapserver/mapblockparser"
"mapserver/mapobjectdb"
"mapserver/luaparser"
"strconv"
)
type FancyVend struct{}
func (this *FancyVend) onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject {
md := block.Metadata.GetMetadata(x, y, z)
nodename := block.GetNodeName(x, y, z)
invMap := block.Metadata.GetInventoryMapAtPos(x, y, z)
parser := luaparser.New()
isAdmin := false
if nodename == "fancy_vend:admin_vendor" || nodename == "fancy_vend:admin_depo" {
isAdmin = true
}
payInv := invMap["wanted_item"]
giveInv := invMap["given_item"]
mainInv := invMap["main"]
if payInv.Items[0].IsEmpty() || giveInv.Items[0].IsEmpty() {
return nil
}
settings, err := parser.ParseMap(md["settings"])
if err != nil {
panic(err)//TODO
}
if settings["input_item_qty"] == nil || settings["output_item_qty"] == nil {
return nil
}
in_count := settings["input_item_qty"].(int)
if in_count < 1 {
in_count = 1
}
out_count := settings["output_item_qty"].(int)
if out_count < 1 {
out_count = 1
}
in_item := payInv.Items[0].Name
out_item := giveInv.Items[0].Name
stock := 0
if isAdmin {
stock = 999
} else {
for _, item := range mainInv.Items {
if item.Name == out_item {
stock += item.Count
}
}
}
stock_factor := int(float64(stock) / float64(out_count))
o := mapobjectdb.NewMapObject(block.Pos, x, y, z, "shop")
o.Attributes["owner"] = md["owner"]
o.Attributes["in_item"] = in_item
o.Attributes["in_count"] = strconv.Itoa(in_count)
o.Attributes["out_item"] = out_item
o.Attributes["out_count"] = strconv.Itoa(out_count)
o.Attributes["stock"] = strconv.Itoa(stock_factor)
return o
}

View File

@ -101,11 +101,15 @@ func Setup(ctx *app.App) {
}
if ctx.Config.MapObjects.Fancyvend {
//TODO
v := &FancyVend{}
l.AddMapObject("fancy_vend:admin_vendor", v)
l.AddMapObject("fancy_vend:admin_depo", v)
l.AddMapObject("fancy_vend:player_vendor", v)
l.AddMapObject("fancy_vend:player_depo", v)
}
if ctx.Config.MapObjects.ATM {
//TODO
l.AddMapObject("atm:atm", &ATM{})
}
ctx.BlockAccessor.Eventbus.AddListener(&l)