Skip to content

Commit

Permalink
added server templates and server class with mongodb integration
Browse files Browse the repository at this point in the history
  • Loading branch information
ezTxmMC committed Jan 2, 2025
1 parent 4c91d5b commit f9ea9f5
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.twoweeksmc.lobbyserver.database;

import java.time.Instant;
import java.util.UUID;

import org.bson.Document;
Expand All @@ -9,6 +10,7 @@
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.twoweeksmc.lobbyserver.server.Server;

import de.eztxm.config.JsonConfig;

Expand All @@ -27,6 +29,20 @@ public MongoDatabaseProcessor(JsonConfig databaseConfiguration) {
this.playerCollection = this.mongoDatabase.getCollection("players");
}

public boolean addServer(UUID ownerId, Server server) {
if (this.getServer(ownerId) != null) {
return true;
}
Document document = new Document();
document.put("ownerId", ownerId);
document.put("start", Instant.now());
document.put("weeks", server.getWeeks());
document.put("max-players", server.getMaxPlayers());
document.put("max-memory", server.getMaxMemory());
document.put("plugins", server.getPlugins());
return this.serverCollection.insertOne(document).wasAcknowledged();
}

public boolean addPlayer(UUID uuid) {
if (this.getPlayer(uuid) != null) {
return true;
Expand All @@ -43,6 +59,10 @@ public Document getPlayer(UUID uuid) {
return this.playerCollection.find(Filters.eq("uuid", uuid.toString())).first();
}

public Document getServer(UUID ownerId) {
return this.serverCollection.find(Filters.eq("ownerId", ownerId.toString())).first();
}

public MongoClient getMongoClient() {
return mongoClient;
}
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/twoweeksmc/lobbyserver/server/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.twoweeksmc.lobbyserver.server;

import java.util.List;

public class Server {
private final int weeks;
private final int maxPlayers;
private final int maxMemory;
private final List<String> plugins;

public Server(int weeks, int maxPlayers, int maxMemory, List<String> plugins) {
this.weeks = weeks;
this.maxPlayers = maxPlayers;
this.maxMemory = maxMemory;
this.plugins = plugins;
}

public static Server getFromTemplate(ServerTemplate template) {
return new Server(template.getWeeks(), template.getMaxPlayers(), template.getMaxMemory(),
template.getPlugins());
}

public List<String> getPlugins() {
return plugins;
}

public int getMaxMemory() {
return maxMemory;
}

public int getMaxPlayers() {
return maxPlayers;
}

public int getWeeks() {
return weeks;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.twoweeksmc.lobbyserver.server;

import java.util.ArrayList;
import java.util.List;

public enum ServerTemplate {
DEFAULT_SURVIVAL(2, 4, 2048, new ArrayList<>(List.of("2weeksmc-core"))),
DEFAULT_BUILDING(2, 2, 2048, new ArrayList<>(List.of(
"2weeksmc-core", "fastasyncworldedit", "voxelsniper"))),
PREMIUM_SURVIVAL(4, 4, 4096, new ArrayList<>(List.of(
"2weeksmc-core", "2weeksmc-survival-premium"))),
PREMIUM_BUILDING(4, 4, 4096, new ArrayList<>(List.of(
"2weeksmc-core", "fastasyncworldedit", "voxelsniper", "gobrush", "gopaint"))),
ENTERPRISE_SURVIVAL(8, 8, 6144, new ArrayList<>(List.of(
"2weeksmc-core",
"2weeksmc-survival-enterprise"))),
ENTERPRISE_BUILDING(8, 8,
6144, new ArrayList<>(List.of(
"2weeksmc-core", "fastasyncworldedit", "voxelsniper", "gobrush", "gopaint", "craftflowers",
"armostandtools")));

private final int weeks;
private final int maxPlayers;
private final int maxMemory;
private final List<String> plugins;

ServerTemplate(int weeks, int maxPlayers, int maxMemory, List<String> plugins) {
this.weeks = weeks;
this.maxPlayers = maxPlayers;
this.maxMemory = maxMemory;
this.plugins = plugins;
}

public List<String> getPlugins() {
return plugins;
}

public int getMaxMemory() {
return maxMemory;
}

public int getMaxPlayers() {
return maxPlayers;
}

public int getWeeks() {
return weeks;
}
}

0 comments on commit f9ea9f5

Please sign in to comment.