Advertisement
Guest User

s

a guest
Jan 9th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3.  
  4. native WP_Hash(buffer[], len, const str[]);
  5.  
  6. #define WHITE 0xFFFFFFAA
  7. #define GREY 0xAFAFAFAA
  8. #define RED 0xFF0000AA
  9. #define YELLOW 0xFFFF00AA
  10. #define LIGHTBLUE 0x33CCFFAA
  11.  
  12. #define CHAT_WHITE "{FFFFFF}"
  13. #define CHAT_GREY "{AFAFAF}"
  14. #define CHAT_RED "{FF0000}"
  15. #define CHAT_YELLOW "{FFFF00}"
  16. #define CHAT_LIGHTBLUE "{33CCFF}"
  17.  
  18. forward OnPlayerDataLoaded(playerid, race_check);
  19. forward OnPlayerRegister(playerid);
  20.  
  21.  
  22. //MySQL configuration
  23. #define SQL_HOST "127.0.0.1"
  24. #define SQL_DB "database"
  25. #define SQL_USER "username"
  26. #define SQL_PASS "password"
  27.  
  28. //MySQL connection handle
  29. new SQL = -1;
  30.  
  31. //player data
  32. enum E_PLAYERS
  33. {
  34. ORM:ORM_ID,
  35.  
  36. ID,
  37. Name[MAX_PLAYER_NAME],
  38. Password[129],
  39. Money,
  40.  
  41. bool:IsLoggedIn,
  42. bool:IsRegistered,
  43. LoginAttempts,
  44. LoginTimer
  45. };
  46. new Player[MAX_PLAYERS][E_PLAYERS];
  47.  
  48. new MysqlRaceCheck[MAX_PLAYERS];
  49.  
  50. //dialog data
  51. enum {
  52. DIALOG_UNUSED,
  53.  
  54. DIALOG_LOGIN,
  55. DIALOG_REGISTER,
  56. };
  57.  
  58. public OnGameModeInit()
  59. {
  60. mysql_log(LOG_ERROR | LOG_WARNING, LOG_TYPE_HTML); //logs errors and warnings into a nice .html log file
  61. SQL = mysql_connect(SQL_HOST, SQL_USER,SQL_DB, SQL_PASS);
  62.  
  63. SetupPlayerTable();
  64. return 1;
  65. }
  66.  
  67. public OnGameModeExit()
  68. {
  69. //save all player data before closing connection
  70. for(new p=0; p < MAX_PLAYERS; ++p)
  71. if(IsPlayerConnected(p) && Player[p][IsLoggedIn] && Player[p][ID] > 0)
  72. orm_save(Player[p][ORM_ID]);
  73.  
  74. mysql_close();
  75. return 1;
  76. }
  77.  
  78. public OnPlayerConnect(playerid)
  79. {
  80. MysqlRaceCheck[playerid]++;
  81. //reset player data
  82. for(new E_PLAYERS:e; e < E_PLAYERS; ++e)
  83. Player[playerid][e] = 0;
  84.  
  85. GetPlayerName(playerid, Player[playerid][Name], MAX_PLAYER_NAME);
  86.  
  87. //create orm instance and register all needed variables
  88. new ORM:ormid = Player[playerid][ORM_ID] = orm_create("players", SQL);
  89.  
  90. orm_addvar_int(ormid, Player[playerid][ID], "id");
  91. orm_addvar_string(ormid, Player[playerid][Name], MAX_PLAYER_NAME, "username");
  92. orm_addvar_string(ormid, Player[playerid][Password], 129, "password");
  93. orm_addvar_int(ormid, Player[playerid][Money], "money");
  94. orm_setkey(ormid, "username");
  95.  
  96. //tell the orm system to load all data, assign it to our variables and call our callback when ready
  97. orm_load(ormid, "OnPlayerDataLoaded", "dd", playerid, MysqlRaceCheck[playerid]);
  98. return 1;
  99. }
  100.  
  101. public OnPlayerDataLoaded(playerid, race_check)
  102. {
  103. /* race condition check:
  104. player A connects -> SELECT query is fired -> this query takes very long
  105. while the query is still processing, player A with playerid 2 disconnects
  106. player B joins now with playerid 2 -> our laggy SELECT query is finally finished, but for the wrong player
  107.  
  108. what do we do against it?
  109. we create a connection count for each playerid and increase it everytime the playerid connects or disconnects
  110. we also pass the current value of the connection count to our OnPlayerDataLoaded callback
  111. then we check if current connection count is the same as connection count we passed to the callback
  112. if yes, everything is okay, if not, we just kick the player
  113. */
  114. if(race_check != MysqlRaceCheck[playerid])
  115. return Kick(playerid);
  116.  
  117. orm_setkey(Player[playerid][ORM_ID], "id");
  118.  
  119. new string[128];
  120. switch(orm_errno(Player[playerid][ORM_ID]))
  121. {
  122. case ERROR_OK:
  123. {
  124. format(string, sizeof(string), CHAT_WHITE "This account " CHAT_YELLOW "(%s)" CHAT_WHITE " is registered. Please login by entering your password in the field below:", Player[playerid][Name]);
  125. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", string, "Login", "Abort");
  126. Player[playerid][IsRegistered] = true;
  127. }
  128. case ERROR_NO_DATA:
  129. {
  130. format(string, sizeof(string), CHAT_WHITE "Welcome " CHAT_YELLOW "%s" CHAT_WHITE ", you can register by entering your password in the field below:", Player[playerid][Name]);
  131. ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration", string, "Register", "Abort");
  132. Player[playerid][IsRegistered] = false;
  133. }
  134. }
  135. return 1;
  136. }
  137.  
  138. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  139. {
  140. switch(dialogid)
  141. {
  142. case DIALOG_LOGIN:
  143. {
  144. if(!response)
  145. return Kick(playerid);
  146.  
  147. if(strlen(inputtext) <= 5)
  148. return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login",
  149. CHAT_RED "Your password must be longer than 5 characters!\n" CHAT_WHITE "Please enter your password in the field below:",
  150. "Login", "Abort");
  151.  
  152. new hashed_pass[129];
  153. WP_Hash(hashed_pass, sizeof(hashed_pass), inputtext);
  154.  
  155. if(strcmp(hashed_pass, Player[playerid][Password]) == 0)
  156. {
  157. //correct password, spawn the player
  158. ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Login", "You have been successfully logged in.", "Okay", "");
  159. Player[playerid][IsLoggedIn] = true;
  160.  
  161. SetSpawnInfo(playerid, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0);
  162. SpawnPlayer(playerid);
  163. }
  164. else
  165. {
  166. Player[playerid][LoginAttempts]++;
  167. if(Player[playerid][LoginAttempts] >= 3)
  168. {
  169. ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Login", CHAT_RED "You have mistyped your password too often (3 times).", "Okay", "");
  170. DelayedKick(playerid);
  171. }
  172. else
  173. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", CHAT_RED "Wrong password!\n" CHAT_WHITE "Please enter your password in the field below:", "Login", "Abort");
  174. }
  175. }
  176.  
  177. case DIALOG_REGISTER:
  178. {
  179. if(!response)
  180. return Kick(playerid);
  181.  
  182. if(strlen(inputtext) <= 5)
  183. return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration",
  184. CHAT_RED "Your password must be longer than 5 characters!\n" CHAT_WHITE "Please enter your password in the field below:",
  185. "Register", "Abort");
  186.  
  187. WP_Hash(Player[playerid][Password], 129, inputtext);
  188. orm_save(Player[playerid][ORM_ID], "OnPlayerRegister", "d", playerid);
  189. }
  190.  
  191. default:
  192. return 0;
  193. }
  194. return 1;
  195. }
  196.  
  197. public OnPlayerRegister(playerid)
  198. {
  199. ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Registration", "Account successfully registered, you have been automatically logged in.", "Okay", "");
  200. Player[playerid][IsLoggedIn] = true;
  201. Player[playerid][IsRegistered] = true;
  202.  
  203. SetSpawnInfo(playerid, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0);
  204. SpawnPlayer(playerid);
  205. return 1;
  206. }
  207.  
  208. public OnPlayerSpawn(playerid)
  209. {
  210. ResetPlayerMoney(playerid);
  211. GivePlayerMoney(playerid, Player[playerid][Money]);
  212.  
  213. return 1;
  214. }
  215.  
  216. public OnPlayerDisconnect(playerid,reason)
  217. {
  218. MysqlRaceCheck[playerid]++;
  219. if(Player[playerid][IsLoggedIn] && Player[playerid][ID] > 0)
  220. orm_save(Player[playerid][ORM_ID]); //if Player[playerid][ID] has a valid value, orm_save sends an UPDATE query, else an INSERT query
  221. return 1;
  222. }
  223.  
  224. stock SetupPlayerTable()
  225. {
  226. mysql_query(SQL, "CREATE TABLE IF NOT EXISTS `players` (`id` int(11) NOT NULL auto_increment PRIMARY KEY,`username` varchar(30) NOT NULL,`password` varchar(130) NOT NULL,`money` int(10) NOT NULL default '0')", false);
  227. return 1;
  228. }
  229.  
  230.  
  231. stock DelayedKick(playerid, time=500)
  232. {
  233. SetTimerEx("_KickPlayerDelayed", time, false, "d", playerid);
  234. return 1;
  235. }
  236.  
  237. forward _KickPlayerDelayed(playerid);
  238. public _KickPlayerDelayed(playerid)
  239. {
  240. Kick(playerid);
  241. return 1;
  242. }
  243.  
  244. main() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement