Advertisement
willbedie

[MySQL R41-4] Update System

Mar 16th, 2019
1,552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. // This is a comment
  2. // uncomment the line below if you want to write a filterscript
  3. #define FILTERSCRIPT
  4.  
  5. #include <a_samp>
  6. #include <a_mysql>
  7. #include <easyDialog>
  8. #include <zcmd>
  9. #include <sscanf2>
  10.  
  11. #if defined FILTERSCRIPT
  12.  
  13. #define MYSQL_HOSTNAME "localhost"
  14. #define MYSQL_USERNAME "root"
  15. #define MYSQL_PASSWORD ""
  16. #define MYSQL_DATABASE "ablodm"
  17.  
  18. new MySQL: Database;
  19.  
  20. public OnFilterScriptInit()
  21. {
  22.  
  23. Database = mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);
  24. if(Database == MYSQL_INVALID_HANDLE || mysql_errno(Database) != 0)
  25. {
  26. print("[SERVER]: MySQL Connection failed, shutting the server down!");
  27. SendRconCommand("exit");
  28. return 1;
  29. }
  30.  
  31. print("[SERVER]: MySQL Connection was successful.");
  32.  
  33. mysql_query(Database, "CREATE TABLE IF NOT EXISTS updates (`UpdateID` int(10) AUTO_INCREMENT PRIMARY KEY, `AddedBy` VARCHAR(24) NOT NULL, `Text` VARCHAR(128) NOT NULL, `Status` int(10), `DateAdded` VARCHAR(30) NOT NULL); ");
  34. return 1;
  35. }
  36.  
  37. CMD:addupdate(playerid, params[])
  38. {
  39. new
  40. text[128], status, query[280]
  41. ;
  42.  
  43. if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "You must be an admin to use this command.");
  44. if(sscanf(params, "is[128]", status, text))
  45. {
  46. SendClientMessage(playerid, -1, "[USAGE]: {AFAFAF}/addupdate [status] [text]");
  47. SendClientMessage(playerid, -1, "Use 1 if you want to display the update as added, 2 if you want to display it as changed, 3 as fixed and 4 as removed.");
  48. return 1;
  49. }
  50.  
  51. mysql_format(Database, query, sizeof(query), "INSERT INTO `updates` (`AddedBy`, `Text`, `Status`, `DateAdded`) VALUES ('%e', '%e', '%i', '%e')", GetName(playerid), text, status, ReturnDate());
  52. mysql_tquery(Database, query, "OnPlayerAddUpdate", "iis", playerid, status, text);
  53. return 1;
  54. }
  55.  
  56. CMD:removeupdate(playerid, params[])
  57. {
  58. new
  59. updateid, query[128]
  60. ;
  61.  
  62. if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "You must be an admin to use this command.");
  63. if(sscanf(params, "i", updateid)) return SendClientMessage(playerid, -1, "[USAGE]: {AFAFAF}/removeupdate [updateid]");
  64.  
  65. mysql_format(Database, query, sizeof(query), "SELECT `UpdateID` FROM `updates` WHERE `UpdateID` = '%i'", updateid);
  66. mysql_tquery(Database, query, "OnPlayerDeleteUpdate", "ii", playerid, updateid);
  67. return 1;
  68. }
  69.  
  70. CMD:updates(playerid, params[])
  71. {
  72. new
  73. query[128]
  74. ;
  75.  
  76. mysql_format(Database, query, sizeof(query), "SELECT * FROM `updates`");
  77. mysql_pquery(Database, query, "Player_ViewUpdates", "i", playerid);
  78. return 1;
  79. }
  80.  
  81. forward Player_ViewUpdates(playerid);
  82. public Player_ViewUpdates(playerid)
  83. {
  84. if(cache_num_rows())
  85. {
  86. new
  87. updateid, addedby[24], text[128], status, sstatus[128], dateadded[30], string[500] // the reason this is huge is because there might be a lot of updates so it should be bigger, you can change this any time you want.
  88. ;
  89.  
  90. format(string, sizeof(string), "This is a list of the new server updates on the last revision:\n\n");
  91. for(new i = 0; i < cache_num_rows(); i ++)
  92. {
  93. cache_get_value_name_int(i, "UpdateID", updateid);
  94. cache_get_value_name(i, "AddedBy", addedby);
  95. cache_get_value_name(i, "Text", text);
  96. cache_get_value_name_int(i, "Status", status);
  97. cache_get_value_name(i, "DateAdded", dateadded);
  98.  
  99. switch(status)
  100. {
  101. case 1: sstatus = "Added";
  102. case 2: sstatus = "Changed";
  103. case 3: sstatus = "Fixed";
  104. case 4: sstatus = "Removed";
  105. }
  106.  
  107. format(string, sizeof(string), "%s[%d] %s - %s [%s] on %s\n", string, updateid, sstatus, text, addedby, dateadded);
  108. }
  109. Dialog_Show(playerid, DIALOG_UPDATES, DIALOG_STYLE_MSGBOX, "Server new Updates", string, "Close", "");
  110. }
  111. else
  112. {
  113. Dialog_Show(playerid, DIALOG_UPDATES, DIALOG_STYLE_MSGBOX, "Server new Updates", "There are currently no updates on the database.", "Close", "");
  114. }
  115. return 1;
  116. }
  117.  
  118. forward OnPlayerDeleteUpdate(playerid, updateid);
  119. public OnPlayerDeleteUpdate(playerid, updateid)
  120. {
  121. if(cache_num_rows())
  122. {
  123. new
  124. string[128], query[128]
  125. ;
  126.  
  127. format(string, sizeof(string), "You have successfully removed UpdateID %d from the database.", updateid);
  128. SendClientMessage(playerid, -1, string);
  129.  
  130. mysql_format(Database, query, sizeof(query), "DELETE FROM `updates` WHERE `UpdateID` = '%i'", updateid);
  131. mysql_query(Database, query);
  132. }
  133. else
  134. {
  135. SendClientMessage(playerid, -1, "That UpdateID was not found in the database.");
  136. }
  137. return 1;
  138. }
  139.  
  140. forward OnPlayerAddUpdate(playerid, status, text[]);
  141. public OnPlayerAddUpdate(playerid, status, text[])
  142. {
  143. new
  144. updateid = cache_insert_id(), string[128], sstring[100]
  145. ;
  146.  
  147. switch(status)
  148. {
  149. case 1: sstring = "Added";
  150. case 2: sstring = "Changed";
  151. case 3: sstring = "Fixed";
  152. case 4: sstring = "Removed";
  153. }
  154.  
  155. format(string, sizeof(string), "You have successfully a new update - [ID: %d] - [Text: %s] - [Status: %s]", updateid, text, sstring);
  156. SendClientMessage(playerid, -1, string);
  157. return 1;
  158. }
  159.  
  160. GetName(playerid)
  161. {
  162. new playerName[MAX_PLAYERS];
  163. GetPlayerName(playerid, playerName, sizeof(playerName));
  164. return playerName;
  165. }
  166.  
  167. ReturnDate()
  168. {
  169. new sendString[90], MonthStr[40], month, day, year;
  170. new hour, minute, second;
  171.  
  172. gettime(hour, minute, second);
  173. getdate(year, month, day);
  174. switch(month)
  175. {
  176. case 1: MonthStr = "January";
  177. case 2: MonthStr = "February";
  178. case 3: MonthStr = "March";
  179. case 4: MonthStr = "April";
  180. case 5: MonthStr = "May";
  181. case 6: MonthStr = "June";
  182. case 7: MonthStr = "July";
  183. case 8: MonthStr = "August";
  184. case 9: MonthStr = "September";
  185. case 10: MonthStr = "October";
  186. case 11: MonthStr = "November";
  187. case 12: MonthStr = "December";
  188. }
  189.  
  190. format(sendString, 90, "%s %d, %d %02d:%02d:%02d", MonthStr, day, year, hour, minute, second);
  191. return sendString;
  192. }
  193.  
  194. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement