Exemple #1
0
 /// <summary>
 /// Send a text message to an initialized room
 /// </summary>
 public void SendTextMessageAsync(MatrixRoom room, string message)
 {
     if (_events.ContainsKey(room))
     {
         Func <string, string> send = room.SendText;
         send.BeginInvoke(message, ar => send.EndInvoke(ar), null);
     }
 }
Exemple #2
0
 public void RaiseRoomSync(MatrixRoom room)
 {
     if (rooms.ContainsKey(room))
     {
         loaded.Add(room);
         RoomSyncCompleted?.Invoke(room);
     }
 }
Exemple #3
0
 public void SendTextMessageAsync(MatrixRoom room, string message)
 {
     if (!SentMessages.ContainsKey(room))
     {
         SentMessages[room] = new List <string>();
     }
     SentMessages[room].Add(message);
 }
 public Lobby(MatrixRoom Room, MatrixUser Owner, MatrixClient Client)
 {
     room   = Room;
     owner  = Owner;
     client = Client;
     room.SendMessage("You've created a new lobby, the game will commence once the owner has said 'START'");
     room.OnEvent += Room_OnEvent;
     users         = new List <MatrixUser>();
 }
Exemple #5
0
 public IEnumerable <string> GetPersonList(MatrixRoom room)
 {
     if (rooms.ContainsKey(room))
     {
         return new[] { CurrentUser.DisplayName }
     }
     ;
     throw new NotImplementedException();
 }
Exemple #6
0
        private void FetchRoomAsync(MatrixRoom room)
        {
            Action <MatrixRoom> fetch = FetchRoom;

            fetch.BeginInvoke(
                room,
                ar => { RoomSyncCompleted?.Invoke(room); fetch.EndInvoke(ar); },
                null);
        }
        public void CreateCheatLobby(string sender)
        {
            MatrixUser user = client.GetUser(sender);
            MatrixRoom room = CreateLobbyRoom(user, "Cheat");

            Lobby lobby = new CheatLobby(room, user, client);

            lobbies.Add(lobby);
        }
        public void CreateTicTacToeLobby(string sender)
        {
            MatrixUser user = client.GetUser(sender);
            MatrixRoom room = CreateLobbyRoom(user, "Tic Tac Toe");

            Lobby lobby = new TicTacToe(room, user, client);

            lobbies.Add(lobby);
        }
Exemple #9
0
        private MatrixRoom JoinRoom(string roomId)
        {
            if (roomId?.Contains(':') != true)
            {
                roomId += ":" + _cp.UrlBody;
            }
            MatrixRoom room = _cp.Client?.JoinRoom(roomId);

            return(room);
        }
Exemple #10
0
 void HandMessage(MatrixRoom uroom, MatrixEvent evt)
 {
     if (GameInSession)
     {
         string data = ((MatrixMRoomMessage)evt.content).body;
         if (data.ToLower() == "hand")
         {
             SendDeckUpdate(evt.sender);
             return;
         }
         if (users[currentPlayer].UserID == evt.sender)
         {
             //Do hand stuff
             string[] cards = data.ToUpper().Trim().Split(' ');
             if (cards.Length > 4)
             {
                 uroom.SendNotice("Dude, think it over. How can you have more than 4 cards of the same value?");
                 return;
             }
             List <PlayingCard> outdeck = new List <PlayingCard>();
             foreach (string scard in cards)
             {
                 PlayingCard card;
                 if (!PlayingCard.TryParse(scard, out card))
                 {
                     uroom.SendNotice("I could not recognise all the cards there, please revise.");
                     return;
                 }
                 outdeck.Add(card);
             }
             List <PlayingCard> player_hand = hands[evt.sender];
             bool all = outdeck.All(x => player_hand.Any(y => (x.suit == y.suit && x.value == y.value)));
             if (all)
             {
                 room.SendNotice(users[currentPlayer].DisplayName + " has put forward " + outdeck.Count + " cards. They have " + (player_hand.Count - outdeck.Count) + " cards remaining");
                 playerLied = !outdeck.All(x => x.value == expectedValue);
                 deck.AddRange(outdeck);
                 CheckForWinner();
                 foreach (PlayingCard card in outdeck)
                 {
                     player_hand.RemoveAll(x => x.suit == card.suit && x.value == card.value);
                 }
                 NextTurn();
             }
             else
             {
                 uroom.SendNotice("Some of the cards you listed aren't in your hand, please revise.");
             }
         }
     }
     else
     {
         room.LeaveRoom();
     }
 }
Exemple #11
0
        public void FeedEventCanonicalAliasTest()
        {
            MatrixRoom room = new MatrixRoom(null, "!abc:localhost");
            var        ev   = new MatrixMRoomCanonicalAlias()
            {
                alias = "#resturant:resturant"
            };

            room.FeedEvent(Utils.MockEvent(ev));
            Assert.That(room.CanonicalAlias, Is.EqualTo("#resturant:resturant"), "The canonical alias is correct.");
        }
Exemple #12
0
        public void FeedEventNameTest()
        {
            MatrixRoom room = new MatrixRoom(null, "!abc:localhost");
            var        ev   = new MatrixMRoomName()
            {
                name = "Snug Fox Party!"
            };

            room.FeedEvent(Utils.MockEvent(ev));
            Assert.That(room.Name, Is.EqualTo("Snug Fox Party!"), "Name is correct.");
        }
Exemple #13
0
        public void SetMemberAvatarNoMemberTest()
        {
            var        mock = Utils.MockApi();
            MatrixRoom room = new MatrixRoom(mock.Object, "!abc:localhost");

            Assert.That(
                () => room.SetMemberAvatar("@foobar:localhost"),
                Throws.TypeOf <MatrixException>()
                .With.Property("Message").EqualTo("Couldn't find the user's membership event")
                );
        }
Exemple #14
0
        public void FeedEventJoinRuleTest()
        {
            MatrixRoom room = new MatrixRoom(null, "!abc:localhost");
            var        ev   = new MatrixMRoomJoinRules()
            {
                join_rule = EMatrixRoomJoinRules.Public
            };

            room.FeedEvent(Utils.MockEvent(ev));
            Assert.That(room.JoinRule, Is.EqualTo(EMatrixRoomJoinRules.Public), "The join rule is correct.");
        }
Exemple #15
0
        public void FeedEventTopicTest()
        {
            MatrixRoom room = new MatrixRoom(null, "!abc:localhost");
            var        ev   = new MatrixMRoomTopic()
            {
                topic = "Foxes welcome!"
            };

            room.FeedEvent(Utils.MockEvent(ev));
            Assert.That(room.Topic, Is.EqualTo("Foxes welcome!"), "Topic is correct.");
        }
Exemple #16
0
 private string getOtherRoomMember(MatrixRoom room)
 {
     foreach (var member in room.Members)
     {
         if (member.Key != this.user.UserID)
         {
             return(member.Key);
         }
     }
     throw new Exception("Room without other member");
 }
Exemple #17
0
        private void LoadRoom(MatrixRoom room)
        {
            if (!_roomBack.IsReady(room))
            {
                CurrentRoomView = AndersonRoom.LoadingRoom;
                CurrentUserList = null;
                return;
            }

            CurrentRoomView = _roomBack.GetRoomView(room);
            CurrentUserList = _roomBack.GetPersonList(room);
        }
Exemple #18
0
        public static void TestCards(string cmd, string sender, MatrixRoom room)
        {
            List <PlayingCard> deck = PlayingCard.GetStandardDeck();

            PlayingCard.ShuffleDeck(ref deck);
            string             cards   = PlayingCard.GetDeckHTML(deck);
            MMessageCustomHTML htmlmsg = new MMessageCustomHTML();

            htmlmsg.formatted_body = cards;
            htmlmsg.body           = string.Join <PlayingCard>(" ", deck);
            room.SendMessage(htmlmsg);
        }
Exemple #19
0
        public void FeedEventRoomMemberFireEventsTest()
        {
            var        mock = Utils.MockApi();
            MatrixRoom room = new MatrixRoom((MatrixAPI)mock.Object, "!abc:localhost");

            bool[] didFire   = new bool[5];
            int    fireCount = 0;

            room.OnUserJoined  += (n, a) => didFire[0] = true;
            room.OnUserChange  += (n, a) => didFire[1] = true;
            room.OnUserLeft    += (n, a) => didFire[2] = true;
            room.OnUserInvited += (n, a) => didFire[3] = true;
            room.OnUserBanned  += (n, a) => didFire[4] = true;
            room.OnEvent       += (n, a) => fireCount++;

            var ev = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Join
            };

            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));

            Assert.That(didFire[0], Is.True, "Procesed join");
            ev = new MatrixMRoomMember()
            {
                membership  = EMatrixRoomMembership.Join,
                displayname = "Foobar!",
            };
            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));

            Assert.That(didFire[1], Is.True, "Processed change");
            ev = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Leave,
            };
            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));
            Assert.That(didFire[2], Is.True, "Processed leave");

            ev = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Invite,
            };
            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));
            Assert.That(didFire[3], Is.True, "Processed invite");

            ev = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Ban,
            };
            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));
            Assert.That(didFire[4], Is.True, "Processed ban");
            Assert.That(fireCount, Is.EqualTo(5), "OnEvent should fire each time.");
        }
Exemple #20
0
        public void SetMemberAvatarTest()
        {
            var        mock = Utils.MockApi();
            MatrixRoom room = new MatrixRoom(mock.Object, "!abc:localhost");
            var        ev   = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Join,
            };

            room.FeedEvent(Utils.MockEvent(ev, "@foobar:localhost"));
            room.SetMemberAvatar("@foobar:localhost");
        }
Exemple #21
0
        private void JoinRoomFinished(IAsyncResult ar)
        {
            var        result = (AsyncResult)ar;
            var        join   = (Func <string, MatrixRoom>)result.AsyncDelegate;
            MatrixRoom room   = join.EndInvoke(ar);

            if (room != null)
            {
                _events[room] = new AndersonRoom(room);
                FetchRoom(room);
            }
            RoomJoined?.Invoke(room, ar.AsyncState.ToString());
        }
Exemple #22
0
        public void FeedEventRoomMemberTest()
        {
            var        mock = Utils.MockApi();
            MatrixRoom room = new MatrixRoom((MatrixAPI)mock.Object, "!abc:localhost");
            var        ev   = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Join
            };

            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));
            Assert.That(room.Members.ContainsKey("@foobar:localhost"), Is.True, "The member is in the room.");
            Assert.That(room.Members.ContainsValue(ev), Is.True, "The member is in the room.");
        }
Exemple #23
0
        public void FeedEventCreatorTest()
        {
            MatrixRoom room          = new MatrixRoom(null, "!abc:localhost");
            var        creationEvent = new MatrixMRoomCreate()
            {
                creator   = "@Half-Shot:localhost",
                mfederate = false,
            };

            room.FeedEvent(Utils.MockEvent(creationEvent));
            Assert.That(room.Creator, Is.EqualTo("@Half-Shot:localhost"), "Creator is correct.");
            Assert.That(room.ShouldFederate, Is.False, "Should not federate.");
        }
Exemple #24
0
 public CheatLobby(MatrixRoom Room, MatrixUser Owner, MatrixClient Client) : base(Room, Owner, Client)
 {
     room.SendMessage("This lobby is for 'Cheat'.");
     room.SendMessage(new MMessageCustomHTML()
     {
         body           = "See https://en.wikipedia.org/wiki/Cheat_%28game%29#Gameplay for help",
         formatted_body = " See <a href=\"https://en.wikipedia.org/wiki/Cheat_%28game%29#Gameplay\">the wikipedia page</a> for help"
     });
     room.SendNotice("This game requires you to keep your hand hidden, so a secondary invite has also been sent");
     room.SendNotice("To call cheat on the current player, say 'Cheat! in this room.'");
     MinPlayers = 2;
     MaxPlayers = 8;
 }
Exemple #25
0
        public static void DownloadTrack(string cmd, string sender, MatrixRoom room)
        {
            try
            {
                List <string[]> videos = new List <string[]>();
                if (Downloaders.YoutubeGetIDFromURL(cmd) != "")
                {
                    videos = DownloadYoutube(cmd, sender, room);
                }
                else if (Uri.IsWellFormedUriString(cmd, UriKind.Absolute))
                {
                    videos = new List <string[]>();
                    videos.Add(DownloadGeneric(cmd, sender, room));
                }
                else
                {
                    room.SendNotice("Sorry, that type of URL isn't supported right now :/");
                    return;
                }

                Program.MPCClient.RequestLibraryUpdate();
                //Program.MPCClient.Idle("update");//Wait for it to start
                Program.MPCClient.Idle("update");                //Wait for it to finish

                foreach (string[] res in videos)
                {
                    Program.MPCClient.AddFile(res[0]);
                }

                Program.MPCClient.Status();

                                #if DEBUG
                Console.WriteLine(JObject.FromObject(Program.MPCClient.lastStatus));
                                #endif

                int position = Program.MPCClient.lastStatus.playlistlength;
                if (position == 1)
                {
                    Program.MPCClient.Play();
                    room.SendNotice("Started playing " + videos[0][1] + " | " + Configuration.Config["mpc"]["streamurl"]);
                }
                else
                {
                    room.SendNotice(videos[0][1] + " has been queued at position " + position + ".");
                }
            }
            catch (Exception e) {
                room.SendNotice("There was an issue with that request, " + sender + ": " + e.Message);
                Console.Error.WriteLine(e);
            }
        }
Exemple #26
0
 private void OnRoomJoined(MatrixRoom room, string roomId)
 {
     if (room == null)
     {
         ErrorMessage = $"Could not join room {roomId}.";
     }
     else
     {
         ErrorMessage = "";
         AllRooms     = _roomBack.GetAllRooms();
         SelectedRoom = room;
         LoadRoom(room);
     }
 }
Exemple #27
0
        public static void LyricSearch(string cmd, string sender, MatrixRoom room)
        {
            string suggestion = Downloaders.GetSongNameByLyric(cmd.Replace("lyrics ", ""));

            if (suggestion == null)
            {
                room.SendNotice("I couldn't find any songs with that lyric :(");
            }
            else
            {
                room.SendNotice(String.Format("Matched '{0}'. Checking Youtube for it", suggestion));
                SearchYTForTrack("search " + suggestion, sender, room);
            }
        }
Exemple #28
0
 private void FetchRoom(MatrixRoom room)
 {
     MatrixEvent[] msgs = room.FetchMessages().chunk;
     for (int i = msgs.Length - 1; i >= 0; i--)
     {
         MatrixEvent message = msgs[i];
         if (IsMessage(message))
         {
             _events[room].AddTextMessage(GetPerson(message.sender), message);
         }
     }
     _readyRooms.Add(room);
     room.OnMessage += AddEvent;
 }
Exemple #29
0
        static void Appservice_OnAliasRequest(string alias, out bool roomExists)
        {
            Console.WriteLine("Got a request for " + alias);
            MatrixClient bot       = appservice.GetClientAsUser();
            string       localpart = alias.Substring(1, alias.IndexOf(':') - 1);
            MatrixRoom   room      = bot.CreateRoom(
                new MatrixSDK.Structures.MatrixCreateRoom()
            {
                room_alias_name = localpart,
                preset          = MatrixSDK.Structures.EMatrixCreateRoomPreset.public_chat
            }
                );

            roomExists = true;
        }
Exemple #30
0
        public static string[] DownloadGeneric(string cmd, string sender, MatrixRoom room)
        {
            Uri uri;

            if (!Uri.TryCreate(cmd, UriKind.Absolute, out uri))
            {
                throw new Exception("Not a url :(");
            }
            FileInfo info     = new FileInfo(uri.Segments.Last());
            string   filename = Convert.ToBase64String(Encoding.UTF8.GetBytes(info.Name)) + info.Extension;

            Downloaders.GenericDownload(cmd, filename);
            return(new string[2] {
                filename, uri.Segments.Last()
            });
        }