added functionality to update Mods automatically

This commit is contained in:
knoxfighter 2017-08-23 17:47:36 +09:00
parent 79042c2f96
commit 95ce89d993
5 changed files with 217 additions and 10 deletions

View File

@ -238,6 +238,50 @@ func DeleteModHandler(w http.ResponseWriter, r *http.Request) {
}
}
func UpdateModHandler(w http.ResponseWriter, r *http.Request) {
var err error
resp := JSONResponse{
Success: false,
}
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
//Get Data out of the request
mod_name := r.FormValue("mod_name")
username := r.FormValue("username")
user_key := r.FormValue("userKey")
download_url := r.FormValue("downloadUrl")
file_name := r.FormValue("filename")
_, err = deleteMod(mod_name)
if err != nil {
resp.Data = fmt.Sprintf("Error in deleteMod, installing Mod will not be executed: %s", err)
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error in deleteMod, installing Mod will not be executed: %s", err)
}
return
}
var statusCode int
resp.Data, err, statusCode = installMod(username, user_key, download_url, file_name, mod_name)
w.WriteHeader(statusCode)
if err != nil {
resp.Data = fmt.Sprintf("Error in deleteMod: %s", err)
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error in DeleteModHandler: %s", err)
}
return
}
resp.Success = true
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error in DeleteModHandler: %s", err)
}
}
// Toggles mod passed in through mod variable
// Updates mod-list.json file to toggle the enabled status of mods
/*func ToggleMod(w http.ResponseWriter, r *http.Request) {

View File

@ -184,6 +184,11 @@ var apiRoutes = Routes{
"/mods/delete",
DeleteModHandler,
}, {
"UpdateMod",
"POST",
"/mods/update",
UpdateModHandler,
}, {
"ListSaves",
"GET",
"/saves/list",

View File

@ -1,18 +1,112 @@
import React from 'react';
class Mod extends React.Component {
render() {
if (this.props.mod.enabled === false) {
this.modStatus = <span className="label label-danger">Disabled</span>
} else {
this.modStatus = <span className="label label-success">Enabled</span>
constructor(props) {
super(props);
this.toggleUpdateStatus = this.toggleUpdateStatus.bind(this);
this.removeVersionAvailableStatus = this.removeVersionAvailableStatus.bind(this);
this.state = {
newVersionAvailable: false,
updateInProgress: false
}
}
componentDidMount() {
this.checkForNewVersion();
}
checkForNewVersion() {
let this_class = this
//send AJAX that will check this
$.ajax({
url: "/api/mods/details",
method: "POST",
data: {
mod_id: this.props.mod.name
},
dataType: "JSON",
success: (data) => {
let newest_release = JSON.parse(data.data).releases[0];
console.log(newest_release);
if(newest_release.version != this.props.mod.version) {
this_class.setState({
newVersionAvailable: true,
newVersion: {
downloadUrl: newest_release.download_url,
file_name: newest_release.file_name
}
});
}
},
error: (jqXHR, status, err) => {
console.log('api/mods/details', status, err.toString());
}
});
}
toggleUpdateStatus() {
console.log("update Status changed");
this.setState({
updateInProgress: !this.state.updateInProgress
})
}
removeVersionAvailableStatus() {
this.setState({
newVersionAvailable: false,
newVersion: null
})
}
render() {
let modStatus;
if(this.props.mod.enabled === false) {
modStatus = <span className="label label-danger">Disabled</span>
} else {
modStatus = <span className="label label-success">Enabled</span>
}
let version;
if(this.state.newVersionAvailable) {
version = <span>{this.props.mod.version}
<a className="btn btn-xs btn-default"
style={{
marginLeft: 10,
display: "inline-flex",
justifyContent: "center",
alignItems: "center",
width: 30,
height: 30
}}
href="#"
onClick={(event) => {
this.state.updateInProgress ? null : this.props.updateMod(event, this.toggleUpdateStatus, this.removeVersionAvailableStatus);
}}
data-download-url={this.state.newVersion.downloadUrl}
data-file-name={this.state.newVersion.file_name}
>
{
this.state.updateInProgress ?
<div className='loader' style={{width: 15, height: 15, marginRight: 0, borderWidth: 3,}}></div>
:
<i className="fa fa-arrow-circle-up" title="Update Mod" style={{fontSize: "15pt"}}></i>
}
</a>
</span>;
} else {
version = this.props.mod.version;
}
return(
<tr data-mod-name={this.props.mod.name}
data-file-name={this.props.mod.file_name}
data-mod-version={this.props.mod.version}
>
<td>{this.props.mod.title}</td>
<td>{this.modStatus}</td>
<td>{modStatus}</td>
<td>{version}</td>
<td>
<input className='btn btn-default btn-sm'
ref='modName'
@ -37,7 +131,8 @@ class Mod extends React.Component {
Mod.propTypes = {
mod: React.PropTypes.object.isRequired,
toggleMod: React.PropTypes.func.isRequired,
deleteMod: React.PropTypes.func.isRequired
deleteMod: React.PropTypes.func.isRequired,
updateMod: React.PropTypes.func.isRequired,
}
export default Mod

View File

@ -47,7 +47,7 @@ class ModOverview extends React.Component {
</div>
<div className="box-body">
<div className="box box-success collapsed-box">
<div className="box box-success collapsed-box" id="add-mod-box">
<button className="btn btn-box-tool" type="button" data-widget="collapse">
<div className="box-header with-border">
<i className="fa fa-plus"></i>
@ -70,6 +70,7 @@ class ModOverview extends React.Component {
<tr>
<th>Name</th>
<th>Status</th>
<th>Version</th>
<th>Toggle/Remove</th>
</tr>
</thead>
@ -98,7 +99,8 @@ ModOverview.propTypes = {
installedMods: React.PropTypes.array,
submitFactorioLogin: React.PropTypes.func.isRequired,
toggleMod: React.PropTypes.func.isRequired,
deleteMod: React.PropTypes.func.isRequired
deleteMod: React.PropTypes.func.isRequired,
updateMod: React.PropTypes.func.isRequired,
};
export default ModOverview;

View File

@ -14,11 +14,13 @@ class ModsContent extends React.Component {
this.loadDownloadListSwalHandler = this.loadDownloadListSwalHandler.bind(this);
this.toggleModHandler = this.toggleModHandler.bind(this);
this.deleteModHandler = this.deleteModHandler.bind(this);
this.updateModHandler = this.updateModHandler.bind(this);
this.state = {
username: "",
userKey: "",
installedMods: []
installedMods: [],
updateInProgress: false
}
}
@ -274,6 +276,64 @@ class ModsContent extends React.Component {
});
}
updateModHandler(e, toggleUpdateStatus, removeVersionAvailableStatus) {
e.preventDefault();
if(!this.state.userKey) {
swal({
type: "error",
title: "Update failed",
text: "please login into Factorio to update mod"
});
let $addModBox = $('#add-mod-box');
if($addModBox.hasClass("collapsed-box")) {
$addModBox.children("button").click();
}
} else {
let $button = $(e.currentTarget);
let download_url = $button.data("downloadUrl");
let filename = $button.data("fileName");
let modname = $button.parents("tr").data("modName");
let this_class = this;
//make button spinning
// let $loader = $("");
// $button.html($loader);
toggleUpdateStatus();
$.ajax({
url: "/api/mods/update",
method: "POST",
data: {
username: this.state.username,
userKey: this.state.userKey,
downloadUrl: download_url,
filename: filename,
mod_name: modname,
},
success: (data) => {
toggleUpdateStatus();
removeVersionAvailableStatus();
this_class.setState({
installedMods: data.data
});
},
error: (jqXHR, status, err) => {
console.log('api/mods/delete', status, err.toString());
toggleUpdateStatus();
swal({
title: "Update Mod went wrong",
text: err.toString(),
type: "error"
});
}
});
}
}
render() {
return(
<div className="content-wrapper">
@ -295,6 +355,7 @@ class ModsContent extends React.Component {
submitFactorioLogin={this.handlerFactorioLogin}
toggleMod={this.toggleModHandler}
deleteMod={this.deleteModHandler}
updateMod={this.updateModHandler}
/>
</section>
</div>