Skip to content

Commit

Permalink
MainMenu: fix Trade
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Jul 21, 2019
1 parent 52179a6 commit ba3609b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
20 changes: 16 additions & 4 deletions src/sock.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const buffer = [];
let socket = new WebSocket(endpoint),
attempts = 0,
attemptTimeout = 0,
pvp = false;
export let trade = false;
pvp = null;
export let trade = null;
const sockEvents = {
clear: () => store.store.dispatch(store.clearChat('Main')),
passchange: data => {
Expand Down Expand Up @@ -150,9 +150,11 @@ const sockEvents = {
},
pvpgive: data => {
if (pvp) {
pvp = false;
pvp = null;
store.store.dispatch(
store.doNav(import('./views/Match'), { game: new Game(data.data) }),
store.doNav(import('./views/Match'), {
game: new Game(data.data),
}),
);
}
},
Expand Down Expand Up @@ -273,3 +275,13 @@ export function sendChallenge(foe) {
userEmit('foewant', msg);
pvp = foe;
}
export function offerTrade(f) {
trade = f;
userEmit('tradewant', { f });
}
export function cancelTrade() {
if (trade) {
trade = null;
sock.userEmit('canceltrade');
}
}
38 changes: 27 additions & 11 deletions src/views/MainMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ export default connect(({ user, opts }) => ({
},
codegold: data => {
this.props.dispatch(
store.updateUser({ gold: this.props.user.gold + data.g }),
store.updateUser({
gold: this.props.user.gold + data.g,
}),
);
this.props.dispatch(
store.chat(
Expand Down Expand Up @@ -337,7 +339,11 @@ export default connect(({ user, opts }) => ({
function changeFunc() {
if (self.state.newpass === self.state.newpass2) {
sock.userEmit('passchange', { p: self.state.newpass });
self.setState({ changepass: false, newpass: '', newpass2: '' });
self.setState({
changepass: false,
newpass: '',
newpass2: '',
});
} else {
self.setState({ newpass: '', newpass2: '' });
self.props.dispatch(
Expand Down Expand Up @@ -646,7 +652,9 @@ export default connect(({ user, opts }) => ({
(self.props.user && self.props.user.name);
if (name)
this.props.dispatch(
store.doNav(import('./Library'), { name }),
store.doNav(import('./Library'), {
name,
}),
);
}}
onMouseOver={this.mkSetTip(
Expand Down Expand Up @@ -675,11 +683,11 @@ export default connect(({ user, opts }) => ({
<input
type="button"
value="Trade"
onClick={foe => {
sock.trade =
typeof foe === 'string' ? foe : self.props.foename;
sock.userEmit('tradewant', { f: sock.trade });
}}
onClick={foe =>
sock.offerTrade(
typeof foe === 'string' ? foe : self.props.foename,
)
}
onMouseOver={this.mkSetTip(
'Initiate trading cards with another player',
)}
Expand All @@ -693,7 +701,9 @@ export default connect(({ user, opts }) => ({
type="button"
value="Reward"
onClick={() => {
sock.userEmit('codesubmit', { code: self.props.foename });
sock.userEmit('codesubmit', {
code: self.props.foename,
});
}}
onMouseOver={this.mkSetTip('Redeem a reward code')}
style={{
Expand Down Expand Up @@ -752,7 +762,11 @@ export default connect(({ user, opts }) => ({
<input
placeholder="New Password"
value={this.state.newpass}
onChange={e => this.setState({ newpass: e.target.value })}
onChange={e =>
this.setState({
newpass: e.target.value,
})
}
onKeyPress={e => {
if (e.which == 13) changeFunc();
}}
Expand All @@ -766,7 +780,9 @@ export default connect(({ user, opts }) => ({
placeholder="Confirm New"
value={this.state.newpass2}
onChange={e =>
this.setState({ newpass2: e.target.value })
this.setState({
newpass2: e.target.value,
})
}
onKeyPress={e => {
if (e.which == 13) changeFunc();
Expand Down
5 changes: 1 addition & 4 deletions src/views/Match.js
Original file line number Diff line number Diff line change
Expand Up @@ -1178,10 +1178,7 @@ export default connect(({ user }) => ({ user }))(
}

componentDidMount() {
if (sock.trade) {
sock.userEmit('canceltrade');
delete sock.trade;
}
sock.cancelTrade();
if (!this.props.replay) {
if (!this.props.game.data.spectate) {
document.addEventListener('keydown', this.onkeydown);
Expand Down

0 comments on commit ba3609b

Please sign in to comment.