forked from theganyo/word-fling
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 640af9f
Showing
31 changed files
with
2,638 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
### Node template | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Dependency directory | ||
node_modules | ||
|
||
# IDE files | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright (c) 2015, Scott Ganyo <[email protected]> | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE | ||
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Word-Fling | ||
------------ | ||
|
||
A simple Node-based word game using Redis. | ||
|
||
|
||
### Redis Schema | ||
|
||
#### word_dictionary (set) | ||
|
||
* values: words | ||
|
||
#### players (hash) | ||
|
||
* key: email | ||
* type: set | ||
* value: | ||
type: hash | ||
* password_hash | ||
* notifications | ||
* type: pub/sub | ||
|
||
#### logins (set) | ||
|
||
* key: email:password_hash | ||
|
||
#### leaderboard (zset) | ||
|
||
* key: email | ||
* value: wins | ||
|
||
#### tile_scores (hash) | ||
|
||
* key: letter | ||
* value: score | ||
|
||
#### games (list) | ||
|
||
* game | ||
* letter_bag | ||
* type: list | ||
* values: tiles | ||
* turns | ||
* type: list | ||
* values: | ||
* players | ||
* type: list | ||
* player | ||
* tray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
var app = require('commander'); | ||
var _ = require('lodash'); | ||
var util = require('util'); | ||
var dictionary = require('../lib/dictionary'); | ||
var tileBag = require('../lib/tile_bag'); | ||
var player = require('../lib/player'); | ||
|
||
// set up commands | ||
|
||
app | ||
.command('ping') | ||
.description('verify the Redis server is available as configured') | ||
.action(function() { | ||
var redisConfig = require('../config/redis_config'); | ||
console.log('attempting to contact using Redis config: %s', JSON.stringify(redisConfig, null, 2)); | ||
var redisClient = require('../lib/redis'); | ||
redisClient.ping(function(err, result) { | ||
if (err) { return console.log(err.stack); } | ||
console.log('result: %s', result); | ||
process.exit(); | ||
}); | ||
}); | ||
|
||
app | ||
.command('import-dictionary <file>') | ||
.description('import word dictionary from a file (eg. /usr/share/dict/words)') | ||
.action(function(file) { | ||
dictionary.importFile(file, function(err, numRecords) { | ||
print(err, util.format('Import finished. %d words in dictionary.', numRecords)); | ||
}); | ||
}); | ||
|
||
app | ||
.command('lookup-word <word>') | ||
.description('lookup word in the dictionary') | ||
.action(print(dictionary.isValidWord)); | ||
|
||
app | ||
.command('score-word <file>') | ||
.description('score a word') | ||
.action(print(tileBag.scoreWord)); | ||
|
||
app | ||
.command('create-player <email> <password>') | ||
.description('create a new player') | ||
.action(print(player.createPlayer)); | ||
|
||
app | ||
.command('get-player <email>') | ||
.description('get a player') | ||
.action(print(player.getPlayer)); | ||
|
||
app | ||
.command('check-password <email> <password>') | ||
.description('validate a player\'s password') | ||
.action(print(player.isValidPassword)); | ||
|
||
app | ||
.command('watch-game <gameId>') | ||
.option('-t, --test', 'watch on test db') | ||
.description('watch a game being played. specify a game number or \'*\' to watch all games') | ||
.action(watchGame); | ||
|
||
app | ||
.command('play-game') | ||
.description('start a game') | ||
.action(playGame); | ||
|
||
// parse command line and run commands | ||
|
||
app.parse(process.argv); | ||
|
||
|
||
// display help as needed | ||
|
||
if (!app.runningCommand) { | ||
var commands = app.commands.map(function(command) { return command._name; }); | ||
if (!_.contains(commands, app.rawArgs[2])) { | ||
app.help(); | ||
} | ||
} | ||
|
||
// print result of a called function | ||
|
||
function print(func) { | ||
function printResult(err, result) { | ||
if (err) { | ||
console.log(err.stack); | ||
process.exit(1); | ||
} else { | ||
console.log(result); | ||
process.exit(0); | ||
} | ||
} | ||
return function doIt(arg1, arg2) { | ||
var args = [ arg1 ]; | ||
if (typeof arg2 === 'string') { args.push(arg2) } | ||
args.push(printResult); | ||
var result = func.apply(this, args); | ||
if (result) { printResult(result); } | ||
} | ||
} | ||
|
||
function watchGame(gameId, options) { | ||
|
||
var redis = options.test ? require('../test/redis') : require('../lib/redis'); | ||
redis.on('pmessage', function(pattern, channel, message) { | ||
|
||
var msg = JSON.parse(message); | ||
console.log('%s %s', channel, msg.event); | ||
|
||
switch (msg.event) { | ||
|
||
case 'new game': | ||
console.log(' Players: %s', Object.keys(msg.playerScores).join(', ')); | ||
break; | ||
|
||
case 'end turn': | ||
console.log(' %s played word "%s" for a score of %s', msg.lastTurn.playerId, msg.lastTurn.word, msg.lastTurn.score); | ||
console.log(' Scores: %j', msg.playerScores); | ||
break; | ||
|
||
case 'end game': | ||
console.log(' Final score: %j', msg.playerScores); | ||
} | ||
}); | ||
|
||
redis.psubscribe('game:' + gameId); | ||
console.log('watching game: %s', gameId); | ||
} | ||
|
||
function playGame() { | ||
ask('how many players?') | ||
repeat(numPlayers(), 'player id?') | ||
newConnection | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var config = { | ||
host: '127.0.0.1', | ||
port: 6379, | ||
options: { | ||
//auth_pass: 'password' // enable as needed | ||
}, | ||
db: 1 // selected Redis database | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Provides access to Dictionary functions. | ||
Redis Key: | ||
dictionary | ||
Redis Schema: | ||
Set of words | ||
*/ | ||
'use strict'; | ||
|
||
module.exports = { | ||
importFile: importFile, | ||
isValidWord: isValidWord, | ||
addWord: addWord | ||
}; | ||
|
||
var redis = require('./redis'); | ||
var DICTIONARY_KEY = 'dictionary'; | ||
|
||
// Public | ||
|
||
function importFile(dictionaryFile, cb) { | ||
|
||
var util = require('util'); | ||
var fs = require('fs'); | ||
var readline = require('readline'); | ||
|
||
var rd = readline.createInterface({ | ||
input: fs.createReadStream(dictionaryFile), | ||
output: process.stdout, | ||
terminal: false | ||
}); | ||
|
||
rd.on('line', function onLine(line) { | ||
var word = line.split('/')[0]; // remove postfix (for SCOWL files) | ||
addWord(word, function(err) { | ||
if (err) { return cb(err); } | ||
}); | ||
}); | ||
|
||
rd.on('close', function onClose() { | ||
redis.scard(DICTIONARY_KEY, cb); | ||
}); | ||
} | ||
|
||
function isValidWord(word, cb) { | ||
redis.sismember(DICTIONARY_KEY, word, function(err, result) { | ||
cb(err, !!result); // convert word to boolean | ||
}) | ||
} | ||
|
||
function addWord(word, cb) { | ||
redis.sadd(DICTIONARY_KEY, word, cb); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
|
||
makeError: makeError, | ||
|
||
inactiveGame: "Sorry, this game is not longer active.", | ||
notYourTurn: "Sorry, it's not your turn.", | ||
notAWord: "%s isn't in the dictionary", | ||
cantPlayLetters: "Your tray doesn't have all these letters: %s", | ||
notAValidPlay: "Your word must include letters from the previous turn: %s or %s" | ||
}; | ||
|
||
var util = require('util'); | ||
var debug = require('debug')('word-fling:errors'); | ||
|
||
function makeError(code, params) { | ||
var message = util.format.apply(this, arguments); | ||
debug('error: %s', message); | ||
return new Error(message); | ||
} |
Oops, something went wrong.