Guest User

Untitled

a guest
Jan 14th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.96 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 "root"
  26. #define SQL_PASS ""
  27.  
  28. //MySQL connection handle
  29. new g_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. Kills,
  41. Deaths,
  42. bool:IsLoggedIn,
  43. bool:IsRegistered,
  44. LoginAttempts,
  45. LoginTimer
  46. };
  47. new Player[MAX_PLAYERS][E_PLAYERS];
  48.  
  49. new g_MysqlRaceCheck[MAX_PLAYERS];
  50.  
  51. //dialog data
  52. enum
  53. {
  54. DIALOG_INVALID,
  55. DIALOG_UNUSED,
  56.  
  57. DIALOG_LOGIN,
  58. DIALOG_REGISTER,
  59. };
  60.  
  61.  
  62. /*
  63. * SA-MP callbacks
  64. */
  65.  
  66. public OnGameModeInit()
  67. {
  68. mysql_log(LOG_ERROR | LOG_WARNING, LOG_TYPE_HTML); //logs errors and warnings into a nice .html log file
  69. g_SQL = mysql_connect(SQL_HOST, SQL_USER,SQL_DB, SQL_PASS);
  70.  
  71. SetupPlayerTable();
  72. PlayerSkins();
  73. return 1;
  74. }
  75.  
  76. public OnGameModeExit()
  77. {
  78. //save all player data before closing connection
  79. for(new p=0; p < MAX_PLAYERS; ++p)
  80. {
  81. if(IsPlayerConnected(p) && Player[p][IsLoggedIn] && Player[p][ID] > 0)
  82. {
  83. orm_save(Player[p][ORM_ID]);
  84. orm_destroy(Player[p][ORM_ID]);
  85. Player[p][IsLoggedIn] = false;
  86. }
  87. }
  88.  
  89. mysql_close();
  90. return 1;
  91. }
  92.  
  93. public OnPlayerConnect(playerid)
  94. {
  95. g_MysqlRaceCheck[playerid]++;
  96. //reset player data
  97. for(new E_PLAYERS:e; e < E_PLAYERS; ++e)
  98. Player[playerid][e] = 0;
  99.  
  100. GetPlayerName(playerid, Player[playerid][Name], MAX_PLAYER_NAME);
  101.  
  102. //create orm instance and register all needed variables
  103. new ORM:ormid = Player[playerid][ORM_ID] = orm_create("players", g_SQL);
  104.  
  105. orm_addvar_int(ormid, Player[playerid][ID], "id");
  106. orm_addvar_string(ormid, Player[playerid][Name], MAX_PLAYER_NAME, "username");
  107. orm_addvar_string(ormid, Player[playerid][Password], 129, "password");
  108. orm_addvar_int(ormid, Player[playerid][Money], "money");
  109. orm_addvar_int(ormid, Player[playerid][Kills], "kills");
  110. orm_addvar_int(ormid, Player[playerid][Deaths], "deaths");
  111. orm_setkey(ormid, "username");
  112.  
  113. //tell the orm system to load all data, assign it to our variables and call our callback when ready
  114. orm_load(ormid, "OnPlayerDataLoaded", "dd", playerid, g_MysqlRaceCheck[playerid]);
  115. return 1;
  116. }
  117.  
  118. public OnPlayerDataLoaded(playerid, race_check)
  119. {
  120. /* race condition check:
  121. player A connects -> SELECT query is fired -> this query takes very long
  122. while the query is still processing, player A with playerid 2 disconnects
  123. player B joins now with playerid 2 -> our laggy SELECT query is finally finished, but for the wrong player
  124.  
  125. what do we do against it?
  126. we create a connection count for each playerid and increase it everytime the playerid connects or disconnects
  127. we also pass the current value of the connection count to our OnPlayerDataLoaded callback
  128. then we check if current connection count is the same as connection count we passed to the callback
  129. if yes, everything is okay, if not, we just kick the player
  130. */
  131. if(race_check != g_MysqlRaceCheck[playerid])
  132. return Kick(playerid);
  133.  
  134. orm_setkey(Player[playerid][ORM_ID], "id");
  135.  
  136. new string[128];
  137. switch(orm_errno(Player[playerid][ORM_ID]))
  138. {
  139. case ERROR_OK:
  140. {
  141. 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]);
  142. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", string, "Login", "Abort");
  143. Player[playerid][IsRegistered] = true;
  144. }
  145. case ERROR_NO_DATA:
  146. {
  147. 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]);
  148. ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration", string, "Register", "Abort");
  149. Player[playerid][IsRegistered] = false;
  150. }
  151. }
  152. return 1;
  153. }
  154.  
  155. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  156. {
  157. if(dialogid == DIALOG_INVALID || dialogid == DIALOG_UNUSED)
  158. return 1;
  159.  
  160.  
  161. switch(dialogid)
  162. {
  163. case DIALOG_LOGIN:
  164. {
  165. if(!response)
  166. return Kick(playerid);
  167.  
  168. if(strlen(inputtext) <= 5)
  169. return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login",
  170. CHAT_RED "Your password must be longer than 5 characters!\n" CHAT_WHITE "Please enter your password in the field below:",
  171. "Login", "Abort");
  172.  
  173. new hashed_pass[129];
  174. WP_Hash(hashed_pass, sizeof(hashed_pass), inputtext);
  175.  
  176. if(strcmp(hashed_pass, Player[playerid][Password]) == 0)
  177. {
  178. //correct password, spawn the player
  179. ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Login", "You have been successfully logged in.", "Okay", "");
  180. Player[playerid][IsLoggedIn] = true;
  181.  
  182. }
  183. else
  184. {
  185. Player[playerid][LoginAttempts]++;
  186. if(Player[playerid][LoginAttempts] >= 3)
  187. {
  188. ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Login", CHAT_RED "You have mistyped your password too often (3 times).", "Okay", "");
  189. DelayedKick(playerid);
  190. }
  191. else
  192. 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");
  193. }
  194. }
  195.  
  196. case DIALOG_REGISTER:
  197. {
  198. if(!response)
  199. return Kick(playerid);
  200.  
  201. if(strlen(inputtext) <= 5)
  202. return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration",
  203. CHAT_RED "Your password must be longer than 5 characters!\n" CHAT_WHITE "Please enter your password in the field below:",
  204. "Register", "Abort");
  205.  
  206. WP_Hash(Player[playerid][Password], 129, inputtext);
  207. orm_save(Player[playerid][ORM_ID], "OnPlayerRegister", "d", playerid);
  208. }
  209.  
  210. default:
  211. return 0;
  212. }
  213. return 1;
  214. }
  215.  
  216. public OnPlayerRegister(playerid)
  217. {
  218. ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Registration", "Account successfully registered, you have been automatically logged in.", "Okay", "");
  219. Player[playerid][IsLoggedIn] = true;
  220. Player[playerid][IsRegistered] = true;
  221. return 1;
  222. }
  223.  
  224. public OnPlayerSpawn(playerid)
  225. {
  226. ResetPlayerMoney(playerid);
  227. GivePlayerMoney(playerid, Player[playerid][Money]);
  228. SetPlayerPos(playerid, 1357.5046,-1284.6573,13.2581);
  229. return 1;
  230. }
  231. public OnPlayerDeath(playerid, killerid, reason)
  232. {
  233. Player[killerid][Kills] ++;
  234. Player[playerid][Deaths] ++;
  235. return 1;
  236. }
  237. public OnPlayerDisconnect(playerid,reason)
  238. {
  239. g_MysqlRaceCheck[playerid]++;
  240. if(Player[playerid][IsLoggedIn] && Player[playerid][ID] > 0)
  241. orm_save(Player[playerid][ORM_ID]); //if Player[playerid][ID] has a valid value, orm_save sends an UPDATE query, else an INSERT query
  242. orm_destroy(Player[playerid][ORM_ID]);
  243.  
  244.  
  245. return 1;
  246. }
  247.  
  248.  
  249. /*
  250. * functions
  251. */
  252.  
  253. SetupPlayerTable()
  254. {
  255. mysql_query(g_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',`kills` int(10) NOT NULL default '0',`deaths` int(10) NOT NULL default '0')", false);
  256. return 1;
  257. }
  258.  
  259.  
  260. DelayedKick(playerid, time=500)
  261. {
  262. SetTimerEx("_KickPlayerDelayed", time, false, "d", playerid);
  263. return 1;
  264. }
  265.  
  266. forward _KickPlayerDelayed(playerid);
  267. public _KickPlayerDelayed(playerid)
  268. {
  269. Kick(playerid);
  270. return 1;
  271. }
  272.  
  273. PlayerSkins()
  274. {
  275. AddPlayerClass(1, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
  276. AddPlayerClass(2, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
  277. }
  278. main() {}
Advertisement
Add Comment
Please, Sign In to add comment