This commit is contained in:
ntr
2019-06-16 15:45:03 +10:00
parent 9fcdbeb370
commit 102a8e9817
6 changed files with 72 additions and 30 deletions
+9 -25
View File
@@ -3,38 +3,22 @@ const preact = require('preact');
const { Component } = require('preact')
const { connect } = require('preact-redux');
const SERVER = process.env.NODE_ENV === 'production' ? '/' : 'http://localhost:40000';
function postData(url = '/', data = {}) {
// Default options are marked with *
return fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
// mode: "no-cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "include", // include, same-origin, *omit
headers: {
'Accept': 'application/json',
'content-type': 'application/json'
},
redirect: "error", // manual, *follow, error
// referrer: "", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
.then(response => response.json()); // parses response to JSON
}
const { postData } = require('../utils');
const addState = connect(
null,
(dispatch) => {
(state) => {
const {
ws
} = state;
function submitLogin(name, password) {
postData(`${SERVER}/login`, { name, password })
.then(data => console.log(JSON.stringify(data)))
postData('/login', { name, password })
.then(data => ws.connect())
.catch(error => console.error(error));
}
function submitRegister(name, password, code) {
postData(`${SERVER}/register`, { name, password, code })
.then(data => console.log(JSON.stringify(data)))
postData('/register', { name, password, code })
.then(data => ws.connect())
.catch(error => console.error(error));
}
+9 -1
View File
@@ -1,8 +1,9 @@
const { connect } = require('preact-redux');
const preact = require('preact');
const { Fragment } = require('preact');
const actions = require('../actions');
const { postData } = require('../utils');
const actions = require('../actions');
const { saw } = require('./shapes');
const testGame = process.env.NODE_ENV === 'development' && require('./../test.game');
@@ -38,6 +39,10 @@ const addState = connect(
return ws.sendInstanceList();
}
function logout() {
postData('/logout').then(() => window.location.reload(true));
}
return {
account,
instances,
@@ -47,6 +52,7 @@ const addState = connect(
sendInstanceState,
sendAccountInstances,
sendInstanceList,
logout,
};
},
function receiveDispatch(dispatch) {
@@ -97,6 +103,7 @@ function Nav(args) {
sendInstanceState,
sendAccountInstances,
sendInstanceList,
logout,
setTestGame,
setTestInstance,
@@ -133,6 +140,7 @@ function Nav(args) {
<h2>Hax</h2>
<button onClick={() => setTestGame(account.id)}>Test Game</button>
<button onClick={() => setTestInstance(account.id)}>Test Instance</button>
<button onClick={() => logout()}>Logout</button>
</Fragment>)
: null;
+20
View File
@@ -345,6 +345,24 @@ const TARGET_COLOURS = {
BROWN: '#583108',
};
const SERVER = process.env.NODE_ENV === 'production' ? '/' : 'http://localhost:40000';
function postData(url = '/', data = {}) {
// Default options are marked with *
return fetch(`${SERVER}${url}`, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
// mode: "no-cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "include", // include, same-origin, *omit
headers: {
'Accept': 'application/json',
'content-type': 'application/json'
},
redirect: "error", // manual, *follow, error
// referrer: "", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
}
module.exports = {
stringSort,
convertItem,
@@ -352,6 +370,8 @@ module.exports = {
eventClasses,
getCombatSequence,
getCombatText,
postData,
SERVER,
NULL_UUID,
STATS,
COLOURS,