Skip to content

Instantly share code, notes, and snippets.

View ecto's full-sized avatar
🙂
building things

Cam Pedersen ecto

🙂
building things
View GitHub Profile
app.get('/top', function(req, res){
db.keys('booking:*:name', function(err,keys){
if (err) { throw err; }
if (typeof(keys.forEach) == 'undefined') { res.redirect('/battle'); }
var bookings = [];
var originalCount = 0;
var returnCount = 0;
try { keys.forEach(function(key){
key = String(key);
var split = key.split(':');
TypeError: Object 394 has no method 'forEach'
at /home/ec2-user/jail/server.js:30:8
at Client.handleReply (/usr/local/lib/node/.npm/redis-node/0.2.7/package/lib/client.js:302:5)
at Client.<anonymous> (native)
at Client.emit (events:31:17)
at Client.handleData (/usr/local/lib/node/.npm/redis-node/0.2.7/package/lib/client.js:242:18)
at Stream.<anonymous> (native)
at Stream.emit (events:31:17)
at IOWatcher.callback (net:489:16)
at node.js:773:9
@ecto
ecto / elo.js
Created January 16, 2011 02:19
function adjust(ratings) {
ratings[0] = ratings[0] ? parseFloat(ratings[0]) : 1000;
ratings[1] = ratings[1] ? parseFloat(ratings[1]) : 1000;
var winnerExpected = 1 / (1 + (Math.pow(10,(ratings[1] - ratings[0]) / 400)));
var loserExpected = 1 / (1 + (Math.pow(10,(ratings[0] - ratings[1]) / 400)));
var k = 30;
var winnerAdjustment = Math.round(ratings[0] + (k * (1 - winnerExpected)));
var loserAdjustment = Math.round(ratings[1] + (k * (0 - loserExpected)));
return [winnerAdjustment, loserAdjustment];
}
@ecto
ecto / simple.js
Created January 24, 2011 18:13
ExpressJS app shell
var express = require('express'),
app = express.createServer();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
npm install express
@ecto
ecto / process.js
Created January 24, 2011 21:35
Used to process new images into Redis for our FaceMash clone
var fs = require('fs'),
redis = require('redis-node'),
db = redis.createClient(),
exec = require('child_process').exec;
var files = fs.readdirSync('./new')
files.forEach(function(file){
db.incr('images', function(err, id){
db.set('image:' + id + ':rating', '1000');
@ecto
ecto / home.js
Created January 24, 2011 22:05
The home page for our FaceMash clone
app.get('/', function(req, res){
db.keys('image:*:location', function(err,keys){
if (err) { throw err; }
var offense = keys[Math.floor(Math.random()*keys.length)];
var defense = keys[Math.floor(Math.random()*keys.length)];
if (defense == offense) {
defense = keys[Math.floor(Math.random()*keys.length)];
}
db.mget('image:' + offense + ':location', 'image:' + offense + ':rating',
'image:' + defense + ':location', 'image:' + defense + ':rating',
<a href="/battle/<%- offense %>/<%- defense %>">
<img src="/images/<%- battle[0] %>" /><br />
Rating: <%- battle[1] %>
</a>
or
<a href="/battle/<%- defense %>/<%- offense %>">
<img src="/images/<%- battle[2] %>" /><br />
Rating: <%- battle[3] %>
</a>
app.get('/battle/:winner/:loser', function(req, res){
if (req.params.winner && req.params.loser) {
var winner = req.params.winner;
var loser = req.params.loser;
db.mget('image:' + winner + ':rating',
'image:' + loser + ':rating',
function (err, ratings) {
if (err) { throw err; }
ratings[0] = ratings[0] ? parseFloat(ratings[0]) : 1000;
ratings[1] = ratings[1] ? parseFloat(ratings[1]) : 1000;
<!doctype html>
<html>
<head>
<title>FaceMash Clone</title>
</head>
<body>
<%- body %>
</body>
</html>