@@ -3,34 +3,76 @@ import Games from '../api/collections/games.js';
3
3
4
4
export default class GameList extends Component {
5
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
6
+ Games . newGame ( this . props . user ) ;
7
+ }
8
+
9
+ handleLeaveGame ( gameId ) {
10
+ Games . leaveGame ( gameId , this . props . user ) ;
11
+ }
12
+
13
+ handleJoinGame ( gameId ) {
14
+ Games . joinGame ( gameId , this . props . user ) ;
10
15
}
11
16
12
17
handleEnterGame ( gameId ) {
13
18
this . props . enterGameHandler ( gameId ) ;
14
19
}
15
20
21
+ myCurrentGameId ( ) {
22
+ // find game where the user is currently in
23
+ let game = _ . find ( this . props . games , ( game ) => {
24
+ return _ . find ( game . players , ( player ) => {
25
+ return player . userId === this . props . user . _id ;
26
+ } ) !== undefined ;
27
+ } ) ;
28
+ if ( game === undefined ) return null ;
29
+ return game . _id ;
30
+ }
31
+
32
+ renderPlayers ( game ) {
33
+ let player1 = game . players . length > 0 ? game . players [ 0 ] . username : '' ;
34
+ let player2 = game . players . length > 1 ? game . players [ 1 ] . username : '' ;
35
+ return (
36
+ < span > [{ player1 } ] vs [{ player2 } ]</ span >
37
+ )
38
+ }
39
+
16
40
render ( ) {
17
41
return (
18
42
< div >
19
43
< div >
20
- < button onClick = { this . handleNewGame . bind ( this ) } > New Game</ button >
21
- </ div >
22
-
23
- < div >
24
44
< h1 > List of games</ h1 >
25
45
{ this . props . games . map ( ( game , index ) => {
26
46
return (
27
47
< div key = { game . _id } >
28
48
< span > Game { index + 1 } </ span >
29
- < button onClick = { this . handleEnterGame . bind ( this , game . _id ) } > Enter</ button >
49
+ { this . renderPlayers ( game ) }
50
+
51
+ { /* can leave only if user is in the game, and the game is not started */ }
52
+ { this . myCurrentGameId ( ) === game . _id && game . players . length < 2 ? (
53
+ < button onClick = { this . handleLeaveGame . bind ( this , game . _id ) } > Leave</ button >
54
+ ) : null }
55
+
56
+ { /* can join only if user is not in any game, and the game is not started */ }
57
+ { this . myCurrentGameId ( ) === null && game . players . length < 2 ? (
58
+ < button onClick = { this . handleJoinGame . bind ( this , game . _id ) } > Join</ button >
59
+ ) : null }
60
+
61
+ { /* can enter only if the game is started */ }
62
+ { game . players . length === 2 ? (
63
+ < button onClick = { this . handleEnterGame . bind ( this , game . _id ) } > Enter</ button >
64
+ ) : null }
30
65
</ div >
31
66
)
32
67
} ) }
33
68
</ div >
69
+
70
+ { /* Only show new game button if player is not in any room */ }
71
+ { this . myCurrentGameId ( ) === null ? (
72
+ < div >
73
+ < button onClick = { this . handleNewGame . bind ( this ) } > New Game</ button >
74
+ </ div >
75
+ ) : null }
34
76
</ div >
35
77
)
36
78
}
0 commit comments