Permalink
Browse files

Rw.io adn stuff

  • Loading branch information...
1 parent a998ca0 commit d0f9a8157de73919610895a3ac91b5be48ea5dae @1602 1602 committed Oct 10, 2012
@@ -0,0 +1,52 @@
+
+load('application');
+layout('application');
+
+if (!app.games) app.games = [];
+
+action('thegame', function () {
+ this.title = 'Reversi Game';
+
+ // already in game
+ if (session.color && session.game) {
+ return ok();
+ }
+
+ // pending game (was), join opponent
+ if (app.game) {
+ session.color = app.game.join(req.sessionID);
+ session.game = app.games.length;
+ socket(app.game.starter).emit('opponent joined');
+ app.games.push(app.game);
+ app.game = false;
+ }
+
+ // new game, join starter
+ else {
+ app.game = new Game;
+ session.color = app.game.join(req.sessionID);
+ session.game = app.games.length;
+ }
+
+ return ok();
+
+ function ok() {
+ render({player: session.color, game: app.games[session.game] || app.game});
+ }
+
+});
+
+action('move', function () {
+ var game = app.games[session.game];
+ var success = game.move(session.player, params);
+ if (success) {
+ var opponent = game.other(req.sessionID);
+ socket(opponent).emit('move', params);
+ socket().emit('wait');
+ if (game.end()) {
+ socket(opponent).emit('end');
+ socket().emit('end');
+ }
+ }
+});
+
@@ -0,0 +1,2 @@
+module.exports = {
+};
View
@@ -0,0 +1,51 @@
+var reversi = require(app.root + '/public/javascripts/reversi.js');
+
+function Game() {
+ this.reversi = reversi.createGame();
+}
+
+Game.prototype.join = function (id) {
+ if (!this.starter) {
+ console.log('Starter joined', id);
+ this.starter = id;
+ } else {
+ console.log('Opponent joined', id);
+ this.opponent = id;
+ }
+ return this.reversi.join();
+};
+
+Game.prototype.state = function (player) {
+ if (!this.opponent) {
+ return 'wait opponent';
+ } else if (this.reversi.can_player_move(player)) {
+ return 'move';
+ } else {
+ return 'wait';
+ }
+};
+
+Game.prototype.move = function (player, coords) {
+ this.player = player;
+ return this.reversi.move(coords);
+};
+
+Game.prototype.other = function (id) {
+ return this.opponent === id ? this.starter : this.opponent;
+};
+
+Game.prototype.end = function () {
+ return this.reversi.board.terminal_board;
+};
+
+Game.prototype.winner = function () {
+ var s = this.reversi.board.board_stats;
+ return s.b > s.w ? 'b' : 'w';
+};
+
+Game.prototype.boardToJSON = function () {
+ return JSON.stringify(this.reversi.board.position);
+};
+
+module.exports = Game;
+
@@ -0,0 +1 @@
+<div class="page-header"><h1>game#thegame</h1></div>
@@ -14,28 +14,36 @@
</div>
</div>
</div>
-
<div class="container">
- <% var flash = request.flash('info').pop(); if (flash) { %>
- <div class="alert alert-info">
- <a class="close" data-dismiss="alert">×</a>
- <%- flash %>
+ <div id="board">
+ <div class="head">
+ <div class="info">
+ </div>
+ <div id="counter">
+ <div class="black"></div>
+ <div class="delim"></div>
+ <div class="white"></div>
+ </div>
</div>
- <% } %>
-
- <% flash = request.flash('error').pop(); if (flash) { %>
- <div class="alert alert-error">
- <a class="close" data-dismiss="alert">×</a>
- <%- flash %>
- </div>
- <% }; %>
-
- <%- body %>
-
- <hr />
- <footer>
- <p>&copy; Company 2012</p>
- </footer>
+ <canvas width="300" height="300"></canvas>
+ </div>
</div>
+ <footer>
+ <p>&copy; Rolling on <a href="http://railwayjs.com">RailwayJS</a> and <a href="socket.io">Socket.IO</a>, 2012</p>
+ </footer>
</body>
-</html>
+
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
+ <script type="text/javascript" src="/javascripts/rails.js"></script>
+ <script type="text/javascript" src="/javascripts/reversi.js"></script>
+ <script type="text/javascript" src="/javascripts/reversi-cli.js"></script>
+ <script type="text/javascript" src="/socket.io/socket.io.js"></script>
+ <script>
+ var BOARD = {
+ player: '<%= player %>',
+ color: '<%= game.color || 'w' %>',
+ state: '<%= game.state(player) %>',
+ position: <%- game.boardToJSON() %>
+ };
+ </script>
+</html>
View
@@ -1,7 +1,11 @@
+
exports.routes = function (map) {
+ map.root('game#thegame');
+ map.socket('move', 'game#move');
// Generic routes. Add all your routes below this line
// feel free to remove generic routes
map.all(':controller/:action');
map.all(':controller/:action/:id');
};
+
View
@@ -1,3 +1,4 @@
+require('rw.io');
require('ejs-ext');
require('jugglingdb');
require('seedjs');
View
@@ -9,6 +9,8 @@
, "railway": ">= 0.2.6"
, "jugglingdb": ">= 0.1.0"
, "coffee-script": ">= 1.1.1"
+ , "socket.io": "latest"
+ , "rw.io": "latest"
},
"devDependencies":
{ "nodeunit": "*"
View
@@ -1,96 +0,0 @@
-<!DOCTYPE html>
-<html>
- <head>
- <title>Welcome to Railway</title>
- <link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css">
- <meta name="viewport" content="width=500, initial-scale=0.5">
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- <script type="text/javascript" src="/javascripts/rails.js"></script>
- </head>
- <body>
- <div class="container">
- <div class="page-header">
- <h1 style="text-align: center; padding: 35px;">Welcome to Railway</h1>
- </div>
- <h6 style="text-align: center;">
- <a href="/railway/environment.json" id="show-env-info-link" data-remote="true" data-jsonp="load">Information about application environment</a>
- </h6>
- <div id="env-info"></div>
- <hr />
- <div class="row">
- <div class="span8 offset4">
- <a href="http://github.com/1602/express-on-railway"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://d3nwyuy0nl342s.cloudfront.net/img/e6bef7a091f5f3138b8cd40bc3e114258dd68ddf/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub"></a>
- <div class="content">
- <h6>1. Start with <a href="http://railwayjs.com/#generators">generators</a></h6>
- <p> this is a fastest way to create application:</p>
- <pre>railway g crud post title content date:date published:boolean</pre>
- <h6>2. Then describe <a href="http://railwayjs.com/#routing">routes</a></h6>
- <p>in <code>config/routes.js</code> and remove this file (<code>public/index.html</code>)</p>
- <pre>exports.routes = function (map) {
- map.get('/', 'posts#index');
-};</pre>
- <h6>3. Design your <a href="http://railwayjs.com/#orm">database</a></h6>
- <p>in <code>db/schema.js</code> and describe models in <code>app/models/*</code></p>
- <h6>4. Keep you <a href="http://railwayjs.com/#controllers">controllers</a> thin</h6>
- <p>write tests, and good luck.<br> If you have any questions feel free to ask at <a href="http://groups.google.com/group/railwayjs">RailwayJS Google Group</a>.</p>
- <p>Track RailwayJS project state on <a href="https://trello.com/board/railwayjs/4f0a0d49128365065e008a1d">trello board</a>, vote for features, discuss. Help us to get better!</p>
- </div>
- </div>
-
- <div class="span4">
- <p>
- <h6>5. Links in sidebar</h6>
- <ul style="padding-left: 0px;">
- <li><a href="http://railwayjs.com">RailwayJS project docs</a></li>
- <li><a href="http://modules.node-js.ru">NodeJS modules rating</a></li>
- <li><a href="http://expressjs.com">ExpressJS project homepage</a></li>
- <li><a href="http://mongoosejs.com">MongooseJS ORM project homepage</a></li>
- <li><a href="http://railwayjs.com/juggling.html">Try JugglingDB in your browser, docs</a></li>
- </ul>
- </p>
- </div>
-
- </div>
- <footer>
- </footer>
- <script>
- function load(data) {
- $('#show-env-info-link').hide();
- if (data.forbidden) {
- return '';
- }
- var html = '<table class=".zebra-striped">';
- html += makeList('Versions', data.versions);
- html += makeList('Settings', data.settings);
- html += makeList('Application', data.application);
- html += makeList('ENV', data.env);
- html += '</table>';
-
- $('#env-info').html(html);
- }
-
- function makeList(title, obj) {
- var res = '<tr><td colspan="2"><h6>' + title + '</h6></td></tr>';
- for (var i in obj) {
- res += '<tr><td>' + i + '</td><td>' + toS(obj[i]) + '</td></tr>';
- }
- return res;
- }
-
- function toS(obj) {
- if (obj instanceof Array) {
- return obj.join('<br />');
- } else if (typeof obj === 'object') {
- var s = [];
- for (var i in obj) {
- s.push(i + ': ' + obj[i]);
- }
- return s.join(', ');
- } else {
- return obj;
- }
- }
- </script>
- </div>
- </body>
-</html>
Oops, something went wrong.

0 comments on commit d0f9a81

Please sign in to comment.