Skip to content

Commit

Permalink
add http status request and display html (Melledy#86)
Browse files Browse the repository at this point in the history
* add http status request

* add maxplayer value

* add http status request

* add http status request

* maxPlayers limit

* maxPlayers limit
  • Loading branch information
yunochan authored Jun 22, 2024
1 parent 53aa035 commit ce9953c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/emu/lunarcore/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static class ServerOptions {
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();

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/emu/lunarcore/server/http/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ private void addRoutes() {

// Fallback handler
getApp().error(404, this::notFoundHandler);
getApp().get("/status/server", new StatusServerHandler()::handle);
}

private void addDispatchRoutes() {
Expand Down Expand Up @@ -199,9 +200,9 @@ private void addGateServerRoutes() {
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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public void handle(GameSession session, byte[] data) throws Exception {
// 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());

Expand Down

0 comments on commit ce9953c

Please sign in to comment.