Skip to content
This repository has been archived by the owner on Aug 4, 2018. It is now read-only.

Commit

Permalink
start coco backend rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
aeosynth committed Apr 29, 2013
1 parent e0046cf commit 995bd7b
Show file tree
Hide file tree
Showing 35 changed files with 769 additions and 649 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
node_modules
.DS_Store
*.swp
cards.json
sets.json
cube.json
data
50 changes: 0 additions & 50 deletions app.js

This file was deleted.

26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var PORT = 1337;

var http = require('http');
var express = require('express');
var engine = require('engine.io');
var router = require('./lib/router');

var app = express()
.use(express.static(__dirname + '/public'))
.use(express.bodyParser())
.post('/create', function(req, res) {
var id = router.create(req.body);
res.send(id);
})
.get('/q/:qid', function(req, res) {
res.sendfile(__dirname + '/public/index.html');
})
;

var httpServer = http.createServer(app);

engine.attach(httpServer).on('connection', router.connect);

httpServer.listen(PORT, function() {
console.log('http://localhost:%d', PORT);
});
26 changes: 26 additions & 0 deletions lib/_.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var rand, _;
rand = function(it){
return Math.random() * it | 0;
};
module.exports = _ = {
next: function(arr, delta, index){
var length;
length = arr.length;
index = (index + delta + length) % length;
return arr[index];
},
rand: rand,
shuffle: function(arr){
return _.choose(arr.length, arr);
},
choose: function(n, arr){
var i, end, j, ref$;
i = arr.length;
end = i - n || 1;
while (i > end) {
j = rand(i--);
ref$ = [arr[j], arr[i]], arr[i] = ref$[0], arr[j] = ref$[1];
}
return arr.slice(-n);
}
};
33 changes: 33 additions & 0 deletions lib/bot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var Player, nop, Bot;
Player = require('./player');
nop = function(){};
Bot = (function(superclass){
Bot.displayName = 'Bot';
var prototype = extend$(Bot, superclass).prototype, constructor = Bot;
function Bot(){
superclass.call(this);
}
prototype.isBot = true;
prototype.name = 'bot';
prototype.send = nop;
prototype.sendPack = function(){
var pack;
pack = this.packs[0];
if (pack.length === 1) {
return this.pick(0, true);
} else {
return process.nextTick(bind$(this, 'autopick'));
}
};
return Bot;
}(Player));
module.exports = Bot;
function extend$(sub, sup){
function fun(){} fun.prototype = (sub.superclass = sup).prototype;
(sub.prototype = new fun).constructor = sub;
if (typeof sup.extended == 'function') sup.extended(sub);
return sub;
}
function bind$(obj, key){
return function(){ return obj[key].apply(obj, arguments) };
}
22 changes: 22 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var request, rand, couch;
request = require('request');
rand = require('./_').rand;
couch = require('../data/couch');
module.exports = function(data){
var options;
options = importAll$({}, couch);
data._id = data.end + rand(1e9).toString(16).slice(-6);
options.uri += data._id;
options.json = data;
console.log(options.uri);
return request(options, function(err, res, body){
if (err) {
throw err;
}
return console.log(body);
});
};
function importAll$(obj, src){
for (var key in src) obj[key] = src[key];
return obj;
}
139 changes: 139 additions & 0 deletions lib/draft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
var EventEmitter, Bot, Human, db, ref$, next, rand, shuffle, Draft;
EventEmitter = require('events').EventEmitter;
Bot = require('./bot');
Human = require('./human');
db = require('./db');
ref$ = require('./_'), next = ref$.next, rand = ref$.rand, shuffle = ref$.shuffle;
Draft = (function(superclass){
Draft.displayName = 'Draft';
var prototype = extend$(Draft, superclass).prototype, constructor = Draft;
function Draft(opts){
importAll$(this, opts);
importAll$(this, {
delta: -1,
id: rand(1e9).toString(16),
players: [],
round: 0,
startTime: Date.now() / 1e3 | 0
});
}
prototype.join = function(sock){
var name, i$, ref$, len$, p, h;
name = sock.name;
for (i$ = 0, len$ = (ref$ = this.players).length; i$ < len$; ++i$) {
p = ref$[i$];
if (p.name === name) {
return p.attach(sock);
}
}
if (this.players.length === this.seats) {
return p.send('error', 'draft full');
}
h = new Human(sock);
h.isHost = sock.id === this.host;
return this.add(h);
};
prototype.add = function(p){
this.players.push(p);
p.on('pass', bind$(this, 'pass'));
if (p.isBot) {
return;
}
this.meta();
if (p.isHost) {
p.once('start', bind$(this, 'start'));
return p.emit('start');
}
};
prototype.meta = function(){
var players, res$, i$, ref$, len$, p, j$, ref1$, len1$, results$ = [];
res$ = [];
for (i$ = 0, len$ = (ref$ = this.players).length; i$ < len$; ++i$) {
p = ref$[i$];
res$.push({
name: p.name,
time: p.time,
packs: p.packs.length
});
}
players = res$;
for (j$ = 0, len1$ = (ref1$ = this.players).length; j$ < len1$; ++j$) {
p = ref1$[j$];
results$.push(p.send('set', {
players: players
}));
}
return results$;
};
prototype.start = function(){
var i, ref$, len$, p;
if (this.addBots) {
while (this.players.length < this.seats) {
this.add(new Bot);
}
}
shuffle(this.players);
for (i = 0, len$ = (ref$ = this.players).length; i < len$; ++i) {
p = ref$[i];
p.index = i;
}
return this.startRound();
};
prototype.startRound = function(){
var set, i$, ref$, len$, p, results$ = [];
if (!(set = this.sets[this.round++])) {
return this.end();
}
this.delta *= -1;
this.activePacks = this.players.length;
for (i$ = 0, len$ = (ref$ = this.players).length; i$ < len$; ++i$) {
p = ref$[i$];
results$.push(p.start(set));
}
return results$;
};
prototype.pass = function(pack, index){
var player;
player = next(this.players, this.delta, index);
if (pack.length) {
player.receive(pack);
} else if (!--this.activePacks) {
this.startRound();
}
return this.meta();
};
prototype.end = function(){
var data, res$, i$, ref$, len$, p;
console.log('end', this.id);
data = {
sets: this.sets,
start: this.startTime
};
data.end = Date.now() / 1e3 | 0;
res$ = [];
for (i$ = 0, len$ = (ref$ = this.players).length; i$ < len$; ++i$) {
p = ref$[i$];
res$.push({
isBot: p.isBot,
picks: p.picks
});
}
data.players = res$;
return db(data);
};
return Draft;
}(EventEmitter));
module.exports = Draft;
function extend$(sub, sup){
function fun(){} fun.prototype = (sub.superclass = sup).prototype;
(sub.prototype = new fun).constructor = sub;
if (typeof sup.extended == 'function') sup.extended(sub);
return sub;
}
function importAll$(obj, src){
for (var key in src) obj[key] = src[key];
return obj;
}
function bind$(obj, key){
return function(){ return obj[key].apply(obj, arguments) };
}
47 changes: 0 additions & 47 deletions lib/genDeck.js

This file was deleted.

Loading

3 comments on commit 995bd7b

@aeosynth
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nami-Doc write a coco linter?

@vendethiel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm more performant than any coco kibit you could find :D

@vendethiel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.