-
Notifications
You must be signed in to change notification settings - Fork 1
/
Calculator.ttslua
365 lines (316 loc) · 11.4 KB
/
Calculator.ttslua
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
--[[
Secret Hitler Probability Calculator
Made by Sionar
--]]
------------------Constants
VERSION = '1.3.4'
COL = {0.5, 1, 1.5, 2}
ROW = {-1, -0.6, -0.2, 0.2, 0.6, 1}
START_POS = {125, 1.1, -29}
SELF_SCALE = {4,1,4}
------------------Variables
lib = 6
fas = 11
draw = 3
nameLabels = {}
valLabels = {}
function onLoad(saveString)
if not (saveString == '') then
local save = JSON.decode(saveString)
lib = save['lib']
fas = save['fas']
draw = save['draw'] or draw
nameLabels = save['nL']
valLabels = save['vL']
end
refreshButtons()
moveBoard()
self.setDescription('v ' .. VERSION .. '\nMade by Sionar')
end
function onSave()
local save = {}
save['lib'] = lib
save['fas'] = fas
save['draw'] = draw
save['nL'] = nameLabels
save['vL'] = valLabels
local saveString = JSON.encode(save)
return saveString
end
function moveBoard()
local global_name = Global.getVar('MOD_NAME')
if global_name == 'Secret Hitler: CE' then
self.setPositionSmooth(START_POS)
self.setScale(SELF_SCALE)
self.setLock(true)
end
end
function onChat(message, player)
local args = {} -- The arguments following a command
local command
for i in string.gmatch(message, "%S+") do
args[#args + 1] = i
end
if args[1] ~= nil then
command = string.lower(args[1])
end
if command == '!calc' then
if args[2] == nil or args[3] == nil or args[4] == nil then
player.broadcast('ERROR: Invalid entry. Please enter three numbers after !calc: The number of fascist cards, lib cards, and how many cards to draw.', {1,0,0})
elseif tonumber(args[2]) == nil or tonumber(args[3]) == nil or tonumber(args[4]) == nil then
player.broadcast('ERROR: Invalid entry. All three arguments must be numbers', {1,0,0})
else
fas = args[2]
lib = args[3]
draw = args[4]
refreshButtons()
calcValues()
end
return false
end
end
function refreshButtons()
self.clearButtons()
self.clearInputs()
local buttonParam = {click_function = 'nullFunc', label = 'Fas', color = {0,0,0,1}, font_color = stringColorToRGB('White'), function_owner = self,
position = {-1.7,0.2,-1}, rotation = {0,0,0}, width = 0, height = 0, font_size = 100}
self.createButton(buttonParam)
local buttonParam = {click_function = 'nullFunc', label = 'Lib', color = {0,0,0,1}, font_color = stringColorToRGB('White'), function_owner = self,
position = {-0.5,0.2,-1}, rotation = {0,0,0}, width = 0, height = 0, font_size = 100}
self.createButton(buttonParam)
local inputParam = {input_function = 'fasInput', color = stringColorToRGB('Red'), function_owner = self, font_color = stringColorToRGB('White'), function_owner = self,
position = {-1.7,0.2,-0.7}, rotation = {0,0,0}, width = 500, height = 174, font_size = 150, value = fas, alignment = 3, validation = 2, tab = 2}
self.createInput(inputParam)
local inputParam = {input_function = 'libInput', color = stringColorToRGB('Blue'), function_owner = self, font_color = stringColorToRGB('White'), function_owner = self,
position = {-0.5,0.2,-0.7}, rotation = {0,0,0}, width = 500, height = 174, font_size = 150, value = lib, alignment = 3, validation = 2, tab = 2}
self.createInput(inputParam)
local buttonParam = {click_function = 'decFas', label = '-', color = stringColorToRGB('White'), function_owner = self,
position = {-1.95,0.2,-0.25}, rotation = {0,0,0}, width = 250, height = 100, font_size = 150}
self.createButton(buttonParam)
local buttonParam = {click_function = 'incFas', label = '+', color = stringColorToRGB('White'), function_owner = self,
position = {-1.45,0.2,-0.25}, rotation = {0,0,0}, width = 250, height = 100, font_size = 150}
self.createButton(buttonParam)
local buttonParam = {click_function = 'decLib', label = '-', color = stringColorToRGB('White'), function_owner = self,
position = {-0.75,0.2,-0.25}, rotation = {0,0,0}, width = 250, height = 100, font_size = 150}
self.createButton(buttonParam)
local buttonParam = {click_function = 'incLib', label = '+', color = stringColorToRGB('White'), function_owner = self,
position = {-0.25,0.2,-0.25}, rotation = {0,0,0}, width = 250, height = 100, font_size = 150}
self.createButton(buttonParam)
local buttonParam = {click_function = 'nullFunc', label = 'Draw:', color = {0,0,0,1}, font_color = stringColorToRGB('White'), function_owner = self,
position = {-1.9,0.2,0.25}, rotation = {0,0,0}, width = 0, height = 0, font_size = 100}
self.createButton(buttonParam)
local inputParam = {input_function = 'drawInput', color = stringColorToRGB('Purple'), function_owner = self, font_color = stringColorToRGB('White'), function_owner = self,
position = {-1.4,0.2,0.25}, rotation = {0,0,0}, width = 150, height = 123, font_size = 100, value = draw, alignment = 3, validation = 2, tab = 2}
self.createInput(inputParam)
local buttonParam = {click_function = 'resetFunc', label = 'Reset', color = stringColorToRGB('White'), function_owner = self,
position = {-0.5,0.2,0.25}, rotation = {0,0,0}, width = 500, height = 200, font_size = 100}
self.createButton(buttonParam)
local buttonParam = {click_function = 'calcValues', label = 'Calc', color = stringColorToRGB('Green'), function_owner = self,
position = {-1.1,0.2,0.9}, rotation = {0,0,0}, width = 1100, height = 300, font_size = 150, tooltip = 'Type !calc [0000FF]Lib [FF0000]Fas [FF00FF]Draw [FFFFFF]in chat to calculate stats.'}
self.createButton(buttonParam)
for i = 1,6 do
local buttonParam = {click_function = 'nullFunc', label = nameLabels[i], color = {0,0,0,1}, function_owner = self,
position = {COL[1],0.2,ROW[i]}, rotation = {0,0,0}, width = 0, height = 0, font_size = 80}
if i == 1 then
if valLabels[i] == nil then
buttonParam.font_color = {1, 1, 1}
else
local value = tonumber(string.sub(valLabels[i],1,-2))/100
buttonParam.font_color = {1-value, 0, value}
end
else
buttonParam.font_color = {(draw-i+2)/draw, 0, (i-2)/draw}
end
self.createButton(buttonParam)
local buttonParam = {click_function = 'nullFunc', label = valLabels[i], color = {0,0,0,1}, font_color = stringColorToRGB('White'), function_owner = self,
position = {COL[2],0.2,ROW[i]}, rotation = {0,0,0}, width = 0, height = 0, font_size = 80}
self.createButton(buttonParam)
end
for i = 7,12 do
local buttonParam = {click_function = 'nullFunc', label = nameLabels[i], color = {0,0,0,1}, function_owner = self,
position = {COL[3],0.2,ROW[i-6]}, rotation = {0,0,0}, width = 0, height = 0, font_size = 80}
buttonParam.font_color = {(draw-i+2)/draw, 0, (i-2)/draw}
self.createButton(buttonParam)
local buttonParam = {click_function = 'nullFunc', label = valLabels[i], color = {0,0,0,1}, font_color = stringColorToRGB('White'), function_owner = self,
position = {COL[4],0.2,ROW[i-6]}, rotation = {0,0,0}, width = 0, height = 0, font_size = 80}
self.createButton(buttonParam)
end
end
function nullFunc() end
function decFas()
local tab = self.getInputs()
if tab[1].value == '' then
fas = 0
else
fas = tonumber(tab[1].value)
if fas > 0 then
fas = fas - 1
end
end
refreshButtons()
end
function incFas()
local tab = self.getInputs()
if tab[1].value == '' then
fas = 1
else
fas = tonumber(tab[1].value)
fas = fas + 1
end
refreshButtons()
end
function decLib()
local tab = self.getInputs()
if tab[2].value == '' then
lib = 0
else
lib = tonumber(tab[2].value)
if lib > 0 then
lib = lib - 1
end
end
refreshButtons()
end
function incLib()
local tab = self.getInputs()
if tab[2].value == '' then
lib = 1
else
lib = tonumber(tab[2].value)
lib = lib + 1
end
refreshButtons()
end
function fasInput(obj, playerColor, input_value, stillEditing)
if not stillEditing then
if tonumber(input_value) < 0 then
input_value = 0
end
fas = input_value
refreshButtons()
end
end
function libInput(obj, playerColor, input_value, stillEditing)
if not stillEditing then
if tonumber(input_value) < 0 then
input_value = 0
end
lib = input_value
refreshButtons()
end
end
function drawInput(obj, playerColor, input_value, stillEditing)
if not stillEditing then
if tonumber(input_value) < 1 then
input_value = 1
elseif tonumber(input_value) > 10 then
input_value = 10
end
draw = input_value
refreshLabels()
refreshButtons()
end
end
function refreshLabels()
local name
nameLabels = {}
valLabels = {}
table.insert(nameLabels, '1+ L')
for i = 0,draw do
name = draw-i .. 'F ' .. i .. 'L'
table.insert(nameLabels, name)
end
end
function resetFunc()
fas = 11
lib = 6
draw = 3
refreshLabels()
refreshButtons()
end
function calcValues(objectButton, playerColor)
local tab = self.getInputs()
if tab[1].value == '' then
fas = 0
else
fas = tonumber(tab[1].value)
end
if fas < 0 then
fas = 0
end
if tab[2].value == '' then
lib = 0
else
lib = tonumber(tab[2].value)
end
if lib < 0 then
lib = 0
end
if tab[3].value == '' then
draw = 3
else
draw = tonumber(tab[3].value)
end
if draw > 10 then
draw = 10
end
if draw < 1 then
draw = 1
end
refreshLabels()
local totalCards = lib + fas
local denom, numer = 1,1
local value, numLibs, numFas
local name, broadcastColor
if totalCards < draw then
broadcastToAll('Error: Not enough cards to draw', {1,0,0})
return
end
for i = 0, draw-1 do
denom = denom * (totalCards - i)
end
valLabels = {}
for numLibs = 0, draw do
numer = 1
for i = 1, numLibs do
numer = numer * (lib - i + 1)
end
numFas = draw - numLibs
for i = 1, numFas do
numer = numer * (fas - i + 1)
end
value = round(numer/denom * combi(draw, numLibs) * 100)
table.insert(valLabels, value..'%')
if numLibs == 0 then
value = 100 - value
table.insert(valLabels, 1, value..'%')
end
end
broadcastToAll(fas .. 'F, ' .. lib .. 'L, draw ' .. draw .. ':', {1,1,1})
for i = 1, draw+2 do
if i == 1 then
broadcastColor = {0,0,1}
else
broadcastColor = {(draw-i+2)/draw, 0, (i-2)/draw}
end
broadcastToAll(nameLabels[i] .. ': [FFFFFF]' .. valLabels[i], broadcastColor)
end
refreshButtons()
end
function round(n)
return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end
function combi(n, k)
local function factorial(x)
if x == 0 then
return 1
else
local product = 1
for i = x,1,-1 do
product = product * i
end
return product
end
end
return factorial(n)/(factorial(k) * factorial(n-k))
end