AXV09

Reg & Log - OS [1.0.5212]

Dec 27th, 2012
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 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. loginAttempt
  45.  
  46. };
  47.  
  48. new
  49. playerData[MAX_PLAYERS][E_PLAYER_DATA],
  50. MySQL:hMYSQL
  51. ;
  52.  
  53. // funkcje publiczne
  54. public OnFilterScriptInit() {
  55.  
  56. hMYSQL = mysql_init(1);
  57. new mHandle = mysql_connect(SQL_HOST, SQL_USR, SQL_PSWRD, SQL_DB, hMYSQL, 1);
  58.  
  59. if (mHandle) {
  60. mysql_query("CREATE TABLE IF NOT EXISTS players ( \
  61. `id` INT(11) NOT NULL AUTO_INCREMENT, \
  62. `nick` VARCHAR(24) NOT NULL, \
  63. `haslo` VARCHAR(32) NOT NULL, \
  64. `score` INT(11) NOT NULL DEFAULT '0', \
  65. PRIMARY KEY `id` (`id`), \
  66. KEY `nick` (`nick`) \
  67. ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;"), printf("połączono z bazą danych!");
  68. } else printf("nie można nawiązać połączania z bazą!"), SendRconCommand("exit");
  69.  
  70. return 1;
  71. }
  72.  
  73. public OnFilterScriptExit() mysql_close(hMYSQL);
  74.  
  75. public OnPlayerConnect(playerid) {
  76.  
  77. playerData[playerid][loggedIn] = false;
  78. playerData[playerid][loginAttempt] = 0;
  79. playerData[playerid][score] = 0;
  80.  
  81. new buf[127];
  82. if (accountPlayerExists(playerid)) {
  83.  
  84. 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));
  85. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logowanie", buf, "Zaloguj", "Opuść");
  86.  
  87. } else {
  88.  
  89. 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));
  90. ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Rejestracja", buf, "Rejestruj", "");
  91. }
  92. }
  93.  
  94. public OnPlayerDisconnect(playerid, reason) {
  95.  
  96. if (playerData[playerid][loggedIn]) {
  97.  
  98. new
  99. buf[127]
  100. ;
  101.  
  102. format(buf, sizeof(buf), "UPDATE players SET score = %d WHERE nick = '%s'", playerData[playerid][score], getPlayerName(playerid));
  103. mysql_query(buf);
  104. }
  105. }
  106.  
  107. public OnPlayerRequestClass(playerid, classid) { }
  108.  
  109. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  110.  
  111. switch (dialogid) {
  112.  
  113. case DIALOG_INFORMATION: return 1;
  114.  
  115. case DIALOG_LOGIN: {
  116.  
  117. if (!response) return clientMsg(playerid, -1, "Anulowałeś logowanie, papa!"), hidePlayerDialog(playerid), Kick(playerid);
  118.  
  119. if (!passwordCorrect(inputtext)) {
  120.  
  121. playerData[playerid][loginAttempt]++;
  122. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logowanie", "Twoje konto jest zarejstrowane.\nPowtórz hasło, ponieważ wprowadzone jest nieprawidłowe.", "Zaloguj", "Opuść");
  123. return clientMsg(playerid, -1, "Nieprawidlowe haslo, sprobuj ponownie.");
  124. }
  125.  
  126. loginPlayer(playerid, inputtext);
  127. }
  128.  
  129. case DIALOG_REGISTER: {
  130.  
  131. if (!response) return 1; // secure
  132.  
  133. new buf[127];
  134.  
  135. if (!passwordCorrect(inputtext)) {
  136.  
  137. 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));
  138. ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Rejestracja", buf, "Rejestruj", "");
  139. return clientMsg(playerid, -1, "Hasło może się składać tylko z liter i cyfr.");
  140. }
  141.  
  142. format(buf, sizeof(buf), "INSERT INTO players (nick, haslo) VALUES ('%s', '%s')", getPlayerName(playerid), MD5_Hash(passHash(inputtext, getPlayerName(playerid))));
  143. if (mysql_query(buf) || mysql_affected_rows() < 1) return clientMsg(playerid, -1, "Wystapil blad podczas wysylania formularza. Sprobuj ponownie."), Kick(playerid);
  144. return 1;
  145. }
  146. }
  147.  
  148. return 1;
  149. }
  150.  
  151. // funkcje
  152. passHash(const pass[], const name[]) { // solanka ;)
  153.  
  154. new hash[32];
  155. format(hash, sizeof(hash), "%sx6m5K%s", pass, name);
  156. return hash;
  157. }
  158.  
  159. loadPlayerData(playerid) {
  160.  
  161. playerData[playerid][score] = strval(getPlayerAccountData(playerid, "score"));
  162. }
  163.  
  164. getPlayerAccountData(playerid, const data[]) {
  165.  
  166. new
  167. buf[127],
  168. result[80]
  169. ;
  170.  
  171. format(buf, sizeof(buf), "SELECT %s FROM players WHERE nick = '%s'", data, getPlayerName(playerid));
  172. if (mysql_query(buf))
  173. mysql_ping();
  174.  
  175. mysql_store_result(), mysql_fetch_row(result), mysql_free_result();
  176. return result;
  177. }
  178.  
  179. accountPlayerExists(playerid) {
  180.  
  181. new
  182. buf[127],
  183. bool:exists = false,
  184. nick[MAX_PLAYER_NAME]
  185. ;
  186.  
  187. mysql_real_escape_string(getPlayerName(playerid), nick);
  188. format(buf, sizeof(buf), "SELECT 1 FROM players WHERE nick = '%s'", nick);
  189. if (mysql_query(buf))
  190. mysql_ping();
  191.  
  192. mysql_store_result();
  193.  
  194. if (mysql_num_rows())
  195. exists = true;
  196.  
  197. mysql_free_result();
  198. return exists;
  199. }
  200.  
  201. passwordCorrect(szPassword[]) {
  202.  
  203. new
  204. length = strlen(szPassword)
  205. ;
  206.  
  207. if (!(5 <= length < 20)) return 0;
  208.  
  209. for (new i = 0; i != length; i++) {
  210. switch (szPassword[i]) {
  211. case 'a' .. 'z': continue;
  212. case 'A' .. 'Z': continue;
  213. case '0' .. '9': continue;
  214. case '!', '@', '#', '$', '%', '^', '&', '*', '(', ')': continue;
  215. default: return 0;
  216. }
  217. }
  218.  
  219. return 1;
  220. }
  221.  
  222. loginPlayer(playerid, const password[]) {
  223.  
  224. new buf[127];
  225.  
  226. if (playerData[playerid][loginAttempt] >= MAX_LOGIN_ATTEMPTS) {
  227. clientMsg(playerid, -1, "Wykorzystałeś limit prób zalogowań! Zostałeś wyrzucony.");
  228. hidePlayerDialog(playerid);
  229. return Kick(playerid);
  230. }
  231.  
  232. format(buf, sizeof(buf), "SELECT haslo FROM players WHERE nick = '%s'", getPlayerName(playerid));
  233. mysql_query(buf), mysql_store_result(), mysql_fetch_row(buf), mysql_free_result();
  234.  
  235. if (strcmp(MD5_Hash(passHash(password, getPlayerName(playerid))), buf, true) != 0 && buf[0]) {
  236. playerData[playerid][loginAttempt]++;
  237. ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Logowanie", "Twoje konto jest zarejstrowane.\nPowtórz hasło, ponieważ wprowadzone jest nieprawidłowe.", "Zaloguj", "Opuść");
  238. return clientMsg(playerid, -1, "Nieprawidlowe haslo, sprobuj ponownie.");
  239. }
  240.  
  241. playerData[playerid][loggedIn] = true;
  242. clientMsg(playerid, -1, "Zostales zalogowany, milej Gry.");
  243. OnPlayerRequestClass(playerid, 0), loadPlayerData(playerid);
  244. return 1;
  245. }
  246.  
  247. getPlayerName(playerid) {
  248.  
  249. static pName[MAX_PLAYER_NAME];
  250.  
  251. pName[0] = 0;
  252. GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
  253. return pName;
  254. }
  255. // The end, thanks for watch. :)
Advertisement
Add Comment
Please, Sign In to add comment