diff --git a/2006Scape Server/ServerConfig.Sample.json b/2006Scape Server/ServerConfig.Sample.json index 2650d66e..443a0426 100644 --- a/2006Scape Server/ServerConfig.Sample.json +++ b/2006Scape Server/ServerConfig.Sample.json @@ -24,7 +24,6 @@ "timeout": 60, "item_requirements": true, "max_players": 200, - "website_integration": false, "cycle_logging": true, "cycle_logging_tick": 10 }, @@ -33,5 +32,10 @@ "welcome_channel_id": "655143683083206667", "log_channel_id": "1083146780507656294", "player_count": false + }, + "website": { + "password": "", + "players_online": false, + "registered_accounts": false } } diff --git a/2006Scape Server/plugins/plugin/integration/website/PlayersOnlineWebsite.kt b/2006Scape Server/plugins/plugin/integration/website/PlayersOnlineWebsite.kt new file mode 100644 index 00000000..59ef0fb1 --- /dev/null +++ b/2006Scape Server/plugins/plugin/integration/website/PlayersOnlineWebsite.kt @@ -0,0 +1,50 @@ +package plugin.integration.website + +import com.rs2.Constants +import com.rs2.game.players.PlayerHandler +import java.util.concurrent.ScheduledFuture +import java.util.concurrent.TimeUnit + +object PlayersOnlineWebsite { + + private var task: ScheduledFuture<*>? = null + + fun start() { + if (!WebsiteConfig.playersOnline) { + return + } + if (WebsiteConfig.password.isEmpty()) { + WebsiteService.warnMissingPassword() + return + } + + stop() + task = WebsiteService.getScheduler().scheduleAtFixedRate({ + try { + update() + } catch (e: Exception) { + if (Constants.SERVER_DEBUG) { + e.printStackTrace() + } else { + System.err.println("[WebsiteIntegration] Error in PlayersOnlineWebsite task: ${e.message}") + } + } + }, 10, 30, TimeUnit.SECONDS) + } + + fun stop() { + task?.cancel(true) + task = null + } + + fun update() { + val pass = WebsiteConfig.password + if (pass.isEmpty()) return + + WebsiteService.sendGetRequest("playersonline.php", mapOf( + "pass" to pass, + "amount" to PlayerHandler.getPlayerCount(), + "world" to Constants.WORLD + )) + } +} diff --git a/2006Scape Server/plugins/plugin/integration/website/RegisteredAccsWebsite.kt b/2006Scape Server/plugins/plugin/integration/website/RegisteredAccsWebsite.kt new file mode 100644 index 00000000..1750335c --- /dev/null +++ b/2006Scape Server/plugins/plugin/integration/website/RegisteredAccsWebsite.kt @@ -0,0 +1,68 @@ +package plugin.integration.website + +import com.rs2.Constants +import java.io.File +import java.nio.file.Files +import java.nio.file.Paths +import java.util.concurrent.ScheduledFuture +import java.util.concurrent.TimeUnit + +object RegisteredAccsWebsite { + + private var task: ScheduledFuture<*>? = null + + fun start() { + if (!WebsiteConfig.registeredAccounts) { + return + } + if (WebsiteConfig.password.isEmpty()) { + WebsiteService.warnMissingPassword() + return + } + + stop() + task = WebsiteService.getScheduler().scheduleAtFixedRate({ + try { + update() + } catch (e: Exception) { + if (Constants.SERVER_DEBUG) { + e.printStackTrace() + } else { + System.err.println("[WebsiteIntegration] Error in RegisteredAccsWebsite task: ${e.message}") + } + } + }, 15, 90, TimeUnit.SECONDS) + } + + fun stop() { + task?.cancel(true) + task = null + } + + fun update() { + val pass = WebsiteConfig.password + if (pass.isEmpty()) return + + val charactersPath = Paths.get("data", "characters") + val amount = if (Files.exists(charactersPath) && Files.isDirectory(charactersPath)) { + try { + val stream = Files.list(charactersPath) + try { + stream.count().toInt() + } finally { + stream.close() + } + } catch (e: Exception) { + File("data/characters/").list()?.size ?: 0 + } + } else { + 0 + } + + WebsiteService.sendGetRequest("accountsregistered.php", mapOf( + "pass" to pass, + "amount" to amount, + "world" to Constants.WORLD + )) + } +} diff --git a/2006Scape Server/plugins/plugin/integration/website/WebsiteConfig.kt b/2006Scape Server/plugins/plugin/integration/website/WebsiteConfig.kt new file mode 100644 index 00000000..dfad2165 --- /dev/null +++ b/2006Scape Server/plugins/plugin/integration/website/WebsiteConfig.kt @@ -0,0 +1,26 @@ +package plugin.integration.website + +import com.rs2.event.EventContext +import com.rs2.event.EventSubscriber +import com.rs2.event.SubscribesTo +import com.rs2.event.impl.ServerConfigEvent +import com.rs2.game.players.Player + +@SubscribesTo(ServerConfigEvent::class) +class WebsiteConfig : EventSubscriber { + + companion object { + var password: String = "" + var playersOnline: Boolean = false + var registeredAccounts: Boolean = false + } + + override fun subscribe(context: EventContext, player: Player?, event: ServerConfigEvent) { + val legacyMaster = event.getBoolean("website_integration", false) + event.group("website") { g -> + g.registerString("password", password, "websitepass") { password = it } + g.registerBoolean("players_online", legacyMaster, "website_players_online") { playersOnline = it } + g.registerBoolean("registered_accounts", legacyMaster, "website_registered_accounts") { registeredAccounts = it } + } + } +} diff --git a/2006Scape Server/plugins/plugin/integration/website/WebsiteService.kt b/2006Scape Server/plugins/plugin/integration/website/WebsiteService.kt new file mode 100644 index 00000000..aa43b785 --- /dev/null +++ b/2006Scape Server/plugins/plugin/integration/website/WebsiteService.kt @@ -0,0 +1,72 @@ +package plugin.integration.website + +import com.rs2.Constants +import java.net.HttpURLConnection +import java.net.URL +import java.net.URLEncoder +import java.nio.charset.StandardCharsets +import java.util.concurrent.Executors +import java.util.concurrent.ScheduledExecutorService + +object WebsiteService { + + private var scheduler: ScheduledExecutorService? = null + private var warnedNoPassword = false + + @Synchronized + fun getScheduler(): ScheduledExecutorService { + val current = scheduler + if (current == null || current.isShutdown) { + val newExec = Executors.newSingleThreadScheduledExecutor { r -> + Thread(r, "WebsiteIntegrationWorker").apply { isDaemon = true } + } + scheduler = newExec + return newExec + } + return current + } + + @Synchronized + fun stop() { + scheduler?.shutdownNow() + scheduler = null + } + + fun warnMissingPassword() { + if (!warnedNoPassword) { + warnedNoPassword = true + println("No Website Password Set So Website Integration Tasks Stopped") + } + } + + fun sendGetRequest(endpoint: String, params: Map): Boolean { + val baseUrl = Constants.WEBSITE_LINK.trimEnd('/') + if (baseUrl.isEmpty()) return false + + val query = params.entries.joinToString("&") { (key, value) -> + val encodedValue = URLEncoder.encode(value.toString(), StandardCharsets.UTF_8.name()) + "$key=$encodedValue" + } + + val fullUrl = "$baseUrl/$endpoint?$query" + return try { + val url = URL(fullUrl) + val connection = url.openConnection() as? HttpURLConnection ?: return false + connection.apply { + requestMethod = "GET" + connectTimeout = 5000 + readTimeout = 5000 + setRequestProperty("User-Agent", "2006Scape-Server") + inputStream.use { it.readBytes() } + } + true + } catch (e: Exception) { + if (Constants.SERVER_DEBUG) { + e.printStackTrace() + } else { + System.err.println("[WebsiteIntegration] Failed to update $endpoint: ${e.message}") + } + false + } + } +} diff --git a/2006Scape Server/plugins/plugin/integration/website/WebsiteShutdownSubscriber.kt b/2006Scape Server/plugins/plugin/integration/website/WebsiteShutdownSubscriber.kt new file mode 100644 index 00000000..1db29a43 --- /dev/null +++ b/2006Scape Server/plugins/plugin/integration/website/WebsiteShutdownSubscriber.kt @@ -0,0 +1,17 @@ +package plugin.integration.website + +import com.rs2.event.EventContext +import com.rs2.event.EventSubscriber +import com.rs2.event.SubscribesTo +import com.rs2.event.impl.ServerShutdownEvent +import com.rs2.game.players.Player + +@SubscribesTo(ServerShutdownEvent::class) +class WebsiteShutdownSubscriber : EventSubscriber { + + override fun subscribe(context: EventContext, player: Player?, event: ServerShutdownEvent) { + PlayersOnlineWebsite.stop() + RegisteredAccsWebsite.stop() + WebsiteService.stop() + } +} diff --git a/2006Scape Server/plugins/plugin/integration/website/WebsiteStartupSubscriber.kt b/2006Scape Server/plugins/plugin/integration/website/WebsiteStartupSubscriber.kt new file mode 100644 index 00000000..2d689b88 --- /dev/null +++ b/2006Scape Server/plugins/plugin/integration/website/WebsiteStartupSubscriber.kt @@ -0,0 +1,16 @@ +package plugin.integration.website + +import com.rs2.event.EventContext +import com.rs2.event.EventSubscriber +import com.rs2.event.SubscribesTo +import com.rs2.event.impl.ServerStartupEvent +import com.rs2.game.players.Player + +@SubscribesTo(ServerStartupEvent::class) +class WebsiteStartupSubscriber : EventSubscriber { + + override fun subscribe(context: EventContext, player: Player?, event: ServerStartupEvent) { + PlayersOnlineWebsite.start() + RegisteredAccsWebsite.start() + } +} diff --git a/2006Scape Server/src/main/java/com/rs2/ConfigLoader.java b/2006Scape Server/src/main/java/com/rs2/ConfigLoader.java index 0d55e03a..fa25f291 100644 --- a/2006Scape Server/src/main/java/com/rs2/ConfigLoader.java +++ b/2006Scape Server/src/main/java/com/rs2/ConfigLoader.java @@ -2,8 +2,6 @@ package com.rs2; import com.rs2.event.impl.ServerConfigEvent; import com.rs2.game.players.Player; -import com.rs2.integrations.PlayersOnlineWebsite; -import com.rs2.integrations.RegisteredAccsWebsite; import org.json.JSONArray; import org.json.JSONObject; @@ -89,16 +87,13 @@ public class ConfigLoader { } } } - bindBoolean(core, "website_integration", val -> Constants.WEBSITE_INTEGRATION = val); bindBoolean(core, "cycle_logging", val -> Constants.CYCLE_LOGGING = val); bindInt(core, "cycle_logging_tick", val -> Constants.CYCLE_LOGGING_TICK = val); } private static void initialize() { JSONObject main = new JSONObject(); - main - .put("websitepass", "") - .put("erssecret", ""); + main.put("erssecret", ""); try (BufferedWriter br = new BufferedWriter(new FileWriter("data/secrets.json"))) { br.write(main.toString()); } catch (IOException e) { @@ -109,8 +104,6 @@ public class ConfigLoader { public static void loadSecrets() throws IOException { if (!new File("data/secrets.json").exists()) { initialize(); - System.out.println("Please open \"data/secrets.json\" file and enter your Website Password there!"); - } else { String out = new String(Files.readAllBytes(Paths.get("data/secrets.json")), StandardCharsets.UTF_8); JSONObject obj = new JSONObject(out); @@ -118,11 +111,6 @@ public class ConfigLoader { /* * Sets External Services Vars */ - if (obj.has("websitepass")) { - String pass = obj.getString("websitepass"); - PlayersOnlineWebsite.password = pass; - RegisteredAccsWebsite.password = pass; - } if (obj.has("erssecret")) GameEngine.ersSecret = obj.getString("erssecret"); diff --git a/2006Scape Server/src/main/java/com/rs2/Constants.java b/2006Scape Server/src/main/java/com/rs2/Constants.java index 1cca2d30..23c50574 100644 --- a/2006Scape Server/src/main/java/com/rs2/Constants.java +++ b/2006Scape Server/src/main/java/com/rs2/Constants.java @@ -36,14 +36,13 @@ public class Constants { * VARIABLE_XP_RATE Allows Players To Choose An XP Rate Set In VARIABLE_XP_RATES * VARIABLE_XP_RATES Defines The XP Rates That Should Be Available To Players When VARIABLE_XP_RATES is true(Array Must Contain Four Entries) * XP_RATE Sets The XP Rate Multiplier For All Players/Skills If VARIABLE_XP_RATES is false - * WEBSITE_INTEGRATION Enables/Disables Website Features(Total Accounts Registered & Players Online) */ public static String SERVER_NAME = "2006Scape", WEBSITE_LINK = "https://2006Scape.org"; public static int WORLD = 1, HTTP_PORT = 8080, JAGGRAB_PORT = 43595, MAX_PLAYERS = 200, SAVE_TIMER = 120, TIMEOUT = 60, RESPAWN_X = 3222, RESPAWN_Y = 3218, CYCLE_LOGGING_TICK = 10; public static boolean GUI_ENABLED = false, FILE_SERVER = true, SERVER_DEBUG = false, MEMBERS_ONLY = false, TUTORIAL_ISLAND = false, PARTY_ROOM_DISABLED = false, CLUES_ENABLED = true, ITEM_REQUIREMENTS = true, ADMIN_CAN_TRADE = false, ADMIN_DROP_ITEMS = false, ADMIN_CAN_SELL_ITEMS = false, VARIABLE_XP_RATE = false, - WEBSITE_INTEGRATION = false, CYCLE_LOGGING = true; + CYCLE_LOGGING = true; public static int[] VARIABLE_XP_RATES = new int[] {1, 2, 5, 10}; public static double TEST_VERSION = 2.3, XP_RATE = 1.0; diff --git a/2006Scape Server/src/main/java/com/rs2/GameEngine.java b/2006Scape Server/src/main/java/com/rs2/GameEngine.java index 5f78772b..f5b5b029 100644 --- a/2006Scape Server/src/main/java/com/rs2/GameEngine.java +++ b/2006Scape Server/src/main/java/com/rs2/GameEngine.java @@ -43,8 +43,6 @@ import com.rs2.game.players.Player; import com.rs2.game.players.PlayerHandler; import com.rs2.game.players.PlayerSave; import com.rs2.game.shops.ShopHandler; -import com.rs2.integrations.PlayersOnlineWebsite; -import com.rs2.integrations.RegisteredAccsWebsite; import com.rs2.util.HostBlacklist; import com.rs2.world.GlobalDropsHandler; import com.rs2.world.ItemHandler; @@ -305,13 +303,6 @@ public class GameEngine { CycleEventHandler.getSingleton().process(); long durationCycleEventHandler = System.currentTimeMillis() - startCycleEventHandler; checkAndLogDuration("CycleEventHandler", durationCycleEventHandler); - long startIntegrationEvents = System.currentTimeMillis(); - if (Constants.WEBSITE_INTEGRATION) { - PlayersOnlineWebsite.addUpdatePlayersOnlineTask(); - RegisteredAccsWebsite.addUpdateRegisteredUsersTask(); - } - long durationIntegrationEvents = System.currentTimeMillis() - startIntegrationEvents; - checkAndLogDuration("IntegrationEvents", durationIntegrationEvents); long startSaveEvents = System.currentTimeMillis(); if (System.currentTimeMillis() - lastMassSave > 300000) { for (Player p : PlayerHandler.players) { diff --git a/2006Scape Server/src/main/java/com/rs2/integrations/PlayersOnlineWebsite.java b/2006Scape Server/src/main/java/com/rs2/integrations/PlayersOnlineWebsite.java deleted file mode 100644 index 9c515774..00000000 --- a/2006Scape Server/src/main/java/com/rs2/integrations/PlayersOnlineWebsite.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.rs2.integrations; - -import java.io.IOException; -import java.net.URL; - -import com.rs2.Constants; -import com.rs2.game.players.PlayerHandler; - -public class PlayersOnlineWebsite { - - public static String password; - private static boolean hasntwared = true; - - private static void setWebsitePlayersOnline(int amount) throws IOException { - URL url; - url = new URL(Constants.WEBSITE_LINK + "/playersonline.php?pass=" + password + "&amount=" + amount + "&world=" + Constants.WORLD); - url.openStream().close(); - } - - private static int count = 50; - public static void addUpdatePlayersOnlineTask() { - if (password != null && !password.equals("")) { - if (count == 0) { - try { - setWebsitePlayersOnline(PlayerHandler.getPlayerCount()); - count = 50; - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } else { - count--; - } - } else if (hasntwared) { - hasntwared = false; - System.out.println("No Website Password Set So Website Integration Tasks Stopped"); - } - } -} diff --git a/2006Scape Server/src/main/java/com/rs2/integrations/RegisteredAccsWebsite.java b/2006Scape Server/src/main/java/com/rs2/integrations/RegisteredAccsWebsite.java deleted file mode 100644 index f86455a1..00000000 --- a/2006Scape Server/src/main/java/com/rs2/integrations/RegisteredAccsWebsite.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.rs2.integrations; - -import java.io.File; -import java.io.IOException; -import java.net.URL; - -import com.rs2.Constants; - -public class RegisteredAccsWebsite { - public static String password; - private static boolean hasntwarned = true; - - private static void setAccountsRegistered(int amount) throws IOException { - URL url; - url = new URL(Constants.WEBSITE_LINK + "/accountsregistered.php?pass=" + password + "&amount=" + amount + "&world=" + Constants.WORLD); - url.openStream().close(); - } - - private static int count = 25; - public static void addUpdateRegisteredUsersTask() { - if (password != null && !password.equals("")) { - if (count == 0) { - try { - setAccountsRegistered(new File("data/characters/").list().length); - count = 150; - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } else { - count--; - } - } else if (hasntwarned) { - hasntwarned = false; - } - } -}