Exemple #1
0
        public IEnumerator <float> StartVoteCoroutine(Vote newVote, CallvoteFunction callback)
        {
            int timerCounter = 0;

            CurrentVote          = newVote;
            CurrentVote.Callback = callback;
            string firstBroadcast = CurrentVote.Question + " Press ~ and type ";
            int    counter        = 0;

            foreach (KeyValuePair <int, string> kv in CurrentVote.Options)
            {
                if (counter == CurrentVote.Options.Count - 1)
                {
                    firstBroadcast += "or ." + kv.Key + " for " + kv.Value + " ";
                }
                else
                {
                    firstBroadcast += "." + kv.Key + " for " + kv.Value + ", ";
                }
                counter++;
            }
            int textsize = firstBroadcast.Length / 10;

            Map.Broadcast(5, "<size=" + (48 - textsize).ToString() + ">" + firstBroadcast + "</size>");
            yield return(Timing.WaitForSeconds(5f));

            for (; ;)
            {
                if (timerCounter >= Plugin.Instance.Config.VoteDuration + 1)
                {
                    if (CurrentVote.Callback == null)
                    {
                        string timerBroadcast = "Final results:\n";
                        foreach (KeyValuePair <int, string> kv in CurrentVote.Options)
                        {
                            timerBroadcast += CurrentVote.Options[kv.Key] + " (" + CurrentVote.Counter[kv.Key] + ") ";
                            textsize        = timerBroadcast.Length / 10;
                        }
                        Map.Broadcast(5, "<size=" + (48 - textsize).ToString() + ">" + timerBroadcast + "</size>");
                    }
                    else
                    {
                        CurrentVote.Callback.Invoke(CurrentVote);
                    }
                    CurrentVote = null;
                    yield break;
                }
                else
                {
                    string timerBroadcast = firstBroadcast + "\n";
                    foreach (KeyValuePair <int, string> kv in CurrentVote.Options)
                    {
                        timerBroadcast += CurrentVote.Options[kv.Key] + " (" + CurrentVote.Counter[kv.Key] + ") ";
                        textsize        = timerBroadcast.Length / 10;
                    }
                    Map.Broadcast(1, "<size=" + (48 - textsize).ToString() + ">" + timerBroadcast + "</size>");
                }
                timerCounter++;
                yield return(Timing.WaitForSeconds(1f));
            }
        }
        public bool StartVote(Vote newVote, CallvoteFunction callback)
        {
            if (CurrentVote != null)
            {
                return(false);
            }

            CurrentVote          = newVote;
            CurrentVote.Callback = callback;
            string firstBroadcast = CurrentVote.Question + " Press ~ and type ";
            int    counter        = 0;

            foreach (KeyValuePair <int, string> kv in CurrentVote.Options)
            {
                if (counter == CurrentVote.Options.Count - 1)
                {
                    firstBroadcast += "or ." + kv.Key + " for " + kv.Value + " ";
                }
                else
                {
                    firstBroadcast += "." + kv.Key + " for " + kv.Value + ", ";
                }
                counter++;
            }
            Extensions.BC(5, firstBroadcast);

            int timerCounter = 0;

            CurrentVote.Timer = new Timer
            {
                Interval  = 5000,
                Enabled   = true,
                AutoReset = true
            };
            CurrentVote.Timer.Elapsed += delegate
            {
                if (CurrentVote.Timer.Interval == 5000)
                {
                    CurrentVote.Timer.Interval = 1000;
                }

                if (timerCounter >= this.VoteDuration + 1)
                {
                    if (CurrentVote.Callback == null)
                    {
                        string timerBroadcast = "Final results:\n";
                        foreach (KeyValuePair <int, string> kv in CurrentVote.Options)
                        {
                            timerBroadcast += CurrentVote.Options[kv.Key] + " (" + CurrentVote.Counter[kv.Key] + ") ";
                        }
                        Extensions.BC(5, timerBroadcast);
                    }
                    else
                    {
                        CurrentVote.Callback.Invoke(CurrentVote);
                    }

                    CurrentVote.Timer.Enabled = false;
                    CurrentVote = null;
                }
                else
                {
                    string timerBroadcast = firstBroadcast + "\n";
                    foreach (KeyValuePair <int, string> kv in CurrentVote.Options)
                    {
                        timerBroadcast += CurrentVote.Options[kv.Key] + " (" + CurrentVote.Counter[kv.Key] + ") ";
                    }
                    Extensions.BC(1, timerBroadcast);
                }
                timerCounter++;
            };
            return(true);
        }