Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Add the ability to specify the cmd to purchase the item (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaidoRen committed May 24, 2019
1 parent d01429b commit 5b86e0a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
7 changes: 5 additions & 2 deletions include/shopapi.inc
Expand Up @@ -77,11 +77,12 @@ native bool: ShopDisableEvent(ShopEvent: event);
* @param discount Default discount
* @param inventory Save item in inventory
* @param strkey Unique key to search for the item
* @param cmd Command for purchase of this item
*
* @return Index of the new item
* @error -1 if creation failed
*/
native ShopItem: ShopPushItem(const name[], const cost, const access = ADMIN_ALL, const ItemFlag: flags = IF_None, const discount = 0, const bool: inventory = false, const strkey[SHOP_MAX_KEY_LENGTH] = "");
native ShopItem: ShopPushItem(const name[], const cost, const access = ADMIN_ALL, const ItemFlag: flags = IF_None, const discount = 0, const bool: inventory = false, const strkey[SHOP_MAX_KEY_LENGTH] = "", const cmd[SHOP_MAX_ITEM_CMD_LENGTH] = "");

/**
* Delete item.
Expand All @@ -105,10 +106,12 @@ native ShopDestroyItem(const ShopItem: item);
* @param access Variable to store access flags to
* @param strkey Item string key buffer
* @param keylen Item string key buffer len
* @param cmd Item cmd buffer
* @param cmdlen Item cmd buffer len
*
* @return Item exists (true/false)
*/
native bool: ShopGetItemInfo(const player, const ShopItem: item, const name[] = "", const namelen = 0, const &cost = 0, bool: costWithDiscount = true, const &discount = 0, const &access = 0, const strkey[] = "", const keylen = 0);
native bool: ShopGetItemInfo(const player, const ShopItem: item, const name[] = "", const namelen = 0, const &cost = 0, bool: costWithDiscount = true, const &discount = 0, const &access = 0, const strkey[] = "", const keylen = 0, const cmd[] = "", const cmdlen = 0);

/**
* Sets info about a item.
Expand Down
1 change: 1 addition & 0 deletions include/shopapi_const.inc
Expand Up @@ -3,6 +3,7 @@
#endif
#define _shopapi_const_included

#define SHOP_MAX_ITEM_CMD_LENGTH 32
#define SHOP_MAX_CATEGORY_NAME_LENGTH 128
#define SHOP_MAX_PLACEHOLDER_LENGTH 32
#define SHOP_MAX_PLACEHOLDER_VAL_LENGTH 128
Expand Down
38 changes: 34 additions & 4 deletions shop_api.sma
Expand Up @@ -22,6 +22,7 @@ enum any: ItemProperties
{
ItemStrKey[SHOP_MAX_KEY_LENGTH],
ItemName[SHOP_MAX_ITEM_NAME_LENGTH],
ItemCmd[SHOP_MAX_ITEM_CMD_LENGTH],
ItemCost,
ItemPlugin,
ItemAccess,
Expand Down Expand Up @@ -55,7 +56,7 @@ enum any: ForwardProperties

new Array: g_pItemsVec, Array: g_pForwardsVec,
Trie: g_pPlaceholdersAssoc, Trie: g_pPlayersDataAssoc,
Array: g_pCategoriesVec;
Array: g_pCategoriesVec, Trie: g_pItemsWithCmdAssoc;

new g_sPlayerData[MAX_PLAYERS + 1][PlayerDataProperties];

Expand All @@ -72,6 +73,7 @@ public plugin_init()

register_dictionary("shop.txt");

g_pItemsWithCmdAssoc = TrieCreate();
g_pPlayersDataAssoc = TrieCreate();
g_pPlaceholdersAssoc = TrieCreate();
g_pItemsVec = ArrayCreate(ItemProperties);
Expand Down Expand Up @@ -106,6 +108,20 @@ public CmdHandle_ShopMenu(const player)
return PLUGIN_HANDLED;
}

public CmdHandle_BuyItem(const player)
{
new szCmd[4 /*'say '*/], szArg[SHOP_MAX_ITEM_CMD_LENGTH];

read_argv(0, szCmd, charsmax(szCmd));
read_args(szArg, charsmax(szArg));
remove_quotes(szArg);

new iItem;
if (TrieGetCell(g_pItemsWithCmdAssoc, fmt("%s %s", szCmd, szArg), iItem)) {
SelectShopItem(player, iItem);
}
}

const KEY_BACK = 7;
const KEY_NEXT = 8;
const KEY_EXIT = 9;
Expand Down Expand Up @@ -480,7 +496,7 @@ public NativeHandle_CategorySetName(amxx)

public NativeHandle_PushItem(amxx)
{
enum { param_name = 1, param_cost, param_access, param_flags, param_discount, param_inventory, param_key };
enum { param_name = 1, param_cost, param_access, param_flags, param_discount, param_inventory, param_key, param_cmd };

new sItemData[ItemProperties];

Expand All @@ -502,7 +518,14 @@ public NativeHandle_PushItem(amxx)
return INVALID_HANDLE;
}

get_string(param_cmd, sItemData[ItemCmd], charsmax(sItemData[ItemCmd]));

new const iID = ArrayPushArray(g_pItemsVec, sItemData);

if (sItemData[ItemCmd][0]) {
TrieSetCell(g_pItemsWithCmdAssoc, sItemData[ItemCmd], iID);
register_clcmd(sItemData[ItemCmd], "CmdHandle_BuyItem");
}

ShopAttachToCategory(ShopOtherCategory, ShopItem: iID);

Expand All @@ -526,7 +549,11 @@ public NativeHandle_DestroyItem(amxx)

public bool: NativeHandle_GetItemInfo(amxx)
{
enum { param_player = 1, param_item, param_namebuffer, param_namelen, param_cost, param_costwithdiscount, param_discount, param_access, param_keybuffer, param_keylen };
enum {
param_player = 1, param_item, param_namebuffer, param_namelen,
param_cost, param_costwithdiscount, param_discount, param_access,
param_keybuffer, param_keylen, param_cmdbuffer, param_cmdlen
};

new const iPlayer = get_param(param_player), iItem = get_param(param_item);

Expand All @@ -542,12 +569,15 @@ public bool: NativeHandle_GetItemInfo(amxx)

RemoveColorSymbols(sItemData[ItemName]);

new const iCost = sItemData[ItemDiscount] && get_param(param_costwithdiscount) ? GET_COST_WITH_DISCOUNT(sItemData[ItemCost], sItemData[ItemDiscount]) : sItemData[ItemCost];
new const iCost = sItemData[ItemDiscount] && get_param(param_costwithdiscount)
? GET_COST_WITH_DISCOUNT(sItemData[ItemCost], sItemData[ItemDiscount]) : sItemData[ItemCost];

set_string(param_namebuffer, sItemData[ItemName], get_param(param_namelen));
set_param_byref(param_cost, iCost);
set_param_byref(param_discount, sItemData[ItemDiscount]);
set_param_byref(param_access, sItemData[ItemAccess]);
set_string(param_keybuffer, sItemData[ItemStrKey], get_param(param_keylen));
set_string(param_cmdbuffer, sItemData[ItemCmd], get_param(param_cmdlen));

return true;
}
Expand Down

0 comments on commit 5b86e0a

Please sign in to comment.