mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 07:33:12 +09:00
Add option to pass map settings to Factorio
Add options to pass map seed, map exchange string, map gen settings and/or map settings when creating a save either through the create save request or the load scenario request.
This commit is contained in:
1 parent
4a8a4c8227
commit
dd216318b0
12 files changed
+462
-59
No files matched your search
@@ -0,0 +1,140 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Button, Col, Form, Input, Modal, Row, Typography } from "antd";
|
||||
|
||||
import libLink from "@clusterio/lib/link";
|
||||
import { readMapExchangeString } from "@clusterio/lib/factorio/exchange_string";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
|
||||
|
||||
export default function CreateSaveModal(props) {
|
||||
let [visible, setVisible] = useState(false);
|
||||
let [creatingSave, setCreatingSave] = useState(false);
|
||||
let [form] = Form.useForm();
|
||||
let control = useContext(ControlContext);
|
||||
|
||||
function createSave() {
|
||||
let values = form.getFieldsValue();
|
||||
let name = values.saveName || "world.zip";
|
||||
let seed = values.seed === undefined ? null : Number.parseInt(values.seed, 10);
|
||||
let mapGenSettings = null;
|
||||
let mapSettings = null;
|
||||
if (values.exchangeString && values.exchangeString.trim()) {
|
||||
let result;
|
||||
try {
|
||||
result = readMapExchangeString(values.exchangeString);
|
||||
} catch (err) {
|
||||
form.setFields([{ name: "exchangeString", errors: [err.message] }]);
|
||||
return;
|
||||
}
|
||||
form.setFields([{ name: "exchangeString", errors: [] }]);
|
||||
mapGenSettings = result.map_gen_settings;
|
||||
mapSettings = result.map_settings;
|
||||
}
|
||||
if (values.mapGenSettings && values.mapGenSettings.trim()) {
|
||||
try {
|
||||
mapGenSettings = JSON.parse(values.mapGenSettings);
|
||||
} catch (err) {
|
||||
form.setFields([{ name: "mapGenSettings", errors: [err.message] }]);
|
||||
return;
|
||||
}
|
||||
form.setFields([{ name: "mapGenSettings", errors: [] }]);
|
||||
}
|
||||
if (values.mapSettings && values.mapSettings.trim()) {
|
||||
try {
|
||||
mapSettings = JSON.parse(values.mapGenSettings);
|
||||
} catch (err) {
|
||||
form.setFields([{ name: "mapSettings", errors: [err.message] }]);
|
||||
return;
|
||||
}
|
||||
form.setFields([{ name: "mapSettings", errors: [] }]);
|
||||
}
|
||||
|
||||
setCreatingSave(true);
|
||||
libLink.messages.createSave.send(control, {
|
||||
instance_id: props.instance.id,
|
||||
name,
|
||||
seed,
|
||||
map_gen_settings: mapGenSettings,
|
||||
map_settings: mapSettings,
|
||||
}).then(() => {
|
||||
setVisible(false);
|
||||
}).catch(
|
||||
notifyErrorHandler("Error creating save")
|
||||
).finally(() => {
|
||||
setCreatingSave(false);
|
||||
});
|
||||
}
|
||||
|
||||
function convertExchangeString() {
|
||||
let exchangeString = form.getFieldValue("exchangeString");
|
||||
let result;
|
||||
try {
|
||||
result = readMapExchangeString(exchangeString);
|
||||
} catch (err) {
|
||||
form.setFields([{ name: "exchangeString", errors: [err.message] }]);
|
||||
return;
|
||||
}
|
||||
form.setFields([{ name: "exchangeString", errors: [], value: "" }]);
|
||||
form.setFieldsValue({
|
||||
mapGenSettings: JSON.stringify(result.map_gen_settings, null, 4),
|
||||
mapSettings: JSON.stringify(result.map_settings, null, 4),
|
||||
});
|
||||
}
|
||||
|
||||
function handleCancel() {
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
return <>
|
||||
<Button
|
||||
loading={creatingSave}
|
||||
disabled={props.instance.status !== "stopped"}
|
||||
onClick={() => {
|
||||
setVisible(true);
|
||||
}}
|
||||
>
|
||||
Create save
|
||||
</Button>
|
||||
<Modal
|
||||
title="Create Save"
|
||||
width={560}
|
||||
okText="Create"
|
||||
visible={visible}
|
||||
confirmLoading={creatingSave}
|
||||
onOk={createSave}
|
||||
onCancel={handleCancel}
|
||||
destroyOnClose
|
||||
>
|
||||
<Form form={form} layout="vertical">
|
||||
<Form.Item name="saveName" label="Save Name">
|
||||
<Input placeholder="world.zip" />
|
||||
</Form.Item>
|
||||
<Form.Item name="seed" label="Seed">
|
||||
<Input type="number" />
|
||||
</Form.Item>
|
||||
<Form.Item name="exchangeString" label="Map Exchange String">
|
||||
<Input.TextArea allowClear={true} autoSize={{ minRows: 3 }} />
|
||||
</Form.Item>
|
||||
<div style={{ textAlign: "right", marginTop: -8 }}>
|
||||
<Button onClick={() => convertExchangeString()}>Convert to settings</Button>
|
||||
</div>
|
||||
<Form.Item
|
||||
name="mapGenSettings"
|
||||
label="Map Gen Settings"
|
||||
tooltip="JSON data to pass to Factorio with --map-gen-settings, overrides Map Exchange String"
|
||||
>
|
||||
<Input.TextArea allowClear={true} autoSize={{ minRows: 1 }} />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="mapSettings"
|
||||
label="Map Settings"
|
||||
tooltip="JSON data to pass to Factorio with --map-settings, overrides Map Exchange String"
|
||||
>
|
||||
<Input.TextArea allowClear={true} autoSize={{ minRows: 1 }} />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</>;
|
||||
}
|
||||
@@ -12,6 +12,8 @@ import InstanceConsole from "./InstanceConsole";
|
||||
import InstanceRcon from "./InstanceRcon";
|
||||
import AssignInstanceModal from "./AssignInstanceModal";
|
||||
import StartStopInstanceButton from "./StartStopInstanceButton";
|
||||
import CreateSaveModal from "./CreateSaveModal";
|
||||
import LoadScenarioModal from "./LoadScenarioModal";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
import { useInstance } from "../model/instance";
|
||||
import { useSlave } from "../model/slave";
|
||||
@@ -29,8 +31,6 @@ export default function InstanceViewPage(props) {
|
||||
let [instance] = useInstance(instanceId);
|
||||
let [slave] = useSlave(Number(instance["assigned_slave"]));
|
||||
|
||||
let [creatingSave, setCreatingSave] = useState(false);
|
||||
|
||||
let nav = [{ name: "Instances", path: "/instances" }, { name: instance.name || "Unknown" }];
|
||||
if (instance.loading) {
|
||||
return <PageLayout nav={nav}><Spin size="large" /></PageLayout>;
|
||||
@@ -45,19 +45,8 @@ export default function InstanceViewPage(props) {
|
||||
|
||||
let instanceButtons = <Space>
|
||||
<StartStopInstanceButton instance={instance} />
|
||||
{instance.status === "stopped" && <Button
|
||||
loading={creatingSave}
|
||||
onClick={() => {
|
||||
setCreatingSave(true);
|
||||
libLink.messages.createSave.send(
|
||||
control, { instance_id: instanceId }
|
||||
).catch(notifyErrorHandler("Error creating save")).finally(() => {
|
||||
setCreatingSave(false);
|
||||
});
|
||||
}}
|
||||
>
|
||||
Create save
|
||||
</Button>}
|
||||
<CreateSaveModal instance={instance} />
|
||||
<LoadScenarioModal instance={instance} />
|
||||
<Popconfirm
|
||||
title="Permanently delete instance and server saves?"
|
||||
okText="Delete"
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Button, Col, Form, Input, Modal, Row, Typography } from "antd";
|
||||
|
||||
import libLink from "@clusterio/lib/link";
|
||||
import { readMapExchangeString } from "@clusterio/lib/factorio/exchange_string";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
|
||||
|
||||
export default function LoadScenarioModal(props) {
|
||||
let [visible, setVisible] = useState(false);
|
||||
let [loadingScenario, setLoadingScenario] = useState(false);
|
||||
let [form] = Form.useForm();
|
||||
let control = useContext(ControlContext);
|
||||
|
||||
function loadScenario() {
|
||||
let values = form.getFieldsValue();
|
||||
let scenario = values.scenario || "base/freeplay";
|
||||
let seed = values.seed === undefined ? null : Number.parseInt(values.seed, 10);
|
||||
let mapGenSettings = null;
|
||||
let mapSettings = null;
|
||||
if (values.exchangeString && values.exchangeString.trim()) {
|
||||
let result;
|
||||
try {
|
||||
result = readMapExchangeString(values.exchangeString);
|
||||
} catch (err) {
|
||||
form.setFields([{ name: "exchangeString", errors: [err.message] }]);
|
||||
return;
|
||||
}
|
||||
form.setFields([{ name: "exchangeString", errors: [] }]);
|
||||
mapGenSettings = result.map_gen_settings;
|
||||
mapSettings = result.map_settings;
|
||||
}
|
||||
if (values.mapGenSettings && values.mapGenSettings.trim()) {
|
||||
try {
|
||||
mapGenSettings = JSON.parse(values.mapGenSettings);
|
||||
} catch (err) {
|
||||
form.setFields([{ name: "mapGenSettings", errors: [err.message] }]);
|
||||
return;
|
||||
}
|
||||
form.setFields([{ name: "mapGenSettings", errors: [] }]);
|
||||
}
|
||||
if (values.mapSettings && values.mapSettings.trim()) {
|
||||
try {
|
||||
mapSettings = JSON.parse(values.mapGenSettings);
|
||||
} catch (err) {
|
||||
form.setFields([{ name: "mapSettings", errors: [err.message] }]);
|
||||
return;
|
||||
}
|
||||
form.setFields([{ name: "mapSettings", errors: [] }]);
|
||||
}
|
||||
|
||||
setLoadingScenario(true);
|
||||
libLink.messages.loadScenario.send(control, {
|
||||
instance_id: props.instance.id,
|
||||
scenario,
|
||||
seed,
|
||||
map_gen_settings: mapGenSettings,
|
||||
map_settings: mapSettings,
|
||||
}).then(() => {
|
||||
setVisible(false);
|
||||
}).catch(
|
||||
notifyErrorHandler("Error creating save")
|
||||
).finally(() => {
|
||||
setLoadingScenario(false);
|
||||
});
|
||||
}
|
||||
|
||||
function convertExchangeString() {
|
||||
let exchangeString = form.getFieldValue("exchangeString");
|
||||
let result;
|
||||
try {
|
||||
result = readMapExchangeString(exchangeString);
|
||||
} catch (err) {
|
||||
form.setFields([{ name: "exchangeString", errors: [err.message] }]);
|
||||
return;
|
||||
}
|
||||
form.setFields([{ name: "exchangeString", errors: [], value: "" }]);
|
||||
form.setFieldsValue({
|
||||
mapGenSettings: JSON.stringify(result.map_gen_settings, null, 4),
|
||||
mapSettings: JSON.stringify(result.map_settings, null, 4),
|
||||
});
|
||||
}
|
||||
|
||||
function handleCancel() {
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
return <>
|
||||
<Button
|
||||
loading={loadingScenario}
|
||||
disabled={props.instance.status !== "stopped"}
|
||||
onClick={() => {
|
||||
setVisible(true);
|
||||
}}
|
||||
>
|
||||
Load scenario
|
||||
</Button>
|
||||
<Modal
|
||||
title="Load Scenario"
|
||||
width={560}
|
||||
okText="Load"
|
||||
visible={visible}
|
||||
confirmLoading={loadingScenario}
|
||||
onOk={loadScenario}
|
||||
onCancel={handleCancel}
|
||||
destroyOnClose
|
||||
>
|
||||
<Form form={form} layout="vertical">
|
||||
<Form.Item name="scenario" label="Scenario">
|
||||
<Input placeholder="base/freeplay" />
|
||||
</Form.Item>
|
||||
<Form.Item name="seed" label="Seed">
|
||||
<Input type="number" />
|
||||
</Form.Item>
|
||||
<Form.Item name="exchangeString" label="Map Exchange String">
|
||||
<Input.TextArea allowClear={true} autoSize={{ minRows: 3 }} />
|
||||
</Form.Item>
|
||||
<div style={{ textAlign: "right", marginTop: -8 }}>
|
||||
<Button onClick={() => convertExchangeString()}>Convert to settings</Button>
|
||||
</div>
|
||||
<Form.Item
|
||||
name="mapGenSettings"
|
||||
label="Map Gen Settings"
|
||||
tooltip="JSON data to pass to Factorio with --map-gen-settings, overrides Map Exchange String"
|
||||
>
|
||||
<Input.TextArea allowClear={true} autoSize={{ minRows: 1 }} />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="mapSettings"
|
||||
label="Map Settings"
|
||||
tooltip="JSON data to pass to Factorio with --map-settings, overrides Map Exchange String"
|
||||
>
|
||||
<Input.TextArea allowClear={true} autoSize={{ minRows: 1 }} />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</>;
|
||||
}
|
||||
Reference in new issue
Block a user