forked from wehale/hig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hig.js
181 lines (150 loc) · 4.2 KB
/
hig.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
var port = process.env.PORT || 3000,
http = require('http'),
fs = require('fs');
var os = require('os').platform();
const Say = require('say').Say;
const say = new Say(os);
const ANNOUNCER = "Announcer";
const util = require('util');
var myArgs = process.argv.slice(2);
switch (myArgs[0]) {
case "server":
runServer();
break;
case "speak":
runSpeech();
break;
default:
runCmdLine();
break;
}
var log = function(entry) {
fs.appendFileSync('/tmp/sample-app.log', new Date().toISOString() + ' - ' + entry + '\n');
};
function Phrase(n, p) {
this.name = n;
this.phrase = p;
}
function runServer() {
var server = http.createServer(function (req, res) {
if (req.method === 'POST') {
var body = '';
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
if (req.url === '/') {
log('Received message: ' + body);
} else if (req.url = '/scheduled') {
log('Received task ' + req.headers['x-aws-sqsd-taskname'] + ' scheduled at ' + req.headers['x-aws-sqsd-scheduled-at']);
}
res.writeHead(200, 'OK', {'Content-Type': 'text/plain'});
res.end();
});
} else {
res.writeHead(200);
var out = [];
genInterview(out);
var html = "";
out.forEach(function (p) {
html = html.concat(p.name + ": " + p.phrase + "\n");
});
res.write(html);
res.end();
}
});
// Listen on port 3000, IP defaults to 127.0.0.1
server.listen(port);
// Put a friendly message on the terminal
console.log('Server running at http://127.0.0.1:' + port + '/');
}
function runCmdLine() {
var out = [];
genInterview(out);
out.forEach(function (p) {
console.log(p.name + ": " + p.phrase);
});
}
function runSpeech() {
const running = true;
var out = [];
genInterview(out);
while (speak(out)) {}
}
function speak(phrases) {
if (phrases.length == 0) {
return false;
}
var p = phrases.shift();
if(os == 'darwin'){ //mac male voice to talk as hockey player
var v = 'Alex';
if (p.name == ANNOUNCER ) { //mac female voice to talk as announcer
v = 'Samantha';
}
}
else if (os=='win32'){ //windows male voice to talk as hockey player
var v = 'Microsoft David Desktop';
if (p.name == ANNOUNCER ) { //windows female voice to talk as announcer
v ='Microsoft Zira Desktop';
}
}
var ph = p.phrase.toString().startsWith('(') ? "..." : p.phrase;
// Here we use util.promisify to convert the function to a promise
const speakAsync = util.promisify(say.speak.bind(say));
speakAsync(ph,v,1)
.then(() => speak(phrases))
.catch(err => console.error(`[Error]: ${err}`));
}
function genInterview(h) {
let rawJson = fs.readFileSync('hig.json');
let hig = JSON.parse(rawJson);
let team = hig.teams;
let statement = hig.statements;
let filler = hig.filler;
let action = hig.actions;
let closing = hig.closing;
let start = hig.starts;
let question = hig.questions;
var rand;
//Team
rand = Math.floor(Math.random() * Math.floor(team.length));
var t = team[rand]
//Player
rand = Math.floor(Math.random() * Math.floor(t.players.length));
var p = t.players[rand]
//Question
rand = Math.floor(Math.random() * Math.floor(question.length));
var q = question[rand];
var s = "Thanks John, I'm here with " + p.name + " of the " + t.name;
h.push(new Phrase(ANNOUNCER, s));
s = "So " + p.nickname + " " + q;
h.push(new Phrase(ANNOUNCER, s))
//Start
addLine(h, p, start);
//Interview of random length
var i = 0;
var count = Math.random() * 10; //get random num from 1 - 10
while (i < count) {
//Possible Filler
if ((i > 0) && (Math.random() < 0.5)) {
addLine(h, p, filler);
}
//Possible Action
if (Math.random() < 0.5) {
addLine(h, p, action);
}
//Statement
addLine(h, p, statement);
i++;
}
//Announcer closing
s = "Ok, that's great to hear, thanks " + p.nickname;
h.push(new Phrase(ANNOUNCER, s));
//Player closing
addLine(h, p, closing);
}
function addLine(h, p, a) {
var rand = Math.floor(Math.random() * Math.floor(a.length));
var s = a.splice(rand, 1); //remove the sentence into var s
h.push(new Phrase(p.nickname, s))
}