예제 #1
0
        private TimelineStreaming StreamTimeline(MastodonClient client, TimelineType type)
        {
            TimelineStreaming streaming = null;

            switch (type)
            {
            case TimelineType.Home:
                streaming = client.GetUserStreaming();
                break;

            default:
                streaming = client.GetPublicStreaming();
                break;
            }
            Console.WriteLine("\n\nStart fetching " + ((int)type == 0 ? "Local" : (int)type == 1 ? "Home" : "Federation") + " Timeline.");
            Console.WriteLine("================================================\n\n");
            streaming.OnUpdate += (sender, e) =>
            {
                StreamUpdateEventArgs s = type == TimelineType.Local ? !Regex.IsMatch(e.Status.Account.AccountName, ".+@.*") ? e : null : e;
                if (s != null)
                {
                    Console.WriteLine(s.Status.Account.DisplayName);
                    Console.WriteLine("@" + s.Status.Account.AccountName);
                    if (s.Status.SpoilerText != "")
                    {
                        Console.WriteLine("Note: " + s.Status.SpoilerText);
                    }
                    Console.WriteLine(HTML_Perser(s.Status.Content));
                    Console.WriteLine();
                }
            };
            return(streaming);
        }
예제 #2
0
 public NotificationsModel(IMastodonClient client)
 {
     this.client      = client;
     streaming        = this.client.GetUserStreaming();
     StreamingStarted = streamingStarted.ToReadOnlyReactiveProperty();
     StreamingStarting.DistinctUntilChanged().Subscribe(OnStreamingChanged);
     streaming.OnNotification += Streaming_OnNotification;
 }
예제 #3
0
 public override void StreamStart(Client client)
 {
     Changed("StreamState");
     stream           = client.GetStream("public:local");
     stream.OnUpdate += this.UpdateListener;
     StreamState      = true;
     Changed("StreamState");
     stream.Start();
 }
예제 #4
0
        private async void Start(Task <IEnumerable <Status> > past, TimelineStreaming streaming)
        {
            foreach (var status in await past)
            {
                OnNext(status);
            }

            streaming.OnUpdate += (_, e) => OnNext(e.Status);
            await streaming.Start();
        }
예제 #5
0
        /// <summary>
        /// コンストラクタです。
        /// </summary>
        /// <param name="client">Mastodon クライアント。</param>
        /// <param name="type">タイムラインの種類。</param>
        public TimeLineMastodon(TwitdonMastodonClient client, Define.MastodonTimeLineType type)
        {
            this.client = client;
            streaming   = type == Define.MastodonTimeLineType.Home ? client.UserStreaming : client.PublicStreaming;
            this.type   = type;
            var name = $"{Utilities.MastodonTimeLineTypeToString(type)}  ";

            TimeLineName    = $"{name}{client.AccountName}";
            statuses        = new List <TimeLineStatus>(Define.StatusesCapacity);
            fetchedStatuses = new Queue <TwitdonMastodonStatus>();
        }
예제 #6
0
 protected TimelineModelBase(IMastodonClient client)
 {
     this.client      = client;
     StreamingStarted = streamingStarted.ToReadOnlyReactiveProperty();
     streaming        = GetStreaming();
     if (streaming != null)
     {
         streaming.OnUpdate += Streaming_OnUpdate;
         streaming.OnDelete += Streaming_OnDelete;
     }
     StreamingStarting.DistinctUntilChanged().Subscribe(OnStreamingChanged);
 }
예제 #7
0
        public async Task Start()
        {
            Results     = new List <BattleResult>();
            AttackCount = 0;
            IsRunning   = true;


            var postStatus = await MastodonClient.PostStatus(Account.Roar, Visibility.Public);

            string accoutName = postStatus.Account.UserName;
            long   accountId  = postStatus.Account.Id;

            // 過去トゥートの削除
            var statuses = await MastodonClient.GetAccountStatuses(accountId, postStatus.Id);

            foreach (var status in statuses)
            {
                await MastodonClient.DeleteStatus(status.Id);
            }

            UserStreaming = MastodonClient.GetUserStreaming();
            UserStreaming.OnNotification += async(sender, e) =>
            {
                var status = e.Notification.Status;
                if (status == null || !status.Content.Contains($"@<span>{accoutName}</span>"))
                {
                    return;
                }

                AttackCount++;
                if (AttackCount > Account.LifePoint)
                {
                    return;
                }

                Results.Add(new BattleResult
                {
                    PostId  = status.Id,
                    Name    = status.Account.AccountName,
                    Content = status.Content
                });
                if (AttackCount == Account.LifePoint)
                {
                    await End(true);

                    return;
                }
                await MastodonClient.PostStatus($"@{status.Account.AccountName} {Account.Roar}", Visibility.Public);
            };

            await UserStreaming.Start();
        }
예제 #8
0
        private async Task Run()
        {
            Dictionary <string, string> uri_list = new Dictionary <string, string>();

            var appRegistration = new AppRegistration {
                Instance     = hostName,
                ClientId     = clientId,
                ClientSecret = clientSecret,
                Scope        = Scope.Read | Scope.Write
            };
            var authClient = new AuthenticationClient(appRegistration);

            var auth = await authClient.ConnectWithPassword(mail, pass);

            client = new MastodonClient(appRegistration, auth);

            var account = await client.GetCurrentUser();

            this.userId = account.AccountName;

            // LTL処理
            streaming           = client.GetLocalStreaming();
            streaming.OnUpdate += (sender, e) => {
                TextWrite(e.Status);
            };

            //通知処理
            homeStreaming = client.GetUserStreaming();
            homeStreaming.OnNotification += (sender, e) => {
                NotificationWrite(e.Notification);
            };

            // メディア処理用
            media = new MediaEditClass(ref client);

            Timer timer = new Timer();

            timer.Interval = DefaultValues.TOOTS_INTERVAL;
            timer.Tick    += (object sender, EventArgs e) => {
                // トゥート回数のリセット
                tootsCounter = 0;
            };
            timer.Start();

            streaming.Start();
            homeStreaming.Start();
        }
예제 #9
0
        public virtual async Task StopAsync()
        {
            if (this.isStreaming)
            {
                try
                {
                    this.Streaming.Stop();
                }
                catch { }
                this.Streaming.OnUpdate -= this.Streaming_OnUpdate;
                this.Streaming.OnDelete -= this.Streaming_OnDelete;
                this.Streaming           = null;
                this.timer.Change(Timeout.Infinite, Timeout.Infinite);

                this.isStreaming = false;
            }
        }
예제 #10
0
        private async Task StartAsync(int tryCount, Exception lastException = null)
        {
            if (this.isStreaming)
            {
                return;
            }

            // 試行回数
            if (tryCount > 3)
            {
                this.Errored?.Invoke(this, new ObjectFunctionErrorEventArgs
                {
                    Exception = lastException,
                    Type      = ObjectFunctionErrorType.StartError,
                });
                return;
            }

            try
            {
                if (this.Streaming == null)
                {
                    this.Streaming = this.GetTimelineStreaming(this.StreamingInstanceUri);

                    this.Streaming.OnUpdate += this.Streaming_OnUpdate;
                    this.Streaming.OnDelete += this.Streaming_OnDelete;
                }

                this.timer.Change(1000 * StreamingTimeOut, Timeout.Infinite);
                this.Streaming.Start();

                this.isStreaming = true;
            }
            catch (Exception e)
            {
                await this.StopAsync();

                await this.StartAsync(tryCount + 1, e);
            }
        }
예제 #11
0
        public async Task Start()
        {
            Results   = new List <BattleResult>();
            Life      = Boss.LifePoint;
            IsRunning = true;

            string accoutName = "boss";

            int[] mediaIds = null; // アイコンのアップロード
            using (var stream = File.OpenRead(Boss.ImagePath))
            {
                var attach = await MastodonClient.UploadMedia(stream);

                mediaIds = new int[] { attach.Id };
            }
            Console.WriteLine("画像をアップロードしました");

            string appear = new StringBuilder()
                            .AppendLine($"({Boss.Name}が現れた! LP: {Boss.LifePoint})")
                            .AppendLine(Boss.VoiceAppear)
                            .ToString();
            await MastodonClient.PostStatus(appear, Visibility.Public, mediaIds : mediaIds);


            UserStreaming = MastodonClient.GetUserStreaming();
            UserStreaming.OnNotification += async(sender, e) =>
            {
                Console.WriteLine("通知を受信しました");

                if (!IsRunning)
                {
                    return;
                }

                var status = e.Notification.Status;
                if (status == null || !status.Content.Contains($"@<span>{accoutName}</span>"))
                {
                    return;
                }

                Console.WriteLine(status.Content);

                string content = DeleteTags(status.Content);
                if (Boss.Weakness != "")
                {
                    bool matchWeakness = false;
                    foreach (string weakness in Boss.Weakness.Replace("\r\n", "\n").Split('\n'))
                    {
                        if (weakness != "" && content.Contains(weakness))
                        {
                            matchWeakness = true;
                            break;
                        }
                    }
                    if (!matchWeakness)
                    {
                        string nodamage = new StringBuilder()
                                          .AppendLine($"{Boss.Name} (LP: {Life}/{Boss.LifePoint})")
                                          .AppendLine($"({Boss.Name}にダメージを与えられない!)")
                                          .AppendLine($"> {GetName(status.Account)}「{content}」")
                                          .ToString();
                        await MastodonClient.PostStatus(nodamage, Visibility.Public);

                        return;
                    }
                }
                if (randomizer.Next(99) < Boss.EvadeRate)
                {
                    string evade = new StringBuilder()
                                   .AppendLine($"{Boss.Name} (LP: {Life}/{Boss.LifePoint})")
                                   .AppendLine($"({Boss.Name}は攻撃を回避した!)")
                                   .AppendLine($"> {GetName(status.Account)}「{content}」")
                                   .ToString();
                    await MastodonClient.PostStatus(evade, Visibility.Public);

                    return;
                }


                Life--;
                if (Life < 0)
                {
                    return;
                }

                Results.Add(new BattleResult
                {
                    PostId  = status.Id,
                    Name    = status.Account.AccountName,
                    Content = status.Content
                });
                if (Life == 0)
                {
                    string dead = new StringBuilder()
                                  .AppendLine($"{Boss.Name} (LP: 0/{Boss.LifePoint})")
                                  .AppendLine($"{Boss.VoiceDead}")
                                  .AppendLine($"> {GetName(status.Account)}「{content}」")
                                  .ToString();
                    await MastodonClient.PostStatus(dead, Visibility.Public, mediaIds : mediaIds);

                    await End(true);

                    return;
                }

                string damage = new StringBuilder()
                                .AppendLine($"{Boss.Name} (LP: {Life}/{Boss.LifePoint})")
                                .AppendLine($"{Boss.VoiceDamage}")
                                .AppendLine($"> {GetName(status.Account)}「{content}」")
                                .ToString();
                await MastodonClient.PostStatus(damage, Visibility.Public);

                if (Life % 2 == 1)
                {
                    string counter = $"@{status.Account.AccountName} {Boss.VoiceCounter}";
                    await MastodonClient.PostStatus(counter, Visibility.Public);
                }
            };

            Console.WriteLine("Battleを開始します");
            await UserStreaming.Start();
        }
예제 #12
0
        /* 主機能の実装 */
        static void Main()
        {
            Program cliMa = new Program();

            Console.SetWindowPosition(0, 0);
            Console.SetWindowSize(50, 30);
            Console.Clear();
            Console.WriteLine(@"
              __  __  __     __
    ____     / / /_/ /  \   /  \    ____
   / __ \   / / __  / /\ \ / /\ \  /  _ \/\
  / /  \_\ / / / / / /  \ V /  \ \ \ \ \   \
 / /  __  / / / / / /    \ /    \ \ \ \ \   \
/ /__/ / / / / / / /     | |     \ \ \ \_\   \
\_____/ /_/ /_/ /_/      |_|      \_\ \____/\_\");
            if (!File.Exists(path))
            {
                cliMa.RegistClient();
            }
            MastodonClient client = cliMa.LoginClient();

            Console.WriteLine("\nLogin Succeeded.");
            TimelineStreaming stream = cliMa.StreamTimeline(client, TimelineType.Local);

            stream.Start();
            while (true)
            {
                string com = Console.ReadLine();
                if (com != String.Empty)
                {
                    string[] Input = com.Split(' ');
                    switch (Input[0])
                    {
                    case "timeline":
                        if (Input.Length > 1)
                        {
                            stream.Stop();
                            switch (Input[1])
                            {
                            case "-l":
                                stream = cliMa.StreamTimeline(client, TimelineType.Local);
                                break;

                            case "-h":
                                stream = cliMa.StreamTimeline(client, TimelineType.Home);
                                break;

                            case "-f":
                                stream = cliMa.StreamTimeline(client, TimelineType.Federation);
                                break;

                            default:
                                ErrorMsg();
                                break;
                            }
                            stream.Start();
                        }
                        else
                        {
                            ErrorMsg();
                        }
                        break;

                    case "toot":
                        if (Input.Length > 1)
                        {
                            switch (Input.Length)
                            {
                            case 2:
                                client.PostStatus(Input[1], Visibility.Public);
                                break;

                            case 3:
                                switch (Input[1])
                                {
                                case "-u":
                                    client.PostStatus(Input[2], Visibility.Unlisted);
                                    break;

                                case "-p":
                                    client.PostStatus(Input[2], Visibility.Private);
                                    break;

                                case "-d":
                                    client.PostStatus(Input[2], Visibility.Direct);
                                    break;

                                default:
                                    ErrorMsg();
                                    break;
                                }
                                break;

                            case 4:
                                client.PostStatus(Input[3], Visibility.Public, sensitive: true, spoilerText: Input[2]);
                                break;

                            case 5:
                                switch (Input[1])
                                {
                                case "-u":
                                    client.PostStatus(Input[4], Visibility.Unlisted, sensitive: true, spoilerText: Input[3]);
                                    break;

                                case "-p":
                                    client.PostStatus(Input[4], Visibility.Private, sensitive: true, spoilerText: Input[3]);
                                    break;

                                case "-d":
                                    client.PostStatus(Input[4], Visibility.Direct, sensitive: true, spoilerText: Input[3]);
                                    break;

                                default:
                                    ErrorMsg();
                                    break;
                                }
                                break;
                            }
                        }
                        else
                        {
                            ErrorMsg();
                        }
                        break;

                    case "help":
                        if (Input.Length > 1)
                        {
                            switch (Input[1])
                            {
                            case "timeline":
                                Console.WriteLine(@"
<Command> timeline:
'timeline [-l|-h|-f]'
 This Command changes Stream type.
 At Startup, Stream type is 'Local'
[Argument]
 -l: Set Stream type to Local Timeline
 -h: Set Stream type to Home Timeline
 -f: Set Stream type to Federation Timeline");
                                break;

                            case "toot":
                                Console.WriteLine(@"
<Command> toot:
'toot [-u|-p|-d] [-s (SpoilerText)] (Content)'
This Command sends Toot Content.
Cannot Toot if Content is null.
Default Toot type is 'Public'
[Argument]
 -u: Set Toot type once to Unlisted
 -p: Set Toot type once to Private
 -d: Set Toot type once to Direct
 -s (SpoilerText): Write SpoilerText to Toot");
                                break;
                            }
                        }
                        else
                        {
                            Console.WriteLine(@"
<Command List>
-> timeline [-l|-h|-f]
-> toot [-u|-p|-d] [-s (SpoilerText)] (Content)
-> help
If you want more help, type 'help [timeline|toot]'");
                        }
                        break;

                    default:
                        ErrorMsg();
                        break;
                    }
                }
            }
        }
예제 #13
0
 public Timeline(Task <IEnumerable <Status> > past, TimelineStreaming streaming)
 {
     Start(past, streaming);
 }
예제 #14
0
 public MastoNetUserStreaming(TimelineStreaming streaming)
 {
     _streaming = streaming;
 }
예제 #15
0
        public async Task Start()
        {
            UserStreaming = MastodonClient.GetUserStreaming();
            UserStreaming.OnNotification += async(sender, e) =>
            {
                var status = e.Notification.Status;
                if (status == null || !status.Content.Contains($"@<span>{Name}</span>"))
                {
                    return;
                }

                if (status.Content.Contains("シャッフル"))
                {
                    cardsSet.Reset();
                    await MastodonClient.PostStatus($"シャッフルしました", Visibility.Public);

                    return;
                }

                int num   = 1;
                var match = Regex.Match(status.Content, "[0-90-9]+");
                if (match.Success)
                {
                    string value = match.Value
                                   .Replace("0", "0")
                                   .Replace("1", "1")
                                   .Replace("2", "2")
                                   .Replace("3", "3")
                                   .Replace("4", "4")
                                   .Replace("5", "5")
                                   .Replace("6", "6")
                                   .Replace("7", "7")
                                   .Replace("8", "8")
                                   .Replace("9", "9");
                    num = int.Parse(match.Value);
                    if (num < 1)
                    {
                        num = 1;
                    }
                    if (num > 5)
                    {
                        num = 5;
                    }
                }

                var builder = new StringBuilder();
                for (int i = 0; i < num; i++)
                {
                    if (cardsSet.Empty)
                    {
                        cardsSet.Reset();
                        builder.AppendLine("シャッフルしました");
                    }

                    string card = cardsSet.Get();
                    builder.AppendLine(card);
                }

                string post = builder.ToString();
                await MastodonClient.PostStatus($"@{status.Account.AccountName} {post}", Visibility.Public);
            };

            await UserStreaming.Start();
        }
예제 #16
0
 public static IUserStreaming ToCommon(this TimelineStreaming streaming)
 => new MastoNetUserStreaming(streaming);