From 8cc6a642b341377bbedf6a8d0cf333eea5a0143b Mon Sep 17 00:00:00 2001 From: gsd Date: Mon, 13 Feb 2023 20:41:46 +0300 Subject: [PATCH] social --- pom.xml | 6 ++ src/main/java/app/entities/Stats.java | 4 +- src/main/java/app/entities/server/Server.java | 4 +- .../java/app/updates/BanCountUpdater.java | 2 +- src/main/java/app/updates/SocialUpdater.java | 67 +++++++++++++++++++ .../java/app/updates/VipCountUpdater.java | 54 +++++++++++++++ src/main/resources/application.yaml | 6 +- src/test/java/app/updates/VkSocialGetter.java | 20 ++++++ 8 files changed, 158 insertions(+), 5 deletions(-) create mode 100644 src/main/java/app/updates/SocialUpdater.java create mode 100644 src/main/java/app/updates/VipCountUpdater.java create mode 100644 src/test/java/app/updates/VkSocialGetter.java diff --git a/pom.xml b/pom.xml index 33e9e34..8af947e 100644 --- a/pom.xml +++ b/pom.xml @@ -92,5 +92,11 @@ 1.9.19 compile + + junit + junit + 4.13.2 + test + \ No newline at end of file diff --git a/src/main/java/app/entities/Stats.java b/src/main/java/app/entities/Stats.java index 6e92473..b8828ec 100644 --- a/src/main/java/app/entities/Stats.java +++ b/src/main/java/app/entities/Stats.java @@ -31,9 +31,9 @@ import java.util.concurrent.CompletionException; public class Stats { long ban_count = 0; int discord_users = 0; - int freevip_players = 0; + long freevip_players = 0; int server_uptime = 0; - int vip_players = 0; + long vip_players = 0; int vk_users = 0; HashMap countries = new HashMap<>(); HashMap servers = new HashMap<>(); diff --git a/src/main/java/app/entities/server/Server.java b/src/main/java/app/entities/server/Server.java index 5c3f38d..57c23bb 100644 --- a/src/main/java/app/entities/server/Server.java +++ b/src/main/java/app/entities/server/Server.java @@ -72,7 +72,9 @@ public class Server { } public void UpdatePlayersFromRCON(String response) { - List players_list = Arrays.stream(response.substring(response.indexOf("# userid"), response.length()).split("\n")).toList(); + int start_index = response.indexOf("# userid"); + if (start_index == -1) return; + List players_list = Arrays.stream(response.substring(start_index, response.length()).split("\n")).toList(); boolean skip_table_header = true; for(String player_text: players_list) { if (skip_table_header || player_text.length() < 1) { diff --git a/src/main/java/app/updates/BanCountUpdater.java b/src/main/java/app/updates/BanCountUpdater.java index c75c584..a9952a6 100644 --- a/src/main/java/app/updates/BanCountUpdater.java +++ b/src/main/java/app/updates/BanCountUpdater.java @@ -36,7 +36,7 @@ public class BanCountUpdater { } public void UpdateBanCount(){ - stats.setBan_count((Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `light_bans`").getSingleResult()); + stats.setBan_count((Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `light_bans` WHERE active = 1").getSingleResult()); stats.getUpdates().merge("ban_count", Instant.now().getEpochSecond(), (x, y) -> y); } } diff --git a/src/main/java/app/updates/SocialUpdater.java b/src/main/java/app/updates/SocialUpdater.java new file mode 100644 index 0000000..28f1ec1 --- /dev/null +++ b/src/main/java/app/updates/SocialUpdater.java @@ -0,0 +1,67 @@ +package app.updates; + +import app.entities.Stats; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.annotation.PostConstruct; +import org.jobrunr.scheduling.JobScheduler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.time.Instant; + +@Component +public class SocialUpdater { + RestTemplate restTemplate; + ObjectMapper objectMapper; + Stats stats; + JobScheduler jobScheduler; + + @Value("${backend.social.discord}") + private String discord_url = ""; + + @Value("${backend.social.vk}") + private String vk_url; + + @Autowired + public SocialUpdater(Stats stats, + JobScheduler jobScheduler) { + restTemplate = new RestTemplate(); + objectMapper = new ObjectMapper(); + this.stats = stats; + this.jobScheduler = jobScheduler; + } + + @PostConstruct + public void SetUpdater(){ + if(!discord_url.isEmpty()) { + jobScheduler.enqueue(() -> UpdateDiscordCount()); + jobScheduler.scheduleRecurrently("*/5 * * * *", () -> UpdateDiscordCount()); + jobScheduler.enqueue(() -> UpdateVKCount()); + jobScheduler.scheduleRecurrently("*/5 * * * *", () -> UpdateVKCount()); + } + } + + public void UpdateDiscordCount() throws IOException { + stats.setDiscord_users(objectMapper.readTree(new URL(discord_url)).get("approximate_member_count").asInt()); + stats.getUpdates().merge("discord_count", Instant.now().getEpochSecond(), (x, y) -> y); + } + + public void UpdateVKCount() { + int count = 0; + String response = restTemplate.getForEntity(vk_url, String.class).getBody(); + int k_start = response.indexOf("

") + "

".length(); + int k_end = response.indexOf("<", k_start); + count += Integer.valueOf(response.substring(k_start, k_end)) * 1000; + int s_start = response.indexOf("", k_end) + "".length(); + int s_end = response.indexOf(" ", s_start); + count += Integer.valueOf(response.substring(s_start, s_end)); + stats.setVk_users(count); + stats.getUpdates().merge("vk_count", Instant.now().getEpochSecond(), (x, y) -> y); + } +} diff --git a/src/main/java/app/updates/VipCountUpdater.java b/src/main/java/app/updates/VipCountUpdater.java new file mode 100644 index 0000000..c767239 --- /dev/null +++ b/src/main/java/app/updates/VipCountUpdater.java @@ -0,0 +1,54 @@ +package app.updates; + +import app.entities.Stats; +import jakarta.annotation.PostConstruct; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.jobrunr.scheduling.JobScheduler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.time.Instant; + +@Component +public class VipCountUpdater { + private Stats stats; + private JobScheduler jobScheduler; + @PersistenceContext + EntityManager entityManager; + + @Value("${backend.updates.vip_count}") + boolean update = false; + + @Autowired + public VipCountUpdater(Stats stats, + JobScheduler jobScheduler) { + this.stats = stats; + this.jobScheduler = jobScheduler; + } + + @PostConstruct + public void SetUpdater(){ + if(update) { + jobScheduler.enqueue(() -> UpdateFreeVIPCount()); + jobScheduler.enqueue(() -> UpdateVIPCount()); + jobScheduler.scheduleRecurrently("*/5 * * * *", () -> UpdateFreeVIPCount()); + jobScheduler.scheduleRecurrently("*/5 * * * *", () -> UpdateVIPCount()); + } + } + + public void UpdateVIPCount() { + stats.setVip_players( + (Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `sm_admins` WHERE `status` LIKE 'VIP' AND (`comment` LIKE 'Donate.User' OR `comment` LIKE 'f13bot.User')") + .getSingleResult()); + stats.getUpdates().merge("vip_count", Instant.now().getEpochSecond(), (x, y) -> y); + } + + public void UpdateFreeVIPCount() { + stats.setFreevip_players( + (Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `sm_admins` WHERE `status` LIKE 'VIP' AND `comment` LIKE 'f13bot.FreeVIP'") + .getSingleResult()); + stats.getUpdates().merge("freevip_count", Instant.now().getEpochSecond(), (x, y) -> y); + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 932c208..8e1cca8 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -12,7 +12,7 @@ spring: jpa: hibernate: ddl-auto: none - show-sql: true + show-sql: ${SHOW_SQL} org: jobrunr: @@ -28,12 +28,16 @@ backend: unique_global: ${UNIQUE_GLOBAL} unique_server: ${UNIQUE_SERVER} ban_count: ${BAN_COUNT} + vip_count: true a2s: ${A2S} countries: ${COUNTRIES} auth: salt: ${AUTH_SALT} steam_api_key: ${STEAM_WEBAPI_KEY} response_redirect: ${AUTH_REDIRECT} + social: + discord: ${DISCORD_INVITE_URL} + vk: ${VK_URL} logging: level: diff --git a/src/test/java/app/updates/VkSocialGetter.java b/src/test/java/app/updates/VkSocialGetter.java new file mode 100644 index 0000000..ff4418c --- /dev/null +++ b/src/test/java/app/updates/VkSocialGetter.java @@ -0,0 +1,20 @@ +package app.updates; + +import org.junit.Test; +import org.springframework.web.client.RestTemplate; + +public class VkSocialGetter { + @Test + public void Check() { + int count = 0; + String response = new RestTemplate().getForEntity("https://m.vk.com/facti13?act=members", String.class).getBody(); + int k_start = response.indexOf("

") + "

".length(); + int k_end = response.indexOf("<", k_start); + System.out.printf("%d %d\n", k_start, k_end); + count += Integer.valueOf(response.substring(k_start, k_end)) * 1000; + int s_start = response.indexOf("", k_end) + "".length(); + int s_end = response.indexOf(" ", s_start); + count += Integer.valueOf(response.substring(s_start, s_end)); + System.out.print(count); + } +}