Guest User

Profile system by [SP]Sinner

a guest
Jun 27th, 2012
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 30.82 KB | None | 0 0
  1. /*
  2.     Profile System by [SP]Sinner
  3.     Public release on SA-MP forums
  4.     on June 27th 2012
  5.    
  6.     Please keep all credits intact.
  7. */
  8.  
  9. #include <a_samp>
  10. #include <a_mysql>
  11. #include <sscanf2>
  12.  
  13. // dcmd: credits to Dracoblue
  14. #define dcmd(%1,%2,%3) if ((strcmp((%3)[1], #%1, true, (%2)) == 0) && ((((%3)[(%2) + 1] == 0) && (dcmd_%1(playerid, "")))||(((%3)[(%2) + 1] == 32) && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
  15.  
  16. #define DIALOG_PROFILE          (400)
  17. #define DIALOG_PROFILE_CREATE0  (401)
  18. #define DIALOG_PROFILE_CREATE1  (402)
  19. #define DIALOG_PROFILE_CREATE2  (403)
  20. #define DIALOG_PROFILE_CREATE3  (404)
  21. #define DIALOG_FRIENDS          (405)
  22.  
  23. #define COLOR_NICEBLUE          0x00FFFFFF
  24. #define COLOR_REDONLY           0xE60000FF
  25. #define COLOR_RED               0xAA3333AA
  26. #define COLOR_GREEN             0x33AA33AA
  27. #define COLOR_ORANGE            0xFF9900AA
  28. #define COLOR_YELLOW            0xFFDD00AA
  29.  
  30. ////////////////////////////////////////////////////////////////////////////////
  31. // FILL IN YOUR MYSQL DATABASE DETAILS HERE
  32. #define SQL_HOST                ""
  33. #define SQL_USER                ""
  34. #define SQL_PASS                ""
  35. #define SQL_DB                  ""
  36. ////////////////////////////////////////////////////////////////////////////////
  37.  
  38. new StrColors[][] = {
  39.     {"White"},{"Green"},{"Blue"},{"Red"},{"Yellow"},{"Pink"},{"Orange"},{"Aqua"},{"Cyan"},{"Lime"}
  40. };
  41. new HexColors[][] = {
  42.     "FFFFFF","00FF00","2222FF","FF0000","FFFF33","FF33CC","FF9900","33FFFF","66CCFF","00FF33"
  43. };
  44. new ProfileCreate[][] = {
  45.     {"{FFFFFF}Profile Creation: Step 1\n\n{00FF00}What is your {FF0000}Sex?\n{FFFFFF}Options: {00FF00}[Male/Female]"},
  46.     {"{FFFFFF}Profile Creation: Step 2\n\n{00FF00}What is your {FF0000}Age?\n{FFFFFF}Options: {00FF00}[1-100]"},
  47.     {"{FFFFFF}Profile Creation: Step 3\n\n{00FF00}What is your {FF0000}email?\n{FFFFFF}Example: {00FF00}[[email protected]]"},
  48.     {"{FFFFFF}{00FF00}Please choose your profile color."}
  49. };
  50.  
  51. public OnFilterScriptInit()
  52. {
  53.     print("\n--------------------------------------");
  54.     print(" Profile system by [SP]Sinner");
  55.     print("--------------------------------------\n");
  56.     f_mysql_connect();
  57.     CreateProfileMySQL();
  58.     return 1;
  59. }
  60.  
  61. public OnFilterScriptExit()
  62. {
  63.     return 1;
  64. }
  65.  
  66. public OnPlayerConnect(playerid)
  67. {
  68.  
  69.     return 1;
  70. }
  71.  
  72. public OnPlayerDisconnect(playerid, reason) {
  73.     if(GetPVarInt(playerid, "call_host")) {
  74.         new str[128];
  75.         format(str, sizeof(str), "The call hosted by %s has been closed because the host left!", PlayerName(playerid));
  76.         SendCallRoomMessage(GetPVarInt(playerid, "call_host"), str);
  77.         CloseCall(playerid);
  78.     }
  79. }
  80.  
  81. public OnPlayerSpawn(playerid)
  82. {
  83.    
  84.     return 1;
  85. }
  86.  
  87. public OnPlayerText(playerid, text[])
  88. {
  89.     if(strcmp("showcalls", text, true) == 0) {
  90.         toString();
  91.     }
  92.     if(text[0] == '&') {
  93.         new str[128];
  94.         format(str, sizeof(str), "[FRIEND-CHAT] %s (ID:%d): %s", PlayerName(playerid), playerid, text[1]);
  95.         for(new i=0; i<MAX_PLAYERS; i++) {
  96.             if(IsPlayerConnected(i)) {
  97.                 if(IsAcceptedFriend(playerid, PlayerName(i)) || playerid == i) {
  98.                     SendClientMessage(i, COLOR_NICEBLUE << 1, str);
  99.                 }
  100.             }
  101.         }
  102.         return 0;
  103.     }
  104.     if(text[0] == '$') {
  105.         if(GetPVarInt(playerid, "callid")) {
  106.             format(text, 128, "[CALL] %s: %s", PlayerName(playerid), text[1]);
  107.             SendCallRoomMessage(GetPVarInt(playerid, "callid"), text);
  108.             return 0;
  109.         }
  110.     }
  111.     return 1;
  112. }
  113.  
  114. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  115. {
  116.     if(dialogid == DIALOG_PROFILE) {
  117.         if(!response) {
  118.             new pname[24];
  119.             GetPVarString(playerid, "profileviewing", pname, sizeof(pname));
  120.             new str[128];
  121.             if(strcmp(pname, PlayerName(playerid), true) != 0) {
  122.                 AddLikes(pname, 1);
  123.                 format(str, sizeof(str), "%s (ID:%d) has liked the profile of player %s!", PlayerName(playerid), playerid, pname);
  124.                 SendClientMessageToAll(COLOR_GREEN, str);
  125.             } else {
  126.                 SendClientMessage(playerid, COLOR_RED, "You can't like your own profile!");
  127.             }
  128.             DeletePVar(playerid, "profileviewing");
  129.         }
  130.     }
  131.     if(dialogid == DIALOG_PROFILE_CREATE0) {
  132.         if(response) {
  133.             if(strcmp(inputtext, "male", true) == 0 || strcmp(inputtext, "female", true) == 0) {
  134.                 SetSex(playerid, inputtext);
  135.                 new query[128];
  136.                 format(query, sizeof(query), "[PROFILE] You have changed your sex to %s", inputtext);
  137.                 SendClientMessage(playerid, COLOR_NICEBLUE, query);
  138.                 ShowPlayerDialog(playerid, DIALOG_PROFILE_CREATE1, DIALOG_STYLE_INPUT, "Profile Creation", ProfileCreate[1], "OK", "CANCEL");
  139.             } else {
  140.                 SendClientMessage(playerid, COLOR_REDONLY, "[PROFILE] Please enter a valid sex [Male/Female]");
  141.                 ShowPlayerDialog(playerid, DIALOG_PROFILE_CREATE0, DIALOG_STYLE_INPUT, "Profile Creation", ProfileCreate[0], "OK", "CANCEL");
  142.             }
  143.         } else ShowPlayerDialog(playerid, DIALOG_PROFILE_CREATE0, DIALOG_STYLE_INPUT, "Profile Creation", ProfileCreate[0], "OK", "CANCEL");
  144.     }
  145.     if(dialogid == DIALOG_PROFILE_CREATE1) {
  146.         if(response) {
  147.             new age;
  148.             sscanf(inputtext, "d", age);
  149.             if(age > 0 && age < 100) {
  150.                 SetAge(playerid, age);
  151.                 new query[128];
  152.                 format(query, sizeof(query), "[PROFILE] You have changed your age to %d", age);
  153.                 SendClientMessage(playerid, COLOR_NICEBLUE, query);
  154.                 ShowPlayerDialog(playerid, DIALOG_PROFILE_CREATE2, DIALOG_STYLE_INPUT, "Profile Creation", ProfileCreate[2], "OK", "CANCEL");
  155.             } else {
  156.                 SendClientMessage(playerid, COLOR_REDONLY, "[PROFILE] Please enter a valid age [1-100]");
  157.                 ShowPlayerDialog(playerid, DIALOG_PROFILE_CREATE1, DIALOG_STYLE_INPUT, "Profile Creation", ProfileCreate[1], "OK", "CANCEL");
  158.             }
  159.         } else ShowPlayerDialog(playerid, DIALOG_PROFILE_CREATE1, DIALOG_STYLE_INPUT, "Profile Creation", ProfileCreate[1], "OK", "CANCEL");
  160.     }
  161.     if(dialogid == DIALOG_PROFILE_CREATE2) {
  162.         new bigstr[512];
  163.         for(new i=0; i<sizeof(StrColors); i++) {
  164.             if(strlen(bigstr)) format(bigstr, sizeof(bigstr), "%s\n%s", bigstr, StrColors[i]);
  165.             else format(bigstr, sizeof(bigstr), "%s", StrColors[i]);
  166.         }
  167.         if(response) {
  168.             new atcount, dotcount, len = strlen(inputtext);
  169.             for(new i=0; i<(strlen(inputtext)); i++) {
  170.                 if(inputtext[i] == '@') atcount++;
  171.                 if(inputtext[i] == '.') dotcount++;
  172.             }
  173.             if(atcount && dotcount && len > 9) {
  174.                 SetEmail(playerid, inputtext);
  175.                 new query[128];
  176.                 format(query, sizeof(query), "[PROFILE] You have changed your email to %s", inputtext);
  177.                 SendClientMessage(playerid, COLOR_NICEBLUE, query);
  178.                 ShowPlayerDialog(playerid, DIALOG_PROFILE_CREATE3, DIALOG_STYLE_LIST, ProfileCreate[3], bigstr, "OK", "CANCEL");
  179.             }
  180.         } else ShowPlayerDialog(playerid, DIALOG_PROFILE_CREATE2, DIALOG_STYLE_INPUT, "Profile Creation", ProfileCreate[2], "OK", "CANCEL");
  181.     }
  182.     if(dialogid == DIALOG_PROFILE_CREATE3) {
  183.         if(response) {
  184.             SetColor(playerid, listitem);
  185.             new query[128];
  186.             format(query, sizeof(query), "[PROFILE] You have changed your profile color to %s", StrColors[listitem]);
  187.             SendClientMessage(playerid, COLOR_NICEBLUE, query);
  188.             SendClientMessage(playerid, COLOR_GREEN, "[PROFILE] Your profile has been created! Go to your profile by typing /Profile, edit it by typing /CreateProfile again");
  189.         } else {
  190.             new bigstr[512];
  191.             for(new i=0; i<sizeof(StrColors); i++) {
  192.                 if(strlen(bigstr)) format(bigstr, sizeof(bigstr), "%s\n%s", bigstr, StrColors[i]);
  193.                 else format(bigstr, sizeof(bigstr), "%s", StrColors[i]);
  194.             }
  195.             ShowPlayerDialog(playerid, DIALOG_PROFILE_CREATE3, DIALOG_STYLE_LIST, ProfileCreate[3], bigstr, "OK", "CANCEL");
  196.         }
  197.     }
  198.     return 0;
  199. }
  200.  
  201. public OnPlayerCommandText(playerid, cmdtext[])
  202. {
  203.     dcmd(profile, 7, cmdtext);
  204.     dcmd(createprofile, 13, cmdtext);
  205.     dcmd(addfriend, 9, cmdtext);
  206.     dcmd(removefriend, 12, cmdtext);
  207.     dcmd(friends, 7, cmdtext);
  208.     dcmd(uafriends, 9, cmdtext);
  209.     dcmd(like, 4, cmdtext);
  210.     dcmd(call, 4, cmdtext);
  211.     dcmd(pickup, 6, cmdtext);
  212.     dcmd(hangup, 6, cmdtext);
  213.     dcmd(invite, 6, cmdtext);
  214.     return 0;
  215. }
  216.  
  217. dcmd_profile(playerid, params[]) {
  218.     new pid;
  219.     if(sscanf(params, "d", pid)) {
  220.         if(HasProfile(PlayerName(playerid))) {
  221.             ShowProfile(playerid, PlayerName(playerid));
  222.         } else {
  223.             SendClientMessage(playerid, COLOR_REDONLY, "You don't have a profile yet, type /CreateProfile to make one!");
  224.         }
  225.     } else if(HasProfile(PlayerName(pid))) {
  226.         ShowProfile(playerid, PlayerName(pid));
  227.     } else {
  228.         SendClientMessage(playerid, COLOR_REDONLY, "This player does not have a profile yet!");
  229.     }
  230.     return 1;
  231. }
  232.  
  233. dcmd_createprofile(playerid, params[]) {
  234.     #pragma unused params
  235.     if(!HasProfile(PlayerName(playerid))) {
  236.         CreateProfile(PlayerName(playerid));
  237.     }
  238.     SetPVarInt(playerid, "creatingprofile", 1);
  239.     ShowPlayerDialog(playerid, DIALOG_PROFILE_CREATE0, DIALOG_STYLE_INPUT, "Profile Creation", ProfileCreate[0], "OK", "CANCEL");
  240.     return 1;
  241. }
  242.  
  243. dcmd_addfriend(playerid, params[]) {
  244.     new pid;
  245.     if(sscanf(params, "d", pid)) return SendClientMessage(playerid, COLOR_REDONLY, "USAGE: /AddFriend [ID] - Add a player as a friend");
  246.     new friend[24];
  247.     GetPlayerName(pid, friend, sizeof(friend));
  248.     if(pid == playerid) return SendClientMessage(playerid, COLOR_REDONLY, "You can't add yourself as a friend");
  249.     if(IsAcceptedFriend(playerid, friend)) return SendClientMessage(playerid, COLOR_REDONLY, "This player is already your friend, type /FriendChat to chat with your friends");
  250.     new str[128];
  251.     if(IsInvitedFriend(playerid, friend)) {
  252.         AcceptFriend(playerid, friend);
  253.         format(str, sizeof(str), "(/Profile) %s and %s are now friends!", PlayerName(playerid), friend);
  254.         SendClientMessageToAll(COLOR_NICEBLUE, str);
  255.     } else {
  256.         AddFriend(playerid, friend);
  257.         format(str, sizeof(str), "[PROFILE] %s invited to be your friend, you will be friends as soon as he/she accepts your request!", friend);
  258.         SendClientMessage(playerid, COLOR_NICEBLUE, str);
  259.         for(new i=0; i<MAX_PLAYERS; i++) {
  260.             if(IsPlayerConnected(i)) {
  261.                 if(strcmp(PlayerName(i), friend, true) == 0) {
  262.                     format(str, sizeof(str), "[FRIEND INVITE] %s wants to be friends with you! Type \"/AddFriend %d\" to add him as a friend!", PlayerName(playerid), playerid);
  263.                     SendClientMessage(pid, COLOR_YELLOW, str);
  264.                 }
  265.             }
  266.         }
  267.     }
  268.     return 1;
  269. }
  270.  
  271. dcmd_removefriend(playerid, params[]) {
  272.     new pid;
  273.     if(sscanf(params, "d", pid)) return SendClientMessage(playerid, COLOR_REDONLY, "USAGE: /RemoveFriend [ID] - Remove one of your friends");
  274.     if(!IsPlayerConnected(pid)) return SendClientMessage(playerid, COLOR_REDONLY, "This player is not connected");
  275.     if(IsInvitedFriend(playerid, PlayerName(pid)) || IsAcceptedFriend(playerid, PlayerName(pid))) {
  276.         RemoveFriend(playerid, PlayerName(pid));
  277.         new str[128];
  278.         format(str, sizeof(str), "You have removed %s (ID:%d) from your friends list", PlayerName(pid), pid);
  279.         SendClientMessage(playerid, COLOR_ORANGE, str);
  280.         format(str, sizeof(str), "%s (ID:%d) has removed you from his/her friends list", PlayerName(playerid), playerid);
  281.         SendClientMessage(pid, COLOR_ORANGE, str);
  282.     } else {
  283.         SendClientMessage(playerid, COLOR_REDONLY, "this player is not your friend");
  284.     }
  285.     return 1;
  286. }
  287.  
  288. dcmd_friends(playerid, params[]) {
  289.     new pid;
  290.     if(sscanf(params, "d", pid)) {
  291.         ShowAcceptedFriends(playerid, PlayerName(playerid));
  292.     } else {
  293.         ShowAcceptedFriends(playerid, PlayerName(pid));
  294.     }
  295.     return 1;
  296. }
  297.  
  298. dcmd_uafriends(playerid, params[]) {
  299.     new pid;
  300.     if(sscanf(params, "d", pid)) {
  301.         ShowUnAcceptedFriends(playerid, PlayerName(playerid));
  302.     } else {
  303.         ShowUnAcceptedFriends(playerid, PlayerName(pid));
  304.     }
  305.     return 1;
  306. }
  307.  
  308. dcmd_like(playerid, params[]) {
  309.     new pid;
  310.     if(sscanf(params, "d", pid)) return SendClientMessage(playerid, COLOR_REDONLY, "USAGE: /Like [ID] - Like a players profile");
  311.     new pname[24];
  312.     GetPlayerName(pid, pname, sizeof(pname));
  313.     new str[128];
  314.     if(pid != playerid) {
  315.         AddLikes(pname, 1);
  316.         format(str, sizeof(str), "%s (ID:%d) has liked the profile of player %s!", PlayerName(playerid), playerid, pname);
  317.         SendClientMessageToAll(COLOR_GREEN, str);
  318.     } else {
  319.         SendClientMessage(playerid, COLOR_RED, "You can't like your own profile!");
  320.     }
  321.     return 1;
  322. }
  323.  
  324. dcmd_call(playerid, params[]) {
  325.     new data[32];
  326.     if(sscanf(params, "s[32]", data)) return SendClientMessage(playerid, COLOR_REDONLY, "USAGE: /Call [ID/phone number] - Call a player who (do /profile [ID] to find a players phone)");
  327.     new pid;
  328.     if(IsPlayerConnected(strval(data))) {
  329.         pid = strval(data);
  330.     } else {
  331.         pid = GetPlayerIDByPhone(data);
  332.     }
  333.     if(IsPlayerConnected(pid)) {
  334.         if(GetPVarInt(playerid, "callid")) return SendClientMessage(playerid, COLOR_REDONLY, "You are already calling someone, type /hangup to hang up first");
  335.         if(GetPVarInt(pid, "callid")) {
  336.             new str[128];
  337.             format(str, sizeof(str), "%s (ID:%d) tried to call you, /invite him to your call or /hangup if you want to speak with him", PlayerName(playerid), playerid);
  338.             SendClientMessage(pid, COLOR_ORANGE, str);
  339.             return SendClientMessage(playerid, COLOR_REDONLY, "This player is already in a call, we told him you tried to contact him");
  340.         }
  341.         if(pid == playerid) return SendClientMessage(playerid, COLOR_REDONLY, "You cant call yourself");
  342.         if(CreateCallRoom(playerid)) {
  343.             if(CallPlayer(playerid, pid)) {
  344.                 new str[128];
  345.                 format(str, sizeof(str), "You started calling, %s will have to /pickup your call", PlayerName(pid));
  346.                 SendClientMessage(playerid, COLOR_ORANGE, str);
  347.                 format(str, sizeof(str), "%s (ID:%d) is calling you!! Type /Pickup to pickup your phone", PlayerName(playerid), playerid);
  348.                 SendClientMessage(pid, COLOR_ORANGE, str);
  349.             }
  350.         } else {
  351.             SendClientMessage(playerid, COLOR_REDONLY, "You are already in a call, or the host of a call, type /Hangup to leave your current conversation");
  352.         }
  353.     } else {
  354.         SendClientMessage(playerid, COLOR_REDONLY, "Error: We could not find this player, he is either offline or you entered a wrong phone number/ID");
  355.     }
  356.     return 1;
  357. }
  358.  
  359. dcmd_invite(playerid, params[]) {
  360.     new data[32];
  361.     if(sscanf(params, "s[32]", data)) return SendClientMessage(playerid, COLOR_REDONLY, "USAGE: /Invite [ID/phone number] - Invite a player to your call (do /profile [ID] to find a players phone)");
  362.     new pid;
  363.     if(IsPlayerConnected(strval(data))) {
  364.         pid = strval(data);
  365.     } else {
  366.         pid = GetPlayerIDByPhone(data);
  367.     }
  368.     if(IsPlayerConnected(pid)) {
  369.         if(!GetPVarInt(playerid, "callid")) return SendClientMessage(playerid, COLOR_REDONLY, "You are currently not in a call");
  370.         if(!GetPVarInt(playerid, "call_host")) return SendClientMessage(playerid, COLOR_REDONLY, "You are not rhe host of this call, can't invite someone");
  371.         if(GetPVarInt(pid, "callid")) return SendClientMessage(playerid, COLOR_REDONLY, "This player is already in a call");
  372.         if(pid == playerid) return SendClientMessage(playerid, COLOR_REDONLY, "You cant invite yourself");
  373.         if(CallPlayer(playerid, pid)) {
  374.             new str[128];
  375.             format(str, sizeof(str), "You started calling, %s will have to /pickup your call", PlayerName(pid));
  376.             SendClientMessage(playerid, COLOR_ORANGE, str);
  377.             format(str, sizeof(str), "%s (ID:%d) is calling you!! Type /Pickup to pickup your phone", PlayerName(playerid), playerid);
  378.             SendClientMessage(pid, COLOR_ORANGE, str);
  379.         }
  380.     } else {
  381.         SendClientMessage(playerid, COLOR_REDONLY, "Error: We could not find this player, he is either offline or you entered a wrong phone number/ID");
  382.     }
  383.     return 1;
  384. }
  385.  
  386. dcmd_pickup(playerid, params[]) {
  387.     #pragma unused params
  388.     if(GetPVarInt(playerid, "call_invite")) {
  389.         if(!GetPVarInt(playerid, "callid")) {
  390.             new callid = GetPVarInt(playerid, "call_invite");
  391.             DeletePVar(playerid, "call_invite");
  392.             if(JoinCall(callid, playerid)) {
  393.                 new str[128];
  394.                 format(str, sizeof(str), "%s (ID:%d) picked up the call! You can talk to him using $[text]", PlayerName(playerid), playerid);
  395.                 SendCallRoomMessage(callid, str);
  396.             } else {
  397.                 SendClientMessage(playerid, COLOR_REDONLY, "Oops! This call room is full or does not exist!");
  398.             }
  399.         } else {
  400.             SendClientMessage(playerid, COLOR_REDONLY, "You are already in a call!");
  401.         }
  402.     }
  403.     return 1;
  404. }
  405.  
  406. dcmd_hangup(playerid, params[]) {
  407.     #pragma unused params
  408.     if(GetPVarInt(playerid, "call_host")) {
  409.         new str[128];
  410.         format(str, sizeof(str), "The call hosted by %s has been closed because the host left!", PlayerName(playerid));
  411.         SendCallRoomMessage(GetPVarInt(playerid, "call_host"), str);
  412.         CloseCall(playerid);
  413.     } else if(GetPVarInt(playerid, "callid")) {
  414.         new str[128];
  415.         format(str, sizeof(str), "%s (ID:%s) has left the call!", PlayerName(playerid), playerid);
  416.         SendCallRoomMessage(GetPVarInt(playerid, "callid"), str);
  417.         LeaveCall(playerid);
  418.     } else {
  419.         SendClientMessage(playerid, COLOR_REDONLY, "You are currently not in a call");
  420.     }
  421.     return 1;
  422. }
  423.  
  424. stock CreateProfileMySQL() {
  425.     mysql_query("\
  426.         CREATE TABLE profiles\
  427.         (\
  428.             ID int NOT NULL AUTO_INCREMENT,\
  429.             PRIMARY KEY(ID),\
  430.             username varchar(24),\
  431.             sex varchar(12),\
  432.             age int,\
  433.             email varchar(64),\
  434.             phone varchar(32),\
  435.             likes int,\
  436.             status int,\
  437.             color int\
  438.         );\
  439.     ");
  440.    
  441.     mysql_query("\
  442.         CREATE TABLE friends\
  443.         (\
  444.             ID int NOT NULL AUTO_INCREMENT,\
  445.             PRIMARY KEY(ID),\
  446.             username varchar(24),\
  447.             friend varchar(24),\
  448.             accepted int\
  449.         );\
  450.     ");
  451.     return 1;
  452. }
  453.  
  454. stock SetSex(playerid, sex[]) {
  455.     new query[128];
  456.     format(query, sizeof(query), "UPDATE `profiles` SET `sex` = '%s' WHERE `username` = '%s'", sex, PlayerName(playerid));
  457.     mysql_query(query);
  458. }
  459.  
  460. stock GetSex(playerid) {
  461.     new sex[12];
  462.     new query[128];
  463.     format(query, sizeof(query), "SELECT * FROM `profiles` WHERE `username` = '%s'", PlayerName(playerid));
  464.     mysql_query(query);
  465.     mysql_store_result();
  466.     if(mysql_retrieve_row()) {
  467.         mysql_get_field("sex", sex);
  468.     }
  469.     mysql_free_result();
  470.     return sex;
  471. }
  472.  
  473. stock SetAge(playerid, age) {
  474.     new query[128];
  475.     format(query, sizeof(query), "UPDATE `profiles` SET `age` = %d WHERE `username` = '%s'", age, PlayerName(playerid));
  476.     mysql_query(query);
  477. }
  478.  
  479. stock GetAge(playerid) {
  480.     new age;
  481.     new query[128];
  482.     format(query, sizeof(query), "SELECT * FROM `profiles` WHERE `username` = '%s'", PlayerName(playerid));
  483.     mysql_query(query);
  484.     mysql_store_result();
  485.     if(mysql_retrieve_row()) {
  486.         age = mysql_get_int("age");
  487.     }
  488.     mysql_free_result();
  489.     return age;
  490. }
  491.  
  492. stock SetEmail(playerid, email[]) {
  493.     new query[128];
  494.     format(query, sizeof(query), "UPDATE `profiles` SET `email` = '%s' WHERE `username` = '%s'", email, PlayerName(playerid));
  495.     mysql_query(query);
  496. }
  497.  
  498. stock GetEmail(playerid) {
  499.     new email[64];
  500.     new query[128];
  501.     format(query, sizeof(query), "SELECT * FROM `profiles` WHERE `username` = '%s'", PlayerName(playerid));
  502.     mysql_query(query);
  503.     mysql_store_result();
  504.     if(mysql_retrieve_row()) {
  505.         mysql_get_field("email", email);
  506.     }
  507.     mysql_free_result();
  508.     return email;
  509. }
  510.  
  511. stock SetPhone(playerid, phone[]) {
  512.     new query[128];
  513.     format(query, sizeof(query), "UPDATE `profiles` SET `phone` = '%s' WHERE `username` = '%s'", phone, PlayerName(playerid));
  514.     mysql_query(query);
  515. }
  516.  
  517. stock GetPhone(playerid) {
  518.     new phone[32];
  519.     new query[128];
  520.     format(query, sizeof(query), "SELECT * FROM `profiles` WHERE `username` = '%s'", PlayerName(playerid));
  521.     mysql_query(query);
  522.     mysql_store_result();
  523.     if(mysql_retrieve_row()) {
  524.         mysql_get_field("phone", phone);
  525.     }
  526.     mysql_free_result();
  527.     return phone;
  528. }
  529.  
  530. stock GetPlayerIDByPhone(data[]) {
  531.     for(new i=0; i<MAX_PLAYERS; i++) {
  532.         if(IsPlayerConnected(i)) {
  533.             if(strcmp(data, GetPhone(i), true) == 0) {
  534.                 return i;
  535.             }
  536.         }
  537.     }
  538.     return -1;
  539. }
  540.  
  541. stock SetLikes(playerid, likes) {
  542.     new query[128];
  543.     format(query, sizeof(query), "UPDATE `profiles` SET `likes` = %d WHERE `username` = '%s'", likes, PlayerName(playerid));
  544.     mysql_query(query);
  545. }
  546.  
  547. stock GetLikes(playerid) {
  548.     new likes;
  549.     new query[128];
  550.     format(query, sizeof(query), "SELECT * FROM `profiles` WHERE `username` = '%s'", PlayerName(playerid));
  551.     mysql_query(query);
  552.     mysql_store_result();
  553.     if(mysql_retrieve_row()) {
  554.         likes = mysql_get_int("likes");
  555.     }
  556.     mysql_free_result();
  557.     return likes;
  558. }
  559.  
  560. stock AddLikes(name[], amount) {
  561.     new query[128];
  562.     format(query, sizeof(query), "UPDATE `profiles` SET `likes` = (`likes` + %d) WHERE `username` = '%s'", amount, name);
  563.     mysql_query(query);
  564. }
  565.  
  566. stock SetStatus(playerid, status) {
  567.     new query[128];
  568.     format(query, sizeof(query), "UPDATE `profiles` SET `status` = %d WHERE `username` = '%s'", status, PlayerName(playerid));
  569.     mysql_query(query);
  570. }
  571.  
  572. stock GetStatus(playerid) {
  573.     new status;
  574.     new query[128];
  575.     format(query, sizeof(query), "SELECT * FROM `profiles` WHERE `username` = '%s'", PlayerName(playerid));
  576.     mysql_query(query);
  577.     mysql_store_result();
  578.     if(mysql_retrieve_row()) {
  579.         status = mysql_get_int("status");
  580.     }
  581.     mysql_free_result();
  582.     return status;
  583. }
  584.  
  585. stock SetColor(playerid, color) {
  586.     new query[128];
  587.     format(query, sizeof(query), "UPDATE `profiles` SET `color` = %d WHERE `username` = '%s'", color, PlayerName(playerid));
  588.     mysql_query(query);
  589. }
  590.  
  591. stock GetColor(playerid) {
  592.     new color;
  593.     new query[128];
  594.     format(query, sizeof(query), "SELECT * FROM `profiles` WHERE `username` = '%s'", PlayerName(playerid));
  595.     mysql_query(query);
  596.     mysql_store_result();
  597.     if(mysql_retrieve_row()) {
  598.         color = mysql_get_int("color");
  599.     }
  600.     mysql_free_result();
  601.     return color;
  602. }
  603.  
  604. stock AddFriend(playerid, friend[]) {
  605.     new query[256];
  606.     format(query, sizeof(query), "INSERT INTO `friends` (`username`, `friend`, `accepted`) VALUES('%s', '%s', 0)", PlayerName(playerid), friend);
  607.     mysql_query(query);
  608. }
  609.  
  610. stock RemoveFriend(playerid, friend[]) {
  611.     new query[256];
  612.     format(query, sizeof(query), "DELETE FROM `friends` WHERE (`username` = '%s' AND `friend` = '%s') OR (`friend` = '%s' AND `username` = '%s')", PlayerName(playerid), friend, PlayerName(playerid), friend);
  613.     mysql_query(query);
  614. }
  615.  
  616. stock IsInvitedFriend(playerid, friend[]) {
  617.     new query[256];
  618.     new bool:isfriend = false;
  619.     format(query, sizeof(query), "SELECT * FROM `friends` WHERE (`username` = '%s' AND `friend` = '%s' AND `accepted` = 0) OR (`username` = '%s' AND `friend` = '%s' AND `accepted` = 0)",
  620.         PlayerName(playerid), friend, friend, PlayerName(playerid));
  621.     mysql_query(query);
  622.     mysql_store_result();
  623.     isfriend = mysql_num_rows()>0;
  624.     mysql_free_result();
  625.     return isfriend;
  626. }
  627.  
  628. stock IsAcceptedFriend(playerid, friend[]) {
  629.     new query[256];
  630.     new bool:isfriend = false;
  631.     format(query, sizeof(query), "SELECT * FROM `friends` WHERE (`username` = '%s' AND `friend` = '%s' AND `accepted` = 1) OR (`username` = '%s' AND `friend` = '%s' AND `accepted` = 1) ",
  632.         PlayerName(playerid), friend, friend, PlayerName(playerid));
  633.     mysql_query(query);
  634.     mysql_store_result();
  635.     isfriend = mysql_num_rows()>0;
  636.     mysql_free_result();
  637.     return isfriend;
  638. }
  639.  
  640. stock AcceptFriend(playerid, friend[]) {
  641.     new query[256];
  642.     format(query, sizeof(query), "UPDATE `friends` SET `accepted` = 1 WHERE (`username` = '%s' AND `friend` = '%s') OR (`username` = '%s' AND `friend` = '%s')",
  643.         PlayerName(playerid), friend, friend, PlayerName(playerid));
  644.     mysql_query(query);
  645. }
  646.  
  647. stock HasProfile(name[]) {
  648.     new query[128];
  649.     new bool:hasprofile = false;
  650.     format(query, sizeof(query), "SELECT * FROM `profiles` WHERE `username` LIKE '%s'", name);
  651.     mysql_query(query);
  652.     mysql_store_result();
  653.     hasprofile = mysql_num_rows()>0;
  654.     mysql_free_result();
  655.     return hasprofile;
  656. }
  657.  
  658. stock ShowProfile(playerid, name[]) {
  659.     new query[128];
  660.     format(query, sizeof(query), "SELECT * FROM `profiles` WHERE `username` = '%s'", name);
  661.     mysql_query(query);
  662.     mysql_store_result();
  663.     if(mysql_retrieve_row()) {
  664.         new str[1024];
  665.         new sex[12], age, email[64], phone[6], likes, status, color;
  666.         mysql_get_field("sex", sex);
  667.         age = mysql_get_int("age");
  668.         mysql_get_field("email", email);
  669.         mysql_get_field("phone", phone);
  670.         likes = mysql_get_int("likes");
  671.         status = mysql_get_int("status");
  672.         color = mysql_get_int("color");
  673.         format(str, sizeof(str), "{%s}Profile of %s\n\nSex\t\t:\t\t%s\nAge\t\t:\t\t%d\nEmail\t\t:\t\t%s\nPhone\t\t:\t\t%s\nProfile Likes\t:\t\t%d\nProfile Status\t:\t\t%d",
  674.             HexColors[color], name, sex, age, email, phone, likes, status);
  675.         ShowPlayerDialog(playerid, DIALOG_PROFILE, DIALOG_STYLE_MSGBOX, "Profile", str, "OK", "LIKE");
  676.         SetPVarString(playerid, "profileviewing", name);
  677.     }
  678.     mysql_free_result();
  679. }
  680.  
  681. stock CreateProfile(name[]) {
  682.     new query[256], phone[6];
  683.     format(phone, sizeof(phone), "%d%d%d%d%d", random(10), random(10), random(10), random(10), random(10));
  684.     format(query, sizeof(query), "INSERT INTO `profiles` (`username`, `sex`, `age`, `email`, `phone`, `likes`, `status`, `color`) VALUES('%s', null, null, null, '%s', 0, 0, 0)",
  685.         name, phone);
  686.     mysql_query(query);
  687. }
  688.  
  689. stock ShowAcceptedFriends(playerid, name[]) {
  690.     new query[256];
  691.    
  692.     format(query, sizeof(query), "SELECT * FROM `friends` WHERE (`username` = '%s' OR `friend` = '%s') AND `accepted` = 1 ORDER BY `accepted` DESC", name, name);
  693.     mysql_query(query);
  694.     mysql_store_result();
  695.     if(mysql_num_rows()>0) {
  696.         new str[1024];
  697.         format(str, sizeof(str), "{00FF00}%s{FFFFFF}'s friends list\n{FFFFFF}=================================\n{00FF00}||| = Accepted   {FF0000}||| = Not accepted yet\n{FFFFFF}=================================\n\n{FFAA00}", name);
  698.         new uname[24], fname[24];
  699.         while(mysql_retrieve_row()) {
  700.             mysql_get_field("username", uname);
  701.             mysql_get_field("friend", fname);
  702.             new color[16];
  703.             format(color, sizeof(color), "{00FF00}");
  704.             if(strcmp(uname, name, true) == 0) {
  705.                 format(str, sizeof(str), "%s%s%s\n", str, color, fname);
  706.             } else {
  707.                 format(str, sizeof(str), "%s%s%s\n", str, color, uname);
  708.             }
  709.         }
  710.         ShowPlayerDialog(playerid, DIALOG_FRIENDS, DIALOG_STYLE_MSGBOX, "Accepted Friends list", str, "OK", "CANCEL");
  711.     } else {
  712.         SendClientMessage(playerid, COLOR_REDONLY, "This player doesn't have any friends yet!");
  713.     }
  714.     mysql_free_result();
  715.     return 1;
  716. }
  717.  
  718. stock ShowUnAcceptedFriends(playerid, name[]) {
  719.     new query[256];
  720.  
  721.     format(query, sizeof(query), "SELECT * FROM `friends` WHERE (`username` = '%s' OR `friend` = '%s') AND `accepted` = 0 ORDER BY `accepted` DESC", name, name);
  722.     mysql_query(query);
  723.     mysql_store_result();
  724.     if(mysql_num_rows()>0) {
  725.         new str[1024];
  726.         format(str, sizeof(str), "{00FF00}%s{FFFFFF}'s friends list\n{FFFFFF}=================================\n{00FF00}||| = Accepted   {FF0000}||| = Not accepted yet\n{FFFFFF}=================================\n\n{FFAA00}", name);
  727.         new uname[24], fname[24];
  728.         while(mysql_retrieve_row()) {
  729.             mysql_get_field("username", uname);
  730.             mysql_get_field("friend", fname);
  731.             new color[16];
  732.             format(color, sizeof(color), "{FF0000}");
  733.             if(strcmp(uname, name, true) == 0) {
  734.                 format(str, sizeof(str), "%s%s%s\n", str, color, fname);
  735.             } else {
  736.                 format(str, sizeof(str), "%s%s%s\n", str, color, uname);
  737.             }
  738.         }
  739.         ShowPlayerDialog(playerid, DIALOG_FRIENDS, DIALOG_STYLE_MSGBOX, "Unaccepted Friends list", str, "OK", "CANCEL");
  740.     } else {
  741.         SendClientMessage(playerid, COLOR_REDONLY, "This player doesn't have any unaccepted friends yet!");
  742.     }
  743.     mysql_free_result();
  744.     return 1;
  745. }
  746.  
  747. stock GetCallHost(callid) {
  748.     for(new i=0; i<MAX_PLAYERS; i++) {
  749.         if(IsPlayerConnected(i)) {
  750.             if(GetPVarInt(i, "call_host") == callid) {
  751.                 return i;
  752.             }
  753.         }
  754.     }
  755.     return -1;
  756. }
  757.  
  758. stock CreateCallRoom(p_host) {
  759.     new callid = 1+p_host;
  760.     if(IsPlayerConnected(p_host)) {
  761.         SetPVarInt(p_host, "call_host", callid);
  762.         JoinCall(callid, p_host);
  763.         return 1;
  764.     } else {
  765.         //printf("DEBUG: Could not create call room");
  766.         return 0;
  767.     }
  768. }
  769.  
  770. stock JoinCall(callid, p_joinid) {
  771.     if(GetCallHost(callid) != -1) {
  772.         SetPVarInt(p_joinid, "callid", callid);
  773.         return 1;
  774.     } else {
  775.         //printf("DEBUG: Player ID:%d could not join call %d", p_joinid, callid);
  776.         return 0;
  777.     }
  778. }
  779.  
  780. stock CallPlayer(p_caller, p_called) {
  781.     new callid = GetPVarInt(p_caller, "callid");
  782.     if(GetPVarInt(p_caller, "call_host") == callid) {
  783.         SetPVarInt(p_called, "call_invite", callid);
  784.         printf("DEBUG: Call host %d invited player ID:%d to call with id %d", p_caller, p_called, callid);
  785.         return 1;
  786.     } else {
  787.         //printf("DEBUG: p_caller ID:%d was not host of call @ CallPlayer(p_caller, p_called), please fix.", p_caller);
  788.         return 0;
  789.     }
  790. }
  791.  
  792. stock SendCallRoomMessage(callid, string[]) {
  793.     for(new i=0; i<MAX_PLAYERS; i++) {
  794.         if(IsPlayerConnected(i)) {
  795.             if(GetPVarInt(i, "callid") == callid) {
  796.                 SendClientMessage(i, COLOR_YELLOW, string);
  797.             }
  798.         }
  799.     }
  800. }
  801.  
  802. stock toString() {
  803.     new str[128];
  804.     printf("Printing out all current calls and their callers:");
  805.     printf("---------------------------------------------------------------------");
  806.     for(new i=0; i<MAX_PLAYERS; i++) {
  807.         if(GetPVarInt(i, "call_host")) {
  808.             format(str, sizeof(str), "Call ID:%d, host: %s ID:%d", i, PlayerName(i), i);
  809.             SendClientMessageToAll(-1, str);
  810.             for(new j=0; j<MAX_PLAYERS; j++) {
  811.                 if(IsPlayerConnected(j) && GetPVarInt(j, "callid") == GetPVarInt(i, "call_host")) {
  812.                     format(str, sizeof(str), "--- Caller %d: %s", j, PlayerName(j));
  813.                     SendClientMessageToAll(-1, str);
  814.                 }
  815.             }
  816.         }
  817.     }
  818. }
  819.  
  820. stock GetCallCount() {
  821.     new count = 0;
  822.     for(new i=0; i<MAX_PLAYERS; i++) {
  823.         if(IsPlayerConnected(i) && GetPVarInt(i, "call_host"))
  824.             count++;
  825.     }
  826.     return count;
  827. }
  828.  
  829. stock LeaveCall(playerid) {
  830.     DeletePVar(playerid, "callid");
  831. }
  832.  
  833. stock CloseCall(host) {
  834.     new count=0;
  835.     for(new i=0; i<MAX_PLAYERS; i++) {
  836.         if(IsPlayerConnected(i) && GetPVarInt(i, "callid") == GetPVarInt(host, "call_host")) {
  837.             LeaveCall(i);
  838.             count++;
  839.         }
  840.     }
  841.     DeletePVar(host, "call_host");
  842.     //printf("DEBUG: The call hosted by %s has been closed, %d people were disconnected", PlayerName(host), count);
  843. }
  844.  
  845. stock f_mysql_connect()
  846. {
  847.     new conn;
  848.     if(!(conn = mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS))) {
  849.         print("SQL connection attempt 1 FAILED!");
  850.         if(!(conn = mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS))) {
  851.             print("SQL connection attempt 2 FAILED!");
  852.             if(!(conn = mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS))) {
  853.                 print("SQL connection attempt 3 FAILED!");
  854.                 print("Server could not connect to SQL database");
  855.                 return 0;
  856.             }
  857.         }
  858.     }
  859.     printf("Server succesfully connected to SQL database \"%d\"", conn);
  860.     SetPVarInt(-1, "mysql_connection", conn);
  861.     return 1;
  862. }
  863.  
  864. stock mysql_get_int(field[])
  865. {
  866.     new str[19];
  867.     mysql_get_field(field, str);
  868.     return strval(str);
  869. }
  870.  
  871. stock PlayerName(playerid)
  872. {
  873.     new name[MAX_PLAYER_NAME];
  874.     GetPlayerName(playerid, name, sizeof(name));
  875.     return name;
  876. }
Advertisement
Add Comment
Please, Sign In to add comment