Advertisement
Bebras

[INC]Inventory v0.4.1 - Bebras

Aug 13th, 2014
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 12.17 KB | None | 0 0
  1.                              
  2.  
  3.                                 /*
  4.                                                 Inventoriaus kūrimo include
  5.                                                 Autorius "Bebras" 2013 - 2014 ©
  6.                                                 v0.4
  7.                                                
  8.                                 */
  9.  
  10.                                
  11. /*
  12.         native AddItem(itemname[]);                                     Ši funkcija pridėta daiktą, nepanaudoję jos negalėsite naudotis tuo daiktu..
  13.         native GivePlayerItem(playerid,item[],amount);                  Ši funkcija duoda žaidėjui tam tikrą daiktų skaičių(galima naudoti ir daiktų atėmimui).
  14.         native ShowPlayerInventory(playerid);                           Ši funkcija parodys žaidėjui GUI su visais jo daiktais ir jų kiekiu(taipat paspaudus įvyks nurodytas veiksmas).
  15.         native ResetPlayerItem(playerid,itemname[]);                    Ši funkcija atiims iš žaidėjo visus nurodytus daiktus.
  16.         native ResetPlayerItems(playerid);                              Ši funkcija attims visus žaidėjo daiktus.
  17.         native GetPlayerItemAmount(playerid,itemname[]);                Ši funkcija gražins nurodyto daikto skaičių.
  18.         native ShowPlayerInventory(playerid);                           Parodys tuščią inventorių nurodytam žaidėjui.
  19.        
  20. */
  21.  
  22.  
  23. /*
  24.  
  25.     SQL Table structure if MySQL used.
  26.  
  27.     CREATE TABLE IF NOT EXISTS player_items (
  28.         `Id` INT(11) AUTO_INCREMENT NOT NULL,
  29.         Username VARCHAR(24) NOT NULL,
  30.         ItemName VARCHAR(30) NOT NULL,
  31.         Quantity INT(11) NOT NULL,
  32.         PRIMARY KEY(`Id`)
  33.     );
  34.  
  35.  
  36. */
  37.  
  38. #define Bebras AUTORIUS
  39.  
  40.  
  41. #if defined INV_SAVE_Y_INI
  42.     #if !defined _inc_y_ini
  43.         #include <YSI\y_ini>
  44.     #endif
  45. #elseif defined INV_SAVE_MYSQL
  46.     #if !defined inc_a_mysql
  47.         #include <a_mysql>
  48.     #endif
  49.     #if !defined INV_MYSQL_R38 && !defined INV_MYSQL_R6
  50.         #define INV_MYSQL_R6
  51.     #endif
  52. #endif
  53.  
  54.  
  55. #if !defined INV_LOAD_ONCONNECT && !defined INV_LOAD_ONSPAWN
  56.     #define INV_LOAD_ONCONNECT
  57. #endif
  58.  
  59.  
  60. #if !defined isnull
  61.     #define isnull(%1) \
  62.                 ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
  63. #endif
  64.  
  65. #if !defined MAX_ITEMS
  66.     #define MAX_ITEMS                       (15)
  67. #endif
  68.  
  69. #if !defined MAX_ITEM_NAME
  70.     #define MAX_ITEM_NAME                   (30)
  71. #endif
  72.  
  73. #if !defined INV_EMPTY_DIALOG_ID
  74.     #define INV_EMPTY_DIALOG_ID             (32760)
  75. #endif
  76. #if !defined INV_INVENTORY_DIALOG_ID
  77.     #define INV_INVENTORY_DIALOG_ID         (457)
  78. #endif
  79.  
  80.  
  81. #define Item:%0(%1)     forward item_%0(%1); public item_%0(%1)
  82.  
  83.  
  84. #if !defined ShowEmptyInventory
  85.     #define ShowEmptyInventory(%0) \
  86.         ShowPlayerDialog(%0, INV_EMPTY_DIALOG_ID, DIALOG_STYLE_MSGBOX,"Inventorius","\t{FF0000}Jūs neturite daiktų!","Gerai","")
  87. #endif
  88.  
  89.  
  90.  
  91. static stock UsableItems[MAX_ITEMS][MAX_ITEM_NAME];
  92.  
  93.  
  94. // If MySQL usage is undefined, this should not be compiled either.
  95. static stock PrimarKeys[MAX_PLAYERS][MAX_ITEMS],inv_con_handle;
  96.  
  97.  
  98. //////////////////////////////////////////////////////////////////////////
  99. //////////////////////////////////////////////////////////////////////////
  100. //
  101. //                      Functions for users
  102. //
  103. //////////////////////////////////////////////////////////////////////////
  104. //////////////////////////////////////////////////////////////////////////
  105.  
  106. stock AddItem(itemname[])
  107. {
  108.     for(new i; i < MAX_ITEMS; i++)
  109.     {
  110.         if(!isnull(UsableItems[i]) && !strcmp(UsableItems[i],itemname)) return 0;
  111.         if(isnull(UsableItems[i]))
  112.         {
  113.             strins(UsableItems[i],itemname,0,MAX_ITEM_NAME);
  114.             return 1;
  115.         }
  116.     }
  117.     return 0;
  118. }
  119.  
  120. stock GivePlayerItem(playerid,item[],amount)
  121. {
  122.     if(GetPVarInt(playerid,item) !=  0) SetPVarInt(playerid,item,GetPVarInt(playerid,item)+amount);
  123.     else SetPVarInt(playerid,item,amount);
  124.     return GetPVarInt(playerid,item);
  125. }
  126. stock GetPlayerItemAmount(playerid,itemname[])
  127.     return GetPVarInt(playerid,itemname);
  128.        
  129. stock ShowPlayerInventory(playerid)
  130. {
  131.     new bigstring[MAX_ITEMS*MAX_ITEM_NAME],str[MAX_ITEM_NAME+5];
  132.     for(new i; i < MAX_ITEMS; i++)
  133.     {
  134.         if(GetPVarInt(playerid,UsableItems[i]) > 0)
  135.         {    
  136.             format(str,sizeof(str),"%s\t%d\n",UsableItems[i],GetPVarInt(playerid,UsableItems[i]));
  137.             strcat(bigstring,str);
  138.         }
  139.     }
  140.     if(isnull(bigstring))
  141.     {
  142.         ShowEmptyInventory(playerid);
  143.         return 0;
  144.     }
  145.     ShowPlayerDialog(playerid,INV_INVENTORY_DIALOG_ID,DIALOG_STYLE_LIST,"Inventorius",bigstring,"Pasirinkti","Išeiti");
  146.     return 1;
  147. }
  148. stock ResetPlayerItem(playerid,itemname[])
  149.     return DeletePVar(playerid,itemname);
  150.  
  151. stock ResetPlayerItems(playerid)
  152. {
  153.     for(new i; i < MAX_ITEMS; i++)
  154.     {
  155.         if(isnull(UsableItems[i])) break;
  156.         DeletePVar(playerid,UsableItems[i]);
  157.     }
  158.     return 1;
  159. }
  160.  
  161. //////////////////////////////////////////////////////////////////////////
  162. //////////////////////////////////////////////////////////////////////////
  163. //
  164. //                      Callbacks, function hooks
  165. //
  166. //////////////////////////////////////////////////////////////////////////
  167. //////////////////////////////////////////////////////////////////////////
  168.  
  169.  
  170. #if defined INV_SAVE_MYSQL && defined INV_MYSQL_R38
  171.     stock inv_mysql_connect(const host[], const user[], const database[], const password[], port = 3306, bool:autoreconnect = true, pool_size = 2)
  172.     {
  173.         inv_con_handle = mysql_connect(host,user,database,password,port,autoreconnect,pool_size);
  174.         return inv_con_handle;
  175.     }
  176.     #if defined _ALS_mysql_connect
  177.         #undef mysql_connect
  178.     #else
  179.         #define _ALS_mysql_connect
  180.     #endif
  181.     #define mysql_connect inv_mysql_connect
  182. #endif
  183.  
  184.  
  185. public OnDialogResponse(playerid,dialogid,response,listitem,inputtext[])
  186. {
  187.     if(dialogid == INV_INVENTORY_DIALOG_ID && response)
  188.     {
  189.         if(isnull(inputtext)) return 1;
  190.  
  191.         new func[MAX_ITEM_NAME+5];
  192.        
  193.         // Išsitraukiam realų pavadinimą, be kiekio.
  194.         strmid(func,inputtext,0,strfind(inputtext,"\t"));
  195.         strins(func,"item_",0);
  196.  
  197.         // Ieškom tarpų.
  198.         new index = 0;
  199.         while((index = strfind(func," ",.pos=index)) != -1)
  200.         {
  201.             strdel(func, index, index+1);
  202.             strins(func,"@",index);
  203.         }
  204.  
  205.         // Kviečiam!
  206.         if(funcidx(func) == -1) return printf("Daikto \"%s\" funkcija nesukurta",func);
  207.         CallLocalFunction(func,"i",playerid);
  208.         return 1;
  209.     }
  210.     #if defined BInv_OnDialogResponse
  211.         BInv_OnDialogResponse(playerid,dialogid,response,listitem,inputtext);
  212.     #endif
  213.     return 0;
  214. }
  215. #if defined _ALS_OnDialogResponse
  216.     #undef OnDialogResponse
  217. #else  
  218.     #define _ALS_OnDialogResponse
  219. #endif
  220. #define OnDialogResponse BInv_OnDialogResponse
  221. #if defined BInv_OnDialogResponse
  222.     forward BInv_OnDialogResponse(playerid,dialogid,response,listitem,inputtext[]);
  223. #endif
  224.  
  225.  
  226. public OnPlayerConnect(playerid)
  227. {
  228.     #if defined INV_LOAD_ONCONNECT
  229.         #if defined INV_SAVE_Y_INI
  230.             Inv_YINILoadItems(playerid);
  231.         #elseif defined INV_SAVE_MYSQL
  232.             Inv_MySQLLoadItems(playerid);
  233.         #endif
  234.     #endif
  235.  
  236.     #if defined BInv_OnPlayerConnect
  237.         BInv_OnPlayerConnect(playerid);
  238.     #endif
  239.     return 1;
  240. }
  241. #if defined _ALS_OnPlayerConnect
  242.     #undef OnPlayerConnect
  243. #else
  244.     #define _ALS_OnPlayerConnect
  245. #endif
  246. #define OnPlayerConnect BInv_OnPlayerConnect
  247. #if defined BInv_OnPlayerConnect
  248.     forward BInv_OnPlayerConnect(playerid);
  249. #endif
  250.  
  251. public OnPlayerSpawn(playerid)
  252. {
  253.     #if defined INV_LOAD_ONSPAWN
  254.         #if defined INV_SAVE_Y_INI
  255.             Inv_YINILoadItems(playerid);
  256.         #elseif defined INV_SAVE_MYSQL
  257.             Inv_MySQLLoadItems(playerid);
  258.         #endif
  259.     #endif
  260.     #if defined BInv_OnPlayerSpawn
  261.         BInv_OnPlayerSpawn(playerid);
  262.     #endif
  263.     return 1;
  264. }
  265. #if defined _ALS_OnPlayerSpawn
  266.     #undef OnPlayerSpawn
  267. #else
  268.     #define _ALS_OnPlayerSpawn
  269. #endif
  270. #if !defined Bebras
  271.     #error Include autorius - Bebras. Nuo tiesos nepabegsi, turek pagarbos.
  272. #endif
  273. #define OnPlayerSpawn BInv_OnPlayerSpawn
  274. #if defined BInv_OnPlayerSpawn
  275.     forward BInv_OnPlayerSpawn(playerid);
  276. #endif
  277.  
  278. public OnPlayerDisconnect(playerid, reason)
  279. {
  280.     #if defined INV_SAVE_Y_INI
  281.         Inv_YINISaveItems(playerid);
  282.     #elseif defined INV_SAVE_MYSQL
  283.         Inv_MySQLSaveItems(playerid);
  284.     #endif
  285.     #if defined BInv_OnPlayerDisconnect
  286.         BInv_OnPlayerDisconnect(playerid,reason);
  287.     #endif
  288.     return 1;
  289. }
  290. #if defined _ALS_OnPlayerDisconnect
  291.     #undef OnPlayerDisconnect
  292. #else
  293.     #define _ALS_OnPlayerDisconnect
  294. #endif
  295. #define OnPlayerDisconnect BInv_OnPlayerDisconnect
  296. #if defined BInv_OnPlayerDisconnect
  297.     forward BInv_OnPlayerDisconnect(playerid,reason);
  298. #endif
  299.  
  300. //////////////////////////////////////////////////////////////////////////
  301. //////////////////////////////////////////////////////////////////////////
  302. //
  303. //                      Internal stuff
  304. //
  305. //////////////////////////////////////////////////////////////////////////
  306. //////////////////////////////////////////////////////////////////////////
  307.  
  308.  
  309.  
  310.  
  311.  
  312. //////////////////////////
  313. //          Y_INI       //
  314. //////////////////////////
  315.  
  316. #if defined INV_SAVE_Y_INI
  317.     forward LoadPlayerItems_Items(playerid,name[],value[]);
  318.     public LoadPlayerItems_Items(playerid,name[],value[])
  319.     {
  320.         new tmp;
  321.         for(new i; i < sizeof(UsableItems); i++)
  322.         {
  323.             if(isnull(UsableItems[i])) continue;
  324.             if(!strcmp(name,UsableItems[i]))
  325.                 GivePlayerItem(playerid,name,strval(value));
  326.         }
  327.         return 1;
  328.     }
  329. #endif
  330.  
  331. stock Inv_YINILoadItems(playerid)
  332. {
  333.     new pv[MAX_PLAYER_NAME],path_to_inv[32];
  334.     GetPlayerName(playerid, pv,sizeof(pv));
  335.     format(path_to_inv,sizeof(path_to_inv),"Inventory_%s.ini",pv);
  336.     if(fexist(path_to_inv))
  337.         INI_ParseFile(path_to_inv,"LoadPlayerItems_%s",.bExtra=true,.extra=playerid);
  338.     return;
  339. }
  340.  
  341. stock Inv_YINISaveItems(playerid)
  342. {
  343.     new pv[MAX_PLAYER_NAME],path_to_inv[32];
  344.     GetPlayerName(playerid, pv,sizeof(pv));
  345.     format(path_to_inv,sizeof(path_to_inv),"Inventory_%s.ini",pv);
  346.     new INI:inv_file = INI_Open(path_to_inv);
  347.     INI_SetTag(inv_file,"Items");
  348.     for(new i; i < sizeof(UsableItems); i++)
  349.     {
  350.         if(isnull(UsableItems[i])) continue;
  351.         INI_WriteInt(inv_file,UsableItems[i],GetPVarInt(playerid, UsableItems[i]));
  352.     }
  353.     INI_Close(inv_file);
  354.     return;
  355. }
  356.  
  357. //////////////////////////
  358. //          MySQL       //
  359. //////////////////////////
  360.  
  361.  
  362. stock Inv_MySQLLoadItems(playerid)
  363. {
  364.     new query[80],name[MAX_PLAYER_NAME];
  365.     new item[MAX_ITEM_NAME],quantity;
  366.     GetPlayerName(playerid,name,sizeof(name));
  367.     #if defined INV_MYSQL_R38
  368.         mysql_format(inv_con_handle,query,sizeof(query),"SELECT * FROM player_items WHERE Username = '%e'",name);
  369.         new Cache:result = mysql_query(inv_con_handle, query);
  370.         {
  371.             for(new i = 0; i < cache_get_row_count(); i++)
  372.             {
  373.                 cache_get_field_content(i,"ItemName",item);
  374.                 quantity = cache_get_field_content_int(i, "Quantity");
  375.                 for(new j; j < sizeof(UsableItems); j++)
  376.                 {
  377.                     if(isnull(UsableItems[j])) continue;
  378.                     if(!strcmp(item,UsableItems[j]))
  379.                     {
  380.                         GivePlayerItem(playerid,item,quantity);
  381.                         PrimarKeys[playerid][j] = cache_get_field_content_int(j,"Id");
  382.                     }
  383.                 }
  384.             }
  385.         }
  386.         cache_delete(result);
  387.     #elseif defined INV_MYSQL_R6
  388.         mysql_real_escape_string(name,name);
  389.         format(query,sizeof(query),"SELECT * FROM player_items WHERE Username = '%s'",name);
  390.         mysql_query(query);
  391.         mysql_store_result();
  392.         while(mysql_retrieve_row())
  393.         {
  394.             mysql_fetch_field_row(item, "Quantity");
  395.             quantity = strval(item);
  396.             mysql_fetch_field_row(item,"ItemName");
  397.             for(new i; i < sizeof(UsableItems); i++)
  398.             {
  399.                 if(isnull(UsableItems[i])) continue;
  400.                 if(!strcmp(item,UsableItems[i]))
  401.                 {
  402.                     GivePlayerItem(playerid,item,quantity);
  403.                     mysql_fetch_field_row(item, "Id");
  404.                     PrimarKeys[playerid][i] = strval(item);
  405.                 }
  406.             }
  407.         }
  408.         mysql_free_result();
  409.     #endif
  410.     return;
  411. }
  412. stock Inv_MySQLSaveItems(playerid)
  413. {
  414.     new query[150],name[MAX_PLAYER_NAME];
  415.     GetPlayerName(playerid, name, sizeof(name));
  416.     for(new i = 0; i < sizeof(UsableItems); i++)
  417.     {
  418.         if(isnull(UsableItems[i])) continue;
  419.         new amount = GetPVarInt(playerid,UsableItems[i]);
  420.         #if defined INV_MYSQL_R38
  421.             mysql_format(inv_con_handle,query,sizeof(query),"INSERT INTO player_items VALUES(%d,'%e','%e',%d) ON DUPLICATE KEY UPDATE Quantity = %d",
  422.                 PrimarKeys[playerid][i],name,UsableItems[i],amount,amount);
  423.             mysql_pquery(inv_con_handle,query);
  424.         #elseif defined INV_MYSQL_R6
  425.             mysql_real_escape_string(name,name);
  426.             mysql_real_escape_string(UsableItems[i],UsableItems[i]);
  427.             format(query,sizeof(query),"INSERT INTO player_items VALUES (%d,'%s','%s',%d) ON DUPLICATE KEY UPDATE Quantity = %d",
  428.                 PrimarKeys[playerid][i],name,UsableItems[i],amount,amount);
  429.             mysql_query(query);
  430.         #endif
  431.     }
  432.     return;
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement