Ejemplo n.º 1
0
        //Basically the constructor/console command parser
        static void Main(string[] args)
        {
            //Check if the stream is live before doing anything
            Write("##Connecting to twitch...");
            IsLive = Live;
            Write("##Connected. Stream is currently " + (IsLive ? "live." : "offline."));

            //Load playerdata
            playerList    = Load();
            onlinePlayers = new HashSet <Player>();

            //Instantiate IRC object
            Write("##Connecting to irc...");
            irc = new IRC(user, oauth, "irc.twitch.tv", channel);
            Write("##Connected.");

            //Add commands to IRC object's command list
            Write("##Adding commands...");
            foreach (var c in commands)
            {
                irc.AddCommand(c);
                Write("##Added " + c);
            }

            //Subscribe to IRC events
            irc.onCommand += new CommandEventHandler(irc_onCommand);
            irc.onOutput  += new OutputEventHandler(handleOutput);

            //Can be used to send messages to IRC when connected
            //Send("Test");

            //Create and start main time thread
            Write("##Starting Main Loop...");
            timer = new Thread(new ThreadStart(TimerLoop));
            timer.IsBackground = true;
            timer.Start();
            Write("##Main Loop started");

            //Parse console commands
            while (true)
            {
                var input = Console.ReadLine();

                //Force a save
                if (input.ToLower() == "save")
                {
                    Write("##Saving...");
                    try
                    {
                        Save();
                    }
                    catch (Exception e)
                    {
                        Write(e.Message);
                        Console.WriteLine(e.StackTrace);
                        Console.ReadLine();
                    }
                    continue;
                }
                //Check time since last event
                else if (input.ToLower() == "etime")
                {
                    var time = TimeSpan.FromMinutes(20).Subtract(TimeSince(prevEvent));

                    Write("##Time until next event: " + time.TotalMinutes.ToString());
                    continue;
                }
                //Force Event
                else if (input.ToLower() == "event")
                {
                    Write("##Forcing Event...");
                    prevEvent = DateTime.Now.Subtract(TimeSpan.FromMinutes(20));
                    continue;
                }
                //Initiate full data wipe
                else if (input.ToLower() == "wipe")
                {
                    Write("##WARNING: This will wipe ALL player data, type yes to confirm: ");
                    var conf = Console.ReadLine();
                    if (conf.ToLower() == "yes")
                    {
                        Write("##Wiping data...");
                        Send("Gunner has initiated a data wipe cos he sux, enjoy being n00bs again natsK");
                        if (File.Exists(dirPath + "\\Save.sqlite"))
                        {
                            File.Delete(dirPath + "\\Save.sqlite");
                        }
                        playerList    = new HashSet <Player>();
                        onlinePlayers = new HashSet <Player>();

                        Write("##Saving...");
                        try
                        {
                            Save();
                        }
                        catch (Exception e)
                        {
                            Write(e.Message);
                            Console.WriteLine(e.StackTrace);
                            Console.ReadLine();
                        }
                        continue;
                    }
                }
                //Add PL to a player or all players
                else if (input.ToLower().StartsWith("addpl"))
                {
                    var ex = input.ToLower().Split(' ');

                    if (ex.Length == 2)
                    {
                        int pl;
                        if (int.TryParse(ex[1], out pl))
                        {
                            var temp = new HashSet <Player>(playerList);
                            foreach (var p in temp)
                            {
                                p.BonusPL += pl;
                            }
                            Write(string.Format("##Added {0} PL to all players", pl));
                            continue;
                        }
                    }
                    else if (ex.Length == 3)
                    {
                        int pl;
                        if (int.TryParse(ex[2], out pl))
                        {
                            var p = GetExistingPlayer(ex[1]);

                            if (p != null)
                            {
                                p.BonusPL += pl;
                                Write(string.Format("##Added {0} PL to {1}", pl, p.User));
                                continue;
                            }
                        }
                    }
                }
                //Send message to IRC
                else if (input.ToLower().StartsWith("say "))
                {
                    Write(string.Format("##Sending {0} to twitch", input.Substring(4)));
                    Send(input.Substring(4));
                    continue;
                }
                //Save and quit
                else if (input.ToLower() == "quit")
                {
                    Write("##Saving...");
                    try
                    {
                        timer.Abort();
                        irc.Close();
                        Save();
                    }
                    catch (Exception e)
                    {
                        Write(e.Message);
                        Console.WriteLine(e.StackTrace);
                        Console.ReadLine();
                    }
                    break;
                }

                Write("##Invalid Command");
            }

            Write("##Quitting application...");
            timer.Abort();
        }