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