Guest User

Untitled

a guest
Jun 6th, 2017
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.92 KB | None | 0 0
  1. #pragma semicolon 1
  2. #include <sourcemod>
  3.  
  4. #undef REQUIRE_EXTENSIONS
  5. #include <SteamWorks>
  6.  
  7. #define STRING(%1) %1, sizeof(%1)
  8.  
  9. #define PLUGIN_VERSION "2.5"
  10.  
  11. // ====[ HANDLES | CVARS | VARIABLES ]===================================================
  12. //new Handle:g_motdID;
  13. new Handle:g_OnConnect;
  14. new Handle:g_immunity;
  15. new Handle:g_OnOther;
  16. new Handle:g_Review;
  17. new Handle:g_forced;
  18. new Handle:g_autoClose;
  19. new Handle:g_always;
  20.  
  21. new const String:g_GamesSupported[][] = {
  22. "tf",
  23. "csgo",
  24. "cstrike",
  25. "dod",
  26. "nucleardawn",
  27. "hl2mp",
  28. "left4dead",
  29. "left4dead2",
  30. "nmrih",
  31. "fof",
  32. "insurgency"
  33. };
  34. new String:gameDir[255];
  35. new String:g_serverIP[16];
  36.  
  37. new g_serverPort;
  38. new g_shownTeamVGUI[MAXPLAYERS+1] = { false, ... };
  39. new g_lastView[MAXPLAYERS+1];
  40. new Handle:g_Whitelist = INVALID_HANDLE;
  41.  
  42. new bool:VGUICaught[MAXPLAYERS+1];
  43. new bool:CanReview;
  44. new bool:LateLoad;
  45.  
  46. // ====[ PLUGIN | FORWARDS ]========================================================================
  47. public Plugin:myinfo =
  48. {
  49. name = "",
  50. author = "",
  51. description = "",
  52. version = PLUGIN_VERSION,
  53. url = ""
  54. }
  55.  
  56. public OnPluginStart()
  57. {
  58. // Global Server Variables //
  59. new bool:exists = false;
  60. GetGameFolderName(gameDir, sizeof(gameDir));
  61. for (new i = 0; i < sizeof(g_GamesSupported); i++)
  62. {
  63. if (StrEqual(g_GamesSupported[i], gameDir))
  64. {
  65. exists = true;
  66. break;
  67. }
  68. }
  69. if (!exists)
  70. SetFailState("The game '%s' isn't currently supported by the ADVERTS plugin!", gameDir);
  71. exists = false;
  72.  
  73. new Handle:serverIP = FindConVar("hostip");
  74. new Handle:serverPort = FindConVar("hostport");
  75. if (serverIP == INVALID_HANDLE || serverPort == INVALID_HANDLE)
  76. SetFailState("Could not determine server ip and port.");
  77.  
  78. new IP = GetConVarInt(serverIP);
  79. g_serverPort = GetConVarInt(serverPort);
  80. Format(g_serverIP, sizeof(g_serverIP), "%d.%d.%d.%d", IP >>> 24 & 255, IP >>> 16 & 255, IP >>> 8 & 255, IP & 255);
  81.  
  82. // Plugin ConVars //
  83. CreateConVar("sm_version", PLUGIN_VERSION, "[SM] ADVERTS Plugin Version", FCVAR_NOTIFY|FCVAR_DONTRECORD);
  84.  
  85. g_immunity = CreateConVar("sm_adverts_immunity", "0", "Enable/Disable advert immunity");
  86. g_OnConnect = CreateConVar("sm_adverts_onconnect", "1", "Enable/Disable advert on connect");
  87. g_autoClose = CreateConVar("sm_adverts_auto_close", "40.0", "Set time (in seconds) to automatically close the MOTD window.", _, true, 30.0);
  88. // Global Server Variables //
  89.  
  90. if (!StrEqual(gameDir, "left4dead2") && !StrEqual(gameDir, "left4dead"))
  91. {
  92. HookEventEx("arena_win_panel", Event_End);
  93. HookEventEx("cs_win_panel_round", Event_End);
  94. HookEventEx("dod_round_win", Event_End);
  95. HookEventEx("player_death", Event_Death);
  96. HookEventEx("round_start", Event_Start);
  97. HookEventEx("round_win", Event_End);
  98. HookEventEx("teamplay_win_panel", Event_End);
  99.  
  100. g_OnOther = CreateConVar("sm_adverts_onother", "2", "Set 0 to disable, 1 to show on round end, 2 to show on player death, 4 to show on round start, 3=1+2, 5=1+4, 6=2+4, 7=1+2+4");
  101. g_Review = CreateConVar("sm_adverts_review", "15.0", "Set time (in minutes) to re-display the ad. ConVar sm_motdgd_onother must be configured", _, true, 15.0);
  102. }
  103.  
  104. if (!StrEqual(gameDir, "left4dead2") && !StrEqual(gameDir, "left4dead") && !StrEqual(gameDir, "csgo"))
  105. {
  106. g_forced = CreateConVar("sm_adverts_forced_duration", "5", "Number of seconds to force an ad view for (except in CS:GO, L4D, L4D2)");
  107. }
  108. // Plugin ConVars //
  109.  
  110. // MOTDgd MOTD Stuff //
  111. new UserMsg:datVGUIMenu = GetUserMessageId("VGUIMenu");
  112. if (datVGUIMenu == INVALID_MESSAGE_ID)
  113. SetFailState("The game '%s' doesn't support VGUI menus.", gameDir);
  114. HookUserMessage(datVGUIMenu, OnVGUIMenu, true);
  115. AddCommandListener(ClosedMOTD, "closed_htmlpage");
  116.  
  117. HookEventEx("player_transitioned", Event_PlayerTransitioned);
  118. // MOTDgd MOTD Stuff //
  119.  
  120. g_always = CreateConVar("sm_adverts_always", "0", "Show adverts even to alive players");
  121.  
  122. AutoExecConfig(true);
  123. LoadWhitelist();
  124.  
  125. if(LateLoad)
  126. {
  127. for(new i=1;i<=MaxClients;i++)
  128. {
  129. if(IsClientInGame(i))
  130. g_lastView[i] = GetTime();
  131. }
  132. }
  133.  
  134. if(LibraryExists("SteamWorks")) {
  135. IP = SteamWorks_GetPublicIPCell();
  136. Format(g_serverIP, sizeof(g_serverIP), "%d.%d.%d.%d", IP >>> 24 & 255, IP >>> 16 & 255, IP >>> 8 & 255, IP & 255);
  137. }
  138.  
  139. CreateTimer(60.0, Adverts, _, TIMER_REPEAT);
  140. }
  141.  
  142. public Action:Adverts(Handle:timer, any:userid)
  143. {
  144. if (!GetConVarBool(g_always))return;
  145.  
  146.  
  147. for(new i=1;i<=MaxClients;i++)
  148. {
  149. if(IsClientInGame(i))
  150. ShowAdv(i);
  151. }
  152. }
  153.  
  154.  
  155.  
  156. public OnLibraryAdded(const String:name[]) {
  157. if(strcmp(name, "SteamWorks")==0) {
  158. new IP = SteamWorks_GetPublicIPCell();
  159. Format(g_serverIP, sizeof(g_serverIP), "%d.%d.%d.%d", IP >>> 24 & 255, IP >>> 16 & 255, IP >>> 8 & 255, IP & 255);
  160. }
  161. }
  162.  
  163. public bool:OnClientConnect(client, String:rejectmsg[], maxlen)
  164. {
  165. // Set the expected defaults for the client
  166. VGUICaught[client] = false;
  167. g_shownTeamVGUI[client] = false;
  168. g_lastView[client] = 0;
  169.  
  170. if (!StrEqual(gameDir, "left4dead2") && !StrEqual(gameDir, "left4dead"))
  171. CanReview = true;
  172.  
  173. return true;
  174. }
  175.  
  176. public OnClientPutInServer(client)
  177. {
  178. // Load the advertisement via conventional means
  179. if (StrEqual(gameDir, "left4dead2") && GetConVarBool(g_OnConnect))
  180. {
  181. CreateTimer(0.1, PreMotdTimer, GetClientUserId(client));
  182. }
  183. }
  184.  
  185. //new g_phraseCount;
  186. new String:g_Phrases[][] = {"http://google.com", "http://youtube.com"};
  187.  
  188. public OnMapStart() {
  189.  
  190. LoadWhitelist();
  191. //g_phraseCount = BuildPhrases();
  192. }
  193.  
  194. BuildPhrases()
  195. {
  196. decl String:imFile[PLATFORM_MAX_PATH];
  197. decl String:line[192];
  198. new i = 0;
  199. new totalLines = 0;
  200.  
  201. BuildPath(Path_SM, imFile, sizeof(imFile), "configs/config.ini");
  202.  
  203. new Handle:file = OpenFile(imFile, "rt");
  204.  
  205. if(file != INVALID_HANDLE)
  206. {
  207. while (!IsEndOfFile(file))
  208. {
  209. if (!ReadFileLine(file, line, sizeof(line)))
  210. {
  211. break;
  212. }
  213.  
  214. TrimString(line);
  215. if( strlen(line) > 0 )
  216. {
  217. FormatEx(g_Phrases[i],192, "%s", line);
  218. totalLines++;
  219. }
  220.  
  221. i++;
  222.  
  223. //check for max no. of entries
  224. if( i >= sizeof(g_Phrases) )
  225. {
  226. SetFailState("Attempted to add more than the maximum allowed phrases from file");
  227. break;
  228. }
  229. }
  230.  
  231. CloseHandle(file);
  232. }
  233. else
  234. {
  235. SetFailState("[SM] no file found (configs/config.ini)");
  236. }
  237.  
  238. return totalLines;
  239. }
  240.  
  241. // ====[ FUNCTIONS ]=====================================================================
  242.  
  243. public LoadWhitelist() {
  244.  
  245. if(g_Whitelist != INVALID_HANDLE) {
  246. ClearArray(g_Whitelist);
  247. } else {
  248. g_Whitelist = CreateArray(32);
  249. }
  250.  
  251. new String:Path[PLATFORM_MAX_PATH];
  252. BuildPath(Path_SM, STRING(Path), "configs/adverts_whitelist.cfg");
  253. new Handle:hFile = OpenFile(Path, "r");
  254. if(!hFile) {
  255. return;
  256. }
  257.  
  258. new String:SteamID[32];
  259. while(ReadFileLine(hFile, STRING(SteamID))) {
  260.  
  261. PushArrayString(g_Whitelist, SteamID[8]);
  262. }
  263.  
  264. CloseHandle(hFile);
  265. }
  266.  
  267. public Action:Event_End(Handle:event, const String:name[], bool:dontBroadcast)
  268. {
  269. new client = GetClientOfUserId(GetEventInt(event, "userid"));
  270.  
  271. // Re-view minutes must be 15 or higher, re-view mode (onother) for this event
  272. if (GetConVarFloat(g_Review) < 15.0 || (g_OnOther && GetConVarInt(g_OnOther) != 1 && GetConVarInt(g_OnOther) != 3 && GetConVarInt(g_OnOther) != 5 && GetConVarInt(g_OnOther) != 7))
  273. return Plugin_Continue;
  274.  
  275. // Only process the re-view event if the client is valid and is eligible to view another advertisement
  276. if (IsValidClient(client) && CanReview && GetTime() - g_lastView[client] >= GetConVarFloat(g_Review) * 60)
  277. {
  278. g_lastView[client] = GetTime();
  279. CreateTimer(0.1, PreMotdTimer, GetClientUserId(client));
  280. }
  281.  
  282. return Plugin_Continue;
  283. }
  284.  
  285. public Action:Event_Death(Handle:event, const String:name[], bool:dontBroadcast)
  286. {
  287. new client = GetClientOfUserId(GetEventInt(event, "userid"));
  288. if(!client)
  289. return Plugin_Continue;
  290.  
  291. CreateTimer(0.5, CheckPlayerDeath, GetClientUserId(client));
  292.  
  293. return Plugin_Continue;
  294. }
  295.  
  296. public Action:Event_Start(Handle:event, const String:name[], bool:dontBroadcast)
  297. {
  298. new client = GetClientOfUserId(GetEventInt(event, "userid"));
  299.  
  300. // Re-view minutes must be 15 or higher, re-view mode (onother) for this event
  301. if (GetConVarFloat(g_Review) < 15.0 || (g_OnOther && GetConVarInt(g_OnOther) != 4 && GetConVarInt(g_OnOther) != 5 && GetConVarInt(g_OnOther) != 6 && GetConVarInt(g_OnOther) != 7))
  302. return Plugin_Continue;
  303.  
  304. // Only process the re-view event if the client is valid and is eligible to view another advertisement
  305. if (IsValidClient(client) && CanReview && GetTime() - g_lastView[client] >= GetConVarFloat(g_Review) * 60)
  306. {
  307. g_lastView[client] = GetTime();
  308. CreateTimer(0.1, PreMotdTimer, GetClientUserId(client));
  309. }
  310.  
  311. return Plugin_Continue;
  312. }
  313.  
  314. ShowAdv(client)
  315. {
  316. // Re-view minutes must be 15 or higher, re-view mode (onother) for this event
  317. if (GetConVarFloat(g_Review) < 15.0)
  318. return;
  319.  
  320. // Only process the re-view event if the client is valid and is eligible to view another advertisement
  321. if (IsValidClient(client) && CanReview && GetTime() - g_lastView[client] >= GetConVarFloat(g_Review) * 60)
  322. {
  323. g_lastView[client] = GetTime();
  324. CreateTimer(0.1, PreMotdTimer, GetClientUserId(client));
  325. }
  326. }
  327.  
  328. public Action:CheckPlayerDeath(Handle:timer, any:userid)
  329. {
  330. new client=GetClientOfUserId(userid);
  331. if(!client)
  332. return Plugin_Stop;
  333.  
  334. // Check if client is valid
  335. if (!IsValidClient(client))
  336. return Plugin_Stop;
  337.  
  338. // We don't want TF2's Dead Ringer triggering a false re-view event
  339. if (IsPlayerAlive(client))
  340. return Plugin_Stop;
  341.  
  342. // Re-view minutes must be 15 or higher, re-view mode (onother) for this event
  343. if (GetConVarFloat(g_Review) < 15.0 || (g_OnOther && GetConVarInt(g_OnOther) != 2 && GetConVarInt(g_OnOther) != 3 && GetConVarInt(g_OnOther) != 6 && GetConVarInt(g_OnOther) != 7))
  344. return Plugin_Stop;
  345.  
  346. // Only process the re-view event if the client is valid and is eligible to view another advertisement
  347. if (CanReview && GetTime() - g_lastView[client] >= GetConVarFloat(g_Review) * 60)
  348. {
  349. g_lastView[client] = GetTime();
  350. CreateTimer(0.1, PreMotdTimer, GetClientUserId(client));
  351. }
  352.  
  353. return Plugin_Stop;
  354. }
  355.  
  356. public Action:Event_PlayerTransitioned(Handle:event, const String:name[], bool:dontBroadcast)
  357. {
  358. new client = GetClientOfUserId(GetEventInt(event, "userid"));
  359.  
  360. if (IsValidClient(client) && GetConVarBool(g_OnConnect))
  361. CreateTimer(0.1, PreMotdTimer, GetClientUserId(client));
  362.  
  363. return Plugin_Continue;
  364. }
  365.  
  366. public Action:OnVGUIMenu(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init)
  367. {
  368. if(!(playersNum > 0))
  369. return Plugin_Handled;
  370. new client = players[0];
  371.  
  372. if (playersNum > 1 || !IsValidClient(client) || VGUICaught[client] || !GetConVarBool(g_OnConnect))
  373. return Plugin_Continue;
  374.  
  375. VGUICaught[client] = true;
  376.  
  377. g_lastView[client] = GetTime();
  378.  
  379. CreateTimer(0.1, PreMotdTimer, GetClientUserId(client));
  380.  
  381. return Plugin_Handled;
  382. }
  383.  
  384. public Action:ClosedMOTD(client, const String:command[], argc)
  385. {
  386. if (!IsValidClient(client))
  387. return Plugin_Handled;
  388.  
  389. if(g_forced != INVALID_HANDLE && GetConVarInt(g_forced) != 0 && g_lastView[client] != 0 && (g_lastView[client]+GetConVarInt(g_forced) >= GetTime()))
  390. {
  391. new timeRemaining = ( ( g_lastView[client]+GetConVarInt(g_forced) )-GetTime() ) + 1;
  392.  
  393. if (timeRemaining == 1)
  394. {
  395. PrintCenterText(client, "Please wait %i second", timeRemaining);
  396. }
  397. else
  398. {
  399. PrintCenterText(client, "Please wait %i seconds", timeRemaining);
  400. }
  401.  
  402. if (StrEqual(gameDir, "cstrike"))
  403. ShowMOTDScreen(client, "", false);
  404. else
  405. ShowMOTDScreen(client, "http://", false);
  406. }
  407. else
  408. {
  409. if (StrEqual(gameDir, "cstrike") || StrEqual(gameDir, "csgo") || StrEqual(gameDir, "insurgency"))
  410. FakeClientCommand(client, "joingame");
  411. else if (StrEqual(gameDir, "nucleardawn") || StrEqual(gameDir, "dod"))
  412. ClientCommand(client, "changeteam");
  413. }
  414.  
  415. return Plugin_Handled;
  416. }
  417.  
  418. public Action:PreMotdTimer(Handle:timer, any:userid)
  419. {
  420. new client=GetClientOfUserId(userid);
  421. if(!client)
  422. return Plugin_Stop;
  423.  
  424. if (!IsValidClient(client))
  425. return Plugin_Stop;
  426.  
  427. decl String:url[255];
  428. decl String:steamid[255];
  429. decl String:name[MAX_NAME_LENGTH];
  430. decl String:name_encoded[MAX_NAME_LENGTH*2];
  431. GetClientName(client, name, sizeof(name));
  432. urlencode(name, name_encoded, sizeof(name_encoded));
  433. /*
  434. if (GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid)))
  435. Format(url, sizeof(url), "http://motdgd.com/motd/?user=%d&ip=%s&pt=%d&v=%s&st=%s&gm=%s&name=%s", GetConVarInt(g_motdID), g_serverIP, g_serverPort, PLUGIN_VERSION, steamid, gameDir, name_encoded);
  436. else
  437. Format(url, sizeof(url), "http://motdgd.com/motd/?user=%d&ip=%s&pt=%d&v=%s&st=NULL&gm=%s&name=%s", GetConVarInt(g_motdID), g_serverIP, g_serverPort, PLUGIN_VERSION, gameDir, name_encoded);
  438. */
  439. //new link = GetRandomInt(0,g_phraseCount-1);
  440. new link = GetRandomInt(0,sizeof(g_Phrases)-1);
  441.  
  442. Format(url, sizeof(url), g_Phrases[link]);
  443. char temport[24];
  444. IntToString(g_serverPort, temport, 24);
  445. ReplaceString(url, 255, "{NAME}", name_encoded, true);
  446. ReplaceString(url, 255, "{IP}", g_serverIP, true);
  447. ReplaceString(url, 255, "{PORT}", temport, true);
  448. ReplaceString(url, 255, "{STEAMID}", steamid, true);
  449. ReplaceString(url, 255, "{GAME}", gameDir, true);
  450.  
  451. if(FindStringInArray(g_Whitelist, steamid[8])!=-1) {
  452. return Plugin_Stop;
  453. }
  454.  
  455. if(g_forced != INVALID_HANDLE && GetConVarInt(g_forced) != 0)
  456. {
  457. CreateTimer(0.2, RefreshMotdTimer, userid);
  458. }
  459.  
  460. CreateTimer(GetConVarFloat(g_autoClose), AutoCloseTimer, userid);
  461. ShowMOTDScreen(client, url, false); // False means show, true means hide
  462.  
  463. return Plugin_Stop;
  464. }
  465.  
  466. public Action:AutoCloseTimer(Handle:timer, any:userid)
  467. {
  468. new client=GetClientOfUserId(userid);
  469.  
  470. if(!client)
  471. return Plugin_Stop;
  472.  
  473. ShowMOTDScreen(client, "http://motdgd.com/motd/blank.php", true);
  474.  
  475. return Plugin_Stop;
  476. }
  477.  
  478. public Action:RefreshMotdTimer(Handle:timer, any:userid)
  479. {
  480. new client=GetClientOfUserId(userid);
  481.  
  482. if(!client)
  483. return Plugin_Stop;
  484.  
  485. if (!IsValidClient(client))
  486. return Plugin_Stop;
  487.  
  488. if(g_forced != INVALID_HANDLE && GetConVarInt(g_forced) != 0 && g_lastView[client] != 0 && (g_lastView[client]+GetConVarInt(g_forced)) >= GetTime())
  489. {
  490. CreateTimer(0.3, RefreshMotdTimer, userid);
  491. }
  492.  
  493. ShowMOTDScreen(client, "http://", false);
  494.  
  495. return Plugin_Stop;
  496. }
  497.  
  498. stock ShowMOTDScreen(client, String:url[], bool:hidden)
  499. {
  500. if (!IsValidClient(client))
  501. return;
  502.  
  503. new Handle:kv = CreateKeyValues("data");
  504.  
  505. if (StrEqual(gameDir, "left4dead") || StrEqual(gameDir, "left4dead2"))
  506. KvSetString(kv, "cmd", "closed_htmlpage");
  507. else
  508. KvSetNum(kv, "cmd", 5);
  509.  
  510. KvSetString(kv, "msg", url);
  511. KvSetString(kv, "title", "MOTDgd AD");
  512. KvSetNum(kv, "type", MOTDPANEL_TYPE_URL);
  513. ShowVGUIPanel(client, "info", kv, !hidden);
  514. CloseHandle(kv);
  515. }
  516.  
  517. stock GetRealPlayerCount()
  518. {
  519. new players;
  520. for (new i = 1; i <= MaxClients; i++)
  521. {
  522. if (IsClientInGame(i) && !IsFakeClient(i))
  523. players++;
  524. }
  525. return players;
  526. }
  527.  
  528. stock bool:IsValidClient(i){
  529. if (!i || !IsClientInGame(i) || IsClientSourceTV(i) || IsClientReplay(i) || IsFakeClient(i) || !IsClientConnected(i))
  530. return false;
  531. if (!GetConVarBool(g_immunity))
  532. return true;
  533. if (CheckCommandAccess(i, "ADVERTS_Immunity", ADMFLAG_RESERVATION))
  534. return false;
  535.  
  536. return true;
  537. }
  538.  
  539. stock urlencode(const String:sString[], String:sResult[], len)
  540. {
  541. new String:sHexTable[] = "0123456789abcdef";
  542. new from, c;
  543. new to;
  544.  
  545. while(from < len)
  546. {
  547. c = sString[from++];
  548. if(c == 0)
  549. {
  550. sResult[to++] = c;
  551. break;
  552. }
  553. else if(c == ' ')
  554. {
  555. sResult[to++] = '+';
  556. }
  557. else if((c < '0' && c != '-' && c != '.') ||
  558. (c < 'A' && c > '9') ||
  559. (c > 'Z' && c < 'a' && c != '_') ||
  560. (c > 'z'))
  561. {
  562. if((to + 3) > len)
  563. {
  564. sResult[to] = 0;
  565. break;
  566. }
  567. sResult[to++] = '%';
  568. sResult[to++] = sHexTable[c >> 4];
  569. sResult[to++] = sHexTable[c & 15];
  570. }
  571. else
  572. {
  573. sResult[to++] = c;
  574. }
  575. }
  576. }
Advertisement
Add Comment
Please, Sign In to add comment