-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft.js
58 lines (51 loc) · 2.02 KB
/
draft.js
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
var fs = require('fs');
var cardList = JSON.parse(fs.readFileSync(__dirname+'/public/js/cardList.js', 'utf8'));
var Draft = function(n, p, c, s){
this.name = n;
this.players = p; //TODO: randomize player array
this.packs = []; //2 dim array
this.assignPacks = function(packsPerPlayer, packsize){
this.packs = makePacks(packsPerPlayer*this.players.length, packsize); //pass packs by reference to be updated by makePacks
for(var i = 0; i<this.players.length; i++) {
this.players[i].packs = this.packs.slice(i*packsPerPlayer, (i+1)*packsPerPlayer);
}
};
this.assignPacks(c, s);
}
function makePacks(packcount, packsize) {
var draftStack = [];
var packs = [];
var colorBuckets = {};
//setup bucket for each color
for (var x in cardList) {
if(cardList[x].Color in colorBuckets) {
colorBuckets[cardList[x].Color].push(cardList[x]); //add card to bucket
} else {
colorBuckets[cardList[x].Color] = [cardList[x]]; //initialize bucket to new array with card
}
}
//TODO: add check for too many players
//TODO: fix divide by 0 error if no colors
var cardsPerColor = Math.ceil((packcount*packsize)/Object.keys(colorBuckets).length);
//randomly select appropriate number of cards from each bucket
var i;
for (var j in colorBuckets) {
for (i=0; i<cardsPerColor; i++) {
var randomNumber=Math.floor(Math.random()*colorBuckets[j].length);
//add selected card to the draft pool
draftStack.push(colorBuckets[j][randomNumber]);
//remove selected card from bucket
colorBuckets[j].splice([randomNumber],1);
}
}
//shuffle up and assign packs (no five piling allowed - gert)
shuffle(draftStack);
for(i = 0; i < (draftStack.length/packsize)|0; packs[i]=[], i+=1); //create each pack
for(i = 0; i < draftStack.length-draftStack.length%packsize; i+=1) //loop through all the cards
packs[(i/packsize)|0][i%packsize]=draftStack[i]; //assign each card to a pack
return packs;
}
function shuffle(a){
for(var j, x, i = a.length; i; j = parseInt(Math.random() * i), x = a[--i], a[i] = a[j], a[j] = x);
}
module.exports = Draft;