add http status request and display html (#86)

* add http status request

* add maxplayer value

* add http status request

* add http status request

* maxPlayers limit

* maxPlayers limit
This commit is contained in:
yunochan
2024-06-22 22:24:03 +08:00
committed by GitHub
parent 53aa035bb5
commit ce9953ceca
4 changed files with 38 additions and 3 deletions

View File

@@ -131,7 +131,7 @@ public class Config {
public boolean autoUpgradeWorldLevel = true; // Automatically upgrades world level when the player reaches a certain TB level
public String language = "EN";
public Set<String> defaultPermissions = Set.of("*");
public int maxPlayers = -1;
public ServerProfile serverFriendInfo = new ServerProfile();
public WelcomeMail welcomeMail = new WelcomeMail();

View File

@@ -135,6 +135,7 @@ public class HttpServer {
// Fallback handler
getApp().error(404, this::notFoundHandler);
getApp().get("/status/server", new StatusServerHandler()::handle);
}
private void addDispatchRoutes() {
@@ -199,9 +200,9 @@ public class HttpServer {
this.modes.add("GATESERVER");
}
private void notFoundHandler(Context ctx) {
private void notFoundHandler(Context ctx) {
ctx.status(404);
ctx.contentType(ContentType.TEXT_PLAIN);
ctx.result("not found");
}
}
}

View File

@@ -0,0 +1,26 @@
package emu.lunarcore.server.http.handlers;
import org.jetbrains.annotations.NotNull;
import emu.lunarcore.GameConstants;
import emu.lunarcore.LunarCore;
import io.javalin.http.Context;
import io.javalin.http.Handler;
public class StatusServerHandler implements Handler {
@Override
public void handle(@NotNull Context ctx) throws Exception {
int playerCount = LunarCore.getGameServer().getPlayerCount();
int maxPlayers = LunarCore.getConfig().getServerOptions().maxPlayers;
String version = GameConstants.VERSION;
String responseJson = String.format(
"{\"retcode\":0,\"status\":{\"playerCount\":%d,\"maxPlayers\":%d,\"version\":\"%s\"}}",
playerCount, maxPlayers, version
);
ctx.status(200);
ctx.contentType("application/json");
ctx.result(responseJson);
}
}

View File

@@ -28,6 +28,14 @@ public class HandlerPlayerGetTokenCsReq extends PacketHandler {
// Set account object for session
session.setAccount(account);
// If playerCount reach the set maxPlayers, newly logged-in players will be kicked out
int maxPlayers = LunarCore.getConfig().getServerOptions().maxPlayers;
int playerCount = LunarCore.getGameServer().getPlayerCount();
if (maxPlayers > -1 && playerCount >= maxPlayers) {
session.close();
return;
}
// Get player from database, if it doesnt exist, we create it
Player player = LunarCore.getGameDatabase().getObjectByField(Player.class, "accountUid", account.getUid());