forked from Refactorio/RedMew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll.lua
1282 lines (1042 loc) · 36.3 KB
/
poll.lua
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local Gui = require 'utils.gui'
local Global = require 'utils.global'
local Event = require 'utils.event'
local UserGroups = require 'user_groups'
local Game = require 'utils.game'
local default_poll_duration = 300 * 60 -- in ticks
local duration_max = 3600 -- in seconds
local duration_step = 15 -- in seconds
local duration_slider_max = duration_max / duration_step
local tick_duration_step = duration_step * 60
local inv_tick_duration_step = 1 / tick_duration_step
local normal_color = {r = 1, g = 1, b = 1}
local focus_color = {r = 1, g = 0.55, b = 0.1}
local polls = {}
local polls_counter = {0}
local no_notify_players = {}
local player_poll_index = {}
local player_create_poll_data = {}
Global.register(
{
polls = polls,
polls_counter = polls_counter,
no_notify_players = no_notify_players,
player_poll_index = player_poll_index,
player_create_poll_data = player_create_poll_data
},
function(tbl)
polls = tbl.polls
polls_counter = tbl.polls_counter
no_notify_players = tbl.no_notify_players
player_poll_index = tbl.player_poll_index
player_create_poll_data = tbl.player_create_poll_data
end
)
local main_button_name = Gui.uid_name()
local main_frame_name = Gui.uid_name()
local create_poll_button_name = Gui.uid_name()
local notify_checkbox_name = Gui.uid_name()
local poll_view_back_name = Gui.uid_name()
local poll_view_forward_name = Gui.uid_name()
local poll_view_vote_name = Gui.uid_name()
local poll_view_edit_name = Gui.uid_name()
local create_poll_frame_name = Gui.uid_name()
local create_poll_duration_name = Gui.uid_name()
local create_poll_label_name = Gui.uid_name()
local create_poll_question_name = Gui.uid_name()
local create_poll_answer_name = Gui.uid_name()
local create_poll_add_answer_name = Gui.uid_name()
local create_poll_delete_answer_name = Gui.uid_name()
local create_poll_close_name = Gui.uid_name()
local create_poll_clear_name = Gui.uid_name()
local create_poll_edit_name = Gui.uid_name()
local create_poll_confirm_name = Gui.uid_name()
local create_poll_delete_name = Gui.uid_name()
local function poll_id()
local count = polls_counter[1] + 1
polls_counter[1] = count
return count
end
local function apply_direction_button_style(button)
local button_style = button.style
button_style.width = 24
button_style.height = 24
button_style.top_padding = 0
button_style.bottom_padding = 0
button_style.left_padding = 0
button_style.right_padding = 0
button_style.font = 'default-listbox'
end
local function apply_button_style(button)
local button_style = button.style
button_style.font = 'default-bold'
button_style.height = 26
button_style.top_padding = 0
button_style.bottom_padding = 0
button_style.left_padding = 2
button_style.right_padding = 2
end
local function do_remaining_time(poll, remaining_time_label)
local end_tick = poll.end_tick
if end_tick == -1 then
remaining_time_label.caption = 'Endless Poll.'
return true
end
local ticks = end_tick - game.tick
if ticks < 0 then
remaining_time_label.caption = 'Poll Finished.'
return false
else
local time = math.ceil(ticks / 60)
remaining_time_label.caption = 'Remaining Time: ' .. time
return true
end
end
local function redraw_poll_viewer_content(data)
local poll_viewer_content = data.poll_viewer_content
local remaining_time_label = data.remaining_time_label
local poll_index = data.poll_index
local player = poll_viewer_content.gui.player
data.vote_buttons = nil
Gui.remove_data_recursivly(poll_viewer_content)
poll_viewer_content.clear()
local poll = polls[poll_index]
if not poll then
return
end
local answers = poll.answers
local voters = poll.voters
local tooltips = {}
for _, a in ipairs(answers) do
tooltips[a] = {}
end
for player_index, answer in pairs(voters) do
local p = Game.get_player_by_index(player_index)
table.insert(tooltips[answer], p.name)
end
for a, t in pairs(tooltips) do
if #t == 0 then
tooltips[a] = ''
else
tooltips[a] = table.concat(t, ', ')
end
end
local created_by_player = poll.created_by
local created_by_text
if created_by_player and created_by_player.valid then
created_by_text = ' Created by ' .. created_by_player.name
else
created_by_text = ''
end
local top_flow = poll_viewer_content.add {type = 'flow', direction = 'horizontal'}
top_flow.add {type = 'label', caption = table.concat {'Poll #', poll.id, created_by_text}}
local edited_by_players = poll.edited_by
if next(edited_by_players) then
local edit_names = {'Edited by '}
for pi, _ in pairs(edited_by_players) do
local p = Game.get_player_by_index(pi)
if p and p.valid then
table.insert(edit_names, p.name)
table.insert(edit_names, ', ')
end
end
table.remove(edit_names)
local edit_text = table.concat(edit_names)
top_flow.add {type = 'label', caption = edit_text, tooltip = edit_text}
end
local poll_enabled = do_remaining_time(poll, remaining_time_label)
local question_flow = poll_viewer_content.add {type = 'table', column_count = 2}
if player.admin or UserGroups.is_regular(player.name) then
local edit_button =
question_flow.add {
type = 'sprite-button',
name = poll_view_edit_name,
sprite = 'utility/rename_icon_normal',
tooltip = 'Edit Poll.'
}
local edit_button_style = edit_button.style
edit_button_style.width = 26
edit_button_style.height = 26
end
local question_label = question_flow.add {type = 'label', caption = poll.question}
question_label.style.height = 32
question_label.style.font_color = focus_color
question_label.style.font = 'default-listbox'
local grid = poll_viewer_content.add {type = 'table', column_count = 2}
local answer = voters[player.index]
local vote_buttons = {}
for i, a in ipairs(answers) do
local vote_button_flow = grid.add {type = 'flow'}
local vote_button =
vote_button_flow.add {
type = 'button',
name = poll_view_vote_name,
caption = a.voted_count,
enabled = poll_enabled
}
local tooltip = tooltips[a]
if tooltip ~= '' then
vote_button.tooltip = tooltip
end
local vote_button_style = vote_button.style
vote_button_style.height = 24
vote_button_style.width = 26
vote_button_style.font = 'default-small'
vote_button_style.top_padding = 0
vote_button_style.bottom_padding = 0
vote_button_style.left_padding = 0
vote_button_style.right_padding = 0
if answer == a then
vote_button_style.font_color = focus_color
vote_button_style.disabled_font_color = focus_color
end
Gui.set_data(vote_button, {answer = a, data = data})
vote_buttons[i] = vote_button
local label = grid.add {type = 'label', caption = a.text}
label.style.height = 24
end
data.vote_buttons = vote_buttons
end
local function update_poll_viewer(data)
local back_button = data.back_button
local forward_button = data.forward_button
local poll_index_label = data.poll_index_label
local poll_index = data.poll_index
if #polls == 0 then
poll_index = 0
else
poll_index = math.clamp(poll_index, 1, #polls)
end
data.poll_index = poll_index
if poll_index == 0 then
poll_index_label.caption = 'No Polls'
else
poll_index_label.caption = table.concat {'Poll ', poll_index, ' / ', #polls}
end
back_button.enabled = poll_index > 1
forward_button.enabled = poll_index < #polls
redraw_poll_viewer_content(data)
end
local function draw_main_frame(left, player)
local frame = left.add {type = 'frame', name = main_frame_name, caption = 'Polls', direction = 'vertical'}
frame.style.maximal_width = 320
local poll_viewer_top_flow = frame.add {type = 'table', column_count = 5}
poll_viewer_top_flow.style.horizontal_spacing = 0
local back_button = poll_viewer_top_flow.add {type = 'button', name = poll_view_back_name, caption = '◀'}
apply_direction_button_style(back_button)
local forward_button = poll_viewer_top_flow.add {type = 'button', name = poll_view_forward_name, caption = '▶'}
apply_direction_button_style(forward_button)
local poll_index_label = poll_viewer_top_flow.add {type = 'label'}
poll_index_label.style.left_padding = 8
local spacer = poll_viewer_top_flow.add {type = 'flow'}
spacer.style.horizontally_stretchable = true
local remaining_time_label = poll_viewer_top_flow.add {type = 'label'}
local poll_viewer_content = frame.add {type = 'scroll-pane'}
poll_viewer_content.style.maximal_height = 250
poll_viewer_content.style.width = 300
local poll_index = player_poll_index[player.index] or #polls
local data = {
back_button = back_button,
forward_button = forward_button,
poll_index_label = poll_index_label,
poll_viewer_content = poll_viewer_content,
remaining_time_label = remaining_time_label,
poll_index = poll_index
}
Gui.set_data(frame, data)
Gui.set_data(back_button, data)
Gui.set_data(forward_button, data)
update_poll_viewer(data)
frame.add {
type = 'checkbox',
name = notify_checkbox_name,
caption = 'Notify me about polls.',
state = not no_notify_players[player.index],
tooltip = 'Receive a message when new polls are created and popup the poll.'
}
local bottom_flow = frame.add {type = 'flow', direction = 'horizontal'}
local left_flow = bottom_flow.add {type = 'flow'}
left_flow.style.align = 'left'
left_flow.style.horizontally_stretchable = true
local close_button = left_flow.add {type = 'button', name = main_button_name, caption = 'Close'}
apply_button_style(close_button)
local right_flow = bottom_flow.add {type = 'flow'}
right_flow.style.align = 'right'
if player.admin or UserGroups.is_regular(player.name) then
local create_poll_button =
right_flow.add {type = 'button', name = create_poll_button_name, caption = 'Create Poll'}
apply_button_style(create_poll_button)
else
local create_poll_button =
right_flow.add {
type = 'button',
caption = 'Create Poll',
enabled = false,
tooltip = 'Sorry, you need to be a regular to create polls.'
}
apply_button_style(create_poll_button)
end
end
local function remove_create_poll_frame(create_poll_frame, player_index)
local data = Gui.get_data(create_poll_frame)
data.edit_mode = nil
player_create_poll_data[player_index] = data
Gui.remove_data_recursivly(create_poll_frame)
create_poll_frame.destroy()
end
local function remove_main_frame(main_frame, left, player)
local player_index = player.index
local data = Gui.get_data(main_frame)
player_poll_index[player_index] = data.poll_index
Gui.remove_data_recursivly(main_frame)
main_frame.destroy()
local create_poll_frame = left[create_poll_frame_name]
if create_poll_frame and create_poll_frame.valid then
remove_create_poll_frame(create_poll_frame, player_index)
end
end
local function toggle(event)
local left = event.player.gui.left
local main_frame = left[main_frame_name]
if main_frame then
remove_main_frame(main_frame, left, event.player)
else
draw_main_frame(left, event.player)
end
end
local function update_duration(slider)
local slider_data = Gui.get_data(slider)
local label = slider_data.duration_label
local value = slider.slider_value
value = math.floor(value)
slider_data.data.duration = value * tick_duration_step
if value == 0 then
label.caption = 'Endless Poll.'
else
label.caption = value * duration_step .. ' seconds.'
end
end
local function redraw_create_poll_content(data)
local grid = data.grid
local answers = data.answers
Gui.remove_data_recursivly(grid)
grid.clear()
grid.add {type = 'flow'}
grid.add {
type = 'label',
caption = 'Duration:',
tooltip = 'Pro tip: Use mouse wheel or arrow keys for more fine control.'
}
local duration_flow = grid.add {type = 'flow', direction = 'horizontal'}
local duration_slider =
duration_flow.add {
type = 'slider',
name = create_poll_duration_name,
minimum_value = 0,
maximum_value = duration_slider_max,
value = math.floor(data.duration * inv_tick_duration_step)
}
duration_slider.style.width = 100
data.duration_slider = duration_slider
local duration_label = duration_flow.add {type = 'label'}
Gui.set_data(duration_slider, {duration_label = duration_label, data = data})
update_duration(duration_slider)
grid.add {type = 'flow'}
local question_label =
grid.add({type = 'flow'}).add {type = 'label', name = create_poll_label_name, caption = 'Question:'}
local question_textfield =
grid.add({type = 'flow'}).add {type = 'textfield', name = create_poll_question_name, text = data.question}
question_textfield.style.width = 400
Gui.set_data(question_label, question_textfield)
Gui.set_data(question_textfield, data)
local edit_mode = data.edit_mode
for count, answer in ipairs(answers) do
local delete_flow = grid.add {type = 'flow'}
local delete_button
if edit_mode or count ~= 1 then
delete_button =
delete_flow.add {
type = 'sprite-button',
name = create_poll_delete_answer_name,
sprite = 'utility/remove',
tooltip = 'Delete answer field.'
}
delete_button.style.height = 26
delete_button.style.width = 26
else
delete_flow.style.height = 26
delete_flow.style.width = 26
end
local label_flow = grid.add {type = 'flow'}
local label =
label_flow.add {
type = 'label',
name = create_poll_label_name,
caption = table.concat {'Answer #', count, ':'}
}
local textfield_flow = grid.add {type = 'flow'}
local textfield = textfield_flow.add {type = 'textfield', name = create_poll_answer_name, text = answer.text}
textfield.style.width = 400
Gui.set_data(textfield, {answers = answers, count = count})
if delete_button then
Gui.set_data(delete_button, {data = data, count = count})
end
Gui.set_data(label, textfield)
end
end
local function draw_create_poll_frame(parent, player, previous_data)
previous_data = previous_data or player_create_poll_data[player.index]
local edit_mode
local question
local answers
local duration
local title_text
local confirm_text
local confirm_name
if previous_data then
edit_mode = previous_data.edit_mode
question = previous_data.question
answers = {}
for i, a in ipairs(previous_data.answers) do
answers[i] = {text = a.text, source = a}
end
duration = previous_data.duration
else
question = ''
answers = {{text = ''}, {text = ''}, {text = ''}}
duration = default_poll_duration
end
if edit_mode then
title_text = 'Edit Poll #' .. previous_data.id
confirm_text = 'Edit Poll'
confirm_name = create_poll_edit_name
else
title_text = 'New Poll'
confirm_text = 'Create Poll'
confirm_name = create_poll_confirm_name
end
local frame =
parent.add {type = 'frame', name = create_poll_frame_name, caption = title_text, direction = 'vertical'}
frame.style.maximal_width = 320
local scroll_pane = frame.add {type = 'scroll-pane', vertical_scroll_policy = 'always'}
scroll_pane.style.maximal_height = 250
scroll_pane.style.maximal_width = 300
local grid = scroll_pane.add {type = 'table', column_count = 3}
local data = {
frame = frame,
grid = grid,
question = question,
answers = answers,
duration = duration,
previous_data = previous_data,
edit_mode = edit_mode
}
Gui.set_data(frame, data)
redraw_create_poll_content(data)
local add_answer_button =
scroll_pane.add {
type = 'button',
name = create_poll_add_answer_name,
caption = 'Add Answer'
}
apply_button_style(add_answer_button)
Gui.set_data(add_answer_button, data)
local bottom_flow = frame.add {type = 'flow', direction = 'horizontal'}
local left_flow = bottom_flow.add {type = 'flow'}
left_flow.style.align = 'left'
left_flow.style.horizontally_stretchable = true
local close_button = left_flow.add {type = 'button', name = create_poll_close_name, caption = 'Close'}
apply_button_style(close_button)
Gui.set_data(close_button, frame)
local clear_button = left_flow.add {type = 'button', name = create_poll_clear_name, caption = 'Clear'}
apply_button_style(clear_button)
Gui.set_data(clear_button, data)
local right_flow = bottom_flow.add {type = 'flow'}
right_flow.style.align = 'right'
if edit_mode then
local delete_button = right_flow.add {type = 'button', name = create_poll_delete_name, caption = 'Delete'}
apply_button_style(delete_button)
Gui.set_data(delete_button, data)
end
local confirm_button = right_flow.add {type = 'button', name = confirm_name, caption = confirm_text}
apply_button_style(confirm_button)
Gui.set_data(confirm_button, data)
end
local function show_new_poll(poll_data)
local message =
table.concat {poll_data.created_by.name, ' has created a new Poll #', poll_data.id, ': ', poll_data.question}
for _, p in ipairs(game.connected_players) do
local left = p.gui.left
local frame = left[main_frame_name]
if no_notify_players[p.index] then
if frame and frame.valid then
local data = Gui.get_data(frame)
update_poll_viewer(data)
end
else
p.print(message)
if frame and frame.valid then
local data = Gui.get_data(frame)
data.poll_index = #polls
update_poll_viewer(data)
else
player_poll_index[p.index] = nil
draw_main_frame(left, p)
end
end
end
end
local function create_poll(event)
local player = event.player
local data = Gui.get_data(event.element)
local frame = data.frame
local question = data.question
if not question:find('%S') then
event.player.print('Sorry, the poll needs a question.')
return
end
local answers = {}
for _, a in ipairs(data.answers) do
local text = a.text
if text:find('%S') then
local index = #answers + 1
answers[index] = {text = text, index = index, voted_count = 0}
end
end
if #answers < 1 then
player.print('Sorry, the poll needs at least one answer.')
return
end
player_create_poll_data[player.index] = nil
local tick = game.tick
local duration = data.duration
local end_tick
if duration == 0 then
end_tick = -1
else
end_tick = tick + duration
end
local poll_data = {
id = poll_id(),
question = question,
answers = answers,
voters = {},
start_tick = tick,
end_tick = end_tick,
duration = duration,
created_by = event.player,
edited_by = {}
}
table.insert(polls, poll_data)
show_new_poll(poll_data)
Gui.remove_data_recursivly(frame)
frame.destroy()
end
local function update_vote(voters, answer, direction)
local count = answer.voted_count + direction
answer.voted_count = count
local tooltip = {}
for pi, a in pairs(voters) do
if a == answer then
local player = Game.get_player_by_index(pi)
table.insert(tooltip, player.name)
end
end
return tostring(count), table.concat(tooltip, ', ')
end
local function vote(event)
local player_index = event.player_index
local voted_button = event.element
local button_data = Gui.get_data(voted_button)
local answer = button_data.answer
local poll_index = button_data.data.poll_index
local poll = polls[poll_index]
local voters = poll.voters
local previous_vote_answer = voters[player_index]
if previous_vote_answer == answer then
return
end
local vote_index = answer.index
voters[player_index] = answer
local previous_vote_button_count
local previous_vote_button_tooltip
local previous_vote_index
if previous_vote_answer then
previous_vote_button_count, previous_vote_button_tooltip = update_vote(voters, previous_vote_answer, -1)
previous_vote_index = previous_vote_answer.index
end
local vote_button_count, vote_button_tooltip = update_vote(voters, answer, 1)
for _, p in ipairs(game.connected_players) do
local frame = p.gui.left[main_frame_name]
if frame and frame.valid then
local data = Gui.get_data(frame)
if data.poll_index == poll_index then
local vote_buttons = data.vote_buttons
if previous_vote_answer then
local vote_button = vote_buttons[previous_vote_index]
vote_button.caption = previous_vote_button_count
vote_button.tooltip = previous_vote_button_tooltip
if p.index == player_index then
local vote_button_style = vote_button.style
vote_button_style.font_color = normal_color
vote_button_style.disabled_font_color = normal_color
end
end
local vote_button = vote_buttons[vote_index]
vote_button.caption = vote_button_count
vote_button.tooltip = vote_button_tooltip
if p.index == player_index then
local vote_button_style = vote_button.style
vote_button_style.font_color = focus_color
vote_button_style.disabled_font_color = focus_color
end
end
end
end
end
local function player_joined(event)
local player = Game.get_player_by_index(event.player_index)
if not player or not player.valid then
return
end
local gui = player.gui
if gui.top[main_button_name] ~= nil then
local frame = gui.left[main_frame_name]
if frame and frame.valid then
local data = Gui.get_data(frame)
update_poll_viewer(data)
end
else
gui.top.add {type = 'sprite-button', name = main_button_name, sprite = 'item/programmable-speaker'}
end
end
local function tick()
for _, p in ipairs(game.connected_players) do
local frame = p.gui.left[main_frame_name]
if frame and frame.valid then
local data = Gui.get_data(frame)
local poll = polls[data.poll_index]
if poll then
local poll_enabled = do_remaining_time(poll, data.remaining_time_label)
if not poll_enabled then
for _, v in ipairs(data.vote_buttons) do
v.enabled = poll_enabled
end
end
end
end
end
end
Event.add(defines.events.on_player_joined_game, player_joined)
Event.on_nth_tick(60, tick)
Gui.on_click(main_button_name, toggle)
Gui.on_click(
create_poll_button_name,
function(event)
local player = event.player
local left = player.gui.left
local frame = left[create_poll_frame_name]
if frame and frame.valid then
remove_create_poll_frame(frame, player.index)
else
draw_create_poll_frame(left, player)
end
end
)
Gui.on_click(
poll_view_edit_name,
function(event)
local player = event.player
local left = player.gui.left
local frame = left[create_poll_frame_name]
if frame and frame.valid then
Gui.remove_data_recursivly(frame)
frame.destroy()
end
local main_frame = left[main_frame_name]
local frame_data = Gui.get_data(main_frame)
local poll = polls[frame_data.poll_index]
poll.edit_mode = true
draw_create_poll_frame(left, player, poll)
end
)
Gui.on_value_changed(
create_poll_duration_name,
function(event)
update_duration(event.element)
end
)
Gui.on_click(
create_poll_delete_answer_name,
function(event)
local button_data = Gui.get_data(event.element)
local data = button_data.data
table.remove(data.answers, button_data.count)
redraw_create_poll_content(data)
end
)
Gui.on_click(
create_poll_label_name,
function(event)
local textfield = Gui.get_data(event.element)
textfield.focus()
end
)
Gui.on_text_changed(
create_poll_question_name,
function(event)
local textfield = event.element
local data = Gui.get_data(textfield)
data.question = textfield.text
end
)
Gui.on_text_changed(
create_poll_answer_name,
function(event)
local textfield = event.element
local data = Gui.get_data(textfield)
data.answers[data.count].text = textfield.text
end
)
Gui.on_click(
create_poll_add_answer_name,
function(event)
local data = Gui.get_data(event.element)
table.insert(data.answers, {text = ''})
redraw_create_poll_content(data)
end
)
Gui.on_click(
create_poll_close_name,
function(event)
local frame = Gui.get_data(event.element)
remove_create_poll_frame(frame, event.player_index)
end
)
Gui.on_click(
create_poll_clear_name,
function(event)
local data = Gui.get_data(event.element)
local slider = data.duration_slider
slider.slider_value = math.floor(default_poll_duration * inv_tick_duration_step)
update_duration(slider)
data.question = ''
local answers = data.answers
for i = 1, #answers do
answers[i].text = ''
end
redraw_create_poll_content(data)
end
)
Gui.on_click(create_poll_confirm_name, create_poll)
Gui.on_click(
create_poll_delete_name,
function(event)
local player = event.player
local data = Gui.get_data(event.element)
local frame = data.frame
local poll = data.previous_data
Gui.remove_data_recursivly(frame)
frame.destroy()
player_create_poll_data[player.index] = nil
local removed_index
for i, p in ipairs(polls) do
if p == poll then
table.remove(polls, i)
removed_index = i
break
end
end
if not removed_index then
return
end
local message = table.concat {player.name, ' has deleted Poll #', poll.id, ': ', poll.question}
for _, p in ipairs(game.connected_players) do
if not no_notify_players[p.index] then
p.print(message)
end
local main_frame = p.gui.left[main_frame_name]
if main_frame and main_frame.valid then
local main_frame_data = Gui.get_data(main_frame)
local poll_index = main_frame_data.poll_index
if removed_index < poll_index then
main_frame_data.poll_index = poll_index - 1
end
update_poll_viewer(main_frame_data)
end
end
end
)
Gui.on_click(
create_poll_edit_name,
function(event)
local player = event.player
local data = Gui.get_data(event.element)
local frame = data.frame
local poll = data.previous_data
local new_question = data.question
if not new_question:find('%S') then
player.print('Sorry, the poll needs a question.')
return
end
local new_answer_set = {}
local new_answers = {}
for _, a in ipairs(data.answers) do
if a.text:find('%S') then
local source = a.source
local index = #new_answers + 1
if source then
new_answer_set[source] = a
source.text = a.text
source.index = index
new_answers[index] = source
else
new_answers[index] = {text = a.text, index = index, voted_count = 0}
end
end
end
if not next(new_answers) then
player.print('Sorry, the poll needs at least one answer.')
return
end
Gui.remove_data_recursivly(frame)
frame.destroy()
local player_index = player.index
player_create_poll_data[player_index] = nil
local old_answers = poll.answers
local voters = poll.voters
for _, a in ipairs(old_answers) do
if not new_answer_set[a] then
for pi, a2 in pairs(voters) do
if a == a2 then