-
Notifications
You must be signed in to change notification settings - Fork 1
/
leaderboard.jsx
199 lines (171 loc) · 5.24 KB
/
leaderboard.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Port of the leaderboard example to use React for rendering.
*
* This directive is necessary to enable preprocessing of JSX tags:
* @jsx React.DOM
*/
// Set up a collection to contain player information. On the server,
// it is backed by a MongoDB collection named "players".
Players = new Meteor.Collection("players");
var User = React.createClass({
mixins: [ReactMeteor.Mixin],
getMeteorState: function() {
return { user: Meteor.isClient && !Meteor.loggingIn() ? Meteor.user() : null };
},
render: function(){
if(this.state.user){
return <p>Welcome, { this.state.user.emails[0].address }</p>;
} else {
return <p>Welcome, guest</p>
}
}
})
var Leaderboard = React.createClass({
mixins: [ReactMeteor.Mixin],
getMeteorState: function() {
var selectedPlayer = Players.findOne(Session.get("selected_player"));
return {
players: Players.find({}, {sort: {score: -1, name: 1}}).fetch(),
selectedPlayer: selectedPlayer,
selectedName: selectedPlayer && selectedPlayer.name
};
},
addFivePoints: function() {
Players.update(Session.get("selected_player"), {$inc: {score: 5}});
},
renderPlayer: function(model) {
return <Player id={model._id} name={model.name} score={model.score} player_id={model.player_id} />;
},
render: function() {
var children = [
<div className="leaderboard" id="leaderboard">
{ this.state.players.map(this.renderPlayer) }
</div>
];
if (this.state.selectedName) {
children.push(
<div className="details">
<div className="name">{this.state.selectedName}</div>
<input
type="button"
className="inc"
value="Give 5 points"
onClick={this.addFivePoints}
/>
</div>
);
} else {
children.push(
<div className="none">Click a player to select</div>
);
}
return <div><User />{ children }</div>;
}
});
var Player = React.createClass({
mixins: [ReactMeteor.Mixin],
getMeteorState: function() {
return {
isSelected: Session.equals("selected_player", this.props.id)
};
},
select: function() {
Session.set("selected_player", this.props.id);
},
render: function() {
var className = "player";
if (this.state.isSelected) {
className += " selected";
}
return (
<div className={className} onClick={this.select}>
<a href={"player/" + this.props.player_id} className="name">{this.props.name}</a>
<span className="score">{this.props.score}</span>
</div>
);
}
});
var PlayerViewRootComponent = React.createClass({
mixins: [ReactMeteor.Mixin],
getMeteorState: function() {
return {player: Players.findOne({player_id: this.props.player_id})};
},
addFivePoints: function() {
Players.update({_id: this.state.player._id}, {$inc: {score: 5}});
},
render: function(){
return (
<div>
<User />
<h1>{this.state.player.name} has {this.state.player.score} points.</h1>
<input
type="button"
className="inc"
value="Give 5 points"
onClick={this.addFivePoints}
/>
</div>
);
}
});
if (Meteor.isClient) {
Meteor.Router.add({
'/': function() {
React.renderComponent(
<Leaderboard />,
document.body
);
},
'/player/:player_id': function(id) {
if(Players.findOne({player_id: id})){
React.renderComponent(
<PlayerViewRootComponent player_id={id} />,
document.body
);
}
},
'*': 'not_found'
});
Meteor.startup(function() {
});
}
// On server startup, create some players if the database is empty.
if (Meteor.isServer) {
Meteor.startup(function () {
var head = "";
var manifest = WebApp.clientProgram.manifest;
for (var i=0; i<manifest.length; i++){
if(manifest[i].where === "client"){
if(manifest[i].type === "js"){
head = head + "<script type='text/javascript' src='" + manifest[i].url + "'></script>\n";
} else if (manifest[i].type === "css"){
head = "<link rel='stylesheet' href='" + manifest[i].url + "'/>\n" + head;
}
}
}
Players.remove({});
if (Players.find().count() === 0) {
var names = ["Ada Lovelace",
"Grace Hopper",
"Marie Curie",
"Carl Friedrich Gauss",
"Nikola Tesla",
"Claude Shannon"];
for (var i = 0; i < names.length; i++)
Players.insert({player_id: names[i].toLowerCase().replace(/ /g, "-"), name: names[i], score: Math.floor(Random.fraction()*10)*5});
}
Session = ServerSession;
React.renderComponentToString(<Leaderboard />, function(html){
Meteor.Router.add('/', function() {
return "<head><title>" + "Leaderboard" + "</title>" + head + "</head><body>" + html + "</body>";
});
});
Players.find().map(function(player){
React.renderComponentToString(<PlayerViewRootComponent player_id={player.player_id} />, function(html){
Meteor.Router.add("/player/" + player.player_id, function() {
return "<head><title>" + player.name + "</title>" + head + "</head><body>" + html + "</body>";
});
});
});
});
}