Browse Source

social

master
gsd 2 years ago
parent
commit
8cc6a642b3
  1. 6
      pom.xml
  2. 4
      src/main/java/app/entities/Stats.java
  3. 4
      src/main/java/app/entities/server/Server.java
  4. 2
      src/main/java/app/updates/BanCountUpdater.java
  5. 67
      src/main/java/app/updates/SocialUpdater.java
  6. 54
      src/main/java/app/updates/VipCountUpdater.java
  7. 6
      src/main/resources/application.yaml
  8. 20
      src/test/java/app/updates/VkSocialGetter.java

6
pom.xml

@ -92,5 +92,11 @@
<version>1.9.19</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

4
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<String, Integer> countries = new HashMap<>();
HashMap<String, Server> servers = new HashMap<>();

4
src/main/java/app/entities/server/Server.java

@ -72,7 +72,9 @@ public class Server {
}
public void UpdatePlayersFromRCON(String response) {
List<String> 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<String> 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) {

2
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);
}
}

67
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("<h3 class=\"slim_header\">") + "<h3 class=\"slim_header\">".length();
int k_end = response.indexOf("<", k_start);
count += Integer.valueOf(response.substring(k_start, k_end)) * 1000;
int s_start = response.indexOf("</span>", k_end) + "</span>".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);
}
}

54
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);
}
}

6
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:

20
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("<h3 class=\"slim_header\">") + "<h3 class=\"slim_header\">".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("</span>", k_end) + "</span>".length();
int s_end = response.indexOf(" ", s_start);
count += Integer.valueOf(response.substring(s_start, s_end));
System.out.print(count);
}
}
Loading…
Cancel
Save