Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions api/src/main/java/brainwine/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static brainwine.shared.LogMarkers.SERVER_MARKER;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import brainwine.api.config.BetaEntry;
Expand All @@ -11,13 +13,16 @@

import brainwine.api.config.ApiConfig;
import brainwine.api.config.NewsEntry;
import brainwine.api.config.SslConfig;
import brainwine.shared.JsonHelper;
import io.javalin.core.LoomUtil;

public class Api {

private static final Logger logger = LogManager.getLogger();
private final ApiConfig config;
private final List<NewsEntry> news;
private final BetaEntry beta;
private final DataFetcher dataFetcher;
private final GatewayService gatewayService;
private final PortalService portalService;
Expand All @@ -33,6 +38,10 @@ public Api(DataFetcher dataFetcher) {
logger.info(SERVER_MARKER, "Using data fetcher {}", dataFetcher.getClass().getName());
logger.info(SERVER_MARKER, "Loading configuration ...");
config = loadConfig();
logger.info(SERVER_MARKER, "Is SSL enabled? {}", config.getSslConfig().isSslEnabled() ? "Yes" : "No");
news = new ArrayList<>(config.getNews()); // Explicit copy
Collections.reverse(news);
beta = config.getBeta();
LoomUtil.useLoomThreadPool = false;
gatewayService = new GatewayService(this, config.getGatewayPort());
portalService = new PortalService(this, config.getPortalPort());
Expand All @@ -55,7 +64,9 @@ private ApiConfig loadConfig() {
return ApiConfig.DEFAULT_CONFIG;
}

return JsonHelper.readValue(file, ApiConfig.class);
ApiConfig config = JsonHelper.readValue(file, ApiConfig.class);
JsonHelper.writeValue(file, config);
return config;
} catch (Exception e) {
logger.fatal(SERVER_MARKER, "Failed to load configuration", e);
System.exit(-1);
Expand All @@ -69,17 +80,21 @@ public void broadcast(String type, Object data) {
}

public List<NewsEntry> getNews() {
return config.getNews();
return news;
}

public BetaEntry getBeta() {
return config.getBeta();
return beta;
}

public String getGameServerHost() {
return config.getGameServerIp() + ":" + config.getGameServerPort();
}

public SslConfig getSslConfig() {
return config.getSslConfig();
}

public DataFetcher getDataFetcher() {
return dataFetcher;
}
Expand Down
15 changes: 12 additions & 3 deletions api/src/main/java/brainwine/api/GatewayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import brainwine.api.config.SslConfig;
import brainwine.api.models.PlayersRequest;
import brainwine.api.models.ServerConnectInfo;
import brainwine.api.models.SessionsRequest;

import brainwine.api.util.JettyUtils;
import brainwine.shared.JsonHelper;
import io.javalin.Javalin;
import io.javalin.http.Context;
Expand All @@ -37,8 +39,15 @@ public class GatewayService {
public GatewayService(Api api, int port) {
this.api = api;
this.dataFetcher = api.getDataFetcher();
logger.info(SERVER_MARKER, "Starting GatewayService @ port {} ...", port);
gateway = Javalin.create(config -> config.jsonMapper(new JavalinJackson(JsonHelper.MAPPER)))
logger.info(SERVER_MARKER, "Starting GatewayService @ port {} ...", port);
SslConfig ssl = api.getSslConfig();
gateway = Javalin.create(config -> {
config.jsonMapper(new JavalinJackson(JsonHelper.MAPPER));

if(ssl.isSslEnabled()) {
config.server(() -> JettyUtils.createJettyServerWithSsl(port, ssl.getKeyStorePath(), ssl.getKeyStorePassword()));
}
})
.exception(Exception.class, this::handleException)
.get("/clients", this::handleNewsRequest)
.get("/players", this::handlePlayerSearch)
Expand Down Expand Up @@ -194,7 +203,7 @@ private void handlePlayerSearch(Context ctx) {
int toIndex = page * playerSearchPageSize;
ctx.json(players.subList(fromIndex < 0 ? 0 : fromIndex > players.size() ? players.size() : fromIndex, toIndex > players.size() ? players.size() : toIndex));
}

/**
* Handler function for registering a new account.
*/
Expand Down
11 changes: 10 additions & 1 deletion api/src/main/java/brainwine/api/PortalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import brainwine.api.config.SslConfig;
import brainwine.api.models.ZoneInfo;
import brainwine.api.util.ImageUtils;
import brainwine.api.util.JettyUtils;
import brainwine.shared.JsonHelper;
import io.javalin.Javalin;
import io.javalin.http.ContentType;
Expand All @@ -45,7 +47,14 @@ public class PortalService {
public PortalService(Api api, int port) {
this.dataFetcher = api.getDataFetcher();
logger.info(SERVER_MARKER, "Starting PortalService @ port {} ...", port);
portal = Javalin.create(config -> config.jsonMapper(new JavalinJackson(JsonHelper.MAPPER)))
SslConfig ssl = api.getSslConfig();
portal = Javalin.create(config -> {
config.jsonMapper(new JavalinJackson(JsonHelper.MAPPER));

if(ssl.isSslEnabled()) {
config.server(() -> JettyUtils.createJettyServerWithSsl(port, ssl.getKeyStorePath(), ssl.getKeyStorePassword()));
}
})
.exception(Exception.class, this::handleException)
.get("/v1/map/{zone}", this::handleMapRequest)
.get("/v1/worlds", this::handleZoneSearch)
Expand Down
16 changes: 12 additions & 4 deletions api/src/main/java/brainwine/api/config/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@
import java.util.Collections;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ApiConfig {

public static final ApiConfig DEFAULT_CONFIG = new ApiConfig("127.0.0.1", 5002, 5001, 5003, Arrays.asList(NewsEntry.DEFAULT_NEWS), new BetaEntry());
public static final ApiConfig DEFAULT_CONFIG = new ApiConfig("127.0.0.1", 5002, 5001, 5003, SslConfig.DEFAULT_CONFIG, Arrays.asList(NewsEntry.DEFAULT_NEWS), new BetaEntry());
private final String gameServerIp;
private final int gameServerPort;
private final int gatewayPort;
private final int portalPort;
private final SslConfig sslConfig;
private final List<NewsEntry> news;
private final BetaEntry beta;
@ConstructorProperties({"game_server_ip", "game_server_port", "gateway_port", "portal_port", "news", "beta"})
public ApiConfig(String gameServerIp, int gameServerPort, int gatewayPort, int portalPort, List<NewsEntry> news, BetaEntry beta) {

@ConstructorProperties({"game_server_ip", "game_server_port", "gateway_port", "portal_port", "ssl", "news", "beta"})
public ApiConfig(String gameServerIp, int gameServerPort, int gatewayPort, int portalPort, SslConfig sslConfig, List<NewsEntry> news, BetaEntry beta) {
this.gameServerIp = gameServerIp;
this.gameServerPort = gameServerPort;
this.gatewayPort = gatewayPort;
this.portalPort = portalPort;
this.sslConfig = sslConfig == null ? SslConfig.DEFAULT_CONFIG : sslConfig;
this.news = news;
this.beta = beta;
Collections.reverse(this.news);
Expand All @@ -45,6 +48,11 @@ public int getPortalPort() {
return portalPort;
}

@JsonGetter("ssl")
public SslConfig getSslConfig() {
return sslConfig;
}

public List<NewsEntry> getNews() {
return news;
}
Expand Down
37 changes: 37 additions & 0 deletions api/src/main/java/brainwine/api/config/SslConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package brainwine.api.config;

import java.beans.ConstructorProperties;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class SslConfig {

public static final SslConfig DEFAULT_CONFIG = new SslConfig(false, "./keystore", "password");
private final boolean enableSsl;
private final String keyStorePath;
private final String keyStorePassword;

@ConstructorProperties({"enable_ssl", "keystore_path", "keystore_password"})
public SslConfig(boolean enableSsl, String keyStorePath, String keyStorePassword) {
this.enableSsl = enableSsl;
this.keyStorePath = keyStorePath;
this.keyStorePassword = keyStorePassword;
}

@JsonGetter("enable_ssl")
public boolean isSslEnabled() {
return enableSsl;
}

@JsonGetter("keystore_path")
public String getKeyStorePath() {
return keyStorePath;
}

@JsonGetter("keystore_password")
public String getKeyStorePassword() {
return keyStorePassword;
}
}
23 changes: 23 additions & 0 deletions api/src/main/java/brainwine/api/util/JettyUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package brainwine.api.util;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;

public class JettyUtils {

public static Server createJettyServerWithSsl(int port, String keyStorePath, String keyStorePassword) {
Server server = new Server();
ServerConnector connector = new ServerConnector(server, createSslContextFactory(keyStorePath, keyStorePassword));
connector.setPort(port);
server.addConnector(connector);
return server;
}

public static SslContextFactory.Server createSslContextFactory(String keyStorePath, String keyStorePassword) {
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(keyStorePath);
sslContextFactory.setKeyStorePassword(keyStorePassword);
return sslContextFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package brainwine.gameserver.item;

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;

public enum InventoryType {

ACCESSORY,
HIDDEN,

@JsonEnumDefaultValue
NONE,
}
35 changes: 27 additions & 8 deletions gameserver/src/main/java/brainwine/gameserver/item/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import brainwine.gameserver.GameServer;
import brainwine.gameserver.command.CommandAccessLevel;
import brainwine.gameserver.item.usetypeconfig.ItemUseTypeConfig;
import brainwine.gameserver.player.AppearanceSlot;
import brainwine.gameserver.player.NotificationType;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonCreator;
Expand All @@ -21,6 +20,7 @@
import com.fasterxml.jackson.annotation.JsonValue;

import brainwine.gameserver.dialog.DialogType;
import brainwine.gameserver.player.AppearanceSlot;
import brainwine.gameserver.player.Skill;
import brainwine.gameserver.util.Pair;
import brainwine.gameserver.util.Vector2i;
Expand Down Expand Up @@ -81,6 +81,12 @@ public class Item {
@JsonProperty("group")
private ItemGroup group = ItemGroup.NONE;

@JsonProperty("inventory type")
private InventoryType inventoryType = InventoryType.NONE;

@JsonProperty("appearance")
private AppearanceSlot appearanceSlot;

@JsonProperty("size")
private Vector2i size = new Vector2i(1, 1);

Expand Down Expand Up @@ -288,9 +294,6 @@ private Item(@JsonProperty(value = "id", required = true) String id,
this.code = code;
}

@JsonProperty("appearance")
public AppearanceSlot appearanceSlot;

@JsonSetter("use")
public void setUseConfigs(Map<ItemUseType, Object> uses) {
useConfigs = new HashMap<>();
Expand Down Expand Up @@ -437,6 +440,26 @@ public ItemGroup getGroup() {
return group;
}

public boolean isAccessory() {
return inventoryType == InventoryType.ACCESSORY;
}

public boolean isHidden() {
return inventoryType == InventoryType.HIDDEN;
}

public InventoryType getInventoryType() {
return inventoryType;
}

public boolean hasAppearanceSlot() {
return appearanceSlot != null;
}

public AppearanceSlot getAppearanceSlot() {
return appearanceSlot;
}

public int getBlockWidth() {
return size.getX();
}
Expand Down Expand Up @@ -744,10 +767,6 @@ public boolean requiresWorkshop() {
public List<CraftingRequirement> getCraftingHelpers() {
return craftingHelpers;
}

public AppearanceSlot getAppearanceSlot() {
return appearanceSlot;
}

public boolean hasUse(ItemUseType... types) {
for(ItemUseType type : types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void interact(Zone zone, Entity entity, int x, int y, Layer layer, Item i
List<MetaBlock> protectors = targetZone.getMetaBlocks(
m -> m.hasOwner()
&& m.getItem().getId().startsWith("mechanical/dish"));

// Check area protection
if(!player.isGodMode() && targetZone.isBlockProtectedByField(targetX, targetY, player, false, protectors)) {
Player owner = metaBlock.getOwner();
Expand Down
10 changes: 8 additions & 2 deletions gameserver/src/main/java/brainwine/gameserver/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,15 @@ public void tick(float deltaTime) {

// Process timers
timers.removeIf(Timer::process);

// Update tracked entities
if(now - lastTrackedEntityUpdate >= TRACKED_ENTITY_UPDATE_INTERVAL) {
updateTrackedEntities();
sendMessage(new EntityPositionMessage(trackedEntities));
lastTrackedEntityUpdate = now;

if(!trackedEntities.isEmpty()) {
sendMessage(new EntityPositionMessage(trackedEntities));
}
}

DailyQuests.tryIssueDailyQuest(this);
Expand Down Expand Up @@ -1773,6 +1776,9 @@ public Map<String, Object> getCustomizedAppearance() {
}
}

appearance.put("to*", "ffff55"); // Top overlay color
appearance.put("fg*", "ffff55"); // Facial gear overlay color

return appearance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ZoneSearchData(Zone zone, Player player) {
this.id = zone.getDocumentId();
this.name = zone.getName();
this.playerCount = zone.getPlayerCount();
this.followeeCount = 0; // TODO
this.followeeCount = (int)zone.getPlayers().stream().filter(player::isFollowing).count();
this.followees = new String[0]; // TODO
this.activeDuration = 0; // TODO
this.explorationProgress = (int)(zone.getExplorationProgress() * 100);
Expand Down
Loading
Loading