dfairplay_serverside.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * DFairPlay - ServerSide 0.2.2
  3. * (c) Copyright 2009 by DracoBlue
  4. *
  5. * @author : DracoBlue (http://dracoblue.com)
  6. * @date : 5th April 2009
  7. * @update : 12th May 2009
  8. *
  9. * This file is provided as is (no warranties).
  10. *
  11. * By accessing the dfairplay-query-servers,
  12. * you agree to the DFairPlay-Server Terms of
  13. * Service.
  14. *
  15. * It's released under the terms of MIT.
  16. *
  17. * Feel free to use it, a little message in
  18. * about box is honouring thing, isn't it?
  19. */
  20. #include <a_samp>
  21. #include <pwncurl>
  22. #pragma dynamic 640000
  23. #define DFAIRPLAY_SERVERSIDE_VERSION "0.2.2"
  24. #define DFAIRPLAY_QUERY_SERVER "http://dfairplay-query.webdevberlin.com/"
  25. #if !defined DF_DEBUG
  26. #define DF_DEBUG 0
  27. #endif
  28. #pragma unused df_server_timer
  29. #define DF_MAX_STRING 512
  30. new df_server_timer;
  31. new dfairplay_query_string[DF_MAX_STRING];
  32. new DF_PLAYER_IP[MAX_PLAYERS];
  33. new DF_PLAYER_HAS_DFP[MAX_PLAYERS];
  34. new df_token[12];
  35. new df_time_since_last_token_call = 0;
  36. forward DFairPlayerServerTimer();
  37. /**
  38. * Returns an element of a string splitted by ' ', default index is 0.
  39. * @param string
  40. * index
  41. */
  42. stock DF_strtok(const string[], &index,seperator=' ')
  43. {
  44. new length = strlen(string);
  45. new offset = index;
  46. new result[116];
  47. while ((index < length) && (string[index] != seperator) && ((index - offset) < (sizeof(result) - 1)))
  48. {
  49. result[index - offset] = string[index];
  50. index++;
  51. }
  52. result[index - offset] = EOS;
  53. if ((index < length) && (string[index] == seperator))
  54. {
  55. index++;
  56. }
  57. return result;
  58. }
  59. stock DF_callDFFuncRAW(funcname[], params[])
  60. {
  61. #if DF_DEBUG
  62. printf("--->Calling--->");
  63. #endif
  64. format(dfairplay_query_string, DF_MAX_STRING, "%s%s/%s",DFAIRPLAY_QUERY_SERVER,funcname,params);
  65. #if DF_DEBUG
  66. printf("\r\n%s",dfairplay_query_string);
  67. #endif
  68. new ret_val[DF_MAX_STRING];
  69. pwncurl_get(dfairplay_query_string, ret_val, DF_MAX_STRING);
  70. #if DF_DEBUG
  71. printf("\r\n%s",ret_val);
  72. #endif
  73. return ret_val;
  74. }
  75. stock DF_callDFFuncWithToken(funcname[], params[])
  76. {
  77. #if DF_DEBUG
  78. printf("--->Calling (with token)--->");
  79. #endif
  80. format(dfairplay_query_string, DF_MAX_STRING, "%s%s/%s/%s",DFAIRPLAY_QUERY_SERVER,funcname,df_token, params);
  81. #if DF_DEBUG
  82. printf("\r\n%s",dfairplay_query_string);
  83. #endif
  84. new ret_val[DF_MAX_STRING];
  85. pwncurl_get(dfairplay_query_string, ret_val, DF_MAX_STRING);
  86. #if DF_DEBUG
  87. printf("\r\n%s",ret_val);
  88. #endif
  89. df_time_since_last_token_call = 0;
  90. return ret_val;
  91. }
  92. /**
  93. * Returns if the user has any incidents registered at dfairplay.
  94. * @return int
  95. */
  96. stock DF_hasIncidents(playerid)
  97. {
  98. new str[32];
  99. format(str,16,"gtasa-samp/%s",DF_PLAYER_IP[playerid]);
  100. return strval(DF_callDFFuncRAW("has_incidents",str));
  101. }
  102. /**
  103. * Returns if the dfairplay query server is alive!
  104. * @return int
  105. */
  106. stock DF_isAlive()
  107. {
  108. return strval(DF_callDFFuncRAW("is_alive",""));
  109. }
  110. /**
  111. * Returns all player_ids on the server, which have incidents.
  112. * @return str[]
  113. */
  114. stock DF_getIncidents()
  115. {
  116. return DF_callDFFuncWithToken("get_incidents","");
  117. }
  118. /**
  119. * Refresh our session
  120. * @return str[]
  121. */
  122. stock DF_ping()
  123. {
  124. return strval(DF_callDFFuncWithToken("ping",""));
  125. }
  126. /**
  127. * Adds a user to the server, returns if the user has DFP activated.
  128. * @return int
  129. */
  130. stock DF_addUser(playerid)
  131. {
  132. new str[20];
  133. format(str, 20, "%d/%s",playerid,DF_PLAYER_IP[playerid]);
  134. return strval(DF_callDFFuncWithToken("add_user",str)) == 1 ? 1 : 0;
  135. }
  136. /**
  137. * Adds a user to the server
  138. * @return int
  139. */
  140. stock DF_deleteUser(playerid)
  141. {
  142. new str[20];
  143. format(str, 20, "%d",playerid);
  144. DF_PLAYER_HAS_DFP[playerid] = 0;
  145. return strval(DF_callDFFuncWithToken("delete_user",str));
  146. }
  147. /**
  148. * This function _must_ be implemented and called, by the filterscript!
  149. */
  150. DF_OnFilterScriptInit() {
  151. printf(" | DracoBlue's DFairPlay ServerSide ");
  152. printf(" | Version : %s",DFAIRPLAY_SERVERSIDE_VERSION);
  153. printf(" | ");
  154. format(df_token,12,"%s",DF_callDFFuncRAW("create_session","gtasa-samp"));
  155. df_time_since_last_token_call = 0;
  156. printf(" SessionToken: %s", df_token);
  157. printf(" ` ... started!");
  158. printf(" ");
  159. df_server_timer=SetTimer("DFairPlayerServerTimer",60000,1);
  160. return 1;
  161. }
  162. DF_OnFilterScriptExit() {
  163. DF_callDFFuncWithToken("destroy_session","");
  164. return 1;
  165. }
  166. /**
  167. * This function _must_ be implemented and called, by the filterscript!
  168. */
  169. DF_OnPlayerConnect(playerid) {
  170. GetPlayerIp(playerid,DF_PLAYER_IP[playerid],16);
  171. DF_PLAYER_HAS_DFP[playerid] = DF_addUser(playerid);
  172. return 1;
  173. }
  174. /**
  175. * This function _must_ be implemented and called, by the filterscript!
  176. */
  177. DF_OnPlayerDisconnect(playerid, reason) {
  178. #pragma unused reason
  179. DF_deleteUser(playerid);
  180. return 1;
  181. }
  182. stock DF_checkAndPunishPlayers()
  183. {
  184. new str[DF_MAX_STRING];
  185. format(str,512,"%s",DF_callDFFuncWithToken("get_incidents",""));
  186. #if DF_DEBUG
  187. printf("DF_punishPlayers %s",str);
  188. #endif
  189. if (strlen(str)>0)
  190. {
  191. #if DF_DEBUG
  192. printf("have to punish players! %s",str);
  193. #endif
  194. new index;
  195. new player_id_str[3];
  196. new playerid = -1;
  197. new done_yet = 0;
  198. while (!done_yet)
  199. {
  200. format(player_id_str,3,"%s",DF_strtok(str, index, ','));
  201. done_yet = (strlen(player_id_str) == 0);
  202. if (!done_yet)
  203. {
  204. playerid = strval(player_id_str);
  205. if (IsPlayerConnected(playerid))
  206. {
  207. #if DF_DEBUG
  208. printf("We have to kick %d, yay ;).",playerid);
  209. #endif
  210. onPlayerHasIncident(playerid);
  211. }
  212. else
  213. {
  214. #if DF_DEBUG
  215. printf("Had to delete a user, which was still registered for DF but not online!");
  216. #endif
  217. DF_deleteUser(playerid);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. stock getDFairPlayServerVersion()
  224. {
  225. new str[DF_MAX_STRING];
  226. format(str, DF_MAX_STRING, "%s", DFAIRPLAY_SERVERSIDE_VERSION);
  227. return str;
  228. }
  229. stock hasPlayerDFairPlay(playerid)
  230. {
  231. if (IsPlayerConnected(playerid))
  232. {
  233. return (DF_PLAYER_HAS_DFP[playerid]==0) ? false : true;
  234. }
  235. return false;
  236. }
  237. public DFairPlayerServerTimer()
  238. {
  239. df_time_since_last_token_call += 5;
  240. new i;
  241. new players_on = 0;
  242. for (i=0; i<MAX_PLAYERS;i++)
  243. {
  244. if (IsPlayerConnected(i))
  245. {
  246. players_on++;
  247. }
  248. }
  249. if (players_on!=0)
  250. {
  251. DF_checkAndPunishPlayers();
  252. }
  253. if (df_time_since_last_token_call > 180)
  254. {
  255. // just refresh our session ;)
  256. DF_ping();
  257. }
  258. }