Improved add-item patch

This commit is contained in:
Eduard Urbach 2018-12-24 15:50:55 +09:00
parent 4acf098853
commit cdeca8609f

View File

@ -12,7 +12,7 @@ var itemID string
var quantity int var quantity int
func init() { func init() {
flag.StringVar(&nick, "nick", "", "Name of the user.") flag.StringVar(&nick, "nick", "", "Name of the user. Leave it out to target all users.")
flag.StringVar(&itemID, "item", "", "ID of the item.") flag.StringVar(&itemID, "item", "", "ID of the item.")
flag.IntVar(&quantity, "q", 1, "Item quantity.") flag.IntVar(&quantity, "q", 1, "Item quantity.")
flag.Parse() flag.Parse()
@ -21,23 +21,29 @@ func init() {
func main() { func main() {
defer arn.Node.Close() defer arn.Node.Close()
if nick == "" || itemID == "" { if itemID == "" {
color.Red("Missing parameters") color.Red("Missing parameters")
return return
} }
// Check that the item exists
_, err := arn.GetShopItem(itemID)
arn.PanicOnError(err)
if nick != "" {
// Single user
user, err := arn.GetUserByNick(nick) user, err := arn.GetUserByNick(nick)
arn.PanicOnError(err) arn.PanicOnError(err)
addItemToUser(user)
item, err := arn.GetShopItem(itemID) } else {
arn.PanicOnError(err) // All users
for user := range arn.StreamUsers() {
if item == nil { addItemToUser(user)
color.Red("Unknown item")
return
} }
}
}
// Add to user inventory func addItemToUser(user *arn.User) {
inventory := user.Inventory() inventory := user.Inventory()
inventory.AddItem(itemID, uint(quantity)) inventory.AddItem(itemID, uint(quantity))
inventory.Save() inventory.Save()