Skip to content

Commit a19dbef

Browse files
committed
added handler for creating save files
1 parent 96139c8 commit a19dbef

File tree

11 files changed

+484
-102
lines changed

11 files changed

+484
-102
lines changed

app/bundle.js

Lines changed: 274 additions & 64 deletions
Large diffs are not rendered by default.

handlers.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func ListInstalledMods(w http.ResponseWriter, r *http.Request) {
4242

4343
resp.Success = true
4444

45-
fmt.Printf("%+v", resp)
4645
if err := json.NewEncoder(w).Encode(resp); err != nil {
4746
log.Printf("Error in list mods: %s", err)
4847
}
@@ -328,6 +327,43 @@ func RemoveSave(w http.ResponseWriter, r *http.Request) {
328327
}
329328
}
330329

330+
// Launches Factorio server binary with --create flag to create save
331+
// Url must include save name for creation of savefile
332+
func CreateSaveHandler(w http.ResponseWriter, r *http.Request) {
333+
var err error
334+
resp := JSONResponse{
335+
Success: false,
336+
}
337+
338+
vars := mux.Vars(r)
339+
saveName := vars["save"]
340+
341+
if saveName == "" {
342+
log.Printf("Error creating save, no name provided: %s", err)
343+
resp.Data = "No save name provided."
344+
if err := json.NewEncoder(w).Encode(resp); err != nil {
345+
log.Printf("Error encoding save handler response: %s", err)
346+
}
347+
return
348+
}
349+
350+
cmdOut, err := createSave(saveName)
351+
if err != nil {
352+
log.Printf("Error creating save: %s", err)
353+
resp.Data = "Error creating savefile."
354+
if err := json.NewEncoder(w).Encode(resp); err != nil {
355+
log.Printf("Error encoding save handler response: %s", err)
356+
}
357+
return
358+
}
359+
360+
resp.Success = true
361+
resp.Data = fmt.Sprintf("Save %s created successfully. Command output: \n%s", saveName, cmdOut)
362+
if err := json.NewEncoder(w).Encode(resp); err != nil {
363+
log.Printf("Error encoding save response: %s", err)
364+
}
365+
}
366+
331367
// Returns last lines of the factorio-current.log file
332368
func LogTail(w http.ResponseWriter, r *http.Request) {
333369
var err error

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func loadFlags() {
3434
config.FactorioSavesDir = config.FactorioDir + "/saves"
3535
config.FactorioModsDir = config.FactorioDir + "/mods"
3636
config.FactorioConfigFile = config.FactorioDir + "/" + *factorioConfigFile
37-
config.FactorioBinary = config.FactorioDir + *factorioBinary
37+
config.FactorioBinary = config.FactorioDir + "/" + *factorioBinary
3838
config.ServerIP = *factorioIP
3939
config.ServerPort = *factorioPort
4040
config.FactorioLog = config.FactorioDir + "/factorio-current.log"

routes.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ var apiRoutes = Routes{
108108
"GET",
109109
"/saves/rm/{save}",
110110
RemoveSave,
111+
}, {
112+
"CreateSave",
113+
"GET",
114+
"/saves/create/{save}",
115+
CreateSaveHandler,
111116
}, {
112117
"LogTail",
113118
"GET",

server.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
11
package main
2+
3+
import (
4+
"log"
5+
"os/exec"
6+
)
7+
8+
func createSave(saveName string) (string, error) {
9+
args := []string{"--create", saveName}
10+
11+
cmdOutput, err := exec.Command(config.FactorioBinary, args...).Output()
12+
if err != nil {
13+
log.Printf("Error in creating Factorio save: %s", err)
14+
return "", err
15+
}
16+
17+
result := string(cmdOutput)
18+
19+
return result, nil
20+
}

ui/App/components/Config/Settings.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class Settings extends React.Component {
99
return(
1010
<tbody>
1111
{Object.keys(this.props.config).map(function(key) {
12-
console.log(typeof key);
1312
return(
1413
<tr key={key}>
1514
<td>{key}</td>

ui/App/components/ConfigContent.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ConfigContent extends React.Component {
2727
}
2828
});
2929
}
30+
3031
render() {
3132
return(
3233
<div className="content-wrapper">
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React from 'react';
2+
3+
class CreateSave extends React.Component {
4+
constructor(props) {
5+
super(props);
6+
this.createSaveFile = this.createSaveFile.bind(this);
7+
this.updateSavesList = this.updateSavesList.bind(this)
8+
this.state = {
9+
loading: false,
10+
}
11+
}
12+
13+
updateSavesList() {
14+
this.props.getSaves();
15+
}
16+
17+
createSaveFile(e) {
18+
this.setState({loading: true});
19+
$.ajax({
20+
url: "/api/saves/create/" + this.refs.savename.value,
21+
dataType: "json",
22+
success: (data) => {
23+
console.log(data);
24+
if (data.success === true) {
25+
alert(data.data)
26+
this.updateSavesList();
27+
this.setState({loading: false});
28+
} else {
29+
alert(data.data)
30+
document.getElementById('uploadsave').removeChild(loading);
31+
this.setState({loading: false});
32+
}
33+
}
34+
})
35+
}
36+
37+
render() {
38+
var loadingOverlay
39+
if (this.state.loading) {
40+
loadingOverlay =
41+
<div className="overlay">
42+
<i className="fa fa-refresh fa-spin"></i>
43+
</div>
44+
} else {
45+
loadingOverlay = ""
46+
}
47+
48+
return(
49+
<div className="box" id="uploadsave">
50+
<div className="box-header">
51+
<h4 className="box-title">Create Save File</h4>
52+
</div>
53+
54+
<div className="box-body">
55+
<form>
56+
<div className="form-group">
57+
<label for="savefile">Enter Savefile Name... </label>
58+
<input className="form-control" ref="savename" type="text" name="savefile" id="savefilename" />
59+
</div>
60+
<div className="form-group">
61+
<input className="form-control btn btn-default" type="button" ref="button" value="Create" onClick={this.createSaveFile} />
62+
</div>
63+
</form>
64+
</div>
65+
{loadingOverlay}
66+
</div>
67+
)
68+
}
69+
}
70+
71+
CreateSave.propTypes = {
72+
getSaves: React.PropTypes.func.isRequired,
73+
}
74+
75+
export default CreateSave

ui/App/components/Saves/SavesList.jsx

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,13 @@ class SavesList extends React.Component {
55
constructor(props) {
66
super(props);
77
this.updateSavesList = this.updateSavesList.bind(this);
8-
this.uploadFile = this.uploadFile.bind(this);
98
this.removeSave = this.removeSave.bind(this);
109
}
1110

1211
updateSavesList () {
1312
this.props.getSaves();
1413
}
1514

16-
uploadFile(e) {
17-
var fd = new FormData();
18-
fd.append('savefile', this.refs.file.files[0]);
19-
20-
$.ajax({
21-
url: "/api/saves/upload",
22-
type: "POST",
23-
data: fd,
24-
processData: false,
25-
contentType: false,
26-
success: (data) => {
27-
alert(data)
28-
}
29-
});
30-
e.preventDefault();
31-
this.updateSavesList();
32-
}
33-
3415
removeSave(saveName, e) {
3516
$.ajax({
3617
url: "/api/saves/rm/" + saveName,
@@ -49,22 +30,6 @@ class SavesList extends React.Component {
4930
</div>
5031

5132
<div className="box-body">
52-
<div className="box">
53-
<div className="box-header">
54-
<h4 className="box-title">Upload Save File</h4>
55-
</div>
56-
<div className="box-body">
57-
<form ref="uploadForm" className="form-inline" encType='multipart/form-data'>
58-
<div className="form-group">
59-
<label for="savefile">Upload Save File...</label>
60-
<input className="form-control btn btn-default" ref="file" type="file" name="savefile" id="savefile" />
61-
</div>
62-
<div className="form-group">
63-
<input className="form-control btn btn-default" type="button" ref="button" value="Upload" onClick={this.uploadFile} />
64-
</div>
65-
</form>
66-
</div>
67-
</div>
6833

6934
<div className="table-responsive">
7035
<table className="table table-striped">
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
3+
class UploadSave extends React.Component {
4+
constructor(props) {
5+
super(props)
6+
this.updateSavesList = this.updateSavesList.bind(this);
7+
this.uploadFile = this.uploadFile.bind(this);
8+
}
9+
10+
updateSavesList() {
11+
this.props.getSaves();
12+
}
13+
14+
uploadFile(e) {
15+
var fd = new FormData();
16+
fd.append('savefile', this.refs.file.files[0]);
17+
18+
$.ajax({
19+
url: "/api/saves/upload",
20+
type: "POST",
21+
data: fd,
22+
processData: false,
23+
contentType: false,
24+
success: (data) => {
25+
alert(data)
26+
}
27+
});
28+
e.preventDefault();
29+
this.updateSavesList();
30+
}
31+
32+
render() {
33+
return(
34+
<div className="box">
35+
<div className="box-header">
36+
<h4 className="box-title">Upload Save File</h4>
37+
</div>
38+
<div className="box-body">
39+
<form ref="uploadForm" className="form-inline" encType='multipart/form-data'>
40+
<div className="form-group">
41+
<label for="savefile">Upload Save File...</label>
42+
<input className="form-control btn btn-default" ref="file" type="file" name="savefile" id="savefile" />
43+
</div>
44+
<div className="form-group">
45+
<input className="form-control btn btn-default" type="button" ref="button" value="Upload" onClick={this.uploadFile} />
46+
</div>
47+
</form>
48+
</div>
49+
</div>
50+
)
51+
}
52+
}
53+
54+
UploadSave.propTypes = {
55+
getSaves: React.PropTypes.func.isRequired,
56+
}
57+
58+
export default UploadSave

0 commit comments

Comments
 (0)