AXV09

Reg & Log - OS [1.0.5214]

Dec 27th, 2012
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. /*
  2. * Praca przykladowa przygotowana przez AXV dnia 2012-27-09 21:22
  3. *
  4. * Zleceniodawca: Kopciu
  5. * Autor: AXV
  6. *
  7. * @edit 2012-12-27 20:31 - Wystawiono prace jako skrypt na serwis Pawno.PL
  8. *
  9. * Wersja Open source.
  10. * (C) 2012, AXV. All rights reserved.
  11. */
  12.  
  13. #define FILTERSCRIPT
  14. #include a_samp
  15.  
  16. #if MAX_PLAYERS == 500
  17. #undef MAX_PLAYERS
  18. #define MAX_PLAYERS (5) // sloty
  19. #endif
  20.  
  21. // biblioteki
  22. #include md5
  23. #include sscanf // Y_Less 2.8.1
  24. #include mysql // StrickenKid 2.1.1
  25.  
  26. #define SQL_HOST "localhost"
  27. #define SQL_USR "user"
  28. #define SQL_PSWRD "password"
  29. #define SQL_DB "datebase"
  30.  
  31. #define DIALOG_INFORMATION (0)
  32. #define DIALOG_LOGIN (1)
  33. #define DIALOG_REGISTER (2)
  34.  
  35. #define clientMsg(%1,%2,%3) SendClientMessage(%1, %2, %3)
  36. #define hidePlayerDialog(%1) ShowPlayerDialog(%1, -1, 0, "", "", "", "")
  37. #define MAX_LOGIN_ATTEMPTS (3)
  38.  
  39. // zmienne
  40. enum E_PLAYER_DATA {
  41.  
  42. bool:loggedIn,
  43. score,
  44. kills,
  45. deaths,
  46. loginAttempt
  47.  
  48. };
  49.  
  50. new
  51. playerData[MAX_PLAYERS][E_PLAYER_DATA],
  52. MySQL:hMYSQL
  53. ;
  54.  
  55. // funkcje publiczne
  56. public OnFilterScriptInit() {
  57.  
  58. hMYSQL = mysql_init(1);
  59. new mHandle = mysql_connect(SQL_HOST, SQL_USR, SQL_PSWRD, SQL_DB, hMYSQL, 1);
  60.  
  61. if (mHandle) {
  62. mysql_query("CREATE TABLE IF NOT EXISTS players ( \
  63. `id` INT(11) NOT NULL AUTO_INCREMENT, \
  64. `nick` VARCHAR(24) NOT NULL, \
  65. `haslo` VARCHAR(32) NOT NULL, \
  66. `score` INT(11) NOT NULL DEFAULT '0', \
  67. `kills` INT(11) NOT NULL DEFAULT '0', \
  68. `deaths` INT(11) NOT NULL DEFAULT '0', \
  69. PRIMARY KEY `id` (`id`), \
  70. KEY `nick` (`nick`) \
  71. ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;"), printf("połączono z bazą danych!");
  72. } else printf("nie można nawiązać połączania z bazą!"), SendRconCommand("exit");
  73.  
  74. return 1;
  75. }
  76.  
  77. public OnFilterScriptExit() mysql_close(hMYSQL);
  78.  
  79. public OnPlayerConnect(playerid) {
  80.  
  81. playerData[playerid][loggedIn] = false;
  82. playerData[playerid][loginAttempt] = 0;
  83. playerData[playerid][score] = 0;
  84. playerData[playerid][kills] = 0;
  85. playerData[playerid][deaths] = 0;
  86.  
  87. new buf[127];
  88. if (accountPlayerExists(playerid)) {
  89.  
  90. format(buf, sizeof(buf), "Witaj, %s!\nTwoje konto jest zarejestrowane.\nProszę wpisać hasło w poniższe okno, a następnie kliknąć przycisk 'ZALOGUJ'", getPlayerName(playerid));
  91. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logowanie", buf, "Zaloguj", "Opuść");
  92.  
  93. } else {
  94.  
  95. format(buf, sizeof(buf), "Witaj, %s!\nTwoje konto nie jest zarejestrowane.\nProszę wpisać hasło w poniższe okno, a następnie kliknąć przycisk 'REJESTRUJ'", getPlayerName(playerid));
  96. ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Rejestracja", buf, "Rejestruj", "");
  97. }
  98. }
  99.  
  100. public OnPlayerDisconnect(playerid, reason) {
  101.  
  102. if (playerData[playerid][loggedIn]) {
  103.  
  104. new
  105. buf[127]
  106. ;
  107.  
  108. format(buf, sizeof(buf), "UPDATE players SET score = %d, kills = %d, deaths = %d WHERE nick = '%s'", playerData[playerid][score], playerData[playerid][kills], playerData[playerid][deaths], getPlayerName(playerid));
  109. mysql_query(buf);
  110. }
  111. }
  112.  
  113. public OnPlayerRequestClass(playerid, classid) { }
  114.  
  115. public OnPlayerDeath(playerid, killerid, reason) {
  116.  
  117. if (killerid != INVALID_PLAYER_ID)
  118. playerData[playerid][kills]++;
  119. else
  120. playerData[playerid][deaths]++;
  121.  
  122. }
  123.  
  124. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  125.  
  126. switch (dialogid) {
  127.  
  128. case DIALOG_INFORMATION: return 1;
  129.  
  130. case DIALOG_LOGIN: {
  131.  
  132. if (!response) return clientMsg(playerid, -1, "Anulowałeś logowanie, papa!"), hidePlayerDialog(playerid), Kick(playerid);
  133.  
  134. if (!passwordCorrect(inputtext)) {
  135.  
  136. playerData[playerid][loginAttempt]++;
  137. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logowanie", "Twoje konto jest zarejstrowane.\nPowtórz hasło, ponieważ wprowadzone jest nieprawidłowe.", "Zaloguj", "Opuść");
  138. return clientMsg(playerid, -1, "Nieprawidlowe haslo, sprobuj ponownie.");
  139. }
  140.  
  141. loginPlayer(playerid, inputtext);
  142. }
  143.  
  144. case DIALOG_REGISTER: {
  145.  
  146. if (!response) return 1; // secure
  147.  
  148. new buf[127];
  149.  
  150. if (!passwordCorrect(inputtext)) {
  151.  
  152. format(buf, sizeof(buf), "Witaj, %s!\nTwoje konto nie jest zarejestrowane.\nProszę wpisać hasło w poniższe okno, a następnie kliknąć przycisk 'REJESTRUJ'", getPlayerName(playerid));
  153. ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Rejestracja", buf, "Rejestruj", "");
  154. return clientMsg(playerid, -1, "Hasło może się składać tylko z liter i cyfr.");
  155. }
  156.  
  157. format(buf, sizeof(buf), "INSERT INTO players (nick, haslo) VALUES ('%s', '%s')", getPlayerName(playerid), MD5_Hash(passHash(inputtext, getPlayerName(playerid))));
  158. if (mysql_query(buf) || mysql_affected_rows() < 1) return clientMsg(playerid, -1, "Wystapil blad podczas wysylania formularza. Sprobuj ponownie."), Kick(playerid);
  159. return 1;
  160. }
  161. }
  162.  
  163. return 1;
  164. }
  165.  
  166. // funkcje
  167. passHash(const pass[], const name[]) { // solanka ;)
  168.  
  169. new hash[32];
  170. format(hash, sizeof(hash), "%sx6m5K%s", pass, name);
  171. return hash;
  172. }
  173.  
  174. loadPlayerData(playerid) {
  175.  
  176. playerData[playerid][score] = strval(getPlayerAccountData(playerid, "score"));
  177. playerData[playerid][kills] = strval(getPlayerAccountData(playerid, "kills"));
  178. playerData[playerid][deaths] = strval(getPlayerAccountData(playerid, "deaths"));
  179. }
  180.  
  181. getPlayerAccountData(playerid, const data[]) {
  182.  
  183. new
  184. buf[127],
  185. result[80]
  186. ;
  187.  
  188. format(buf, sizeof(buf), "SELECT %s FROM players WHERE nick = '%s'", data, getPlayerName(playerid));
  189. if (mysql_query(buf))
  190. mysql_ping();
  191.  
  192. mysql_store_result(), mysql_fetch_row(result), mysql_free_result();
  193. return result;
  194. }
  195.  
  196. accountPlayerExists(playerid) {
  197.  
  198. new
  199. buf[127],
  200. bool:exists = false,
  201. nick[MAX_PLAYER_NAME]
  202. ;
  203.  
  204. mysql_real_escape_string(getPlayerName(playerid), nick);
  205. format(buf, sizeof(buf), "SELECT 1 FROM players WHERE nick = '%s'", nick);
  206. if (mysql_query(buf))
  207. mysql_ping();
  208.  
  209. mysql_store_result();
  210.  
  211. if (mysql_num_rows())
  212. exists = true;
  213.  
  214. mysql_free_result();
  215. return exists;
  216. }
  217.  
  218. passwordCorrect(szPassword[]) {
  219.  
  220. new
  221. length = strlen(szPassword)
  222. ;
  223.  
  224. if (!(5 <= length < 20)) return 0;
  225.  
  226. for (new i = 0; i != length; i++) {
  227. switch (szPassword[i]) {
  228. case 'a' .. 'z': continue;
  229. case 'A' .. 'Z': continue;
  230. case '0' .. '9': continue;
  231. case '!', '@', '#', '$', '%', '^', '&', '*', '(', ')': continue;
  232. default: return 0;
  233. }
  234. }
  235.  
  236. return 1;
  237. }
  238.  
  239. loginPlayer(playerid, const password[]) {
  240.  
  241. new buf[127];
  242.  
  243. if (playerData[playerid][loginAttempt] >= MAX_LOGIN_ATTEMPTS) {
  244. clientMsg(playerid, -1, "Wykorzystałeś limit prób zalogowań! Zostałeś wyrzucony.");
  245. hidePlayerDialog(playerid);
  246. return Kick(playerid);
  247. }
  248.  
  249. format(buf, sizeof(buf), "SELECT haslo FROM players WHERE nick = '%s'", getPlayerName(playerid));
  250. mysql_query(buf), mysql_store_result(), mysql_fetch_row(buf), mysql_free_result();
  251.  
  252. if (strcmp(MD5_Hash(passHash(password, getPlayerName(playerid))), buf, true) != 0 && buf[0]) {
  253. playerData[playerid][loginAttempt]++;
  254. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logowanie", "Twoje konto jest zarejstrowane.\nPowtórz hasło, ponieważ wprowadzone jest nieprawidłowe.", "Zaloguj", "Opuść");
  255. return clientMsg(playerid, -1, "Nieprawidlowe haslo, sprobuj ponownie.");
  256. }
  257.  
  258. playerData[playerid][loggedIn] = true;
  259. clientMsg(playerid, -1, "Zostales zalogowany, milej Gry.");
  260. OnPlayerRequestClass(playerid, 0), loadPlayerData(playerid);
  261. return 1;
  262. }
  263.  
  264. getPlayerName(playerid) {
  265.  
  266. static pName[MAX_PLAYER_NAME];
  267.  
  268. pName[0] = 0;
  269. GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
  270. return pName;
  271. }
  272. // The end, thanks for watch. :)
Advertisement
Add Comment
Please, Sign In to add comment