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
} }
user, err := arn.GetUserByNick(nick) // Check that the item exists
_, err := arn.GetShopItem(itemID)
arn.PanicOnError(err) arn.PanicOnError(err)
item, err := arn.GetShopItem(itemID) if nick != "" {
arn.PanicOnError(err) // Single user
user, err := arn.GetUserByNick(nick)
if item == nil { arn.PanicOnError(err)
color.Red("Unknown item") addItemToUser(user)
return } else {
// All users
for user := range arn.StreamUsers() {
addItemToUser(user)
}
} }
}
// 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()