public TradeMenu(id)
{
new offer;
if(1 <= TradeTarget[id] <= 32)
{
new target = TradeTarget[id];
new p_target = TradeTarget[target];
if(is_user_connected(target) && p_target == id)
{ // Both trading
showTradeMenu(id);
}
else if((offer = PlayerHasTradingOffer(id)) > 0)
{
new szText[128], menu;
new name[64];
get_user_name(offer, name, charsmax(name));
formatex(szText, charsmax(szText), "%L", LANG_PLAYER, "TRADE_TRADING", PREFIX_EVENT, name);
menu = menu_create(szText, "TradeOfferHandler");
formatex(szText, charsmax(szText), "%L", LANG_PLAYER, "TRADE_ACCEPT");
menu_additem(menu, szText);
formatex(szText, charsmax(szText), "%L", LANG_PLAYER, "TRADE_DECLINE");
menu_additem(menu, szText);
menu_display(id, menu, 0);
}
else
{
showTradeTargets(id);
}
}
else if((offer = PlayerHasTradingOffer(id)) > 0)
{
new szText[128], menu;
new name[64];
get_user_name(offer, name, charsmax(name));
formatex(szText, charsmax(szText), "%L", LANG_PLAYER, "TRADE_TRADING", PREFIX_EVENT, name);
menu = menu_create(szText, "TradeOfferHandler");
formatex(szText, charsmax(szText), "%L", LANG_PLAYER, "TRADE_ACCEPT");
menu_additem(menu, szText);
formatex(szText, charsmax(szText), "%L", LANG_PLAYER, "TRADE_DECLINE");
menu_additem(menu, szText);
menu_display(id, menu, 0);
}
else
{
showTradeTargets(id);
}
}
public TradeOfferHandler(id, menu, item)
{
if(item == MENU_EXIT)
{
menu_destroy(menu);
return PLUGIN_HANDLED;
}
new offer = PlayerHasTradingOffer(id);
if(!(1 <= offer <= 32) || !is_user_connected(offer))
{
TradeMenu(id);
return PLUGIN_HANDLED;
}
new name[64];
get_user_name(id, name, charsmax(name));
switch(item)
{
case 0:
{
TradeTarget[id] = offer;
client_print_color(offer, print_team_default, "%L", LANG_PLAYER, "TRADE_YPLAYER_ACCEPT", Prefix, name);
ResetTrade(id, false);
ResetTrade(offer, false);
TradeMenu(offer);
}
case 1:
{
TradeTarget[offer] = 0;
client_print_color(offer, print_team_default, "%L", LANG_PLAYER, "TRADE_YPLAYER_DECLINE", Prefix, name);
}
}
TradeMenu(id);
return PLUGIN_HANDLED;
}
stock PlayerHasTradingOffer(id)
{
for(new i = 1; i <= 32; i++)
{
if(is_user_connected(i) && TradeTarget[i] == id) return i;
}
return -1;
}
public showTradeTargets(id)
{
new szText[512];
formatex(szText, charsmax(szText), "%L", LANG_PLAYER, "TRADE_SHOW_TARGETS", PREFIX_EVENT);
new menu = menu_create(szText, "TradePlayersHandler");
new szName[32], szUserId[32], players[32], pnum, tempid;
get_players(players, pnum);
for(new i; i < pnum; i++)
{
tempid = players[i];
if(tempid == id)
continue;
get_user_name(tempid, szName, charsmax(szName));
formatex(szUserId, charsmax(szUserId), "%d", get_user_userid(tempid));
menu_additem(menu, szName, szUserId, 0);
}
menu_display(id, menu, 0);
}
public TradePlayersHandler(id, menu, item)
{
if(item == MENU_EXIT)
{
menu_destroy(menu);
return PLUGIN_HANDLED;
}
new szData[6], szName[64];
new _access, item_callback;
menu_item_getinfo(menu, item, _access, szData,charsmax(szData), szName,charsmax(szName), item_callback);
new userid = str_to_num(szData);
new player = find_player("k", userid); // flag "k" : find player from userid
if(player && is_user_connected(player))
{
if(canOffer[id])
{
new name[64];
get_user_name(id, name, charsmax(name));
TradeTarget[id] = player;
client_print_color(player, print_team_default, "%L", LANG_PLAYER, "TRADE_PLAYER", Prefix, name);
TradeMenu(player);
set_task(10.0, "resetOffering", id+8080);
canOffer[id] = false;
}
else client_print_color(id, print_team_default, "%L", LANG_PLAYER, "TRADE_WAITING_TARGETS", Prefix);
}
TradeMenu(id);
return PLUGIN_HANDLED;
}
public resetOffering(id)
{
id -= 8080;
canOffer[id] = true;
}
public showTradeMenu(id)
{
if(1 <= TradeTarget[id] <= 32)
{
new target = TradeTarget[id];
if(is_user_connected(target))
{
new szText[512], target_name[64];
get_user_name(target, target_name, charsmax(target_name));
formatex(szText, charsmax(szText), "\y[\rPublic\y] \wKeičiatės su \r%s\d:\w", target_name);
new menu = menu_create(szText, "TradeMenuHandler");
if(TradeSkin[id] != -1)
{
new skin_id = TradeSkin[id];
formatex(szText, charsmax(szText), "\wSkiną: \y%s \d[\r%s\d]", WeaponNames[skin_id], szWeaponNames[Weapons[skin_id]]);
}
else
{
formatex(szText, charsmax(szText), "\wSkiną: \dnepasirinkta");
}
menu_additem(menu, szText);
if(TradeChest[id] != -1 && TradeChestCount[id] > 0)
{
new chest_id = TradeChest[id];
formatex(szText, charsmax(szText), "\wSkrynią: \y%s \d[%d]", chest_name(chest_id), TradeChestCount[id]);
}
else
{
formatex(szText, charsmax(szText), "\wSkrynią: \dnepasirinkta");
}
menu_additem(menu, szText);
formatex(szText, charsmax(szText), "\wRaktai: \d%d", TradeKey[id]);
menu_additem(menu, szText);
formatex(szText, charsmax(szText), "\wTaškai: \d$%d", TradeMoney[id]);
new extraText[512];
formatex(extraText, charsmax(extraText), "%s^n^n\w--- \y%s \w---^n^n", szText, target_name);
if(TradeSkin[target] != -1)
{
new skin_id = TradeSkin[target];
formatex(extraText, charsmax(extraText), "%s\wSkiną: \y%s \d[\r%s\d]^n", extraText, WeaponNames[skin_id], szWeaponNames[Weapons[skin_id]]);
}
else
{
formatex(extraText, charsmax(extraText), "%s\wSkiną: \dnepasirinkta^n", extraText);
}
if(TradeChest[target] != -1 && TradeChestCount[id] > 0)
{
new chest_id = TradeChest[target];
formatex(extraText, charsmax(extraText), "%s\wSkrynią: \y%s \d[%d]^n", extraText, chest_name(chest_id), TradeChestCount[target]);
}
else
{
formatex(extraText, charsmax(extraText), "%s\wSkrynią: \dnepasirinkta^n", extraText);
}
formatex(extraText, charsmax(extraText), "%s\wRaktai: \d%d^n", extraText, TradeKey[target]);
formatex(extraText, charsmax(extraText), "%s\wTaškai: \d$%d^n^n", extraText, TradeMoney[target]);
menu_additem(menu, extraText);
if(TradeAccept[id] == -1)
{
formatex(szText, charsmax(szText), "\yPatvirtinti");
menu_additem(menu, szText);
}
else
{
formatex(szText, charsmax(szText), "\yPatvirtinti \d[Waiting for other player]");
menu_additem(menu, szText);
}
formatex(szText, charsmax(szText), "\rAtšaukti");
menu_additem(menu, szText);
menu_display(id, menu, 0);
}
else
{
TradeMenu(id);
}
}
else
{
TradeMenu(id);
}
}
public TradeMenuHandler(id, menu, item)
{
if(item == MENU_EXIT)
{
menu_destroy(menu);
return PLUGIN_HANDLED;
}
if(TradeAccept[id] == -1)
{
switch(item)
{
case 0:
{ // Skin
showTradeSkinsMenu(id);
}
case 1:
{ // Chest
showTradeChestsMenu(id);
}
case 2:
{ // Keys
inMM[id] = true;
client_cmd(id, "messagemode SET_TRADE_KEYS");
TradeMenu(id);
}
case 3:
{ // Money
inMM[id] = true;
client_cmd(id, "messagemode SET_TRADE_MONEY");
TradeMenu(id);
}
}
}
new target = TradeTarget[id];
if(item == 4)
{ // Accept
if(TradeAccept[id] == -1)
{
TradeAccept[id] = 1;
}
else
{
TradeAccept[id] = -1;
}
if(TradeAccept[id] != -1)
{
client_print_color(id, print_team_default, "%L", LANG_PLAYER, "TRADE_PLAYER_ACCEPT", Prefix);
if(TradeAccept[target] != -1)
{
MakeTrade(id, target);
}
TradeMenu(id);
}
}
else if(item == 5)
{ // Decline
client_print_color(id, print_team_default, "%L", LANG_PLAYER, "TRADE_PLAYER_DECLINE", Prefix);
ResetTrade(id, true);
ResetTrade(target, true);
TradeMenu(id);
}
return PLUGIN_HANDLED;
}
public showTradeSkinsMenu(id)
{
new szText[512];
formatex(szText, charsmax(szText), "%L", LANG_PLAYER, "TRADE_ITEM_SKINS", PREFIX_EVENT);
new menu = menu_create(szText, "skinsTradeMenuHandler");
menu_additem(menu, "None", "-1");
new String[32], All;
for(new i = 1; i < AllWeapon; i++)
{
if(uWeapon[i][id] <= 0)
continue;
num_to_str(i, String, 31);
formatex(szText, sizeof(szText), "%L", LANG_PLAYER, "SKINPIECE", (bUsingGuns[id][Weapons[i]] == i) ? "\w" : "\y", WeaponNames[i], szWeaponNames[Weapons[i]], uWeapon[i][id]);
menu_additem(menu, szText, String);
All++;
}
menu_display(id, menu, 0);
}
public skinsTradeMenuHandler(id, menu, item)
{
if(item == MENU_EXIT)
{
menu_destroy(menu);
TradeMenu(id);
return PLUGIN_HANDLED;
}
new szData[6], szName[64];
new _access, item_callback;
menu_item_getinfo(menu, item, _access, szData, charsmax(szData), szName, charsmax(szName), item_callback);
new skin_id = str_to_num(szData);
if(skin_id != -1)
{
new name[64];
get_user_name(id, name, charsmax(name));
TradeSkin[id] = skin_id;
client_print_color(TradeTarget[id], print_team_default, "[^4Public^1] ^3%s ^1set ^4%s ^1[^4%s^1] skiną!", name, WeaponNames[skin_id], szWeaponNames[Weapons[skin_id]]);
}
TradeMenu(id);
TradeMenu(TradeTarget[id]);
return PLUGIN_HANDLED;
}
public showTradeChestsMenu(id)
{
new szText[512];
formatex(szText, charsmax(szText), "%L", LANG_PLAYER, "TRADE_ITEM_CHESTS", PREFIX_EVENT);
new menu = menu_create(szText, "chestsTradeMenuHandler");
menu_additem(menu, "None", "-1");
for(new i = 1; i <= CHEST_COUNT; i++)
{
if(Chest[id][i] > 0)
{
formatex(szText, sizeof(szText), "%L", LANG_PLAYER, "CHESTPIECE", chest_name(i), Chest[id][i]);
new szButton[128]; formatex(szButton, charsmax(szButton), "%d", i);
menu_additem(menu, szText, szButton);
}
}
menu_display(id, menu, 0);
}
public chestsTradeMenuHandler(id, menu, item)
{
if(item == MENU_EXIT)
{
menu_destroy(menu);
TradeMenu(id);
return PLUGIN_HANDLED;
}
new szData[6], szName[64];
new _access, item_callback;
menu_item_getinfo( menu, item, _access, szData,charsmax(szData), szName, charsmax(szName), item_callback);
new chest_id = str_to_num(szData);
if(chest_id != -1)
{
new name[64];
get_user_name(id, name, charsmax(name));
TradeChest[id] = chest_id;
inMM[id] = true;
client_cmd(id, "messagemode SET_TRADE_CHESTS");
}
TradeMenu(id);
return PLUGIN_HANDLED;
}
public ResetTrade(id, bool:reset)
{
if(reset)
TradeTarget[id] = 0;
TradeAccept[id] = -1;
TradeSkin[id] = -1;
TradeChest[id] = -1;
TradeKey[id] = 0;
TradeMoney[id] = 0;
}
public cmdKeys(id)
{
if(inMM[id])
{
new szTemp[64];
read_args(szTemp, charsmax(szTemp));
remove_quotes(szTemp);
if(IsInteger(szTemp))
{
new pts = str_to_num(szTemp);
if(TradeKey[id] != pts)
{
if(pts > pKey[id]) pts = pKey[id];
TradeKey[id] = pts;
new name[64];
get_user_name(id, name, charsmax(name));
client_print_color(TradeTarget[id], print_team_default, "[^4Public^1] ^3%s ^1set ^4%d ^1raktų!", name, pts);
TradeMenu(TradeTarget[id]);
}
else
{
client_print_color(id, print_team_default, "%L", LANG_PLAYER, "TRADE_PLAYER_DECLINE", Prefix);
}
}
else
{
client_cmd(id, "messagemode SET_TRADE_KEYS");
return PLUGIN_HANDLED;
}
}
inMM[id] = false;
set_task(0.5, "TradeMenu", id);
return PLUGIN_HANDLED;
}
public cmdMoney(id)
{
if(inMM[id])
{
new szTemp[64];
read_args(szTemp, charsmax(szTemp));
remove_quotes(szTemp);
if(IsInteger(szTemp))
{
new pts = str_to_num(szTemp);
if(TradeMoney[id] != pts)
{
if(pts > Points[id]) pts = Points[id];
TradeMoney[id] = pts;
new name[64];
get_user_name(id, name, charsmax(name));
client_print_color(TradeTarget[id], print_team_default, "[^4Public^1] ^3%s ^1set ^4%d ^1taškų!", name, pts);
TradeMenu(TradeTarget[id]);
}
else
{
client_print_color(id, print_team_default, "%L", LANG_PLAYER, "TRADE_PLAYER_DECLINE", Prefix);
}
}
else
{
client_cmd(id, "messagemode SET_TRADE_MONEY");
return PLUGIN_HANDLED;
}
}
inMM[id] = false;
set_task(0.5, "TradeMenu", id);
return PLUGIN_HANDLED;
}
public cmdChests(id)
{
if(inMM[id])
{
new szTemp[64];
read_args(szTemp, charsmax(szTemp));
remove_quotes(szTemp);
if(IsInteger(szTemp) && str_to_num(szTemp) > 0)
{
new pts = str_to_num(szTemp);
if(TradeChestCount[id] != pts && TradeChest[id] != -1 && (1 <= TradeChest[id] <= CHEST_COUNT) )
{
new cnt = Chest[id][TradeChest[id]];
if(pts > cnt) pts = cnt;
if(pts <= 0) TradeChest[id] = -1;
TradeChestCount[id] = pts;
new name[64];
get_user_name(id, name, charsmax(name));
client_print_color(TradeTarget[id], print_team_default, "[^4Public^1] ^3%s ^1set ^4%s %d ^1skrynių!", name, chest_name(TradeChest[id]), pts);
TradeMenu(TradeTarget[id]);
}
else
{
client_print_color(id, print_team_default, "%L", LANG_PLAYER, "TRADE_PLAYER_DECLINE", Prefix);
}
}
else
{
client_cmd(id, "messagemode SET_TRADE_CHESTS");
return PLUGIN_HANDLED;
}
}
inMM[id] = false;
set_task(0.5, "TradeMenu", id);
return PLUGIN_HANDLED;
}
bool:IsInteger(buffer[])
{
new len = strlen(buffer);
for(new i = 0; i < len; i++)
{
if(!isdigit(buffer[i]))
return false;
}
return true;
}
public MakeTrade(id, target)
{
if(!is_user_connected(id) || !is_user_connected(target)) return;
new bool:TradeWillHappen = true;
new skin_id, target_skin_id, chest_id, target_chest_id, chest_count, target_chest_count, keys, target_keys, money, target_money;
if(TradeSkin[id] != -1)
{
if(uWeapon[TradeSkin[id]][id] <= 0)
{
TradeWillHappen = false;
}
else
{
skin_id = TradeSkin[id];
}
}
if(TradeSkin[target] != -1)
{
if(uWeapon[TradeSkin[target]][target] <= 0)
{
TradeWillHappen = false;
}
else
{
target_skin_id = TradeSkin[target];
}
}
if(TradeChest[id] != -1 && TradeChestCount[id] > 0)
{
if(Chest[id][TradeChest[id]] < TradeChestCount[id])
{
TradeWillHappen = false;
}
else
{
chest_id = TradeChest[id];
chest_count = TradeChestCount[id];
}
}
if(TradeChest[target] != -1 && TradeChestCount[target] > 0)
{
if(Chest[target][TradeChest[target]] < TradeChestCount[target])
{
TradeWillHappen = false;
}
else
{
target_chest_id = TradeChest[target];
target_chest_count = TradeChestCount[target];
}
}
if(TradeKey[id] > 0)
{
if(pKey[id] < TradeKey[id]) TradeWillHappen = false;
else keys = TradeKey[id];
}
if(TradeKey[target] > 0)
{
if(pKey[target] < TradeKey[target]) TradeWillHappen = false;
else target_keys = TradeKey[target];
}
if(TradeMoney[id] > 0)
{
if(Points[id] < TradeMoney[id]) TradeWillHappen = false;
else money = TradeMoney[id];
}
if(TradeMoney[target] > 0)
{
if(Points[target] < TradeMoney[target]) TradeWillHappen = false;
else target_money = TradeMoney[target];
}
if(TradeWillHappen)
{
pKey[id] -= keys; pKey[target] -= target_keys; pKey[id] += target_keys; pKey[target] += keys;
Points[id] -= money; Points[target] -= target_money; Points[id] += target_money; Points[target] += money;
uWeapon[skin_id][id]--; uWeapon[target_skin_id][target]--; uWeapon[target_skin_id][id]++; uWeapon[skin_id][target]++;
Chest[id][chest_id] -= chest_count;
Chest[id][target_chest_id] += target_chest_count;
Chest[target][target_chest_id] -= target_chest_count;
Chest[target][chest_id] += chest_count;
client_print_color(id, print_team_default, "%L", LANG_PLAYER, "TRADE_SUCCESSFULLY_MAKE", Prefix);
SaveData(id);
SaveData(target);
}
else
{
client_print_color(id, print_team_default, "%L", LANG_PLAYER, "TRADE_FAILED_MISSING", Prefix);
client_print_color(target, print_team_default, "%L", LANG_PLAYER, "TRADE_FAILED_MISSING", Prefix);
}
ResetTrade(id, true);
ResetTrade(target, true);
}