Began work on spying code

This commit is contained in:
2019-06-04 13:03:25 +10:00
parent e248003478
commit eb9c42ca56
14 changed files with 215 additions and 37 deletions
+18 -4
View File
@@ -233,16 +233,30 @@ class Profile extends React.Component {
setWarning={this.setWarning.bind(this)}
attacker={this.props.account.username}
defender={this.props.profile.username}
token={this.props.account.token}
/>
statusRequest={'/attackstatusrequest'}
attackRequest={'/attackrequest'}
pendingStatus={'attacking'}
pendingMsg={'Your soldiers are attacking'}
parseUnits={(json) => json.soldiers}
>Attack</AttackButton>
</div>
<div className='row'>
<p className='col'>Soldiers:</p>
<p className='col'>{this.props.profile.soldiers}</p>
<div className='col' />
<div className='col' />
<AttackButton
className='col'
style={{flex: '2 1 2%'}}
setWarning={this.setWarning.bind(this)}
attacker={this.props.account.username}
defender={this.props.profile.username}
statusRequest={'/spystatusrequest'}
attackRequest={'/spyrequest'}
pendingStatus={'spying'}
pendingMsg={'Your spies are spying on'}
parseUnits={(json) => json.spies}
>Send Spies</AttackButton>
</div>
<div className='row'>
+14 -10
View File
@@ -6,11 +6,11 @@ class AttackButton extends React.Component {
constructor(props) {
super(props);
this.state = {
soldiers: 0, //NOTE: not stored in profile afterall
units: 0,
message: ''
};
this.sendRequest('/attackstatusrequest', {attacker: this.props.attacker}, this.attackStatus.bind(this));
this.sendRequest(this.props.statusRequest, {/* SO MUCH FOR DEFAULT ARGUMENTS IN NODE */}, this.attackStatus.bind(this));
this.sendRequest('/profilerequest', {username: this.props.attacker}, this.profileData.bind(this));
}
@@ -20,22 +20,21 @@ class AttackButton extends React.Component {
<p className={this.props.className} style={this.props.style}>{this.state.message}</p>
);
} else {
//inject something extra
let onClick = (e) => {
this.sendRequest('/attackrequest', {attacker: this.props.attacker, defender: this.props.defender}, this.attackStatus.bind(this));
this.sendRequest(this.props.attackRequest, {attacker: this.props.attacker, defender: this.props.defender}, this.attackStatus.bind(this));
if (this.props.onClick) {
this.props.onClick(e);
this.props.onClick(e); //inject something extra
}
};
return (
<button className={this.props.className} style={this.props.style} onClick={onClick} disabled={!this.state.soldiers}>Attack</button>
<button className={this.props.className} style={this.props.style} onClick={onClick} disabled={!this.state.units}>{this.props.children}</button>
);
}
}
//gameplay functions
sendRequest(url, args = {}, onSuccess) { //send a unified request, using my credentials
sendRequest(url, args, onSuccess) { //send a unified request, using my credentials
//build the XHR
let xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
@@ -62,13 +61,13 @@ class AttackButton extends React.Component {
}
attackStatus(json) {
if (json.status === 'attacking') {
this.setState({ message: `Your soldiers are attacking ${json.defender}` });
if (json.status === this.props.pendingStatus) {
this.setState({ message: `${this.props.pendingMsg} ${json.defender}` });
}
}
profileData(json) {
this.setState({ soldiers: json.soldiers });
this.setState({units: this.props.parseUnits(json)});
}
};
@@ -78,6 +77,11 @@ AttackButton.propTypes = {
attacker: PropTypes.string.isRequired,
defender: PropTypes.string.isRequired,
statusRequest: PropTypes.string.isRequired,
attackRequest: PropTypes.string.isRequired,
pendingStatus: PropTypes.string.isRequired,
pendingMsg: PropTypes.string.isRequired,
parseUnits: PropTypes.func.isRequired,
className: PropTypes.string,
style: PropTypes.object,
+2 -2
View File
@@ -30,12 +30,12 @@ class Login extends React.Component {
<form action='/loginrequest' method='post' onSubmit={ this.submit.bind(this) } >
<div>
<label for='email'>Email:</label>
<label htmlFor='email'>Email:</label>
<input id='email' type='text' name='email' value={this.state.email} onChange={ this.updateEmail.bind(this) } />
</div>
<div>
<label for='password'>Password:</label>
<label htmlFor='password'>Password:</label>
<input id='password' type='password' name='password' value={this.state.password} onChange={ this.updatePassword.bind(this) } />
</div>
+2 -2
View File
@@ -30,12 +30,12 @@ class PasswordChange extends React.Component {
<form action='/passwordchangerequest' method='post' onSubmit={this.submit.bind(this)}>
<div>
<label for='password'>Password:</label>
<label htmlFor='password'>Password:</label>
<input id='password' type='password' name='password' value={this.state.password} onChange={this.updatePassword.bind(this)} />
</div>
<div>
<label for='retype'>Retype Password:</label>
<label htmlFor='retype'>Retype Password:</label>
<input id='retype' type='password' name='retype' value={this.state.retype} onChange={this.updateRetype.bind(this)} />
</div>
+1 -1
View File
@@ -27,7 +27,7 @@ class PasswordRecover extends React.Component {
<form action='/passwordrecoverrequest' method='post' onSubmit={this.submit.bind(this)}>
<div>
<label for='email'>Email:</label>
<label htmlFor='email'>Email:</label>
<input id='email' type='text' name='email' value={this.state.email} onChange={this.updateEmail.bind(this)} />
</div>
+2 -2
View File
@@ -29,12 +29,12 @@ class PasswordReset extends React.Component {
<form action='/passwordresetrequest' method='post' onSubmit={this.submit.bind(this)}>
<div>
<label for='password'>Password:</label>
<label htmlFor='password'>Password:</label>
<input id='password' type='password' name='password' value={this.state.password} onChange={this.updatePassword.bind(this)} />
</div>
<div>
<label for='retype'>Retype Password:</label>
<label htmlFor='retype'>Retype Password:</label>
<input id='retype' type='password' name='retype' value={this.state.retype} onChange={this.updateRetype.bind(this)} />
</div>
+4 -4
View File
@@ -30,22 +30,22 @@ class Signup extends React.Component {
<form action='/signuprequest' method='post' onSubmit={this.submit.bind(this)}>
<div>
<label for='email'>Email:</label>
<label htmlFor='email'>Email:</label>
<input id='email' type='text' name='email' value={this.state.email} onChange={this.updateEmail.bind(this)} />
</div>
<div>
<label for='username'>User Name:</label>
<label htmlFor='username'>User Name:</label>
<input id='username' type='text' name='username' value={this.state.username} onChange={this.updateUsername.bind(this)} />
</div>
<div>
<label for='password'>Password:</label>
<label htmlFor='password'>Password:</label>
<input id='password' type='password' name='password' value={this.state.password} onChange={this.updatePassword.bind(this)} />
</div>
<div>
<label for='retype'>Retype Password:</label>
<label htmlFor='retype'>Retype Password:</label>
<input id='retype' type='password' name='retype' value={this.state.retype} onChange={this.updateRetype.bind(this)} />
</div>