Example #1
0
        // Process each mention as command
        public static async Task DoProcess(Tokens tokens, LiteDatabase db, Dungeon dungeon)
        {
            Console.WriteLine("Processing players...");
            var pn = await tokens.Notifications.GetAsync();

            foreach (var notification in pn)
            {
                if (notification.Type == "mention")
                {
                    var mentionToReturn = TagRegex.Replace(notification.Status.Content.Replace("<br />", "\n"), "").Trim();
                    mentionToReturn = (mentionToReturn.Length > 55) ? mentionToReturn.Substring(0, 54) : mentionToReturn;
                    Console.WriteLine("M#" + notification.Status.Id + " " + notification.Account.Acct + ": " + mentionToReturn);
                    var    currentStatusId = notification.Status.Id;
                    Player pl = dungeon.LoadPlayer(notification.Account.Acct);
                    if (pl != null && (pl.LastStatusId < currentStatusId || Program.Debug))  // Only last command! unless debugging
                    {
                        var tootToSend = dungeon.Process(pl, notification.Status.Id, notification.Status.Content);
                        dungeon.UpsertPlayer(pl);
                        if (tootToSend.Content != "")
                        {
                            if (!Program.Debug) // Only send toots if not debugging
                            {
                                tootToSend.AccountId = notification.Account.Id;
                                await tokens.Statuses.PostAsync(status => "@" + tootToSend.Username + " " + tootToSend.Content, inReplyToAccountId => tootToSend.AccountId, visibility => tootToSend.Privacy);
                            }
                            Console.Write(" >> toot to " + tootToSend.Username);
                            Console.WriteLine(" : " + dungeon.FormatForConsole(tootToSend.Content));
                        }
                    }
                }
            }

            // Process/Move monsters
            var tootsToSend = dungeon.ProcessMonsters();

            if (tootsToSend != null)
            {
                foreach (var ts in tootsToSend)
                {
                    if (!Program.Debug) // Only send toots if not debugging
                    {
                        await tokens.Statuses.PostAsync(status => "@" + ts.Username + " " + ts.Content, in_reply_to_account_id => ts.AccountId, visibility => ts.Privacy);
                    }
                    Console.Write(" >> toot to " + ts.Username);
                    Console.WriteLine(" : " + dungeon.FormatForConsole(ts.Content));
                }
            }
        }
Example #2
0
        // Add new users to game
        public static async Task DoAdds(Tokens tokens, LiteDatabase db, Dungeon dungeon)
        {
            Console.WriteLine("Checking for followers...");
            var addNotif = await tokens.Notifications.GetAsync();

            foreach (var notification in addNotif)
            {
                if (notification.Type == "follow")
                {
                    Player p = dungeon.LoadPlayer(notification.Account.Acct);
                    if (p != null)
                    {
                        Console.WriteLine("Player " + p.Username + " already exists in db...");
                    }
                    else
                    {
                        //Console.WriteLine("Adding new player " + notification.Account.Acct + "...");
                        p = dungeon.AddPlayer(notification.Account.Acct, notification.Account.Id);
                        Console.WriteLine("New player (" + p.Id + ") #" + p.AccountId + " - " + p.Username + " in rm " + p.X + "," + p.Y + "," + p.Z);
                        var addMessage = "Welcome @" + notification.Account.Acct +
                                         ", you have joined the game. Type 'help' for commands. " +
                                         "You are in " + dungeon.GetRoomName(p.X, p.Y, p.Z) + "\r\n" +
                                         dungeon.GetRoomExits(p.X, p.Y, p.Z);
                        if (!Program.Debug) // Only send toots if not debugging
                        {
                            await tokens.Statuses.PostAsync(status => addMessage, in_reply_to_account_id => notification.Account.Id, visibility => "private");
                        }
                    }
                }
                //if (notification.Type == "unfollow")
                //{
                //    dungeon.DeletePlayer(notification.Account.Acct);
                //    var delMessage = "Farewell @" + notification.Account.Acct +
                //                     ", you have left the game. Follow to to play again. ";
                //    if (!Program.Debug) // Only send toots if not debugging
                //    {
                //        await tokens.Statuses.PostAsync(status => delMessage, in_reply_to_account_id => notification.Account.Id, visibility => "direct");
                //    }
                //}
            }
            // Unfollow, there is no unfollow notification, so we have to check each player!
            // Get collection
            // https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md#accounts
            var dbPlayers = db.GetCollection <Player>("players");

            dbPlayers.EnsureIndex(x => x.Username);
            Console.WriteLine("Checking for unfollows...");
            var allPlayers = dbPlayers.Find(r => r.Username != null);

            foreach (var p in allPlayers)
            {
                var u = await tokens.Accounts.RelationshipsAsync(id => p.AccountId);

                if (u != null && !u[0].FollowedBy)
                {
                    Console.WriteLine("Player unfollowed (" + p.Id + ") #" + p.AccountId + " - " + p.Username + " in rm " + p.X + "," + p.Y + "," + p.Z);
                    Console.WriteLine("...deleteing");
                    dungeon.DeletePlayer(p.Username);
                }
            }
            Console.WriteLine("Complete...");
        }