Skip to content

Commit

Permalink
Add lobby server functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielv123 authored Jul 26, 2022
1 parent 4c5b69b commit 8ef576a
Show file tree
Hide file tree
Showing 15 changed files with 866 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module/flib/* linguist-vendored
17 changes: 16 additions & 1 deletion info.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ InstanceConfigGroup.define({
type: "number",
initial_value: 512,
});
InstanceConfigGroup.define({
name: "is_lobby_server",
title: "Server is a lobby server",
description: "Make this instance act as a lobby server for a gridworld",
type: "boolean",
initial_value: false,
});
InstanceConfigGroup.define({
name: "grid_id",
title: "Grid ID",
description: "Grid identifier - used to run multiple gridworlds on the same cluster",
type: "number",
initial_value: 0,
});
InstanceConfigGroup.finalize();

libUsers.definePermission({
Expand Down Expand Up @@ -121,8 +135,9 @@ module.exports = {
}),
getMapData: new libLink.Request({
type: "gridworld:get_map_data",
links: ["control-master"],
links: ["control-master", "instance-slave", "slave-master"],
permission: "gridworld.overview.view",
forwardTo: "master",
responseProperties: {
map_data: {
type: "array",
Expand Down
18 changes: 9 additions & 9 deletions instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
world_x: this.instance.config.get("gridworld.grid_x_position"),
world_y: this.instance.config.get("gridworld.grid_y_position"),
};
await this.sendRcon(`/c gridworld.create_world_limit("${data.x_size}","${data.y_size}","${data.world_x}","${data.world_y}", false)`, true);
await this.sendRcon(`/c gridworld.create_spawn("${data.x_size}","${data.y_size}","${data.world_x}","${data.world_y}", false)`, true);
if (this.instance.config.get("gridworld.is_lobby_server")) {
await this.sendRcon("/sc gridworld.register_lobby_server(true)");
// Get gridworld data
const { map_data } = await this.info.messages.getMapData.send(this.instance);
await this.sendRcon(`/sc gridworld.register_map_data('${JSON.stringify(map_data)}')`);
} else {
await this.sendRcon(`/sc gridworld.create_world_limit("${data.x_size}","${data.y_size}","${data.world_x}","${data.world_y}", false)`, true);
await this.sendRcon(`/sc gridworld.create_spawn("${data.x_size}","${data.y_size}","${data.world_x}","${data.world_y}", false)`, true);
}
}

async onStop() {
Expand All @@ -59,13 +66,6 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
onMasterConnectionEvent(event) {
if (event === "connect") {
this.disconnecting = false;
(async () => {
if (this.disconnecting) {

}
})().catch(
err => this.logger.error(`Unexpected error:\n${err.stack}`)
);
}
}

Expand Down
69 changes: 65 additions & 4 deletions master.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
let instances = [];

if (!message.data.use_edge_transports) { return; }
const lobby_server = await this.createLobbyServer(message.data.slave);
try {
for (let x = 1; x <= message.data.x_count; x++) {
for (let y = 1; y <= message.data.y_count; y++) {
Expand All @@ -205,7 +206,8 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
x,
y,
message.data.x_size,
message.data.y_size
message.data.y_size,
lobby_server.config.get("gridworld.grid_id"),
),
x,
y,
Expand Down Expand Up @@ -263,7 +265,66 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
}
}

async createInstance(name, x, y, x_size, y_size) {
async createLobbyServer(slaveId) {
// Create instance
this.logger.info("Creating lobby server");
const name = "Gridworld lobby server";
let instanceConfig = new libConfig.InstanceConfig("master");
await instanceConfig.init();
instanceConfig.set("instance.name", name);
instanceConfig.set("instance.auto_start", true);
instanceConfig.set("gridworld.is_lobby_server", true);
instanceConfig.set("gridworld.grid_id", Math.ceil(Math.random()*1000));

let instanceId = instanceConfig.get("instance.id");
if (this.master.instances.has(instanceId)) {
throw new libErrors.RequestError(`Instance with ID ${instanceId} already exists`);
}

// Add common settings for the Factorio server
let settings = {
...instanceConfig.get("factorio.settings"),

"name": `${this.master.config.get("master.name")} - ${name}`,
"description": `Clusterio instance for ${this.master.config.get("master.name")}`,
"tags": ["clusterio", "gridworld"],
"max_players": 0,
"visibility": { "public": true, "lan": true },
"username": "",
"token": "",
"game_password": "",
"require_user_verification": true,
"max_upload_in_kilobytes_per_second": 0,
"max_upload_slots": 5,
"ignore_player_limit_for_returning_players": false,
"allow_commands": "admins-only",
"autosave_interval": 10,
"autosave_slots": 1,
"afk_autokick_interval": 0,
"auto_pause": false,
"only_admins_can_pause_the_game": true,
"autosave_only_on_server": true,
};
instanceConfig.set("factorio.settings", settings);

let instance = { config: instanceConfig, status: "unassigned" };
this.master.instances.set(instanceId, instance);
await libPlugin.invokeHook(this.master.plugins, "onInstanceStatusChanged", instance, null);
this.master.addInstanceHooks(instance);
const instance_id = instanceConfig.get("instance.id");
// Assign instance to a slave (using first slave as a placeholder)
await this.assignInstance(instance_id, instance.slaveId);

// Create map
await this.createSave(
instance_id,
this.master.config.get("gridworld.gridworld_seed"),
this.master.config.get("gridworld.gridworld_map_exchange_string")
);
return instance;
}

async createInstance(name, x, y, x_size, y_size, grid_id) {
this.logger.info("Creating instance", name);
let instanceConfig = new libConfig.InstanceConfig("master");
await instanceConfig.init();
Expand All @@ -280,6 +341,8 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {

// Add common settings for the Factorio server
let settings = {
...instanceConfig.get("factorio.settings"),

"name": `${this.master.config.get("master.name")} - ${name}`,
"description": `Clusterio instance for ${this.master.config.get("master.name")}`,
"tags": ["clusterio"],
Expand All @@ -299,8 +362,6 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
"auto_pause": false,
"only_admins_can_pause_the_game": true,
"autosave_only_on_server": true,

...instanceConfig.get("factorio.settings"),
};
instanceConfig.set("factorio.settings", settings);

Expand Down
21 changes: 21 additions & 0 deletions module/flib/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 raiguard

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions module/flib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Fork of Factorio Library

This is a GUI only fork of Raiguards [flib](https://github.com/factoriolib/flib).
Loading

0 comments on commit 8ef576a

Please sign in to comment.