Added a dummied attack button as a component
This commit is contained in:
@@ -38,6 +38,7 @@ app.post('/recruitrequest', profiles.recruitRequest(connection));
|
|||||||
app.post('/trainrequest', profiles.trainRequest(connection));
|
app.post('/trainrequest', profiles.trainRequest(connection));
|
||||||
app.post('/untrainrequest', profiles.untrainRequest(connection));
|
app.post('/untrainrequest', profiles.untrainRequest(connection));
|
||||||
app.post('/ladderrequest', profiles.ladderRequest(connection));
|
app.post('/ladderrequest', profiles.ladderRequest(connection));
|
||||||
|
app.post('/attackrequest', profiles.attackRequest(connection));
|
||||||
profiles.runGoldTick(connection);
|
profiles.runGoldTick(connection);
|
||||||
|
|
||||||
//static directories
|
//static directories
|
||||||
|
|||||||
@@ -366,6 +366,11 @@ const ladderRequest = (connection) => (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const attackRequest = (connection) => (req, res) => {
|
||||||
|
res.status(400).write(log('Not yet implemented'));
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
|
||||||
const runGoldTick = (connection) => {
|
const runGoldTick = (connection) => {
|
||||||
let goldTickJob = new CronJob('0 */30 * * * *', () => {
|
let goldTickJob = new CronJob('0 */30 * * * *', () => {
|
||||||
let query = 'UPDATE profiles SET gold = gold + recruits;';
|
let query = 'UPDATE profiles SET gold = gold + recruits;';
|
||||||
@@ -386,5 +391,6 @@ module.exports = {
|
|||||||
trainRequest: trainRequest,
|
trainRequest: trainRequest,
|
||||||
untrainRequest: untrainRequest,
|
untrainRequest: untrainRequest,
|
||||||
ladderRequest: ladderRequest,
|
ladderRequest: ladderRequest,
|
||||||
|
attackRequest: attackRequest,
|
||||||
runGoldTick: runGoldTick
|
runGoldTick: runGoldTick
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
export const SET_ATTACK_DISABLED = 'SET_ATTACK_DISABLED';
|
||||||
|
|
||||||
|
export function setAttackDisabled(disabled) {
|
||||||
|
return {
|
||||||
|
type: SET_ATTACK_DISABLED,
|
||||||
|
disabled: disabled
|
||||||
|
}
|
||||||
|
}
|
||||||
+28
-2
@@ -5,11 +5,37 @@ import Loadable from 'react-loadable';
|
|||||||
//other stuff
|
//other stuff
|
||||||
import Footer from './panels/footer.jsx';
|
import Footer from './panels/footer.jsx';
|
||||||
|
|
||||||
//lazy route loading
|
//lazy route loading (with error handling)
|
||||||
const LazyRoute = (props) => {
|
const LazyRoute = (props) => {
|
||||||
const component = Loadable({
|
const component = Loadable({
|
||||||
loader: props.component,
|
loader: props.component,
|
||||||
loading: () => <div className='page'><p className='centered'>Loading...</p></div>,
|
|
||||||
|
loading: (props) => {
|
||||||
|
if (props.error) {
|
||||||
|
return (
|
||||||
|
<div className='page'>
|
||||||
|
<div className='warning' style={{display: 'flex'}}>
|
||||||
|
<p className='centered'>{props.error}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
} else if (props.timeOut) {
|
||||||
|
return (
|
||||||
|
<div className='page'>
|
||||||
|
<div className='warning' style={{display: 'flex'}}>
|
||||||
|
<p className='centered'>Timed Out</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
<div className='page'>
|
||||||
|
<p className='centered'>Loading...</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
timeout: 10000
|
||||||
});
|
});
|
||||||
|
|
||||||
return <Route {...props} component={component} />;
|
return <Route {...props} component={component} />;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import queryString from 'query-string';
|
|||||||
|
|
||||||
//panels
|
//panels
|
||||||
import CommonLinks from '../panels/common_links.jsx';
|
import CommonLinks from '../panels/common_links.jsx';
|
||||||
|
import AttackButton from '../panels/attack_button.jsx';
|
||||||
|
|
||||||
class Profile extends React.Component {
|
class Profile extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
@@ -123,7 +124,7 @@ class Profile extends React.Component {
|
|||||||
|
|
||||||
//panel functions
|
//panel functions
|
||||||
MyProfileSidePanel() {
|
MyProfileSidePanel() {
|
||||||
//finally return the side panel
|
//return the side panel
|
||||||
return (
|
return (
|
||||||
<div className='sidePanel'>
|
<div className='sidePanel'>
|
||||||
<CommonLinks />
|
<CommonLinks />
|
||||||
@@ -178,7 +179,7 @@ class Profile extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NotMyProfileSidePanel() {
|
NotMyProfileSidePanel() {
|
||||||
//finally return the side panel
|
//return the side panel
|
||||||
return (
|
return (
|
||||||
<div className='sidePanel'>
|
<div className='sidePanel'>
|
||||||
<CommonLinks onClickProfile={() => {e.preventDefault(); this.sendRequest('/profilerequest', this.props.username); this.setWarning(''); this.props.history.push('/profile');}} />
|
<CommonLinks onClickProfile={() => {e.preventDefault(); this.sendRequest('/profilerequest', this.props.username); this.setWarning(''); this.props.history.push('/profile');}} />
|
||||||
@@ -206,8 +207,7 @@ class Profile extends React.Component {
|
|||||||
<div className='row'>
|
<div className='row'>
|
||||||
<p className='col'>Recruits:</p>
|
<p className='col'>Recruits:</p>
|
||||||
<p className='col'>{this.state.recruits}</p>
|
<p className='col'>{this.state.recruits}</p>
|
||||||
<div className='col'></div>
|
<AttackButton className='col' style={{flex: '2 1 1.5%'}} setWarning={this.setWarning.bind(this)} attacker={this.props.username} defender={this.state.username} />
|
||||||
<div className='col'></div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className='row'>
|
<div className='row'>
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
import { connect } from 'react-redux';
|
||||||
|
|
||||||
|
import { setAttackDisabled } from '../../actions/combat.js';
|
||||||
|
|
||||||
|
class AttackButton extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
//
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<button className={this.props.className} style={this.props.style} onClick={this.sendAttackRequest.bind(this)} disabled={this.props.disabled}>Attack</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendAttackRequest() {
|
||||||
|
//build the XHR
|
||||||
|
let xhr = new XMLHttpRequest();
|
||||||
|
xhr.open('POST', '/attackrequest', true);
|
||||||
|
|
||||||
|
xhr.onreadystatechange = () => {
|
||||||
|
if (xhr.readyState === 4) {
|
||||||
|
if (xhr.status === 200) {
|
||||||
|
//DO NOTHING
|
||||||
|
} else if (xhr.status === 400) {
|
||||||
|
if (this.props.setWarning) {
|
||||||
|
this.props.setWarning(xhr.responseText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
||||||
|
xhr.send(JSON.stringify({
|
||||||
|
attacker: this.props.attacker,
|
||||||
|
defender: this.props.defender
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (this.props.onClick) {
|
||||||
|
this.props.onClick();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.props.setDisabled(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
AttackButton.propTypes = {
|
||||||
|
className: PropTypes.string,
|
||||||
|
style: PropTypes.object,
|
||||||
|
onClick: PropTypes.func,
|
||||||
|
setWarning: PropTypes.func,
|
||||||
|
attacker: PropTypes.string.isRequired,
|
||||||
|
defender: PropTypes.string.isRequired,
|
||||||
|
|
||||||
|
disabled: PropTypes.bool.isRequired,
|
||||||
|
setDisabled: PropTypes.func.isRequired
|
||||||
|
};
|
||||||
|
|
||||||
|
function mapStoreToProps(store) {
|
||||||
|
return {
|
||||||
|
disabled: store.combat.attackDisabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function mapDispatchToProps(dispatch) {
|
||||||
|
return {
|
||||||
|
setDisabled: (disabled) => dispatch(setAttackDisabled(disabled))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AttackButton = connect(mapStoreToProps, mapDispatchToProps)(AttackButton);
|
||||||
|
|
||||||
|
export default AttackButton;
|
||||||
@@ -128,7 +128,7 @@ function mapStoreToProps(store) {
|
|||||||
|
|
||||||
function mapDispatchToProps(dispatch) {
|
function mapDispatchToProps(dispatch) {
|
||||||
return {
|
return {
|
||||||
login: (id, email, username, token) => { dispatch(login(id, email, username, token)) }
|
login: (id, email, username, token) => dispatch(login(id, email, username, token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { SET_ATTACK_DISABLED } from '../actions/combat.js';
|
||||||
|
|
||||||
|
const initialStore = {
|
||||||
|
attackDisabled: true
|
||||||
|
};
|
||||||
|
|
||||||
|
export function combatReducer(store = initialStore, action) {
|
||||||
|
switch(action.type) {
|
||||||
|
case SET_ATTACK_DISABLED: {
|
||||||
|
let newStore = JSON.parse(JSON.stringify(initialStore));
|
||||||
|
newStore.attackDisabled = action.disabled;
|
||||||
|
return newStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return store;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
import { combineReducers } from 'redux';
|
import { combineReducers } from 'redux';
|
||||||
import { accountReducer } from './accounts.js';
|
import { accountReducer } from './accounts.js';
|
||||||
|
import { combatReducer } from './combat.js';
|
||||||
|
|
||||||
//compile all reducers together
|
//compile all reducers together
|
||||||
export default combineReducers({
|
export default combineReducers({
|
||||||
account: accountReducer
|
account: accountReducer,
|
||||||
|
combat: combatReducer
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user