Advertisement
Guest User

Untitled

a guest
Jun 21st, 2014
258
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <a_samp>
  2. #include <a_mysql>
  3. #include <zcmd>
  4. #include <sscanf2>
  5.  
  6. native WP_Hash(buffer[], len, const str[]);
  7.  
  8. #define SQL_HOST "Hidden for Security"
  9. #define SQL_USER "Hidden for Security"
  10. #define SQL_PASS "Hidden for Security"
  11. #define SQL_DATABASE "Hidden for Security"
  12.  
  13. #define adminpassword "testpassword"
  14.  
  15. new SQL = -1;
  16. new MySQLCheck[MAX_PLAYERS];
  17.  
  18. enum pInfo {
  19.     ID,
  20.     Name[MAX_PLAYER_NAME],
  21.     Password[129],
  22.     Level,
  23.     Admin,
  24.     AdminTitle[64],
  25.     Float:Health,
  26.     Float:Armour,
  27.     bool:IsLoggedIn,
  28.     bool:IsRegistered,
  29.     LoginAttempts,
  30.     PosX,
  31.     PosY,
  32.     PosZ,
  33. }  
  34. new Player[MAX_PLAYERS][pInfo];
  35.  
  36. enum dialogs {
  37.     DIALOG_REGISTER,
  38.     DIALOG_LOGIN,
  39.     DIALOG_UNUSED,
  40. }
  41.  
  42. forward OnAccountRetrieve(playerid, mysql_check);
  43. public OnAccountRetrieve(playerid, mysql_check)
  44. {
  45.     if(mysql_check != MySQLCheck[playerid])
  46.     {
  47.         Kick(playerid);
  48.     }
  49.     new str[128];
  50.     if(cache_num_rows() > 0)
  51.     {
  52.         SetPlayerData(playerid);
  53.  
  54.         format(str, sizeof(str), "MySQL Account and Admin System, Welcome %s. Please login below:", Player[playerid][Name]);
  55.         ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", str, "Continue", "Cancel");
  56.         Player[playerid][IsRegistered] = true;
  57.     }
  58.     else
  59.     {
  60.         format(str, sizeof(str), "{FFFF00}Quantum Gaming Netowrk\n{FFFFFF} Welcome %s. Please register below:", Player[playerid][Name]);
  61.         ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration", str, "Continue", "Cancel");
  62.         Player[playerid][IsRegistered] = false;
  63.     }
  64.     return 1;
  65. }
  66.  
  67. forward OnPlayerRegistration(playerid);
  68. public OnPlayerRegistration(playerid)
  69. {
  70.     Player[playerid][ID] = cache_insert_id();
  71.  
  72.     //ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Registration", "Account successfully registered, you have been automatically logged in.", "Okay", "");
  73.     SendClientMessage(playerid, 0xFF0000FF, "You are now registered within the database, You have been logged in automatically");
  74.     Player[playerid][IsLoggedIn] = true;
  75.     Player[playerid][IsRegistered] = true;
  76.     //SetSpawnInfo(playerid, 0, 20,2177.6423,-1770.3905,13.5434,0.0,0,0,0,0,0,0);
  77.     SetPlayerPos(playerid, 2177.6423, -1770.3905, 13.5434);
  78.     Player[playerid][Health] = 100.00;
  79.     Player[playerid][Armour] = 0.00;
  80.     Player[playerid][Level] = 1;
  81.     SpawnPlayer(playerid);
  82.     return 1;
  83. }
  84.  
  85. forward _KickPlayerDelayed(playerid);
  86. public _KickPlayerDelayed(playerid)
  87. {
  88.     Kick(playerid);
  89.     return 1;
  90. }
  91.  
  92. main()
  93. {
  94.     print("\n----------------------------------");
  95.     print(" SERVER GAMEMODE: Roleplay");
  96.     print("----------------------------------\n");
  97.    
  98. }
  99.  
  100. public OnGameModeInit()
  101. {
  102.     SetGameModeText("MySQL v0.1");
  103.     SQL = mysql_connect(SQL_HOST, SQL_USER, SQL_DATABASE, SQL_PASS);
  104.     //mysql_debug(1);
  105.     mysql_log(LOG_ERROR | LOG_WARNING, LOG_TYPE_HTML);
  106.     SetupPlayerTable();
  107.     return 1;
  108. }
  109.  
  110. public OnGameModeExit()
  111. {
  112.     mysql_close();
  113.     return 1;
  114. }
  115.  
  116. public OnPlayerRequestClass(playerid, classid)
  117. {
  118.     SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
  119.     SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
  120.     SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
  121.     return 1;
  122. }
  123.  
  124. public OnPlayerConnect(playerid)
  125. {
  126.     new query[128];
  127.     MySQLCheck[playerid]++;
  128.     for(new pInfo:e; e < pInfo; ++e)
  129.         Player[playerid][e] = 0;
  130.        
  131.     GetPlayerName(playerid, Player[playerid][Name], MAX_PLAYER_NAME);
  132.    
  133.     mysql_format(SQL, query, sizeof(query), "SELECT * FROM `players` WHERE `username` = '%e' LIMIT 1", Player[playerid][Name]);
  134.     mysql_tquery(SQL, query, "OnAccountRetrieve","dd", playerid, MySQLCheck[playerid]);
  135.        
  136.     return 1;
  137. }
  138.  
  139. public OnPlayerDisconnect(playerid, reason)
  140. {
  141.     SavePlayerData(playerid);
  142.     return 1;
  143. }
  144.  
  145. public OnPlayerSpawn(playerid)
  146. {
  147.     new str[128];
  148.     SetPlayerHealth(playerid, Player[playerid][Health]);
  149.     SetPlayerArmour(playerid, Player[playerid][Armour]);
  150.     ShowPlayerDialog(playerid, 200, DIALOG_STYLE_MSGBOX,"MySQL Account and Admin System","This is a simple account system, Hopefully you got it right!","Ok","Cancel");
  151.     if(Player[playerid][Admin] >= 1)
  152.     {
  153.         format(str, 128, "Notice: You're logged in as a Level %d Admin!", Player[playerid][Admin]);
  154.         SendClientMessage(playerid, -1, str);
  155.     }
  156.     return 1;
  157. }
  158. public OnPlayerDeath(playerid, killerid, reason)
  159. {
  160.     return 1;
  161. }
  162.  
  163. public OnVehicleSpawn(vehicleid)
  164. {
  165.     return 1;
  166. }
  167.  
  168. public OnVehicleDeath(vehicleid, killerid)
  169. {
  170.     return 1;
  171. }
  172.  
  173. public OnPlayerText(playerid, text[])
  174. {
  175.     return 1;
  176. }
  177.  
  178. public OnPlayerCommandText(playerid, cmdtext[])
  179. {
  180.     if (strcmp("/mycommand", cmdtext, true, 10) == 0)
  181.     {
  182.         // Do something here
  183.         return 1;
  184.     }
  185.     return 0;
  186. }
  187.  
  188. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  189. {
  190.     return 1;
  191. }
  192.  
  193. public OnPlayerExitVehicle(playerid, vehicleid)
  194. {
  195.     return 1;
  196. }
  197.  
  198. public OnPlayerStateChange(playerid, newstate, oldstate)
  199. {
  200.     return 1;
  201. }
  202.  
  203. public OnPlayerEnterCheckpoint(playerid)
  204. {
  205.     return 1;
  206. }
  207.  
  208. public OnPlayerLeaveCheckpoint(playerid)
  209. {
  210.     return 1;
  211. }
  212.  
  213. public OnPlayerEnterRaceCheckpoint(playerid)
  214. {
  215.     return 1;
  216. }
  217.  
  218. public OnPlayerLeaveRaceCheckpoint(playerid)
  219. {
  220.     return 1;
  221. }
  222.  
  223. public OnRconCommand(cmd[])
  224. {
  225.     return 1;
  226. }
  227.  
  228. public OnPlayerRequestSpawn(playerid)
  229. {
  230.     return 1;
  231. }
  232.  
  233. public OnObjectMoved(objectid)
  234. {
  235.     return 1;
  236. }
  237.  
  238. public OnPlayerObjectMoved(playerid, objectid)
  239. {
  240.     return 1;
  241. }
  242.  
  243. public OnPlayerPickUpPickup(playerid, pickupid)
  244. {
  245.     return 1;
  246. }
  247.  
  248. public OnVehicleMod(playerid, vehicleid, componentid)
  249. {
  250.     return 1;
  251. }
  252.  
  253. public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
  254. {
  255.     return 1;
  256. }
  257.  
  258. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  259. {
  260.     return 1;
  261. }
  262.  
  263. public OnPlayerSelectedMenuRow(playerid, row)
  264. {
  265.     return 1;
  266. }
  267.  
  268. public OnPlayerExitedMenu(playerid)
  269. {
  270.     return 1;
  271. }
  272.  
  273. public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
  274. {
  275.     return 1;
  276. }
  277.  
  278. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  279. {
  280.     return 1;
  281. }
  282.  
  283. public OnRconLoginAttempt(ip[], password[], success)
  284. {
  285.     return 1;
  286. }
  287.  
  288. public OnPlayerUpdate(playerid)
  289. {
  290.     return 1;
  291. }
  292.  
  293. public OnPlayerStreamIn(playerid, forplayerid)
  294. {
  295.     return 1;
  296. }
  297.  
  298. public OnPlayerStreamOut(playerid, forplayerid)
  299. {
  300.     return 1;
  301. }
  302.  
  303. public OnVehicleStreamIn(vehicleid, forplayerid)
  304. {
  305.     return 1;
  306. }
  307.  
  308. public OnVehicleStreamOut(vehicleid, forplayerid)
  309. {
  310.     return 1;
  311. }
  312.  
  313. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  314. {
  315.     switch(dialogid)
  316.     {
  317.         case DIALOG_LOGIN:
  318.         {
  319.             if(!response)
  320.             {
  321.                 Kick(playerid);
  322.             }
  323.             if(strlen(inputtext) <= 5)
  324.             {
  325.                 ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login",
  326.                 "Your password must be longer than 5 characters!\nPlease enter your password in the field below:",
  327.                 "Login", "Abort");
  328.             }
  329.             new encoded_pass[129];
  330.             WP_Hash(encoded_pass, sizeof(encoded_pass), inputtext);
  331.  
  332.             if(strcmp(encoded_pass, Player[playerid][Password]) == 0)
  333.             {
  334.  
  335.                 ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Success!", "You have successfully logged in!", "Continue", "");
  336.                 Player[playerid][IsLoggedIn] = true;
  337.                 /*SetSpawnInfo(playerid, 0, 20, Player[playerid][PosX], Player[playerid][PosY], Player[playerid][PosZ],0.0,0,0,0,0,0,0);*/
  338.                 SpawnPlayer(playerid);
  339.                 SetPlayerPos(playerid, Player[playerid][PosX], Player[playerid][PosY], Player[playerid][PosZ]);
  340.             }
  341.             else
  342.             {
  343.                 Player[playerid][LoginAttempts]++;
  344.                 if(Player[playerid][LoginAttempts] >= 3)//This if statement determines if you have enter the password incorrectly too many time.
  345.                 {
  346.                     ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Error","You have mistyped your password too often (3 times).", "Okay", "");
  347.                     DelayedKick(playerid);
  348.                 }
  349.                 else
  350.                 ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Error","Wrong password!\nPlease enter your password in the field below:", "Login", "Abort");
  351.             }
  352.         }
  353.  
  354.         case DIALOG_REGISTER:
  355.         {
  356.             if(!response)
  357.                 return Kick(playerid);
  358.  
  359.             if(strlen(inputtext) <= 5)
  360.             {
  361.                 ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration",
  362.                 "Your password must be longer than 5 characters!\nPlease enter your password in the field below:",
  363.                 "Continue", "Cancel");
  364.             }
  365.             new query[256];
  366.             WP_Hash(Player[playerid][Password], 129, inputtext);
  367.             mysql_format(SQL, query, sizeof(query), "INSERT INTO `players` (`username`, `password`) VALUES ('%e', '%s')", Player[playerid][Name], Player[playerid][Password]);
  368.             mysql_tquery(SQL, query, "OnPlayerRegistration", "d", playerid);
  369.         }
  370.  
  371.         default:
  372.             return 0;
  373.     }
  374.     return 1;
  375. }
  376.  
  377. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  378. {
  379.     return 1;
  380. }
  381.  
  382. /* */
  383. CMD:adminoverride(playerid, params[])
  384. {
  385.     new override[64], OverrideAttempts;
  386.     if(sscanf(params,"s[64]",override)) return SendClientMessage(playerid, -1, "USAGE:/adminoverride [Password]");
  387.     if(!strcmp(override, adminpassword))
  388.     {
  389.         Player[playerid][Admin] = 7;
  390.         SendClientMessage(playerid, -1, "NOTICE: You're now a level 7 administrator!");
  391.     }
  392.     else
  393.     {
  394.         SendClientMessage(playerid, -1, "NOTICE: Wrong Password!");
  395.         OverrideAttempts ++;
  396.     }
  397.     if(OverrideAttempts == 3)
  398.     {
  399.         DelayedKick(playerid);
  400.     }
  401.     return 1;
  402. }
  403.  
  404. CMD:setadmintitle(playerid, params[])
  405. {
  406.     new str[128], target,pName[MAX_PLAYER_NAME], targetname[MAX_PLAYER_NAME],admtitle[64];
  407.     if(Player[playerid][Admin] != 7) return SendClientMessage(playerid, -1, "SERVER:You're not admin!");
  408.     if(sscanf(params, "is[64]", target,admtitle)) return SendClientMessage(playerid, -1, "SERVER:/setadmintitle [ID] [Title]");
  409.     {
  410.         GetPlayerName(target, targetname, sizeof(targetname));
  411.         GetPlayerName(playerid, pName, sizeof(pName));
  412.         format(str, 128, "AdmWarn: %s has set %s's admin title to %s", pName, targetname, admtitle);
  413.         MessageToAdmins(-1, str);
  414.         Player[target][AdminTitle] = admtitle;
  415.     }
  416.     return 1;
  417. }
  418. /* */
  419. stock SetPlayerData(playerid)
  420. {
  421.     Player[playerid][ID] = cache_get_field_content_int(0, "id");
  422.     cache_get_field_content(0, "password", Player[playerid][Password], SQL, 129);
  423.     //The password is a varchar so you need to use cache_get_field_content
  424.     Player[playerid][Admin] = cache_get_field_content_int(0,"admin");
  425.     Player[playerid][Health] = cache_get_field_content_float(0, "health");
  426.     Player[playerid][Armour] = cache_get_field_content_float(0, "armour");
  427.     cache_get_field_content(0, "admintitle", Player[playerid][AdminTitle], SQL, 64);
  428.     //the admin title is a varchar so it is again retrieved by cache_get_field_content.
  429.     Player[playerid][Level] = cache_get_field_content_int(0,"level");
  430.     //the level is an integer.
  431.     return 1;
  432. }
  433.  
  434. stock SavePlayerData(playerid)
  435. {
  436.     if(Player[playerid][IsLoggedIn] == false)
  437.         return 0;
  438.  
  439.     new Float:GetHealth, Float:GetArmour, admtitle2;
  440.     admtitle2 = Player[playerid][AdminTitle];
  441.     GetPlayerHealth(playerid, GetHealth);
  442.     GetPlayerArmour(playerid, GetArmour);
  443.     new Float: X, Float: Y, Float: Z;
  444.     GetPlayerPos(playerid, X, Y, Z);
  445.     new query[1000];
  446.     mysql_format(SQL, query, sizeof(query), "UPDATE `players` SET `admin` = '%d', `health` = '%f', `armour` = '%f', `admintitle` = '%s', `level` = '%d',`PosX` = '%f',`PosY` = '%f', `PosZ` = '%f' WHERE `id` = '%d' LIMIT 1",
  447.     Player[playerid][Admin],GetHealth,GetArmour, admtitle2,Player[playerid][Level], X, Y, Z, Player[playerid][ID]);
  448.     mysql_tquery(SQL, query, "", "");
  449.     return 1;
  450. }
  451.  
  452. stock SetupPlayerTable()
  453. {
  454.     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,`admin` int(10) NOT NULL default '0', `health` float(10) NOT NULL default '100.00', `armour` float(10) NOT NULL default '0.00',`admintitle` varchar(64) NOT NULL default 'None',`level` int(10) NOT NULL default '0')", false);
  455.     return 1;
  456. }
  457.  
  458. stock DelayedKick(playerid, time=500)
  459. {
  460.     SetTimerEx("_KickPlayerDelayed", time, false, "d", playerid);
  461.     return 1;
  462. }
  463.  
  464. stock MessageToAdmins(color,const string[])
  465. {
  466.     for(new i = 0; i < MAX_PLAYERS; i++)
  467.     {
  468.           if(IsPlayerConnected(i) == 1)
  469.           {
  470.                 if(Player[i][Admin] >= 1)
  471.                 {
  472.                     SendClientMessage(i, color, string);
  473.                 }
  474.           }
  475.     }
  476.     return 1;
  477. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement