mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-12 13:24:36 +01:00
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:
@@ -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();
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user