Skip to content

Commit 3a504cb

Browse files
author
hiukim
committed
part 2 - game lobby
1 parent 3c85a5d commit 3a504cb

File tree

4 files changed

+101
-37
lines changed

4 files changed

+101
-37
lines changed

imports/ui/App.jsx

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,51 @@
11
import React, { Component } from 'react';
22
import { createContainer } from 'meteor/react-meteor-data';
33
import Games from '../api/collections/games.js';
4+
import GameList from './GameList.jsx';
45
import GameBoard from './GameBoard.jsx';
56

67
class App extends Component {
8+
constructor(props) {
9+
super(props);
10+
this.state = {
11+
selectedGameId: null,
12+
}
13+
}
14+
15+
handleEnterGame(gameId) {
16+
this.setState({selectedGameId: gameId});
17+
}
18+
19+
handleBackToGameList() {
20+
this.setState({selectedGameId: null});
21+
}
22+
23+
selectedGame() {
24+
let selectedGame = _.find(this.props.games, (game) => {
25+
return game._id === this.state.selectedGameId;
26+
});
27+
return selectedGame;
28+
}
29+
730
render() {
8-
return (
9-
<div>
10-
{this.props.game?
11-
<GameBoard game={this.props.game}/>
12-
: null}
13-
</div>
14-
)
31+
if (this.state.selectedGameId === null) {
32+
return (
33+
<GameList
34+
games={this.props.games}
35+
enterGameHandler={this.handleEnterGame.bind(this)}/>
36+
)
37+
} else {
38+
return (
39+
<GameBoard
40+
game={this.selectedGame()}
41+
backToGameListHandler={this.handleBackToGameList.bind(this)}/>
42+
)
43+
}
1544
}
1645
}
1746

1847
export default createContainer(() => {
1948
return {
20-
game: Games.findOne()
49+
games: Games.find().fetch()
2150
};
2251
}, App);

imports/ui/GameBoard.jsx

+26-19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export default class GameBoard extends Component {
2222
});
2323
}
2424

25+
handleBackToGameList() {
26+
this.props.backToGameListHandler();
27+
}
28+
2529
renderCell(row, col) {
2630
let value = this.props.game.board[row][col];
2731
if (value === 0) return (<td>O</td>);
@@ -32,25 +36,28 @@ export default class GameBoard extends Component {
3236
}
3337
render() {
3438
return (
35-
<table className="game-board">
36-
<tbody>
37-
<tr>
38-
{this.renderCell(0, 0)}
39-
{this.renderCell(0, 1)}
40-
{this.renderCell(0, 2)}
41-
</tr>
42-
<tr>
43-
{this.renderCell(1, 0)}
44-
{this.renderCell(1, 1)}
45-
{this.renderCell(1, 2)}
46-
</tr>
47-
<tr>
48-
{this.renderCell(2, 0)}
49-
{this.renderCell(2, 1)}
50-
{this.renderCell(2, 2)}
51-
</tr>
52-
</tbody>
53-
</table>
39+
<div>
40+
<button onClick={this.handleBackToGameList.bind(this)}>Back</button>
41+
<table className="game-board">
42+
<tbody>
43+
<tr>
44+
{this.renderCell(0, 0)}
45+
{this.renderCell(0, 1)}
46+
{this.renderCell(0, 2)}
47+
</tr>
48+
<tr>
49+
{this.renderCell(1, 0)}
50+
{this.renderCell(1, 1)}
51+
{this.renderCell(1, 2)}
52+
</tr>
53+
<tr>
54+
{this.renderCell(2, 0)}
55+
{this.renderCell(2, 1)}
56+
{this.renderCell(2, 2)}
57+
</tr>
58+
</tbody>
59+
</table>
60+
</div>
5461
)
5562
}
5663
}

imports/ui/GameList.jsx

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { Component } from 'react';
2+
import Games from '../api/collections/games.js';
3+
4+
export default class GameList extends Component {
5+
handleNewGame() {
6+
let gameDoc = {
7+
board: [[null, null, null], [null, null, null], [null, null, null]]
8+
};
9+
Games.insert(gameDoc); // insert a new game document into the collection
10+
}
11+
12+
handleEnterGame(gameId) {
13+
this.props.enterGameHandler(gameId);
14+
}
15+
16+
render() {
17+
return (
18+
<div>
19+
<div>
20+
<button onClick={this.handleNewGame.bind(this)}>New Game</button>
21+
</div>
22+
23+
<div>
24+
<h1>List of games</h1>
25+
{this.props.games.map((game, index) => {
26+
return (
27+
<div key={game._id}>
28+
<span>Game {index+1}</span>
29+
<button onClick={this.handleEnterGame.bind(this, game._id)}>Enter</button>
30+
</div>
31+
)
32+
})}
33+
</div>
34+
</div>
35+
)
36+
}
37+
}

server/main.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
import { Meteor } from 'meteor/meteor';
1+
import {Meteor} from 'meteor/meteor';
22
import Games from '../imports/api/collections/games.js'; // import Games collection
33

44
Meteor.startup(() => {
5-
// code to run on server at startup
6-
7-
Games.remove({}); // remove all existing game documents
8-
9-
let gameDoc = {
10-
board: [[null, null, null], [null, null, null], [null, null, null]]
11-
};
12-
13-
Games.insert(gameDoc); // insert a new game document into the collection
145
});

0 commit comments

Comments
 (0)