Advertisement
Bebras

[INC]Inventory v0.3 - Bebras

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