Skip to content

Commit 3c85a5d

Browse files
author
hiukim
committed
part 1 - data sync
0 parents  commit 3c85a5d

File tree

16 files changed

+247
-0
lines changed

16 files changed

+247
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.meteor/.finished-upgraders

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file contains information which helps Meteor properly upgrade your
2+
# app when you run 'meteor update'. You should check it into version control
3+
# with your project.
4+
5+
notices-for-0.9.0
6+
notices-for-0.9.1
7+
0.9.4-platform-file
8+
notices-for-facebook-graph-api-2
9+
1.2.0-standard-minifiers-package
10+
1.2.0-meteor-platform-split
11+
1.2.0-cordova-changes
12+
1.2.0-breaking-changes
13+
1.3.0-split-minifiers-package
14+
1.4.0-remove-old-dev-bundle-link

.meteor/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local

.meteor/.id

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file contains a token that is unique to your project.
2+
# Check it into your repository along with the rest of this directory.
3+
# It can be used for purposes such as:
4+
# - ensuring you don't accidentally deploy one app on top of another
5+
# - providing package authors with aggregated statistics
6+
7+
7m234ynbgevk1q3x9sj

.meteor/packages

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Meteor packages used by this project, one per line.
2+
# Check this file (and the other files in this directory) into your repository.
3+
#
4+
# 'meteor add' and 'meteor remove' will edit this file for you,
5+
# but you can also edit it by hand.
6+
7+
[email protected] # Packages every Meteor app needs to have
8+
[email protected] # Packages for a great mobile UX
9+
[email protected] # The database Meteor supports right now
10+
[email protected] # Compile .html files into Meteor Blaze views
11+
[email protected] # Reactive variable for tracker
12+
[email protected] # Helpful client-side library
13+
[email protected] # Meteor's client-side reactive programming library
14+
15+
[email protected] # CSS minifier run for production mode
16+
[email protected] # JS minifier run for production mode
17+
[email protected] # ECMAScript 5 compatibility for older browsers.
18+
[email protected] # Enable ECMAScript2015+ syntax in app code
19+
20+
[email protected] # Publish all data to the clients (for prototyping)
21+
[email protected] # Allow all DB writes from clients (for prototyping)
22+
react-meteor-data

.meteor/platforms

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
server
2+
browser

.meteor/release

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

.meteor/versions

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+

client/main.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
table.game-board tr td {
2+
border: 1px solid black;
3+
width: 30px;
4+
height: 30px;
5+
text-align: center;
6+
}

client/main.html

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<head>
2+
<title>my-game-site</title>
3+
</head>
4+
<body>
5+
<div id="render-target"></div>
6+
</body>

client/main.jsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import { Meteor } from 'meteor/meteor';
3+
import { render } from 'react-dom';
4+
import App from '../imports/ui/App.jsx';
5+
6+
Meteor.startup(() => {
7+
render(<App />, document.getElementById('render-target'));
8+
});

imports/api/collections/games.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Mongo } from 'meteor/mongo';
2+
3+
export default Games = new Mongo.Collection('games');

imports/ui/App.jsx

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React, { Component } from 'react';
2+
import { createContainer } from 'meteor/react-meteor-data';
3+
import Games from '../api/collections/games.js';
4+
import GameBoard from './GameBoard.jsx';
5+
6+
class App extends Component {
7+
render() {
8+
return (
9+
<div>
10+
{this.props.game?
11+
<GameBoard game={this.props.game}/>
12+
: null}
13+
</div>
14+
)
15+
}
16+
}
17+
18+
export default createContainer(() => {
19+
return {
20+
game: Games.findOne()
21+
};
22+
}, App);

imports/ui/GameBoard.jsx

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React, { Component } from 'react';
2+
3+
export default class GameBoard extends Component {
4+
currentPlayer() {
5+
// determine the current player by counting the filled cells
6+
// if even, then it's first player, otherwise it's second player
7+
let filledCount = 0;
8+
for (let r = 0; r < 3; r++) {
9+
for (let c = 0; c < 3; c++) {
10+
if (this.props.game.board[r][c] !== null) filledCount++;
11+
}
12+
}
13+
return (filledCount % 2 === 0? 0: 1);
14+
}
15+
16+
handleCellClick(row, col) {
17+
let currentPlayer = this.currentPlayer();
18+
let game = this.props.game;
19+
game.board[row][col] = currentPlayer;
20+
Games.update(game._id, {
21+
$set: {board: game.board}
22+
});
23+
}
24+
25+
renderCell(row, col) {
26+
let value = this.props.game.board[row][col];
27+
if (value === 0) return (<td>O</td>);
28+
if (value === 1) return (<td>X</td>);
29+
if (value === null) return (
30+
<td onClick={this.handleCellClick.bind(this, row, col)}></td>
31+
);
32+
}
33+
render() {
34+
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>
54+
)
55+
}
56+
}

package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "my-game-site",
3+
"private": true,
4+
"scripts": {
5+
"start": "meteor run"
6+
},
7+
"dependencies": {
8+
"meteor-node-stubs": "~0.2.0",
9+
"react": "^15.3.0",
10+
"react-addons-pure-render-mixin": "^15.3.0",
11+
"react-dom": "^15.3.0"
12+
}
13+
}

server/main.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Meteor } from 'meteor/meteor';
2+
import Games from '../imports/api/collections/games.js'; // import Games collection
3+
4+
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
14+
});

0 commit comments

Comments
 (0)