GeoDanny

Faction system SQLite

Nov 21st, 2016
1,359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 18.68 KB | None | 0 0
  1. //------------------------------------------------------------------------------
  2. //=== Include ==================================================================
  3. //------------------------------------------------------------------------------
  4. #include <a_samp>
  5. #include <Pawn.CMD>
  6. #include <sscanf2>
  7. //------------------------------------------------------------------------------
  8. //=== Enum =====================================================================
  9. //------------------------------------------------------------------------------
  10. enum fInfo
  11. {
  12.     FactionMember, FactionLeader, FactionRank,
  13.     FactionPunish, FactionWarn, FactionPoints
  14. };
  15. //------------------------------------------------------------------------------
  16. //=== New ======================================================================
  17. //------------------------------------------------------------------------------
  18. new FactionInfo[MAX_PLAYERS][fInfo];
  19. new DB:Database;
  20. new String[1024], Query[256], Field[128];
  21. new Factiune[35], RankFactiune[15];
  22. new userid, rank, factionid;
  23. new DBResult:facResult;
  24. //------------------------------------------------------------------------------
  25. //=== Init/Exit ================================================================
  26. //------------------------------------------------------------------------------
  27. public OnGameModeInit()
  28. {
  29.     Database = db_open("Database.db");
  30.     strcat(String, "CREATE TABLE IF NOT EXISTS Factions(");
  31.     strcat(String, "Name VARCHAR(24),");
  32.     strcat(String, "Member INTEGER DEFAULT 0 NOT NULL,");
  33.     strcat(String, "Leader INTEGER DEFAULT 0 NOT NULL,");
  34.     strcat(String, "Rank INTEGER DEFAULT 0 NOT NULL,");
  35.     strcat(String, "Punish INTEGER DEFAULT 0 NOT NULL,");
  36.     strcat(String, "Warn INTEGER DEFAULT 0 NOT NULL,");
  37.     strcat(String, "Points INTEGER DEFAULT 0 NOT NULL)");
  38.     db_query(Database, String);
  39.     return (true);
  40. }
  41. //------------------------------------------------------------------------------
  42. public OnGameModeExit()
  43. {
  44.     db_close(Database);
  45.     return (true);
  46. }
  47. main(){}
  48. //------------------------------------------------------------------------------
  49. //=== Connect/Disconnect =======================================================
  50. //------------------------------------------------------------------------------
  51. public OnPlayerConnect(playerid)
  52. {
  53.     format(Query, sizeof(Query), "SELECT * FROM Factions WHERE Name = '%q'", DB_Escape(PlayerName(playerid)));
  54.     facResult = db_query(Database, Query);
  55.     if(db_num_rows(facResult))
  56.     {
  57.         db_get_field_assoc(facResult, "Member", Field, 128);
  58.         FactionInfo[playerid][FactionMember] = strval(Field);
  59.         db_get_field_assoc(facResult, "Leader", Field, 128);
  60.         FactionInfo[playerid][FactionLeader] = strval(Field);
  61.         db_get_field_assoc(facResult, "Rank", Field, 128);
  62.         FactionInfo[playerid][FactionRank] = strval(Field);
  63.         db_get_field_assoc(facResult, "Punish", Field, 128);
  64.         FactionInfo[playerid][FactionPunish] = strval(Field);
  65.         db_get_field_assoc(facResult, "Warn", Field, 128);
  66.         FactionInfo[playerid][FactionWarn] = strval(Field);
  67.         db_get_field_assoc(facResult, "Points", Field, 128);
  68.         FactionInfo[playerid][FactionPoints] = strval(Field);
  69.     }
  70.     else
  71.     {
  72.         format(Query, sizeof(Query), "INSERT INTO Factions (Name) VALUES ('%q')", DB_Escape(PlayerName(playerid)));
  73.         db_free_result(db_query(Database, Query));
  74.         FactionInfo[playerid][FactionMember] = 0; FactionInfo[playerid][FactionLeader] = 0;
  75.         FactionInfo[playerid][FactionRank] = 0; FactionInfo[playerid][FactionPoints] = 0;
  76.         FactionInfo[playerid][FactionPunish] = 0; FactionInfo[playerid][FactionWarn] = 0;
  77.     }
  78.     db_free_result(facResult);
  79.     return (true);
  80. }
  81. //------------------------------------------------------------------------------
  82. public OnPlayerDisconnect(playerid, reason)
  83. {
  84.     SavePlayerFaction(playerid);
  85.     return (true);
  86. }
  87. //------------------------------------------------------------------------------
  88. //=== Spawn ====================================================================
  89. //------------------------------------------------------------------------------
  90. public OnPlayerSpawn(playerid)
  91. {
  92.     if(FactionInfo[playerid][FactionMember] == 1 || FactionInfo[playerid][FactionLeader] == 1)
  93.     {
  94.         GivePlayerWeapon(playerid, 24, 64);
  95.         GivePlayerWeapon(playerid, 3, 1);
  96.         SetPlayerArmour(playerid, 100);
  97.         /*--------------------------------------------------------------------*/
  98.         SetPlayerColor(playerid, 0x2641FEAA);
  99.         SetPlayerSkin(playerid, 280);
  100.         SendClientMessage(playerid, -1, "{ffff00}Info: {ffffff}You are a member from LSPD.");
  101.         /*--------------------------------------------------------------------*/
  102.         SetPlayerInterior(playerid, 6);
  103.         SetPlayerPos(playerid, 246.3295, 66.5572, 1003.6406);
  104.         SetPlayerFacingAngle(playerid, 0);
  105.         return (true);
  106.     }
  107.     if(FactionInfo[playerid][FactionMember] == 2 || FactionInfo[playerid][FactionLeader] == 2)
  108.     {
  109.         GivePlayerWeapon(playerid, 23, 64);
  110.         /*--------------------------------------------------------------------*/
  111.         SetPlayerColor(playerid, 0xFF6347AA);
  112.         SetPlayerSkin(playerid, 274);
  113.         SendClientMessage(playerid, -1, "{ffff00}Info: {ffffff}You are a member from Paramedics.");
  114.         /*--------------------------------------------------------------------*/
  115.         SetPlayerInterior(playerid, 0);
  116.         SetPlayerPos(playerid, 1607.1846, 1818.9738, 10.8203);
  117.         SetPlayerFacingAngle(playerid, 0);
  118.         return (true);
  119.     }
  120.     if(FactionInfo[playerid][FactionMember] == 3 || FactionInfo[playerid][FactionLeader] == 3)
  121.     {
  122.         GivePlayerWeapon(playerid, 24, 64);
  123.         GivePlayerWeapon(playerid, 31, 64);
  124.         /*--------------------------------------------------------------------*/
  125.         SetPlayerColor(playerid, 0x1E519DAA);
  126.         SetPlayerSkin(playerid, 287);
  127.         SendClientMessage(playerid, -1, "{ffff00}Info: {ffffff}You are a member from National Guard.");
  128.         /*--------------------------------------------------------------------*/
  129.         SetPlayerInterior(playerid, 0);
  130.         SetPlayerPos(playerid, 246.6305, 1860.1299, 14.0840);
  131.         SetPlayerFacingAngle(playerid, 0);
  132.         return (true);
  133.     }
  134.     if(FactionInfo[playerid][FactionMember] == 4 || FactionInfo[playerid][FactionLeader] == 4)
  135.     {
  136.         GivePlayerWeapon(playerid, 24, 64);
  137.         GivePlayerWeapon(playerid, 31, 64);
  138.         /*--------------------------------------------------------------------*/
  139.         SetPlayerColor(playerid, 0x2641FEAA);
  140.         SetPlayerSkin(playerid, 286);
  141.         SendClientMessage(playerid, -1, "{ffff00}Info: {ffffff}You are a member from FBI.");
  142.         /*--------------------------------------------------------------------*/
  143.         SetPlayerInterior(playerid, 10);
  144.         SetPlayerPos(playerid, 246.375991, 109.245994, 1003.218750);
  145.         SetPlayerFacingAngle(playerid, 90);
  146.         return (true);
  147.     }
  148.     else if(FactionInfo[playerid][FactionMember] == 0 || FactionInfo[playerid][FactionLeader] == 0)
  149.     {
  150.         SetPlayerPos(playerid, 2306.8252, -1675.4340, 13.9221);
  151.         SetPlayerFacingAngle(playerid, 0);
  152.         SetPlayerSkin(playerid, 217);
  153.         SetPlayerColor(playerid, 0xFFFFFFFF);
  154.     }
  155.     return (true);
  156. }
  157. //------------------------------------------------------------------------------
  158. //=== Commands =================================================================
  159. //------------------------------------------------------------------------------
  160. cmd:stats(playerid, params[])
  161. {
  162.     SavePlayerFaction(playerid);
  163.     if(IsPlayerConnected(playerid))
  164.     {
  165.         if(FactionInfo[playerid][FactionMember] == 1) Factiune = "Los Santos Police Department";
  166.         else if(FactionInfo[playerid][FactionMember] == 0) Factiune = "Civil";
  167.         if(FactionInfo[playerid][FactionMember] == 2) Factiune = "Paramedics";
  168.         else if(FactionInfo[playerid][FactionMember] == 0) Factiune = "Civil";
  169.         if(FactionInfo[playerid][FactionMember] == 3) Factiune = "National Guard";
  170.         else if(FactionInfo[playerid][FactionMember] == 0) Factiune = "Civil";
  171.         if(FactionInfo[playerid][FactionMember] == 4) Factiune = "Federal Bureau of Investigations";
  172.         else if(FactionInfo[playerid][FactionMember] == 0) Factiune = "Civil";
  173.         /*--------------------------------------------------------------------*/
  174.         if(FactionInfo[playerid][FactionRank] == 1) RankFactiune = "New Member";
  175.         else if(FactionInfo[playerid][FactionRank] == 0) RankFactiune = "None";
  176.         if(FactionInfo[playerid][FactionRank] == 2) RankFactiune = "Member";
  177.         else if(FactionInfo[playerid][FactionRank] == 0) RankFactiune = "None";
  178.         if(FactionInfo[playerid][FactionRank] == 3) RankFactiune = "Senior Member";
  179.         else if(FactionInfo[playerid][FactionRank] == 0) RankFactiune = "None";
  180.         if(FactionInfo[playerid][FactionRank] == 4) RankFactiune = "Advisor";
  181.         else if(FactionInfo[playerid][FactionRank] == 0) RankFactiune = "None";
  182.         if(FactionInfo[playerid][FactionRank] == 5) RankFactiune = "Co-Leader";
  183.         else if(FactionInfo[playerid][FactionRank] == 0) RankFactiune = "None";
  184.         if(FactionInfo[playerid][FactionRank] == 6) RankFactiune = "Leader";
  185.         else if(FactionInfo[playerid][FactionRank] == 0) RankFactiune = "None";
  186.     }
  187.     SendClientMessage(playerid, -1, "-----------------------------------------------------------------------");
  188.     format(String, sizeof(String), "%q | Punish: %i | Warn: %i | Points: %i", PlayerName(playerid), FactionInfo[playerid][FactionPunish], FactionInfo[playerid][FactionWarn], FactionInfo[playerid][FactionPoints]);
  189.     SendClientMessage(playerid, -1, String);
  190.     format(String, sizeof(String), "Faction: %q | Rank: %q", Factiune, RankFactiune);
  191.     SendClientMessage(playerid, -1, String);
  192.     SendClientMessage(playerid, -1, "-----------------------------------------------------------------------");
  193.     return (true);
  194. }
  195. //------------------------------------------------------------------------------
  196. cmd:fhelp(playerid, params[])
  197. {
  198.     SendClientMessage(playerid, -1, "Commands: /finvite | /fkick | /frankup | /setleader");
  199.     return (true);
  200. }
  201. //------------------------------------------------------------------------------
  202. cmd:finvite(playerid, params[])
  203. {
  204.     if(!FactionInfo[playerid][FactionLeader]) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}You are not leader of this faction!");
  205.     if(sscanf(params, "u", userid)) return SendClientMessage(playerid, -1, "{ffff00}Usage: {ffffff}/finvite <name|playerid>");
  206.     if(playerid == userid) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}You can't invite yourself!");
  207.     if(FactionInfo[userid][FactionMember] != 0 || FactionInfo[userid][FactionLeader] != 0) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}This player is a member of another faction!");
  208.     /*------------------------------------------------------------------------*/
  209.     FactionInfo[userid][FactionMember] = FactionInfo[playerid][FactionLeader];
  210.     FactionInfo[userid][FactionRank] = 1;
  211.     /*------------------------------------------------------------------------*/
  212.     format(String, sizeof(String), "{00ffff}AdmBot: {ffffff}%q invited you in his faction.", PlayerName(playerid));
  213.     SendClientMessage(userid, -1, String);
  214.     format(String, sizeof(String), "{00ff00}Succes: {ffffff}You invited %q to join in your faction.", PlayerName(userid));
  215.     SendClientMessage(playerid, -1, String);
  216.     /*------------------------------------------------------------------------*/
  217.     format(String, sizeof(String), "UPDATE Factions SET Member = '%i', Rank = '%i' WHERE Name = '%q'", FactionInfo[userid][FactionMember], FactionInfo[userid][FactionRank], DB_Escape(PlayerName(userid)));
  218.     db_free_result(db_query(Database, String));
  219.     return (true);
  220. }
  221. //------------------------------------------------------------------------------
  222. cmd:fkick(playerid, params[])
  223. {
  224.     if(!FactionInfo[playerid][FactionLeader]) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}You are not leader of this faction!");
  225.     if(sscanf(params, "u", userid)) return SendClientMessage(playerid, -1, "{ffff00}Usage: {ffffff}/fkick <name|playerid>");
  226.     if(playerid == userid) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}You can't dismiss yourself!");
  227.     if(FactionInfo[userid][FactionMember] != FactionInfo[playerid][FactionLeader]) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}This player is a member of another faction!");
  228.     /*------------------------------------------------------------------------*/
  229.     FactionInfo[userid][FactionMember] = 0;
  230.     FactionInfo[userid][FactionRank] = 0;
  231.     FactionInfo[userid][FactionPunish] = 40;
  232.     FactionInfo[playerid][FactionPoints] = 0;
  233.     /*------------------------------------------------------------------------*/
  234.     format(String, sizeof(String), "{00ffff}AdmBot: {ffffff}You were kicked out from this faction by %q.", PlayerName(playerid));
  235.     SendClientMessage(userid, -1, String);
  236.     format(String, sizeof(String), "{00ff00}Succes: {ffffff}You kicked out %q from your faction.", PlayerName(userid));
  237.     SendClientMessage(playerid, -1, String);
  238.     /*------------------------------------------------------------------------*/
  239.     format(String, sizeof(String), "UPDATE Factions SET Member = '%i', Rank = '%i', Punish = '%i', Points = '%i' WHERE Name = '%q'", FactionInfo[playerid][FactionMember], FactionInfo[playerid][FactionRank], FactionInfo[playerid][FactionPunish], FactionInfo[playerid][FactionPoints], DB_Escape(PlayerName(playerid)));
  240.     db_free_result(db_query(Database, String));
  241.     return (true);
  242. }
  243. //------------------------------------------------------------------------------
  244. cmd:frankup(playerid, params[])
  245. {
  246.     if(!FactionInfo[playerid][FactionLeader]) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}You are not leader of this faction!");
  247.     if(sscanf(params, "ui", userid, rank)) return SendClientMessage(playerid, -1, "{ffff00}Usage: {ffffff}/frankup <name|playerid> <rank>");
  248.     else if(rank < 1 || rank > 6) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}The rank can only be 1 or 6.");
  249.     if(FactionInfo[userid][FactionMember] != FactionInfo[playerid][FactionLeader]) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}This player is a member of another faction!");
  250.     /*------------------------------------------------------------------------*/
  251.     FactionInfo[userid][FactionRank] = rank;
  252.     /*------------------------------------------------------------------------*/
  253.     format(String, sizeof(String), "{00ffff}AdmBot: {ffffff}%q has made you rank %i.", PlayerName(playerid), rank);
  254.     SendClientMessage(userid, -1, String);
  255.     format(String, sizeof(String), "{00ff00}Succes: {ffffff}You have made %q rank %i.", PlayerName(userid), rank);
  256.     SendClientMessage(playerid, -1, String);
  257.     /*------------------------------------------------------------------------*/
  258.     format(String, sizeof(String), "UPDATE Factions SET Rank = '%i' WHERE Name = '%q'", FactionInfo[userid][FactionRank], DB_Escape(PlayerName(userid)));
  259.     db_free_result(db_query(Database, String));
  260.     return (true);
  261. }
  262. //------------------------------------------------------------------------------
  263. cmd:setleader(playerid, params[])
  264. {
  265.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}You are not a admin!");
  266.     if(sscanf(params, "ui", userid, factionid)) return SendClientMessage(playerid, -1, "{ffff00}Usage: {ffffff}/setleader <name|playerid> <faction>");
  267.     else if(factionid < 1 || factionid > 20) return SendClientMessage(playerid, -1, "{ff0000}Error: {ffffff}The faction can only be 1 or 20.");
  268.     /*------------------------------------------------------------------------*/
  269.     FactionInfo[userid][FactionLeader] = factionid;
  270.     FactionInfo[userid][FactionMember] = factionid;
  271.     FactionInfo[userid][FactionRank] = 6;
  272.     /*------------------------------------------------------------------------*/
  273.     format(String, sizeof(String), "{00ffff}AdmBot: {ffffff}%q has made you leader for a faction.", PlayerName(playerid));
  274.     SendClientMessage(userid, -1, String);
  275.     format(String, sizeof(String), "{00ff00}Succes: {ffffff}You have made %q leader for a faction.", PlayerName(userid));
  276.     SendClientMessage(playerid, -1, String);
  277.     /*------------------------------------------------------------------------*/
  278.     format(String, sizeof(String), "UPDATE Factions SET Leader = '%i', Member = '%i', Rank = '%i' WHERE Name = '%q'", FactionInfo[userid][FactionLeader], FactionInfo[userid][FactionMember], FactionInfo[userid][FactionRank], DB_Escape(PlayerName(userid)));
  279.     db_free_result(db_query(Database, String));
  280.     return (true);
  281. }
  282. //------------------------------------------------------------------------------
  283. //=== Stock ====================================================================
  284. //------------------------------------------------------------------------------
  285. stock SavePlayerFaction(playerid)
  286. {
  287.     if(IsPlayerConnected(playerid))
  288.     {
  289.         format(Query, sizeof(Query), "UPDATE Factions SET Member = '%i' WHERE Name = '%q'", FactionInfo[playerid][FactionMember], DB_Escape(PlayerName(playerid)));
  290.         db_free_result(db_query(Database, Query));
  291.         format(Query, sizeof(Query), "UPDATE Factions SET Leader = '%i' WHERE Name = '%q'", FactionInfo[playerid][FactionLeader], DB_Escape(PlayerName(playerid)));
  292.         db_free_result(db_query(Database, Query));
  293.         format(Query, sizeof(Query), "UPDATE Factions SET Rank = '%i' WHERE Name = '%q'", FactionInfo[playerid][FactionRank], DB_Escape(PlayerName(playerid)));
  294.         db_free_result(db_query(Database, Query));
  295.         format(Query, sizeof(Query), "UPDATE Factions SET Punish = '%i' WHERE Name = '%q'", FactionInfo[playerid][FactionPunish], DB_Escape(PlayerName(playerid)));
  296.         db_free_result(db_query(Database, Query));
  297.         format(Query, sizeof(Query), "UPDATE Factions SET Warn = '%i' WHERE Name = '%q'", FactionInfo[playerid][FactionWarn], DB_Escape(PlayerName(playerid)));
  298.         db_free_result(db_query(Database, Query));
  299.         format(Query, sizeof(Query), "UPDATE Factions SET Points = '%i' WHERE Name = '%q'", FactionInfo[playerid][FactionPoints], DB_Escape(PlayerName(playerid)));
  300.         db_free_result(db_query(Database, Query));
  301.     }
  302. }
  303. //------------------------------------------------------------------------------
  304. stock PlayerName(playerid)
  305. {
  306.     new pName[MAX_PLAYER_NAME];
  307.     GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
  308.     return (pName);
  309. }
  310. //------------------------------------------------------------------------------
  311. stock DB_Escape(text[])
  312. {
  313.     new ret[80*2], ch, i, j;
  314.     while((ch = text[i++]) && j < sizeof(ret))
  315.     {
  316.         if(ch == '\'')
  317.         {
  318.             if(j < sizeof(ret) - 2)
  319.             {
  320.                 ret[j++] = '\'';
  321.                 ret[j++] = '\'';
  322.             }
  323.         }
  324.         else if(j < sizeof(ret))
  325.         {
  326.             ret[j++] = ch;
  327.         }
  328.         else
  329.         {
  330.             j++;
  331.         }
  332.     }
  333.     ret[sizeof(ret) - 1] = '\0';
  334.     return (ret);
  335. }
  336. //------------------------------------------------------------------------------
  337. //=== End of Script ============================================================
  338. //------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment