forked from EshwarAnad/TwitchControlsMinecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
196 lines (151 loc) · 5.11 KB
/
service.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
const uuidv1 = require('uuid/v1');
const commandsConfig = require('./config/commands');
const votingRoundDurationMilliseconds = 60 * 1000;
const state = {
chatConnected: false,
command: {},
};
const voting = {
command: {
name: null,
description: null,
options: [],
optionVotes: [ [] ], // should be an object and not array of arrays, hash lookups are faster than filter.
},
roundEndsAt: 0,
};
const run = () => {
setInterval(function() {
if (Date.now() < voting.roundEndsAt) {
const roundEndSeconds = Math.floor((voting.roundEndsAt - Date.now()) / 1000);
if (!(roundEndSeconds % 10)) {
console.log(`[ SERVICE:VOTING ] ROUND ENDS IN ${roundEndSeconds} SECONDS...`);
}
return;
}
/*
* Setup the winning command for server to run.
*/
console.log('[ SERVICE:VOTING ] ROUND ENDED');
const winningCommandOptionIndex = voting.command.optionVotes
.map(votes => votes.length)
.reduce((winningIndex, totalVotes, index, votesArray ) => {
return (totalVotes > votesArray[winningIndex]) ? index : winningIndex;
}, 0);
const winningCommandOptionVotesTotal = voting.command.optionVotes[winningCommandOptionIndex].length;
const winningCommandOption = voting.command.options[winningCommandOptionIndex];
if (winningCommandOption && winningCommandOptionVotesTotal > 0 && !winningCommandOption.pass) {
state.command = {
uuid: voting.command.uuid,
name: voting.command.name,
commandTemplate: voting.command.commandTemplate,
option: winningCommandOption,
};
console.log('[ SERVICE:VOTING ] WINNING VOTE:');
console.log(winningCommandOption);
}
/*
* Setup the next round of voting.
*/
voting.command = getRandomizedCommand();
voting.roundEndsAt = Date.now() + votingRoundDurationMilliseconds;
console.log('[ SERVICE:VOTING ] NEXT ROUND STARTING');
console.log(`[ SERVICE:VOTING ] VOTE - ${voting.command.name}`);
console.log(`[ SERVICE:VOTING ] VOTE OPTIONS:`);
console.log(voting.command.options);
}, 1000);
};
const handleChatMessage = (target, context, message, self) => {
if (isNaN(message)) {
return
}
const userId = context['user-id'];
const voteIndex = parseInt(message) - 1;
if (!voting.command.optionVotes[voteIndex]) {
return;
}
for (let i = 0; i < voting.command.optionVotes.length; i++) {
voting.command.optionVotes[i] = voting.command.optionVotes[i].filter(votedUserId => {
return userId != votedUserId;
});
}
voting.command.optionVotes[voteIndex].push(userId);
console.log(voting.command);
};
const handleChatConnected = () => {
console.log('[ SERVICE:CHAT ] CONNECTED');
state.chatConnected = true;
};
const handleChatDisconnected = () => {
console.log('[ SERVICE:CHAT ] DISCONNECTED');
state.chatConnected = false;
};
module.exports = {
state,
voting,
handleChatMessage,
handleChatConnected,
handleChatDisconnected,
run,
};
/*
* Helpers
*/
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomArrayElement(array) {
return array[Math.floor(Math.random() * array.length)];
}
function getUniqueRandomArrayElements(array, count) {
const uniqueElements = [];
while (uniqueElements.length < count) {
const randomElement = getRandomArrayElement(array);
if (!uniqueElements.includes(randomElement)) {
uniqueElements.push(randomElement);
}
}
return uniqueElements;
}
function shuffleArray(array) {
for(let i = array.length - 1; i > 0; i--){
const j = Math.floor(Math.random() * i)
const temp = array[i]
array[i] = array[j]
array[j] = temp
}
}
function getRandomizedCommand() {
let weightedCommands = [];
commandsConfig.forEach(command => {
const weightedArray = Array.apply(null, Array(command.weight)).map(() => Object.assign({}, command));
weightedCommands = weightedCommands.concat(weightedArray);
});
const command = getRandomArrayElement(weightedCommands);
shuffleArray(command.options);
command.options = getUniqueRandomArrayElements(command.options, command.presentableOptionsExcludingDefault);
command.options = command.options.map(option => {
if (option.durationRange) {
option.duration = getRandomInt(option.durationRange[0], option.durationRange[1]);
}
if (option.amplificationRange) {
option.amplification = getRandomInt(option.amplificationRange[0], option.amplificationRange[1]);
}
if (option.experienceRange) {
option.experience = getRandomInt(option.experienceRange[0], option.experienceRange[1]);
}
if (option.quantityRange) {
option.quantity = getRandomInt(option.quantityRange[0], option.quantityRange[1]);
}
if (option.repeatCommandRange) {
option.repeatCommand = getRandomInt(option.repeatCommandRange[0], option.repeatCommandRange[1]);
}
return option;
});
command.uuid = uuidv1();
command.options.push(command.defaultOption);
command.optionVotes = Array.apply(null, Array(command.options.length)).map(() => []);
return command;
}