Esempio n. 1
0
        public async Task UpdateStatus()
        {
            try
            {
                string status = String.Empty;
                string game   = String.Empty;

                // Read Status from File
                if (!string.IsNullOrEmpty(Settings.StatusFile))
                {
                    if (!File.Exists(Settings.StatusFile))
                    {
                        Logger.Instance.LogMessage(TracingLevel.WARN, $"Status file does not exists: {Settings.StatusFile}");
                    }
                    else
                    {
                        string[] lines = File.ReadAllLines(Settings.StatusFile);
                        if (lines.Length > 1) // There are multiple lines in the file, choose a random one.
                        {
                            int retries = 0;
                            do
                            {
                                // Choose a random one
                                status = lines[RandomGenerator.Next(lines.Length)];
                                retries++;
                            } while (String.IsNullOrEmpty(status) && retries < 5);
                        }
                        else if (lines.Length == 1)
                        {
                            status = lines[0];
                        }
                    }
                }

                // Read Game from File
                if (!string.IsNullOrEmpty(Settings.GameFile))
                {
                    if (!File.Exists(Settings.GameFile))
                    {
                        Logger.Instance.LogMessage(TracingLevel.WARN, $"Game file does not exists: {Settings.GameFile}");
                    }
                    else
                    {
                        game = File.ReadAllText(Settings.GameFile);
                    }
                }

                if (String.IsNullOrEmpty(status) && String.IsNullOrEmpty(game))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"UpdateStatus called but both status and game are empty");
                    return;
                }

                using (TwitchComm comm = new TwitchComm())
                {
                    if (await comm.UpdateChannelStatus(status, game))
                    {
                        await Connection.ShowOk();
                    }
                    else
                    {
                        await Connection.ShowAlert();
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessage(TracingLevel.ERROR, $"UpdateStatus Exception: {ex}");
            }
        }