Advertisement
Guest User

Untitled

a guest
Aug 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. /*
  2. Friend System
  3. © Jingles - 2015
  4.  
  5. Make sure to import the friendsdb.sql into your SA:MP database.
  6.  
  7.  
  8. DB Info:
  9. db = `friendsdb`
  10. UserID = `UserDBID`
  11. FriendID = `FriendDBID`
  12. */
  13.  
  14.  
  15. #include <a_samp>
  16. #include <a_mysql>
  17. #include <foreach>
  18. #include <ZCMD>
  19.  
  20. #define MYSQL_IP "127.0.0.1"
  21. #define MYSQL_USERNAME "root"
  22. #define MYSQL_DATABASE "samp"
  23. #define MYSQL_PASS "INSERT_PASSWORD_HERE"
  24.  
  25. #define MAX_FRIENDS 100
  26.  
  27. #define COLOR_WHITE 0xFFFFFFAA
  28.  
  29. #define THREAD_ACCOUNT_LOADDATA (1)
  30. #define THREAD_SAVE_ACCOUNT_1 (2)
  31. #define THREAD_NO_RESULT (3)
  32.  
  33. #define ADD_FRIEND_THREAD (50)
  34. #define FINAL_FRIEND_THREAD (51)
  35. #define REQUEST_FRIEND_THREAD (52)
  36.  
  37. enum {
  38. DIALOG_FRIENDLIST
  39. }
  40.  
  41. new mysql_iConnectionHandle[1];
  42.  
  43.  
  44. enum pData {
  45. pDBID,
  46. pUsername[MAX_PLAYER_NAME]
  47. }
  48. new PlayerInfo[MAX_PLAYERS][pData];
  49.  
  50. new szFriendList[MAX_FRIENDS * MAX_PLAYER_NAME + 30];
  51.  
  52.  
  53. public OnFilterScriptInit()
  54. {
  55. mysql_iConnectionHandle[0] = mysql_connect(MYSQL_IP, MYSQL_USERNAME, MYSQL_DATABASE, MYSQL_PASS);
  56.  
  57. foreach(Player, playerid)
  58. {
  59. LoadData(playerid);
  60. }
  61. return 1;
  62. }
  63.  
  64. public OnFilterScriptExit()
  65. {
  66. mysql_close(mysql_iConnectionHandle[0]);
  67. return 1;
  68. }
  69.  
  70.  
  71.  
  72. public OnPlayerConnect(playerid)
  73. {
  74. LoadData(playerid);
  75. return 1;
  76. }
  77.  
  78. forward LoadData(playerid);
  79. public LoadData(playerid)
  80. {
  81. new szQuery[128],
  82. name[MAX_PLAYER_NAME];
  83. GetPlayerName(playerid, name, sizeof(name));
  84. mysql_real_escape_string(name, PlayerInfo[playerid][pUsername], mysql_iConnectionHandle[0], MAX_PLAYER_NAME);
  85. format(szQuery, sizeof szQuery, "SELECT * FROM `accountdb` WHERE `Username` = '%s'", PlayerInfo[playerid][pUsername]);
  86. mysql_function_query(mysql_iConnectionHandle[0], szQuery, true, "OnQueryFinish", "ii", THREAD_ACCOUNT_LOADDATA, playerid);
  87. return 1;
  88. }
  89.  
  90.  
  91. CMD:friends(playerid, params[])
  92. {
  93. new query[128], name[MAX_PLAYER_NAME];
  94. GetPlayerName(playerid, name, sizeof(name));
  95.  
  96. mysql_format(mysql_iConnectionHandle[0], query, sizeof(query), "SELECT * FROM `friendsdb` WHERE `UserDBID` = %i", PlayerInfo[playerid][pDBID]);
  97. mysql_function_query(mysql_iConnectionHandle[0], query, true, "OnPlayerRequestFriends", "ii", REQUEST_FRIEND_THREAD, playerid);
  98. return 1;
  99. }
  100.  
  101.  
  102.  
  103. CMD:addfriend(playerid, params[])
  104. {
  105. new giveplayerid,
  106. query[512];
  107.  
  108. if(sscanf(params, "u", giveplayerid))
  109. return SendClientMessage(playerid, COLOR_WHITE, "/addfriend [player]");
  110.  
  111. if(IsPlayerConnected(giveplayerid))
  112. {
  113. mysql_format(mysql_iConnectionHandle[0], query, sizeof(query), "SELECT * FROM `accountdb` WHERE `Username` = '%s'", GetPlayerNameExt(giveplayerid));
  114. mysql_function_query(mysql_iConnectionHandle[0], query, true, "OnPlayerRequestFriends", "ii", ADD_FRIEND_THREAD, playerid);
  115. }
  116. else
  117. {
  118. mysql_format(mysql_iConnectionHandle[0], query, sizeof(query), "SELECT * FROM `accountdb` WHERE `Username` = '%s'", params);
  119. mysql_function_query(mysql_iConnectionHandle[0], query, true, "OnPlayerRequestFriends", "ii", ADD_FRIEND_THREAD, playerid);
  120. }
  121. return 1;
  122. }
  123.  
  124. forward OnPlayerRequestFriends(thread, playerid, Friend_ID);
  125. public OnPlayerRequestFriends(thread, playerid, Friend_ID)
  126. {
  127. switch(thread)
  128. {
  129. case THREAD_NO_RESULT:
  130. {
  131. return 1;
  132. }
  133. case ADD_FRIEND_THREAD:
  134. {
  135. new rows,
  136. fields;
  137.  
  138. cache_get_data(rows, fields, mysql_iConnectionHandle[0]);
  139.  
  140. if(rows)
  141. {
  142. new
  143. query[128],
  144. szResult[64],
  145. szFriendName[64],
  146. FriendDBID;
  147.  
  148. for(new row; row < rows; row++) {
  149. cache_get_field_content(row, "Username", szResult, mysql_iConnectionHandle[0], sizeof(szResult)); szFriendName = szResult;
  150. cache_get_field_content(row, "id", szResult, mysql_iConnectionHandle[0], sizeof(szResult)); FriendDBID = strval(szResult);
  151.  
  152. /* DEBUG
  153. format(szResult, sizeof(szResult), "Found in DB: %s with ID: %i", szFriendName, FriendDBID);
  154. SendClientMessage(playerid, COLOR_WHITE, szResult);
  155. */
  156.  
  157. SetPVarInt(playerid, "New_Friend_DBID", FriendDBID);
  158. SetPVarString(playerid, "New_Friend_Name", szFriendName);
  159. mysql_format(mysql_iConnectionHandle[0], query, sizeof(query), "SELECT * FROM `friendsdb` WHERE `UserDBID` = %i AND `FriendDBID` = %i", PlayerInfo[playerid][pDBID], FriendDBID);
  160. mysql_function_query(mysql_iConnectionHandle[0], query, true, "OnPlayerRequestFriends", "ii", FINAL_FRIEND_THREAD, playerid);
  161. return 1;
  162. }
  163. }
  164. else {
  165. SendClientMessage(playerid, COLOR_WHITE, "That name does not exist.");
  166. }
  167. return 1;
  168. }
  169. case FINAL_FRIEND_THREAD:
  170. {
  171. new rows,
  172. fields;
  173.  
  174. cache_get_data(rows, fields, mysql_iConnectionHandle[0]);
  175.  
  176. if(!rows) {
  177. new query[128],
  178. FriendName[MAX_PLAYER_NAME+1];
  179. mysql_format(mysql_iConnectionHandle[0], query, sizeof(query), "INSERT INTO `friendsdb` (`UserDBID`, `FriendDBID`) VALUES (%i, %i)", PlayerInfo[playerid][pDBID], GetPVarInt(playerid, "New_Friend_DBID"));
  180. mysql_function_query(mysql_iConnectionHandle[0], query, false, "OnPlayerRequestFriends", "i", THREAD_NO_RESULT);
  181.  
  182. GetPVarString(playerid, "New_Friend_Name", FriendName, sizeof FriendName);
  183. format(query, sizeof query, "Successfully added %s to your friendlist.", FriendName);
  184. SendClientMessage(playerid, COLOR_WHITE, query);
  185.  
  186. DeletePVar(playerid, "New_Friend_Name");
  187. DeletePVar(playerid, "New_Friend_DBID");
  188.  
  189. return 1;
  190. }
  191. else {
  192. SendClientMessage(playerid, COLOR_WHITE, "You already have a friend with that name.");
  193. }
  194. return 1;
  195. }
  196. case REQUEST_FRIEND_THREAD:
  197. {
  198. new rows,
  199. fields;
  200.  
  201. cache_get_data(rows, fields, mysql_iConnectionHandle[0]);
  202.  
  203. if(rows) {
  204. new szResult[64];
  205.  
  206. for(new row; row < rows; row++) {
  207. cache_get_field_content(row, "FriendDBID", szResult, mysql_iConnectionHandle[0], sizeof(szResult));
  208. /* print("Friend IDs:");
  209. print(szResult);
  210. */
  211.  
  212. new query[128];
  213. mysql_format(mysql_iConnectionHandle[0], query, sizeof(query), "SELECT * FROM `accountdb` WHERE `id` = %i", strval(szResult));
  214. mysql_function_query(mysql_iConnectionHandle[0], query, true, "OnPlayerRequestFriendNames", "ii", playerid, strval(szResult));
  215.  
  216. /*if(strcmp(szResult, GetPlayerNameExt(extraid), true) != 0) continue;
  217. cache_get_field_content(row, "ID", szResult, mysql_iConnectionHandle[0], sizeof szResult); playerData[extraid][pDBID] = strval(szResult);
  218. cache_get_field_content(row, "ONotice", szResult, mysql_iConnectionHandle[0], sizeof szResult); SetPVarString(extraid, "PlayerONotice", szResult);
  219. */
  220. }
  221. }
  222. return 1;
  223. }
  224. }
  225. return 1;
  226. }
  227.  
  228. forward OnPlayerRequestFriendNames(playerid, Friend_DBID, thread);
  229. public OnPlayerRequestFriendNames(playerid, Friend_DBID, thread)
  230. {
  231. switch(thread)
  232. {
  233. case 0:
  234. {
  235. new rows,
  236. fields;
  237.  
  238. cache_get_data(rows, fields, mysql_iConnectionHandle[0]);
  239.  
  240. if(rows) {
  241. new szResult[64];
  242.  
  243. new
  244. szPlayerName[MAX_PLAYER_NAME],
  245. iPhoneNumber,
  246. szTitle[64];
  247.  
  248. SetPVarInt(playerid, "Friend_DBID", Friend_DBID);
  249.  
  250. for(new row; row < rows; row++)
  251. {
  252. cache_get_field_content(row, "Username", szPlayerName, mysql_iConnectionHandle[0], sizeof(szResult));
  253. cache_get_field_content(row, "PhoneNr", szResult, mysql_iConnectionHandle[0], sizeof(szResult)); iPhoneNumber = strval(szResult);
  254.  
  255. foreach(Player, i) {
  256. if(PlayerInfo[i][pDBID] == Friend_DBID) {
  257. format(szFriendList, sizeof(szFriendList), "%s\n* {6CBBE3}Name: {FFFFFF}%s | {8BA870}(online)", szFriendList, szPlayerName, iPhoneNumber);
  258. }
  259. else {
  260. format(szFriendList, sizeof(szFriendList), "%s\n* {6CBBE3}Name: {FFFFFF}%s | {FF0000}(offline)", szFriendList, szPlayerName, iPhoneNumber);
  261. }
  262. }
  263. }
  264. format(szTitle, sizeof szTitle, "%s's Friendlist", getPlayerName(playerid));
  265. ShowPlayerDialog(playerid, DIALOG_FRIENDLIST, DIALOG_STYLE_LIST, szTitle, szFriendList, "Select", "Cancel");
  266. }
  267. }
  268. case 1:
  269. {
  270.  
  271. }
  272. }
  273. }
  274.  
  275. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  276. {
  277. if(dialogid == DIALOG_FRIENDLIST)
  278. {
  279. strdel(szFriendList, 0, strlen(szFriendList));
  280. }
  281. return 0;
  282. }
  283.  
  284.  
  285. forward OnQueryFinish(resultid, extraid);
  286. public OnQueryFinish(resultid, extraid) {
  287. switch(resultid) {
  288. case THREAD_ACCOUNT_LOADDATA: {
  289. if(IsPlayerConnected(extraid)) {
  290.  
  291.  
  292. new rows,
  293. fields;
  294.  
  295. cache_get_data(rows, fields, mysql_iConnectionHandle[0]);
  296.  
  297. if(rows) {
  298. new szResult[64];
  299.  
  300. for(new row; row < rows; row++) {
  301. cache_get_field_content(row, "id", szResult, mysql_iConnectionHandle[0], MAX_PLAYER_NAME); PlayerInfo[extraid][pDBID] = strval(szResult);
  302.  
  303. /*if(strcmp(szResult, GetPlayerNameExt(extraid), true) != 0) continue;
  304. cache_get_field_content(row, "ID", szResult, mysql_iConnectionHandle[0], sizeof szResult); playerData[extraid][pDBID] = strval(szResult);
  305. cache_get_field_content(row, "ONotice", szResult, mysql_iConnectionHandle[0], sizeof szResult); SetPVarString(extraid, "PlayerONotice", szResult);
  306. */
  307. break;
  308. }
  309. }
  310. }
  311. return 1;
  312. }
  313. case THREAD_NO_RESULT: { return 1; }
  314. /*case THREAD_SAVE_ACCOUNT_1: {
  315.  
  316. new
  317. szQuery[2048];
  318. format(szQuery, sizeof(szQuery), "UPDATE `friendsdb` SET `FriendDBID` = %d WHERE `UserDBID` = %i", playerData[extraid][pDBID], playerData[);
  319. return mysql_function_query(mysql_iConnectionHandle[0], szQuery, false, "OnQueryFinish", "iii", THREAD_SAVE_ACCOUNT_2, extraid, g_iPlayerHandle[extraid]);
  320. }*/
  321. }
  322. return 1;
  323. }
  324.  
  325. stock GetPlayerNameExt(playerid) {
  326. new name[MAX_PLAYER_NAME];
  327. GetPlayerName(playerid, name, sizeof(name));
  328. return name;
  329. }
  330.  
  331. stock getPlayerName(playerid, underscore = 0) {
  332. new
  333. szName[MAX_PLAYER_NAME],
  334. iPos;
  335.  
  336. GetPlayerName(playerid, szName, MAX_PLAYER_NAME);
  337.  
  338. if(!underscore) while((iPos = strfind(szName, "_", false, iPos)) != -1) szName[iPos] = ' ';
  339.  
  340. return szName;
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement