159 lines
3.1 KiB
GDScript
159 lines
3.1 KiB
GDScript
extends Node2D
|
|
|
|
var client_id = ""
|
|
var oauth = "oauth:"
|
|
|
|
onready var twicil = get_node("TwiCIL");
|
|
|
|
onready var label = $TimerLabel;
|
|
|
|
onready var PollItem = preload("res://resources/customControls/Twitch/TwitchPollItem.tscn");
|
|
|
|
var voted_users:Array = [];
|
|
var votes = {};
|
|
|
|
var timeToVote:int;
|
|
|
|
var numOfChoices:int;
|
|
|
|
var isPolling:bool;
|
|
|
|
var inited:bool;
|
|
|
|
func _ready():
|
|
isPolling = false;
|
|
inited = false;
|
|
|
|
func Clear():
|
|
for i in $VBoxContainer.get_children():
|
|
$VBoxContainer.remove_child(i);
|
|
voted_users.clear();
|
|
votes.clear();
|
|
|
|
func Init(amount:int):
|
|
inited = true;
|
|
|
|
var nick = CreateNickname();
|
|
var channelName = SettingsSingleton.GetTwitchChannel();
|
|
|
|
|
|
var attemps = 0;
|
|
while true:
|
|
attemps += 1;
|
|
|
|
var _temp = twicil.connect_to_twitch_chat()
|
|
yield (twicil, "ConnectedToTwitch");
|
|
|
|
if attemps == 4:
|
|
print("TWITCH CONNECTION KAPETS")
|
|
return
|
|
|
|
if twicil.IsConnected():
|
|
break;
|
|
|
|
twicil.connect_to_channel(channelName, client_id, oauth, nick)
|
|
|
|
$SizeTimer.start(0.01);
|
|
|
|
Clear();
|
|
numOfChoices = amount;
|
|
for i in amount:
|
|
votes[i + 1] = 0;
|
|
var pollItem = PollItem.instance();
|
|
$VBoxContainer.add_child(pollItem);
|
|
|
|
if not twicil.is_connected("message_recieved", self, "_on_message_recieved"):
|
|
twicil.connect("message_recieved", self, "_on_message_recieved")
|
|
|
|
func StartTimer():
|
|
if not inited:
|
|
Disconnect();
|
|
return ;
|
|
|
|
z_index = 10;
|
|
visible = true;
|
|
isPolling = true;
|
|
$Timer.start(SettingsSingleton.GetTwitchTimer());
|
|
|
|
func Disconnect():
|
|
visible = false;
|
|
isPolling = false;
|
|
Clear();
|
|
if twicil.is_connected("message_recieved", self, "_on_message_recieved"):
|
|
twicil.disconnect("message_recieved", self, "_on_message_recieved")
|
|
twicil.Disconnect();
|
|
label.text = "";
|
|
|
|
inited = false;
|
|
|
|
const messageCap:int = 5;
|
|
|
|
func _on_message_recieved(user_name:String, text:String, _emotes)->void :
|
|
if user_name in voted_users:
|
|
return ;
|
|
|
|
if text.length() > messageCap:
|
|
text = text.left(messageCap)
|
|
|
|
var number = int(text);
|
|
|
|
if not votes.has(number):
|
|
return ;
|
|
|
|
votes[number] += 1
|
|
voted_users.push_back(user_name)
|
|
|
|
$VBoxContainer.get_child(number - 1).AddVote(user_name);
|
|
|
|
func _process(_delta):
|
|
if not isPolling:
|
|
return ;
|
|
var timer = $Timer;
|
|
if timer.time_left < 6.0:
|
|
label.set("custom_colors/font_color", Color(0.78, 0, 0, 1))
|
|
else :
|
|
label.set("custom_colors/font_color", Color(0.75, 0.75, 0.75, 1))
|
|
|
|
label.text = "%d" % $Timer.time_left;
|
|
|
|
func CreateNickname()->String:
|
|
var rnd = RandomNumberGenerator.new();
|
|
rnd.randomize();
|
|
var nick = str("justinfan", rnd.randi_range(10000, 99999));
|
|
return nick;
|
|
|
|
func Result()->int:
|
|
var candidates = [];
|
|
|
|
var values = votes.values();
|
|
values.sort();
|
|
var maxValue = values.max();
|
|
|
|
for i in votes.keys():
|
|
if votes[i] == maxValue:
|
|
candidates.push_back(i);
|
|
|
|
if candidates.size() > 1:
|
|
candidates.shuffle()
|
|
|
|
return candidates[0];
|
|
|
|
func StopTimerInMenu():
|
|
if not isPolling:
|
|
return
|
|
|
|
if $Timer.is_stopped():
|
|
return
|
|
z_index = 0;
|
|
$Timer.set_paused(true)
|
|
|
|
func ResumeTimerInMenu():
|
|
if not isPolling:
|
|
return
|
|
|
|
if $Timer.paused:
|
|
z_index = 10;
|
|
$Timer.set_paused(false)
|
|
|
|
func AppearTimer():
|
|
visible = isPolling;
|