mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 15:43:13 +09:00
More functionality in factorioServer and added unit tests
This commit is contained in:
1 parent
fff1989897
commit
e1ab5b4d91
2 files changed
+110
-19
No files matched your search
+56
-19
@@ -11,14 +11,23 @@ This class has the responsiblity of :
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
const child_process = require("child_process");
|
||||||
|
|
||||||
class factorioServer {
|
const mkdirp = require("mkdirp");
|
||||||
|
|
||||||
|
const fileOps = require("_app/fileOps");
|
||||||
|
|
||||||
|
module.exports = class factorioServer {
|
||||||
constructor({
|
constructor({
|
||||||
factorioDirectory = "./../../factorio",
|
factorioDirectory = "factorio",
|
||||||
instanceDirectory = "./../../instances/data",
|
instanceDirectory = "instances/data",
|
||||||
instanceName = "Default server name",
|
instanceName = "Default server name",
|
||||||
factorioPort = Math.floor(Math.random() * 65535),
|
factorioPort = Math.floor(Math.random() * 65535),
|
||||||
serversettings = {}
|
serversettings = {},
|
||||||
|
onProgress = function(type, a, b, c){
|
||||||
|
if(type == "progress") console.log("Progress: ",a," of ",b," unit: ",c);
|
||||||
|
if(type == "log") console.log(a);
|
||||||
|
},
|
||||||
} = {}){
|
} = {}){
|
||||||
this.factorioDirectory = factorioDirectory;
|
this.factorioDirectory = factorioDirectory;
|
||||||
this.instanceDirectory = instanceDirectory;
|
this.instanceDirectory = instanceDirectory;
|
||||||
@@ -40,18 +49,22 @@ class factorioServer {
|
|||||||
"autosave_slots": 5,
|
"autosave_slots": 5,
|
||||||
"afk_autokick_interval": 0,
|
"afk_autokick_interval": 0,
|
||||||
"auto_pause": false,
|
"auto_pause": false,
|
||||||
}, serversettings)
|
}, serversettings);
|
||||||
|
this.onProgress = (type, a, b, c) => {
|
||||||
// ensure instance exists
|
if(typeof onProgress == "function") onProgress(type, a, b, c);
|
||||||
initialize();
|
};
|
||||||
}
|
}
|
||||||
async initialize(){
|
async initialize(){
|
||||||
if(!isValidInstance()){
|
if(!await this.isValidInstance()){
|
||||||
await createInstance();
|
await this.createInstance();
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
async createInstance(path = this.instanceDirectory){
|
async createInstance(path = this.instanceDirectory){
|
||||||
|
if(await this.isValidInstance(path)){
|
||||||
|
throw new Error("Instance already exists!");
|
||||||
|
}
|
||||||
|
|
||||||
// create folders
|
// create folders
|
||||||
let folders = [
|
let folders = [
|
||||||
"/script-output/",
|
"/script-output/",
|
||||||
@@ -59,7 +72,7 @@ class factorioServer {
|
|||||||
"/mods/",
|
"/mods/",
|
||||||
"/instanceMods/",
|
"/instanceMods/",
|
||||||
];
|
];
|
||||||
fs.mkdirSync(path)
|
mkdirp.sync(path);
|
||||||
folders.forEach(folder => fs.mkdirSync(path + folder));
|
folders.forEach(folder => fs.mkdirSync(path + folder));
|
||||||
|
|
||||||
// create files
|
// create files
|
||||||
@@ -67,14 +80,17 @@ class factorioServer {
|
|||||||
{name: "/script-output/output.txt", content: ""},
|
{name: "/script-output/output.txt", content: ""},
|
||||||
{name: "/script-output/orders.txt", content: ""},
|
{name: "/script-output/orders.txt", content: ""},
|
||||||
{name: "/script-output/txbuffer.txt", content: ""},
|
{name: "/script-output/txbuffer.txt", content: ""},
|
||||||
{name: "/config.ini", content: "[path]\r\n\ read-data=__PATH__executable__/../../data\r\n\ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ "},
|
{name: "/config.ini", content: "[path]\r\n\ read-data=__PATH__executable__/../../data\r\n\ write-data=__PATH__executable__/../../../" + this.instanceDirectory + "\r\n\ "},
|
||||||
{name: "/config.json", content: JSON.stringify({
|
{name: "/config.json", content: JSON.stringify({
|
||||||
factorioPort: this.factorioPort,
|
factorioPort: this.factorioPort,
|
||||||
clientPassword: Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 8),
|
clientPassword: Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 8),
|
||||||
}, null, 4)},
|
}, null, 4)},
|
||||||
{name: "/server-settings.json", content: JSON.stringify(this.serversettings)},
|
{name: "/server-settings.json", content: JSON.stringify(this.serversettings, null, 4)},
|
||||||
];
|
];
|
||||||
files.forEach(file => fs.writeFile(path + file.name, file.content, doneWritingHandler));
|
files.forEach(file => {
|
||||||
|
fs.writeFile(path + file.name, file.content, doneWritingHandler);
|
||||||
|
// console.log("Created: ",path+file.name);
|
||||||
|
});
|
||||||
|
|
||||||
let filesToWrite = files.length;
|
let filesToWrite = files.length;
|
||||||
function doneWritingHandler(){
|
function doneWritingHandler(){
|
||||||
@@ -83,17 +99,38 @@ class factorioServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
isValidInstance(path = this.instanceDirectory){
|
async deleteInstance(path = this.instanceDirectory){
|
||||||
if(fs.existsSync(instancedirectory)){
|
if(await this.isValidInstance(path)){
|
||||||
|
await fileOps.deleteFolderRecursive(path);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
isValidInstance(path = this.instanceDirectory){
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
fs.stat(path, (err, stat) => {
|
||||||
|
if(err){
|
||||||
|
// console.log(err)
|
||||||
|
resolve(false);
|
||||||
|
} else {
|
||||||
|
// console.log(stat)
|
||||||
|
resolve(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
async createMap(name = "map.zip"){
|
async createMap(name = "map.zip"){
|
||||||
if(!this.isValidInstance()){
|
if(!await this.isValidInstance()){
|
||||||
await createInstance();
|
await createInstance();
|
||||||
}
|
}
|
||||||
|
this.onProgress("log", "Starting map creation");
|
||||||
|
child_process.spawnSync(
|
||||||
|
'./'+this.factorioDirectory + '/bin/x64/factorio', [
|
||||||
|
'-c', this.instanceDirectory + '/config.ini',
|
||||||
|
'--create', this.instanceDirectory + '/saves/'+name,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
this.onProgress("log", "Map created");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
const assert = require("assert");
|
||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
const factorioServer = require("./index.js");
|
||||||
|
|
||||||
|
describe("factorioServer/index.js", ()=>{
|
||||||
|
it("is a class (or at least a function)", ()=>{
|
||||||
|
assert.equal(typeof factorioServer, "function");
|
||||||
|
});
|
||||||
|
describe(".deleteInstance()", ()=>{
|
||||||
|
it("deletes an instance, defaulting to the classes instanceDirectory", async () => {
|
||||||
|
let server = new factorioServer({
|
||||||
|
instanceDirectory: path.resolve(__dirname, "_testData_deleteInstance"),
|
||||||
|
instanceName: "Unit testing instance",
|
||||||
|
});
|
||||||
|
await server.initialize();
|
||||||
|
await server.deleteInstance();
|
||||||
|
assert.equal(await server.isValidInstance(), false);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe(".isValidInstance()", ()=>{
|
||||||
|
it("tells us whether an instance exists or not", async () => {
|
||||||
|
let server = new factorioServer({
|
||||||
|
instanceDirectory: path.resolve(__dirname, "_testData_isValidInstance"),
|
||||||
|
instanceName: "Unit testing instance",
|
||||||
|
});
|
||||||
|
await server.deleteInstance();
|
||||||
|
let status = await server.isValidInstance();
|
||||||
|
assert.equal(status, false);
|
||||||
|
let x = await server.initialize(); // creates our instance
|
||||||
|
let status2 = await server.isValidInstance();
|
||||||
|
assert.equal(status2, true);
|
||||||
|
|
||||||
|
// clean up folder
|
||||||
|
await server.deleteInstance();
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe(".initialize()", ()=>{
|
||||||
|
it("server.initialize creates an instance", done => {
|
||||||
|
let server = new factorioServer({
|
||||||
|
instanceDirectory: path.resolve(__dirname, "_testData_initialize"),
|
||||||
|
instanceName: "Unit testing instance",
|
||||||
|
});
|
||||||
|
server.initialize().then(()=>{
|
||||||
|
return server.deleteInstance();
|
||||||
|
}).then(() => {
|
||||||
|
done();
|
||||||
|
}).catch(e => console.log(e));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user