Guest User

Achievements System [MySQL | ZCMD | SSCANF2]

a guest
Nov 3rd, 2014
1,413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.32 KB | None | 0 0
  1. //================================= [ Includes ] ===============================
  2.  
  3. #include <a_samp>
  4. #include <a_mysql>
  5. #include <sscanf2>
  6. #include <zcmd>
  7.  
  8. //============================ [ MySQL Configuration] ==========================
  9.  
  10. #define MySQL_Host                                              "localhost" // Change this
  11. #define MySQL_User                                              "root"      // Change this
  12. #define MySQL_Pass                                              ""          // Change this
  13. #define MySQL_Data                                              "TestDB"    // Change this
  14.  
  15. #define Achievements_                                           "tehachz1944" // Change this (optional)
  16.  
  17. //================================= [ Defines ] ================================
  18.  
  19. new FALSE = false;
  20. #define GivePlayerScore(%0,%1)                  SetPlayerScore(%0,GetPlayerScore(%0)+%1)
  21. #define SendFormattedMessageToAll(%1,%2,%3)     do{new _str[190]; format(_str,150,%2,%3); SendClientMessageToAll(%1,_str);}while(FALSE)
  22. #define function%0(%1)                          forward%0(%1);public%0(%1)
  23.  
  24. //==============================================================================
  25.  
  26. enum AchievementInfo
  27. {
  28.     Username[24], Ach1, Ach2, Ach3, AchsCompleted
  29. };
  30. new AInfo[MAX_PLAYERS][AchievementInfo], gName[MAX_PLAYER_NAME], gQuery[520], MySQL;
  31.  
  32. //==============================================================================
  33.  
  34. public OnFilterScriptInit()
  35. {
  36.     MySQL = mysql_connect(MySQL_Host, MySQL_User, MySQL_Data, MySQL_Pass);
  37.     mysql_log(LOG_ALL);
  38.    
  39.     // Let's create the table if it doesn't exist
  40.     mysql_tquery(MySQL, "CREATE TABLE IF NOT EXISTS `"Achievements_"` (" \
  41.                         "`Username` varchar(24) NOT NULL," \
  42.                         "`Ach1` int(5) NOT NULL," \
  43.                         "`Ach2` int(5) NOT NULL," \
  44.                         "`Ach3` int(5) NOT NULL," \
  45.                         "`AchsCompleted` int(5) NOT NULL," \
  46.                         "PRIMARY KEY (`Username`)" \
  47.                     ") ENGINE=InnoDB DEFAULT CHARSET=latin1;");
  48.  
  49.     // We'll use this if there are players connected when filterscript is loaded
  50.     for(new i = 0; i < MAX_PLAYERS; i++)
  51.     {
  52.         if(IsPlayerConnected(i))
  53.         {
  54.             // Reseting the achievement variables
  55.             AInfo[i][Ach1] = 0, AInfo[i][Ach2] = 0, AInfo[i][Ach3] = 0, AInfo[i][AchsCompleted] = 0;
  56.  
  57.             // Let's check if there is a row with this username
  58.             mysql_format(MySQL, gQuery, 256, "SELECT * FROM `"Achievements_"` WHERE Username = '%s' LIMIT 1", GetPName(i));
  59.             mysql_tquery(MySQL, gQuery, "CheckAchievements", "i", i);
  60.         }
  61.     }
  62.    
  63.     // Let's check if the connection was successful
  64.     if(mysql_errno(MySQL) == 0) print("ACHS: Connection established to the database!");
  65.     else print("ACHS: Connection couldn't be established to the database!");
  66.     return 1;
  67. }
  68.  
  69. public OnFilterScriptExit()
  70. {
  71.     for(new i = 0; i < MAX_PLAYERS; i++)
  72.     {
  73.         if(IsPlayerConnected(i))
  74.         {
  75.             // Saving the achievements
  76.             mysql_format(MySQL, gQuery, 256, "UPDATE `"Achievements_"` SET Ach1 = '%i', Ach2 = '%i', Ach3 = '%i', AchsCompleted = '%i' WHERE Username = '%s'",
  77.             AInfo[i][Ach1], AInfo[i][Ach2], AInfo[i][Ach3], AInfo[i][AchsCompleted], GetPName(i));
  78.             mysql_tquery(MySQL, gQuery, "", "");
  79.  
  80.             // Reseting the achievement variables
  81.             AInfo[i][Ach1] = 0, AInfo[i][Ach2] = 0, AInfo[i][Ach3] = 0, AInfo[i][AchsCompleted] = 0;
  82.         }
  83.     }
  84.     return 1;
  85. }
  86.  
  87. public OnPlayerConnect(playerid)
  88. {
  89.     // Reseting the achievement variables
  90.     AInfo[playerid][Ach1] = 0, AInfo[playerid][Ach2] = 0, AInfo[playerid][Ach3] = 0, AInfo[playerid][AchsCompleted] = 0;
  91.  
  92.     // Let's check if there is a row with this username
  93.     mysql_format(MySQL, gQuery, 256, "SELECT * FROM `"Achievements_"` WHERE Username = '%s' LIMIT 1", GetPName(playerid));
  94.     mysql_tquery(MySQL, gQuery, "CheckAchievements", "i", playerid);
  95.     return 1;
  96. }
  97.  
  98. public OnPlayerDisconnect(playerid, reason)
  99. {
  100.     // Saving the achievements
  101.     mysql_format(MySQL, gQuery, 256, "UPDATE `"Achievements_"` SET Ach1 = '%i', Ach2 = '%i', Ach3 = '%i', AchsCompleted = '%i' WHERE Username = '%s'",
  102.     AInfo[playerid][Ach1], AInfo[playerid][Ach2], AInfo[playerid][Ach3], AInfo[playerid][AchsCompleted], GetPName(playerid));
  103.     mysql_tquery(MySQL, gQuery, "", "");
  104.  
  105.     // Reseting the achievement variables
  106.     AInfo[playerid][Ach1] = 0, AInfo[playerid][Ach2] = 0, AInfo[playerid][Ach3] = 0, AInfo[playerid][AchsCompleted] = 0;
  107.     return 1;
  108. }
  109.  
  110. public OnPlayerUpdate(playerid)
  111. {
  112.     CheckAchs(playerid);
  113.     return 1;
  114. }
  115.  
  116. //================================== [ Functions ] =============================
  117.  
  118. function CheckAchievements(playerid)
  119. {
  120.     new rows, fields;
  121.     cache_get_data(rows, fields, MySQL);
  122.     if(rows)
  123.     {
  124.         cache_get_field_content(0, "Username", AInfo[playerid][Username], MySQL, 24);
  125.         AInfo[playerid][Ach1] = cache_get_field_content_int(0, "Ach1", MySQL);
  126.         AInfo[playerid][Ach2] = cache_get_field_content_int(0, "Ach2", MySQL);
  127.         AInfo[playerid][Ach3] = cache_get_field_content_int(0, "Ach3", MySQL);
  128.         AInfo[playerid][AchsCompleted] = cache_get_field_content_int(0, "AchsCompleted", MySQL);
  129.     }
  130.     else
  131.     {
  132.         mysql_format(MySQL, gQuery, 256, "INSERT INTO `"Achievements_"` (Username, Ach1, Ach2, Ach3, AchsCompleted) VALUES ('%s', '0', '0', '0', '0')", GetPName(playerid));
  133.         mysql_tquery(MySQL, gQuery, "", "");
  134.     }
  135.     return 1;
  136. }
  137.  
  138. function CheckAchs(playerid)
  139. {
  140.     if(AInfo[playerid][Ach1] != 1)
  141.     {
  142.         if(GetPlayerMoney(playerid) >= 5000000)
  143.         {
  144.             AInfo[playerid][Ach1] = 1;
  145.             AInfo[playerid][AchsCompleted]++;
  146.             SendFormattedMessageToAll(0xFF0000FF, "ACHS: {8CAB62}%s(%i) has unlocked the 'Millionare' achievement!", GetPName(playerid), playerid);
  147.             GivePlayerMoney(playerid, 200000), GivePlayerScore(playerid, 10);
  148.             if(AInfo[playerid][AchsCompleted] >= 3)
  149.             {
  150.                 GivePlayerMoney(playerid, 10000000), GivePlayerScore(playerid, 2500);
  151.                 SendFormattedMessageToAll(0xFF0000FF, "ACHS: {8CAB62}%s(%i) has unlocked all of the achievements!", GetPName(playerid), playerid);
  152.                 return 1;
  153.             }
  154.         }
  155.     }
  156.     if(AInfo[playerid][Ach2] != 1)
  157.     {
  158.         if(GetPlayerScore(playerid) >= 2500)
  159.         {
  160.             AInfo[playerid][Ach2] = 1;
  161.             AInfo[playerid][AchsCompleted]++;
  162.             SendFormattedMessageToAll(0xFF0000FF, "ACHS: {8CAB62}%s(%i) has unlocked the 'Score Whore' achievement!", GetPName(playerid), playerid);
  163.             GivePlayerMoney(playerid, 200000), GivePlayerScore(playerid, 10);
  164.             if(AInfo[playerid][AchsCompleted] >= 3)
  165.             {
  166.                 GivePlayerMoney(playerid, 10000000), GivePlayerScore(playerid, 2500);
  167.                 SendFormattedMessageToAll(0xFF0000FF, "ACHS: {8CAB62}%s(%i) has unlocked all of the achievements!", GetPName(playerid), playerid);
  168.                 return 1;
  169.             }
  170.         }
  171.     }
  172.     if(AInfo[playerid][Ach3] != 1)
  173.     {
  174.         if(GetPlayerPing(playerid) >= 500)
  175.         {
  176.             AInfo[playerid][Ach3] = 1;
  177.             AInfo[playerid][AchsCompleted]++;
  178.             SendFormattedMessageToAll(0xFF0000FF, "ACHS: {8CAB62}%s(%i) has unlocked the 'Mother of Lag' achievement!", GetPName(playerid), playerid);
  179.             GivePlayerMoney(playerid, 200000), GivePlayerScore(playerid, 10);
  180.             if(AInfo[playerid][AchsCompleted] >= 3)
  181.             {
  182.                 GivePlayerMoney(playerid, 10000000), GivePlayerScore(playerid, 2500);
  183.                 SendFormattedMessageToAll(0xFF0000FF, "ACHS: {8CAB62}%s(%i) has unlocked all of the achievements!", GetPName(playerid), playerid);
  184.                 return 1;
  185.             }
  186.         }
  187.     }
  188.     return 1;
  189. }
  190.  
  191. //================================= [ Stocks ] =================================
  192.  
  193. stock GetPName(playerid)
  194. {
  195.     GetPlayerName(playerid, gName, sizeof gName);
  196.     return gName;
  197. }
  198.  
  199. stock ShowPlayerAchievements(playerid, targetid)
  200. {
  201.     new TempStr[520], complete[20], incomplete[20];
  202.     complete = "{05E200}[•]", incomplete = "{FF0000}[•]";
  203.     strcat(TempStr, "%s {FFFFFF}Millionare \t\t{FF4500}- {00FFFF}Get $5,000,000 money to unlock this!\n");
  204.     strcat(TempStr, "%s {FFFFFF}Score Whore\t\t{FF4500}- {00FFFF}Get 2,500 score to unlock this!\n");
  205.     strcat(TempStr, "%s {FFFFFF}Mother of Lag \t   {FF4500}- {00FFFF}Have a ping greater than 500! xd\n");
  206.     strcat(TempStr, "%s(%i) has completed %i/3 achievements!\n");
  207.     strcat(TempStr, "%s {FF4500}- {00FFFF}Completed Achievement {FFFFFF} | %s {FF4500}- {00FFFF}Incomplete Achievement");
  208.     format(TempStr, sizeof TempStr, TempStr, (AInfo[targetid][Ach1] == 1) ? (complete) : (incomplete), (AInfo[targetid][Ach2] == 1) ? (complete) : (incomplete),
  209.     (AInfo[targetid][Ach3] == 1) ? (complete) : (incomplete), GetPName(targetid), targetid, AInfo[targetid][AchsCompleted], complete, incomplete);
  210.  
  211.     ShowPlayerDialog(playerid, 7182, DIALOG_STYLE_MSGBOX, "             Player Achievements", TempStr, "Great", "");
  212.     return 1;
  213. }
  214.  
  215. //=============================== [ Command(s) ] ===============================
  216.  
  217. CMD:achievements(playerid, params[])
  218. {
  219.     new id;
  220.     if(sscanf(params, "u", id)) return SendClientMessage(playerid, 0x00B9FFFF, "Use /ach(ievements) [player name/id] to view another player's achievements info!"), ShowPlayerAchievements(playerid, playerid);
  221.     if(!IsPlayerConnected(id)) return  SendClientMessage(playerid, 0xFF0000FF, "ERROR: {828282}You have entered an invalid player name/id!");
  222.     ShowPlayerAchievements(playerid, id);
  223.     return 1;
  224. }
  225.  
  226. CMD:ach(playerid, params[]) return cmd_achievements(playerid, params);
  227.  
  228. //==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment