Guest User

fixed version

a guest
Oct 14th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.03 KB | None | 0 0
  1. #include <a_samp>
  2. #include <YSI/y_ini>
  3. #include <zcmd>
  4. native WP_Hash(buf[], len, const str[]);
  5.  
  6.  
  7. #define PATH "BankAccount/%s.ini" // this will define the path to the folder where the files will be saved
  8. #define COLOR_YELLOW 0xFFFF00FF
  9. #define COLOR_RED 0xFF0000FF
  10. // those will be the dialog ids, change them to your own dialogids
  11. #define DIALOG_BANKHOME 5
  12. #define DIALOG_BANKWITHDRAW 6
  13. #define DIALOG_BANKDEPOSIT 7
  14.  
  15. stock BankPath(playerid)
  16. {
  17. new string[50];
  18. format (string, sizeof string, PATH, GetName(playerid) );
  19. return string;
  20. }
  21.  
  22. stock GetName(playerid)
  23. {
  24. new name[24];
  25. GetPlayerName(playerid, name, sizeof name);
  26. return name;
  27. }
  28.  
  29.  
  30. enum pInfo { // add those things which are inside this to your own enum of your PlayerInfo, or just create a new variable for all the bank things
  31. BankPassword[129], // the password is stored in this
  32. BankWealth, // the amount of money you have on the ban
  33. BankLogged // are you logged
  34. }
  35. new PlayerInfo[MAX_PLAYERS][pInfo];
  36.  
  37.  
  38. public OnPlayerConnect(playerid)
  39. {
  40. // NOTE: if youre making this in your GM, add this at the top, or down
  41. if( !fexist( BankPath(playerid) ) ) // checks if you have a bank account file
  42. {
  43. SendClientMessage(playerid, COLOR_YELLOW, "You do not own a bank account yet! Please use /registerbank to register a bank account! ");
  44. // you dont need a 'return' here, otherwise if you dont have a bank account it wont continue with the things that are used down here
  45. }
  46. else if( fexist( BankPath(playerid) ) )
  47. {
  48. INI_ParseFile( BankPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid); // this will parse all the things that are in the files to the variables
  49. // for this 'INI_ParseFile' we will have to create a new 'callback', to load everything
  50. }
  51. return 1;
  52. }
  53.  
  54. // if you already have something like this, just add those 2 lines to the existing one, and it should work aswell
  55. forward LoadUser_data(playerid, name[], value[]);
  56. public LoadUser_data(playerid, name[], value[])
  57. {
  58. INI_Int("Bank_Wealth", PlayerInfo[playerid][BankWealth]);
  59. INI_String("Bank_Password", PlayerInfo[playerid][BankPassword], 129); // make sure you have the size behind the INI_String, otherwise it will give you an error
  60. return 1;
  61. }
  62.  
  63.  
  64. COMMAND:registerbank(playerid, params[])
  65. {
  66. if( !fexist( BankPath(playerid) ) ) // this will check if you dont already have a bank file
  67. {
  68. if( !strlen( params ) ) return SendClientMessage(playerid, COLOR_RED, "Usage: /registerbank [pin]"); // if you didnt enter anything in the params, it shows this message
  69. if( strlen( params ) < 4 || strlen( params ) > 4 ) return SendClientMessage(playerid, COLOR_RED, "A pin has 4 numbers, please use numbers only! "); // if the params have under 4 characters or above 4 characters it shows the message
  70. if( !isnumeric( params ) ) return SendClientMessage(playerid, COLOR_RED, "Numbers only please! "); // when the player doesnt enter numbers, it shows him the message
  71. new Buffer[129], Message[128]; // create some variables which we will use to format a message, and where we will save the password in
  72. new INI:file = INI_Open( BankPath(playerid) ); // opening the file using the variable 'file'
  73. WP_Hash( Buffer, sizeof Buffer, params ); // hashing the params, so you got an encrypted pin code
  74. INI_WriteString( file, "Bank_Password", Buffer ); // saves the password inside the file
  75. INI_WriteInt( file, "Bank_Wealth", 0 ); // setting the bank health to 0 so you will have the line in it
  76. INI_WriteInt( file, "Bank_Logged", 1 ); // you are now logged in
  77. INI_Close( file ); // closing the file
  78. INI_ParseFile( BankPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid); // parsing all the things that are in the file in your variables
  79. format( Message, sizeof Message, "Thank you for registering %s! Your bank pin is: %s", GetName(playerid), params ); // formatting a message, you should know how this works
  80. SendClientMessage( playerid, COLOR_YELLOW, Message ); // sending the message.. duh :)
  81. }
  82. else
  83. {
  84. SendClientMessage(playerid, COLOR_RED, "You already have a bank account and dont need to register. "); // if you already have a file, you get this message and stops the command
  85. return 0;
  86. }
  87. return 1;
  88. }
  89.  
  90. COMMAND:banklogin(playerid, params[])
  91. {
  92. if( PlayerInfo[playerid][BankLogged] == 1 ) return SendClientMessage(playerid, COLOR_RED, "You're already logged in! "); // if youre already logged in it gives you this message
  93. if( !fexist( BankPath(playerid) ) ) // if you dont have a bank account yet (the file doesnt exist) it will show this message and stops the command
  94. {
  95. SendClientMessage(playerid, COLOR_RED, "You dont have a bank account yet, please use /registerbank to register it! ");
  96. return 0;
  97. }
  98. else if( fexist( BankPath(playerid) ) ) // if your file does exist, it continues with this
  99. {
  100. if( !strlen( params ) ) return SendClientMessage(playerid, COLOR_RED, "Usage: /banklogin [pin]"); // like the register command
  101. if( strlen( params ) < 4 || strlen( params ) > 4 ) return SendClientMessage(playerid, COLOR_RED, "A pin has 4 numbers, please use numbers only! "); // idem dito
  102. if( !isnumeric( params ) ) return SendClientMessage(playerid, COLOR_RED, "Numbers only please! "); // what can i say? :)
  103. if( !strcmp( params, PlayerInfo[playerid][BankPassword], true ) ) // with this it compares the params with the variable BankPassword, if they both compare, strcmp will return '0', and thats why we have '1' in front of strcmp. the 'true' wont really matter in this case, since its for to check if youre using upper cased letters or lower cased
  104. {
  105. PlayerInfo[playerid][BankLogged] = 1; // youre now logged in
  106. SendClientMessage(playerid, COLOR_YELLOW, "Thank you for logging in to your bank account! "); // sending a message
  107. }
  108. else // if you enter the wrong pin
  109. {
  110. SendClientMessage(playerid, COLOR_RED, "Wrong pin! Please try again. ");
  111. return 0;
  112. }
  113. return 1;
  114. }
  115. return 1;
  116. }
  117.  
  118. COMMAND:bankhome(playerid, params[])
  119. {
  120. if( PlayerInfo[playerid][BankLogged] != 1 ) return SendClientMessage( playerid, COLOR_RED, "You are not logged in and can't open your bank yet. "); // this will check if you are logged in, and if youre not it stops the command
  121. ShowPlayerDialog( playerid, DIALOG_BANKHOME, DIALOG_STYLE_LIST, "Bank home", "Wealth check \nWithdraw money \nDeposit money", "Ok", "Leave" ); // showing the bank home dialog
  122. return 1;
  123. }
  124.  
  125. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  126. {
  127. switch( dialogid ) // this will 'switch' through the dialogids, you can use if() aswell, but if im right its faster with switch()
  128. {
  129. case DIALOG_BANKHOME: // when the dialog is DIALOG_BANKHOME (5)
  130. {
  131. if( response )
  132. {
  133. switch( listitem )
  134. {
  135. case 0: // when you selected the first listitem ('Wealth check') and press the first button
  136. {
  137. new Wealth[128];
  138. format( Wealth, sizeof Wealth, "You have $%i on your bank account. ", PlayerInfo[playerid][BankWealth] ); // this will get the amount of money that is in your bank account
  139. SendClientMessage(playerid, COLOR_YELLOW, Wealth); // this will send the message.. hm how is that possible o.O
  140. ShowPlayerDialog( playerid, DIALOG_BANKHOME, DIALOG_STYLE_LIST, "Bank home", "Wealth check \nWithdraw money \nDeposit money", "Ok", "Leave" ); // Showing the dialog again, so you dotn have to do /bankhome again
  141. return 0;
  142. }
  143. case 1: // when you selected the listitem 'Withdraw money' and press the first button
  144. {
  145. ShowPlayerDialog( playerid, DIALOG_BANKWITHDRAW, DIALOG_STYLE_INPUT, "Bank Withdraw", "Please enter the amount of money you want to withdraw. ", "Ok", "Back" ); // showing a new dialog
  146. }
  147. case 2: // when you selected the listitem 'Deposit money' and press the first button
  148. {
  149. ShowPlayerDialog( playerid, DIALOG_BANKDEPOSIT, DIALOG_STYLE_INPUT, "Bank Deposit", "Please enter the amoutn of money you want to deposit. ", "Ok", "Back" ); // showing a new dialog
  150. }
  151. }
  152. }
  153.  
  154. }
  155. case DIALOG_BANKWITHDRAW: // when the dialog is DIALOG_BANKWITHDRAW (6)
  156. {
  157. if( response )
  158. {
  159. if( !isnumeric( inputtext ) ) return SendClientMessage(playerid, COLOR_RED, "Numbers only! "); // this will check if the inputtext contains numbers
  160. if( strval( inputtext ) > PlayerInfo[playerid][BankWealth] ) return SendClientMessage( playerid, COLOR_RED, "You dont have that amount of money on your bank! " ), ShowPlayerDialog( playerid, DIALOG_BANKWITHDRAW, DIALOG_STYLE_INPUT, "Bank Withdraw", "Please enter the amount of money you want to withdraw.", "Ok", "Back" ); // if the inputtext is higher then you have on your bank, it will show an error message
  161. PlayerInfo[playerid][BankWealth] = ( PlayerInfo[playerid][BankWealth] - strval( inputtext ) ); // this will remove the money from your bank account
  162. GivePlayerMoney( playerid, strval( inputtext ) ); // this will add the money from the bank account to your own money
  163. new String[128];
  164. format( String, sizeof String, "You withdrew $%i from your bank, and now got $%i on your bank left. ", strval( inputtext ), PlayerInfo[playerid][BankWealth] ); // formatting amessage which shows the amount you withdrew, and the money you have left
  165. SendClientMessage( playerid, COLOR_YELLOW, String ); // sending the message
  166. ShowPlayerDialog( playerid, DIALOG_BANKHOME, DIALOG_STYLE_LIST, "Bank home", "Wealth check \nWithdraw money \nDeposit money", "Ok", "Leave" ); // showing the dialog again, so you dont have to do /bankhome
  167. }
  168. else if( !response )
  169. {
  170. ShowPlayerDialog( playerid, DIALOG_BANKHOME, DIALOG_STYLE_LIST, "Bank home", "Wealth check \nWithdraw money \nDeposit money", "Ok", "Leave" ); // when you press the button 'Back', it will go to /bankhome
  171. }
  172. }
  173. case DIALOG_BANKDEPOSIT: // when the dialog is DIALOG_BANKDEPOSIT (7)
  174. {
  175. if( response )
  176. {
  177. if( !isnumeric( inputtext ) ) return SendClientMessage(playerid, COLOR_RED, "Numbers only! "); // this will check if the inputtext contains numbers
  178.  
  179. if( GetPlayerMoney( playerid ) < strval( inputtext ) ) return SendClientMessage( playerid, COLOR_RED, "You dont have that amount of money on you. "), ShowPlayerDialog( playerid, DIALOG_BANKDEPOSIT, DIALOG_STYLE_INPUT, "Bank Deposit", "Please enter the amoutn of money you want to deposit. ", "Ok", "Back" ); // if your money is lower as the amount you entered, it will give you an error message and show the same dialog
  180. PlayerInfo[playerid][BankWealth] = ( PlayerInfo[playerid][BankWealth] + strval( inputtext ) ); // adding the amount of money to your bank account
  181. GivePlayerMoney( playerid, - strval( inputtext) ); // removing the money from 'pocket'
  182. new String[128];
  183. format( String, sizeof String, "You depositted $%i to your bank, and now got $%i on your bank. ", strval( inputtext ), PlayerInfo[playerid][BankWealth] ); // formatting the message which howmutch you depositted, and the amount you now got on your bank
  184. SendClientMessage( playerid, COLOR_YELLOW, String ); // sending the message
  185. ShowPlayerDialog( playerid, DIALOG_BANKHOME, DIALOG_STYLE_LIST, "Bank home", "Wealth check \nWithdraw money \nDeposit money", "Ok", "Leave" ); // showing /bankhome dialog again
  186. }
  187. else if( !response )
  188. {
  189. ShowPlayerDialog( playerid, DIALOG_BANKHOME, DIALOG_STYLE_LIST, "Bank home", "Wealth check \nWithdraw money \nDeposit money", "Ok", "Leave" ); // when you press the button 'Back', it will go to /bankhome
  190. }
  191. }
  192. }
  193. return 1;
  194. }
  195.  
  196. public OnPlayerDisconnect(playerid, reason)
  197. {
  198. if( fexist( BankPath(playerid) ) )
  199. {
  200. new INI:file = INI_Open(BankPath(playerid)); // opening the file
  201. INI_WriteInt( file, "Bank_Wealth", PlayerInfo[playerid][BankWealth] ); // saving the bankwealth in the file
  202. INI_WriteInt( file, "Bank_Logged", 0 ); // changing that you arent logged in to your bank account
  203. INI_Close( file ); // closing the file
  204. }
  205. return 1;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment