forked from lycan-city/werewolf-brain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (126 loc) · 3.95 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var cards = require('./cards');
var templates = require('./templates');
const BALANCEDFLEX = 1;
const CHAOSFLEX = 10;
var availableCards = {};
var game = {
deck : {},
weight : 100,
players: 0
};
var gameCandite = {
deck : {},
weight : 100,
players: 0
};
var allPlayers = true;
exports.getAllCards = function () {
return cards.all;
}
exports.getAllTemplates = function () {
return templates.all;
}
exports.getBalancedGame = function (players, chosenCards) {
return _getBalancedGame(players, chosenCards);
}
exports.getGameFromTemplate = function (players, template) {
return _getBalancedGame(players, _getCardsFromTemplate(template));
}
exports.getChaosGame = function (players, chosenCards) {
return _getChaosGame(players, chosenCards);
}
exports.getChaosGameFromTemplate = function (players, template){
return _getChaosGame(players, _getCardsFromTemplate(template));
}
function _getChaosGame(players, chosenCards) {
return _getGame(players, _classifyCards(chosenCards || cards.all), CHAOSFLEX);
}
function _getBalancedGame(players, chosenCards) {
return _getGame(players, _classifyCards(chosenCards || cards.all), BALANCEDFLEX);
}
function _getGame(players, chosenCards, flex){
var tries = 0;
while (!allPlayers || game.weight < -1 * flex || game.weight > flex) {
tries++;
_setGame(players, chosenCards);
if(gameCandite.players <= game.players) gameCandite = game;
if (tries % 500 == 0) flex++;
if (tries > 5000) break;
}
return gameCandite;
}
function _classifyCards(cards) {
var deck = { negatives: [], nonnegatives: [] };
cards.map(function (card) {
if (card.value < 0)
for (var i = 0; i < card.amount; i++)
deck.negatives.push({ role: card.role, value: card.value, amount: 1 });
else
for (var i = 0; i < card.amount; i++)
deck.nonnegatives.push({ role: card.role, value: card.value, amount: 1 });
});
return deck;
}
function _setGame(players, chosenCards) {
_resetValues(chosenCards);
//get first card randomly
_addCardToDeck(_getRandom(0, 1));
players--;
for (var i = 0; i < players; i++) {
_addCardToDeck(game.weight >= 0);
}
}
function _resetValues(chosenCards) {
game = {
deck : {},
weight : 0,
players: 0
};
allPlayers = true;
availableCards = JSON.parse(JSON.stringify(chosenCards));
}
function _addCardToDeck(isNegative) {
while (true) {
if (isNegative) {
if (availableCards.negatives.length < 1){
allPlayers = false;
break;
}
var rand = _getRandom(0, availableCards.negatives.length - 1);
if (availableCards.negatives[rand].amount > 0) {
_addRandomCard(availableCards.negatives[rand]);
availableCards.negatives.splice(rand, 1);
break;
}
}
else {
if (availableCards.nonnegatives.length < 1) {
allPlayers = false;
break;
}
var rand = _getRandom(0, availableCards.nonnegatives.length - 1);
if (availableCards.nonnegatives[rand].amount > 0) {
_addRandomCard(availableCards.nonnegatives[rand]);
availableCards.nonnegatives.splice(rand, 1);
break;
}
}
}
}
function _getCardsFromTemplate(template) {
templateCards = templates.all[template.toLowerCase()];
return cards.all.filter(function (card) {
return templateCards.indexOf(card.role) >= 0;
});
}
function _addRandomCard(selectedCard) {
game.weight += selectedCard.value;
game.players++;
if (game.deck[selectedCard.role]) game.deck[selectedCard.role]++;
else game.deck[selectedCard.role] = 1;
}
function _getRandom(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}