-
Notifications
You must be signed in to change notification settings - Fork 17
/
twitch_master.js
156 lines (137 loc) · 4.86 KB
/
twitch_master.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
var fs = require('fs');
var irc = require('irc');
var pub = require('./lib/comm').sender();
process.stdin.resume();
process.stdin.on('data', function(data) {
process.stdout.write('Control: ' + data);
var args = data.toString().split(' ');
switch(args[0].trim()) {
case 'map_load':
map_load();
break;
case 'reset_voting':
// For when this inevitably breaks
voting_command = null;
break;
default:
console.log("Sending... ", args);
pub.send(args);
break;
}
});
// Load json command mapping
var map = {}
function map_load() {
fs.exists('map.json', function() {
try {
var map_new = JSON.parse(fs.readFileSync('map.json', 'utf8'));
map = map_new;
console.log('(Re)loaded map.json');
} catch (ex) {
console.log('Could not load map.json');
console.log(ex);
}
});
}
map_load();
// Load json config
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var twitch_chat = new irc.Client('irc.twitch.tv', config['nick'], {
channels: ['#' + config['nick']],
userName: config['nick'],
password: config['password'],
autoConnect: false
});
twitch_chat.connect(0, function() {
console.log("Twitch connected!");
});
var last_tally = {};
twitch_chat.addListener('message#' + config['nick'], function(from, msg) {
msg = msg.trim();
if (map[msg] != null) {
console.log(from + ': ' + msg + ' -> ' + map[msg]);
pub.send(['client-console', '> ' + from + ': ' + msg]);
last_tally[from.trim()] = msg;
}
});
var voting_command = null;
setInterval(function() {
var command_count = {};
for (var user in last_tally) {
if (command_count[last_tally[user]] == null)
command_count[last_tally[user]] = 0;
command_count[last_tally[user]] += 1;
}
var top_array = [];
var top_count = 0;
var second_array = [];
var second_count = 0;
for (var command in command_count) {
if (command_count[command] > top_count) {
second_array = top_array.slice();
second_count = top_count;
top_array = [];
top_array.push(command);
top_count = command_count[command];
} else if (command_count[command] == top_count) {
top_array.push(command);
} else if (command_count[command] > second_count) {
second_array = [];
second_array.push(command);
second_count = command_count[command]
} else if (command_count[command] == second_count) {
second_array.push(command);
}
}
var counts = '';
var commands = top_array.concat(second_array);
for (var index in commands) {
var command = commands[index];
counts += '\'' + command + '\' = ' + Math.round(command_count[command]/Object.keys(last_tally).length * 100, 2) + '%';
if (index != Object.keys(commands).length - 1)
counts += ', '
}
if (top_array.length > 0) {
var selected_command = top_array[Math.floor(Math.random()*top_array.length)];
console.log('Selected: ' + selected_command);
twitch_chat.say('#' + config['nick'], 'Winning command: ' + selected_command);
pub.send(['client-status', 'WINNING COMMAND: ' + selected_command]);
console.log('Votes: ' + counts);
twitch_chat.say('#' + config['nick'], 'Votes: ' + counts);
pub.send(['client-status', 'VOTES: ' + counts]);
if (voting_command != null) {
// We are voting to run a dangerous command
if (selected_command == 'yes') {
console.log('Vote succeeded: ' + voting_command);
twitch_chat.say('#' + config['nick'], 'Vote succeeded: ' + voting_command)
pub.send(['client-status', 'VOTING SUCCEEDED: ' + voting_command]);
// Send
var command_qemu = map[voting_command].replace(/^VOTE /, '');
console.log('Sending to qemu: ' + command_qemu);
pub.send(['qemu-master', command_qemu]);
voting_command = null;
} else {
console.log('Vote failed: ' + voting_command);
twitch_chat.say('#' + config['nick'], 'Vote failed: ' + voting_command)
pub.send(['client-status', 'VOTING FAILED: ' + voting_command]);
voting_command = null;
}
} else if (map[selected_command].indexOf("VOTE") == 0) {
// This command requires a vote
console.log('Voting on command: ' + selected_command);
twitch_chat.say('#' + config['nick'], 'Vote \'yes\' to run this command: ' + selected_command);
pub.send(['client-status', 'VOTING ON COMMAND (yes to run this command): ' + selected_command]);
voting_command = selected_command;
} else if (map[selected_command] != "") {
// Normal command, not NOOP
console.log('Sending to qemu: ' + map[selected_command]);
pub.send(['qemu-master', map[selected_command]]);
}
} else {
console.log('Not enough votes');
twitch_chat.say('#' + config['nick'], 'Not enough votes');
pub.send(['client-status', 'NOT ENOUGH VOTES PLACED!']);
}
// Clear last tally
last_tally = {};
}, 15 * 1000);