This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayers.js
204 lines (160 loc) · 7 KB
/
players.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env node
// players subsystem
// Creates an Identity for each player in the game
// writes it to players.json
var commandLineArgs = require('command-line-args');
const objectDefinitions = [
{ name: 'blu', alias: 'b', type: Number },
{ name: 'red', alias: 'r', type: Number }
];
const options = commandLineArgs(objectDefinitions);
if (typeof options.blu === 'undefined' || typeof options.red === 'undefined') {
console.log('USAGE: ./players.js --blu 5 --red 6');
process.exit();
}
console.log('%s BLU players, %s RED players', options.blu, options.red);
var Handlebars = require('handlebars');
var classes = require('./client_app/classes.json');
var abilities = require('./client_app/abilities.json');
var faker = require('faker');
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
var os = require('os');
var qr = require('./qr');
// validate class data
for (var c in classes) {
if (classes.hasOwnProperty(c)) {
if (typeof classes[c].perTeam === 'undefined') throw new Error('each class must have a perTeam property');
if (typeof classes[c].priority === 'undefined') throw new Error('each class must have a priority property');
if (typeof classes[c].abilities === 'undefined') throw new Error('each class must have a abilities property');
if (typeof classes[c].loadout === 'undefined') throw new Error('each class must have a loadout property');
}
}
// validate player data @todo
// if (typeof classes[c].dob === 'undefined') throw new Error('each class must have a dob property');
// if (typeof classes[c].id === 'undefined') throw new Error('each class must have a id property');
// if (typeof classes[c].firstName === 'undefined') throw new Error('each class must have a firstName property');
// if (typeof classes[c].lastName === 'undefined') throw new Error('each class must have a lastName property');
// if (typeof classes[c]['class'] === 'undefined') throw new Error('each class must have a class property');
function getClasses(teamCount, classes) {
// find out what classes we have to work with.
// sort them by priority.
var classes = _.sortBy(classes, ['priority']);
var output = [];
var createdPlayerCount = 0;
for (var c in classes) {
if (createdPlayerCount < teamCount) {
var thisClass = classes[c];
// give the team this classtype, up to the maximum allowed classtype per team
// there is one special exception, if perTeam is -1, continue forever* until the team is populated.
if (thisClass.perTeam === -1) {
thisClass.perTeam = 1000000; // * 1 Million players is close enough to "forever"
}
for (i=0; i < thisClass.perTeam; i++) {
if (createdPlayerCount < teamCount) {
// create a new player if this team is not populated yet
// console.log('adding %s to output. i=%s, thisClass.perTeam=%s, createdPlayerCount=%s, teamCount=%s',
// thisClass.name,
// i,
// thisClass.perTeam,
// createdPlayerCount,
// teamCount
// );
output.push(thisClass);
createdPlayerCount += 1;
}
else {
break;
}
}
}
else {
break;
}
}
return output;
}
function getPlayers(colour, teamCount, classes) {
// for each player, assign them a random class
classes = _.shuffle(classes);
var players = [];
for (i=0; i < teamCount; i++) {
var player = {};
player.id = faker.random.alphaNumeric(12);
player.firstName = faker.name.firstName();
player.lastName = faker.name.lastName();
player.affiliation = colour;
player.dob = faker.date.between(new Date('1 Jan 1970'), new Date('25 Dec 1995'));
player['class'] = classes[i].name;
player.loadout = classes[i].loadout;
player.abilities = classes[i].abilities;
player.tagline = classes[i].tagline;
// disabling temporarily because its slow
//var command = path.join(os.homedir(), 'phantom/bin/phantomjs');
//var args = [path.resolve('./face.js')];
//var picture = child_process.spawnSync(command, args).stdout;
//player.picture = picture.toString().split(/\r\n|\r|\n/g)[0];
player.picture = 'placekitten.com/150/150';
// assign the player QR codes which they can use to interact with the game
player.scans = qr.compilePlayer(player);
console.log(player.scans);
players.push(player);
}
return players;
// generate player list like this--
// {
// shu7zojjm67t: {
// affiliation: 'blu',
// id: 'shu7zojjm67t',
// firstName: Mariam,
// lastName: Gusikowski,
// dob: '10/31/1980',
// class: 'Grenadier',
// loadout: [
// 'Assault Rifle',
// 'Pistol',
// 'High-output smoke grenade',
// 'Frag grenade'
// ],
// abilities: [
// ...
// ]
// }
// }
}
var redClasses = getClasses(options.red, classes);
var bluClasses = getClasses(options.blu, classes);
var redPlayers = getPlayers('red', options.red, redClasses);
var bluPlayers = getPlayers('blu', options.blu, bluClasses);
// write player data to disk as JSON
var data = {};
data['red'] = redPlayers;
data['blu'] = bluPlayers;
fs.writeFileSync('./client_app/players.json', JSON.stringify(data), { 'encoding': 'utf8' });
// use data to generate an html template (used for printing cards)
console.log(data);
var idCardFile = fs.readFileSync('./templates/idcard.hbs', { 'encoding': 'utf8' });
var qrFigureFile = fs.readFileSync('./templates/qrfigure.hbs', { 'encoding': 'utf8' });
Handlebars.registerPartial('idCard', idCardFile);
Handlebars.registerPartial('qrFigure', qrFigureFile);
Handlebars.registerHelper('uppercase', function(options) {
return options.fn(this).toUpperCase();
});
Handlebars.registerHelper('bullet', function(items, options) {
var out = "<ul>";
for (var i=0, l=items.length; i<l; i++) {
out = out + "<li>" + items[i] + "</li>";
}
out = out + "</ul>";
return new Handlebars.SafeString(out);
});
Handlebars.registerHelper('date', function(dateObject) {
return (dateObject.getMonth()+1)+'/'+dateObject.getDate()+'/'+dateObject.getFullYear();
})
var templateFile = fs.readFileSync('./templates/identity.hbs', { 'encoding': 'utf8' });
var template = Handlebars.compile(templateFile);
var html = template(data);
fs.writeFileSync('./client_app/players.html', html, { 'encoding': 'utf8' });
console.log('HTML written. Open this file in your browser and print. file://%s ', path.resolve('./client_app/players.html'));