-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js.jsx
137 lines (122 loc) · 3.44 KB
/
app.js.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
window.App = React.createClass({
getInitialState: function () {
return {
setsFound: [],
buildingSet: [],
showNotif: false,
notif: "",
gameWon: false
}
},
gameWon: function () {
if (this.state.setsFound.length == this.props.cards.solution.length) {
return true;
} else {
return false;
}
},
checkCurrentSet: function () {
var set = this.state.buildingSet,
duplicate = false,
isASet = window.Deck.isASet(set);
this.state.setsFound.forEach(function (oldSet) {
if (oldSet.sort().toString() === set.sort().toString()) {
duplicate = true;
isASet = false;
}
})
this.handleCurrentSetStatus(isASet, duplicate);
},
handleCurrentSetStatus: function (status, dup) {
if (status) {
this.state.setsFound.push(this.state.buildingSet);
var gameStatus = this.gameWon();
this.setState({
buildingSet: [],
showNotif: true,
notif: "Found a set!",
gameWon: gameStatus
});
} else {
var notif;
if (dup) {
notif = "You already found that set.";
} else {
notif = "That's not a set.";
}
this.setState({
buildingSet: [],
showNotif: true,
notif: notif
});
}
},
handleClick: function (cardValue) {
// this.setState({gameWon: true});
this.state.buildingSet.push(cardValue);
if (this.state.buildingSet.length === 3) {
this.checkCurrentSet();
} else {
this.state.showNotif = false;
this.forceUpdate();
}
},
handleUnclick: function (cardValue) {
var idx = this.state.buildingSet.indexOf(cardValue);
this.state.buildingSet.splice(idx, 1);
this.forceUpdate();
},
renderModal: function () {
if (this.state.gameWon) {
startParade();
return <Modal message="You won!"/>
}
},
renderNotif: function () {
var notifClass;
if (this.state.showNotif) {
notifClass = "notif show-notif";
} else {
notifClass = "notif hide-notif";
}
return notifClass;
},
showInstructions: function () {
this.setState({ showInstructions: true });
},
turnOffInstructions: function () {
this.setState({ showInstructions: false });
},
renderInstructions: function () {
if (this.state.showInstructions) {
return <Instruction turnOffInstructions={this.turnOffInstructions}/>
}
},
render: function () {
var totalSets = this.props.cards.solution.length;
return (
<div className="app">
<div className="game-stats">
Found <span className="sets-found">{this.state.setsFound.length}</span> set(s) out of {totalSets}.
</div>
<div className={this.renderNotif()}>{this.state.notif}</div>
{this.renderModal()}
{this.renderInstructions()}
<div className="cards-container">
{
this.props.cards.playableCards.map(function (card, idx) {
return <CardComponent key={idx}
card={card}
selectedCards={this.state.buildingSet}
handleClick={this.handleClick}
handleUnclick={this.handleUnclick}/>
}.bind(this))
}
</div>
<div className="how-to-play" onClick={this.showInstructions}>
How To Play Set
</div>
</div>
);
}
});