feature: create default multiplayer world

This commit is contained in:
Filip Pajic 2024-08-07 12:04:09 +02:00
parent c7dc446e3a
commit 7f4bfdee82
No known key found for this signature in database
6 changed files with 28 additions and 11 deletions

View File

@ -18,4 +18,6 @@ IS_BEHIND_PROXY=# Set to true if NGINX or similiar solution is being used to run
WORLD_FILE_NAME=main_instance.world
# Used for CORS policy
CLIENT_ORIGIN_URL=
CLIENT_ORIGIN_URL=
USE_FALLBACK_DEFAULT_WORLD=true

View File

@ -22,14 +22,21 @@ function initWorld() {
if (world.loadFromFile()) {
logger.info( "Loaded the world from file." );
} else {
logger.info( "Creating a new empty world." );
if (Config.USE_FALLBACK_DEFAULT_WORLD) {
logger.info( "Creating a new world from a template of default world." );
const defaultWorldSeed = "acid-info";
const magnitude = 0.1;
world.loadFromFile(world.getDefaultWorldFilePath())
} else {
logger.info( "Creating a new empty world." );
const defaultWorldSeed = "acid-info";
const magnitude = 0.1;
world.createRandomisedWorld(
Config.WORLD_GROUNDHEIGHT, defaultWorldSeed, undefined, magnitude
);
}
world.createRandomisedWorld(
Config.WORLD_GROUNDHEIGHT, defaultWorldSeed, undefined, magnitude
);
world.prepareNewSaveDir()
world.saveToFile();
}

View File

@ -15,7 +15,8 @@ const {
ONE_USER_PER_IP,
IS_BEHIND_PROXY,
WORLD_FILE_NAME,
CLIENT_ORIGIN_URL
CLIENT_ORIGIN_URL,
USE_FALLBACK_DEFAULT_WORLD
} = process.env;
if (!PORT) {
@ -54,7 +55,8 @@ const Config: AppConfig = {
CORS_POLICY: { credentials: true,
origin: CLIENT_ORIGIN_URL,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE'
}
},
USE_FALLBACK_DEFAULT_WORLD: USE_FALLBACK_DEFAULT_WORLD === "true"
}
export default Config;

View File

@ -14,4 +14,5 @@ export type AppConfig = {
WORLD_FILE_NAME: string
WORLD_FILE_FOLDER: string
CORS_POLICY: CorsOptions
USE_FALLBACK_DEFAULT_WORLD: boolean
}

View File

@ -17,6 +17,10 @@ class ServerWorld extends World {
return path.join(process.cwd(), this.worldSaveDirName, this.worldSaveFileName);
}
public getDefaultWorldFilePath() {
return path.join(process.cwd(), "worlds", "default.world")
}
public prepareNewSaveDir() {
const dir = path.join(process.cwd(), this.worldSaveDirName);
@ -25,9 +29,9 @@ class ServerWorld extends World {
}
}
public loadFromFile() {
public loadFromFile(_path?: string) {
try {
const path = this.getWorldFilePath();
const path = _path || this.getWorldFilePath();
const data = FileUtil.readFileSync(path).toString('utf8');
const [spawnX, spawnY, spawnZ] = data.split(',');

File diff suppressed because one or more lines are too long