Guest User

RegisterLoginProblem

a guest
Jul 22nd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.71 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3. #include <hash>
  4.  
  5. #define SERVERNAME "Test"
  6.  
  7. #define COLOR_RED 0xFF0000FF
  8.  
  9. #define SQL_HOST "localhost"
  10. #define SQL_USER "root"
  11. #define SQL_PASS ""
  12. #define SQL_DATA "samp_db"
  13. static handle;
  14.  
  15. forward OnPlayerCheck(playerid);
  16. forward OnPlayerRegister(playerid);
  17. forward OnPlayerLogin(playerid);
  18.  
  19. enum {
  20.     DIALOG_REGISTER,
  21.     DIALOG_LOGIN
  22. };
  23.  
  24. enum PlayerData{
  25.     p_ID,
  26.     pName[MAX_PLAYER_NAME],
  27.     pPassword[65],
  28.     pSalt[11],
  29.     pScore,
  30.     pMoney,
  31.     bool:isLoggedIn
  32. };
  33. new pData[MAX_PLAYERS][PlayerData];
  34.  
  35. main()
  36. {
  37.     print("\n----------------------------------");
  38.     print(" Register-&Loginsystem for Test");
  39.     print("----------------------------------\n");
  40. }
  41.  
  42. public OnGameModeInit()
  43. {
  44.     Connect_To_Database();
  45.     EnableStuntBonusForAll(0);
  46.     DisableInteriorEnterExits();
  47.     SetGameModeText("Test");
  48.     AddPlayerClass(0, 1480.9744, -1771.6888, 18.7958, 269.1425, 0, 0, 0, 0, 0, 0);
  49.     return 1;
  50. }
  51.  
  52. public OnGameModeExit()
  53. {
  54.     mysql_close(handle);
  55.     return 1;
  56. }
  57.  
  58. public OnPlayerRequestClass(playerid, classid)
  59. {
  60.     if(!pData[playerid][isLoggedIn])
  61.     {
  62.         new query[128];
  63.         mysql_format(handle, query, sizeof(query), "SELECT `salt` FROM `accounts` WHERE `username` = '%e'", pData[playerid][pName]);
  64.         mysql_pquery(handle, query, "OnPlayerCheck", "d", playerid);
  65.     }
  66.     return 1;
  67. }
  68.  
  69. public OnPlayerConnect(playerid)
  70. {
  71.     pData[playerid][p_ID]       = 0;
  72.     pData[playerid][pScore]     = 0;
  73.     pData[playerid][pMoney]     = 0;
  74.     pData[playerid][isLoggedIn]  = false;
  75.     GetPlayerName(playerid, pData[playerid][pName], MAX_PLAYER_NAME);
  76.     return 1;
  77. }
  78.  
  79. public OnPlayerDisconnect(playerid, reason)
  80. {
  81.     SavePlayerStats(playerid);
  82.     return 1;
  83. }
  84.  
  85. public OnPlayerSpawn(playerid)
  86. {
  87.     return 1;
  88. }
  89.  
  90. public OnPlayerDeath(playerid, killerid, reason)
  91. {
  92.     return 1;
  93. }
  94.  
  95. public OnVehicleSpawn(vehicleid)
  96. {
  97.     return 1;
  98. }
  99.  
  100. public OnVehicleDeath(vehicleid, killerid)
  101. {
  102.     return 1;
  103. }
  104.  
  105. public OnPlayerText(playerid, text[])
  106. {
  107.     return 1;
  108. }
  109.  
  110. public OnPlayerCommandText(playerid, cmdtext[])
  111. {
  112.     return 0;
  113. }
  114.  
  115. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  116. {
  117.     return 1;
  118. }
  119.  
  120. public OnPlayerExitVehicle(playerid, vehicleid)
  121. {
  122.     return 1;
  123. }
  124.  
  125. public OnPlayerStateChange(playerid, newstate, oldstate)
  126. {
  127.     return 1;
  128. }
  129.  
  130. public OnPlayerEnterCheckpoint(playerid)
  131. {
  132.     return 1;
  133. }
  134.  
  135. public OnPlayerLeaveCheckpoint(playerid)
  136. {
  137.     return 1;
  138. }
  139.  
  140. public OnPlayerEnterRaceCheckpoint(playerid)
  141. {
  142.     return 1;
  143. }
  144.  
  145. public OnPlayerLeaveRaceCheckpoint(playerid)
  146. {
  147.     return 1;
  148. }
  149.  
  150. public OnRconCommand(cmd[])
  151. {
  152.     return 1;
  153. }
  154.  
  155. public OnPlayerRequestSpawn(playerid)
  156. {
  157.     return 1;
  158. }
  159.  
  160. public OnObjectMoved(objectid)
  161. {
  162.     return 1;
  163. }
  164.  
  165. public OnPlayerObjectMoved(playerid, objectid)
  166. {
  167.     return 1;
  168. }
  169.  
  170. public OnPlayerPickUpPickup(playerid, pickupid)
  171. {
  172.     return 1;
  173. }
  174.  
  175. public OnVehicleMod(playerid, vehicleid, componentid)
  176. {
  177.     return 1;
  178. }
  179.  
  180. public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
  181. {
  182.     return 1;
  183. }
  184.  
  185. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  186. {
  187.     return 1;
  188. }
  189.  
  190. public OnPlayerSelectedMenuRow(playerid, row)
  191. {
  192.     return 1;
  193. }
  194.  
  195. public OnPlayerExitedMenu(playerid)
  196. {
  197.     return 1;
  198. }
  199.  
  200. public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
  201. {
  202.     return 1;
  203. }
  204.  
  205. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  206. {
  207.     return 1;
  208. }
  209.  
  210. public OnRconLoginAttempt(ip[], password[], success)
  211. {
  212.     return 1;
  213. }
  214.  
  215. public OnPlayerUpdate(playerid)
  216. {
  217.     return 1;
  218. }
  219.  
  220. public OnPlayerStreamIn(playerid, forplayerid)
  221. {
  222.     return 1;
  223. }
  224.  
  225. public OnPlayerStreamOut(playerid, forplayerid)
  226. {
  227.     return 1;
  228. }
  229.  
  230. public OnVehicleStreamIn(vehicleid, forplayerid)
  231. {
  232.     return 1;
  233. }
  234.  
  235. public OnVehicleStreamOut(vehicleid, forplayerid)
  236. {
  237.     return 1;
  238. }
  239.  
  240. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  241. {
  242.     switch(dialogid)
  243.     {
  244.         case DIALOG_REGISTER:
  245.         {
  246.             if(!response) return Kick(playerid);
  247.             if(strlen(inputtext) < 4) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, #SERVERNAME "- Registration", "Bitte registriere Dich:\n{FF0000}Mindestens 4 Zeichen!", "Ok", "Abbrechen");
  248.             new query[256], salt[11];
  249.             for(new i; i < 10; i++){
  250.                 salt[i] = random(79) + 47;
  251.             } salt[10] = 0;
  252.             SHA256_PassHash(inputtext, salt, pData[playerid][pPassword], 65);
  253.             mysql_format(handle, query, sizeof(query), "INSERT INTO `accounts` (`username`, `password`, `salt`) VALUES ('%e', '%e', '%e')", pData[playerid][pName], pData[playerid][pPassword], salt);
  254.             mysql_pquery(handle, query, "OnPlayerRegister", "d", playerid);
  255.             return 1;
  256.         }
  257.         case DIALOG_LOGIN:
  258.         {
  259.             if(!response) return Kick(playerid);
  260.             if(strlen(inputtext) < 4) return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, #SERVERNAME "- Anmeldung", "Bitte logge Dich ein:\n{FF0000}Mindestens 4 Zeichen!", "Ok", "Abbrechen");
  261.             new query[256], hash[65];
  262.             SHA256_PassHash(inputtext, pData[playerid][pSalt], hash, 65);
  263.             mysql_format(handle, query, sizeof(query), "SELECT * FROM `accounts` WHERE `username` = '%e' AND `password` = ('%e')", pData[playerid][pName], pData[playerid][pPassword]);
  264.             mysql_pquery(handle, query, "OnPlayerLogin", "d", playerid);
  265.             return 1;
  266.         }
  267.     }
  268.     return 0;
  269. }
  270.  
  271. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  272. {
  273.     return 1;
  274. }
  275.  
  276. stock SavePlayerStats(playerid)
  277. {
  278.     if(!pData[playerid][isLoggedIn]) return 1;
  279.     new query[256];
  280.     mysql_format(handle, query, sizeof(query), "UPDATE `accounts` SET `score` = '%d', `money` = '%d' WHERE `id` = '%d'",
  281.     pData[playerid][pScore], pData[playerid][pMoney], pData[playerid][p_ID]);
  282.     mysql_pquery(handle, query);
  283.     return 1;
  284. }
  285.  
  286. public OnPlayerLogin(playerid)
  287. {
  288.     if(cache_get_row_count() == 0)
  289.     {
  290.         ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, #SERVERNAME "- Anmeldung", "Bitte logge Dich ein:\n{FF0000}Falsches Passwort!", "Ok", "Abbrechen");
  291.     }
  292.     else
  293.     {
  294.         pData[playerid][p_ID]       = cache_get_field_content_int(0, "id", handle);
  295.         pData[playerid][pScore]     = cache_get_field_content_int(0, "score", handle);
  296.         pData[playerid][pMoney]     = cache_get_field_content_int(0, "money", handle);
  297.         pData[playerid][isLoggedIn]  = true;
  298.         SendClientMessage(playerid, 0x00FF00FF, "[Konto] Eingeloggt.");
  299.         SetPlayerScore(playerid, pData[playerid][pScore]);
  300.         GivePlayerMoney(playerid, pData[playerid][pMoney]);
  301.     }
  302.     return 1;
  303. }
  304.  
  305. public OnPlayerRegister(playerid)
  306. {
  307.     pData[playerid][p_ID] = cache_insert_id();
  308.     SendClientMessage(playerid, 0x00FF00FF, "[Konto] Registration erfolgreich.");
  309.     return 1;
  310. }
  311.  
  312. public OnPlayerCheck(playerid)
  313. {
  314.     if(cache_get_row_count() == 0)
  315.     {
  316.         ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, #SERVERNAME "- Registration", "Bitte registriere Dich:", "Ok", "Abbrechen");
  317.     }
  318.     else
  319.     {
  320.         cache_get_field_content(0, "salt", pData[playerid][pSalt], handle, 11);
  321.         ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, #SERVERNAME "- Anmeldung", "Bitte logge Dich ein:", "Ok", "Abbrechen");
  322.     }
  323.     return 1;
  324. }
  325.  
  326. stock Connect_To_Database(ttl = 3)
  327. {
  328.     print("[MySQL] Verbindungsaufbau...");
  329.     //mysql_log(LOG_ALL);
  330.  
  331.     handle = mysql_connect(SQL_HOST, SQL_USER, SQL_DATA, SQL_PASS);
  332.  
  333.     if(mysql_errno(handle) != 0){
  334.         if(ttl > 1){
  335.             print("[MySQL] Es konnte keine Verbindung zur Datenbank hergestellt werden.");
  336.             printf("[MySQL] Starte neuen Verbindungsversuch (TTL: %d).", ttl-1);
  337.             return Connect_To_Database(ttl-1);
  338.         } else {
  339.             print("[MySQL] Es konnte keine Verbindung zur Datenbank hergestellt werden.");
  340.             print("[MySQL] Bitte prüfen Sie die Verbindungsdaten.");
  341.             print("[MySQL] Der Server wird heruntergefahren.");
  342.             return SendRconCommand("exit");
  343.         }
  344.     }
  345.     printf("[MySQL] Die Verbindung zur Datenbank wurde erfolgreich hergestellt! Handle: %d", handle);
  346.     return 1;
  347. }
Advertisement
Add Comment
Please, Sign In to add comment