Files
PIA-Servers/src/main/java/cc/fascinated/piaservers/readme/ReadMeManager.java

51 lines
1.9 KiB
Java
Raw Normal View History

2024-04-27 05:49:10 +01:00
package cc.fascinated.piaservers.readme;
import cc.fascinated.piaservers.Main;
2024-04-27 05:52:58 +01:00
import cc.fascinated.piaservers.model.PiaServer;
2024-04-27 05:49:10 +01:00
import cc.fascinated.piaservers.pia.PiaManager;
import lombok.SneakyThrows;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Date;
2024-04-27 05:52:58 +01:00
import java.util.HashMap;
2024-04-27 05:49:10 +01:00
import java.util.List;
2024-04-27 05:52:58 +01:00
import java.util.Map;
2024-04-27 05:49:10 +01:00
public class ReadMeManager {
@SneakyThrows
public ReadMeManager() {
InputStream readmeStream = Main.class.getResourceAsStream("/README.md");
if (readmeStream == null) {
System.out.println("Failed to find README.md");
return;
}
File readmeFile = new File("README.md");
2024-04-27 05:52:58 +01:00
if (!readmeFile.exists()) { // Create the file if it doesn't exist
2024-04-27 05:49:10 +01:00
readmeFile.createNewFile();
}
2024-04-27 05:52:58 +01:00
// Get the contents of the README.md
2024-04-27 05:49:10 +01:00
String contents = new String(readmeStream.readAllBytes());
2024-04-27 05:52:58 +01:00
// Replace the placeholders in the README.md file
2024-04-27 05:49:10 +01:00
contents = contents.replace("{server_count}", String.valueOf(PiaManager.SERVERS.size()));
contents = contents.replace("{last_update}", new Date().toString().replaceAll(" ", "_"));
2024-04-27 05:52:58 +01:00
// Write total servers per-region
Map<String, Integer> regionCounts = new HashMap<>();
for (PiaServer server : PiaManager.SERVERS) {
String region = server.getRegion();
regionCounts.put(region, regionCounts.getOrDefault(region, 0) + 1);
}
contents = contents.replace("{server_table}", regionCounts.entrySet().stream()
.map(entry -> "| " + entry.getKey() + " | " + entry.getValue() + " |") // Map the region to the count
.reduce((a, b) -> a + "\n" + b).orElse("")); // Reduce the entries to a single string
2024-04-27 05:49:10 +01:00
Files.write(readmeFile.toPath(), contents.getBytes());
}
}