Example #1
0
        public void PriorityTest()
        {
            var last = EventPriority.Highest;
            var check = new Action<EventPriority>(i =>
            {
                if (last > i)
                    Assert.Fail("Wrong order! Failed at: " + i);

                last = i;
            });
            var lowest = new EventRaiseHandler<TestEvent>(e => check(EventPriority.Lowest));
            var low = new EventRaiseHandler<TestEvent>(e => check(EventPriority.Low));
            var normal = new EventRaiseHandler<TestEvent>(e => check(EventPriority.Normal));
            var high = new EventRaiseHandler<TestEvent>(e => check(EventPriority.High));
            var highest = new EventRaiseHandler<TestEvent>(e => check(EventPriority.Highest));
            var botBits = new BotBitsClient();

            TestEvent.Of(botBits).Bind(highest, EventPriority.Highest);
            TestEvent.Of(botBits).Bind(lowest, EventPriority.Lowest);
            TestEvent.Of(botBits).Bind(high, EventPriority.High);
            TestEvent.Of(botBits).Bind(low, EventPriority.Low);
            TestEvent.Of(botBits).Bind(normal);

            new TestEvent().RaiseIn(botBits);
        }
 public static Player[] GetPlayersIn(this ParsedRequest request, BotBitsClient client, int index)
 {
     var query = request.Args[index];
     PlayerSearchFilter filter;
     query = MatchHelper.TrimFilterPrefix(query, out filter);
     return MatchPlayers(client, query, filter);
 }
Example #3
0
 static void Main()
 {
     var bot = new BotBitsClient();
     ChatFormatsExtension.LoadInto(bot, new CakeChatSyntaxProvider("Bot"));
     CommandsExtension.LoadInto(bot, '!');
     Login.Of(bot).WithEmail("*****@*****.**", "guest").JoinRoom("PWAARLDluVa0I");
     InitEvent.Of(bot).WaitOneAsync().Wait();
     JoinCompleteEvent.Of(bot).WaitOneAsync().Wait();
     Thread.Sleep(Timeout.Infinite);
 }
        public static Player GetPlayerIn(this ParsedRequest request, BotBitsClient client, int index)
        {
            var query = request.Args[index];
            PlayerSearchFilter filter;
            query = MatchHelper.TrimFilterPrefix(query, out filter);
            var list = MatchPlayers(client, query, filter);

            if (!filter.HasFlag(PlayerSearchFilter.FirstResult) && list.Length >= 2)
                throw new CommandException("More than one player was found.");

            return list.First();
        }
Example #5
0
        private Command(BotBitsClient client, Action<IInvokeSource, ParsedRequest> callback, MethodInfo innerMethod)
        {
            this._command = (CommandAttribute)innerMethod.GetCustomAttributes(typeof(CommandAttribute), false).FirstOrDefault();
            if (this._command == null) throw new ArgumentException("The given callback is not a command", nameof(callback));
            
            this.Names = this._command.Names ?? new string[0];
            this.Usages = this._command.Usages ?? new string[0];
            this.MinArgs = this._command.MinArgs;

            this.Callback = this._command.DoTransformations(client, this, callback);
            this._command.OnAdd(client, this);
        }
Example #6
0
        public void EventLoadTest()
        {
            var botBits = new BotBitsClient();

            EventLoader
                .Of(botBits)
                .Load(this);

            Assert.IsTrue(
                TestEvent
                    .Of(botBits)
                    .Contains(this.OnTest));
        }
Example #7
0
        public void EventLoadStaticTest()
        {
            var botBits = new BotBitsClient();

            EventLoader
                .Of(botBits)
                .LoadStatic<EventLoaderTests>();

            Assert.IsTrue(
                TestEvent
                    .Of(botBits)
                    .Contains(OnStaticTest));
        }
Example #8
0
        public void BindTest()
        {
            var botBits = new BotBitsClient();
            var callback = new EventRaiseHandler<TestEvent>(delegate {});

            TestEvent
                .Of(botBits)
                .Bind(callback);

            Assert.IsTrue(
                TestEvent
                    .Of(botBits)
                    .Contains(callback));
        }
Example #9
0
        public void DoubleRaiseTest()
        {
            var botBits = new BotBitsClient();
            var e = new TestEvent();

            e.RaiseIn(botBits);

            try
            {
                e.RaiseIn(botBits);

                Assert.Fail();
            }
            catch (InvalidOperationException) { } // Expected
        }
 public static Action<IInvokeSource, ParsedRequest> WrapTryCatch
     (BotBitsClient client, Action<IInvokeSource, ParsedRequest> callback)
 {
     return (source, req) =>
     {
         try
         {
             callback(source, req);
         }
         catch (CommandException ex)
         {
             new CommandExceptionEvent(source, req, ex)
                 .RaiseIn(client);
         }
     };
 }
        public static string[] GetUsernamesIn(this ParsedRequest request, BotBitsClient client, int index)
        {
            var query = request.Args[index];
            PlayerSearchFilter filter;
            query = MatchHelper.TrimFilterPrefix(query, out filter);

            try
            {
                return request
                    .GetPlayersIn(client, index)
                    .Select(p => p.Username)
                    .Distinct()
                    .ToArray();
            }
            catch (UnknownPlayerCommandException)
            {
                return new []{query};
            }
        }
        private static Player[] MatchPlayers(BotBitsClient client, string query, PlayerSearchFilter filter)
        {
            IList<Player> list = new List<Player>();

            var exactMatch = filter.HasFlag(PlayerSearchFilter.ExactMatch);
            var regex = filter.HasFlag(PlayerSearchFilter.Regex);
            foreach (var player in Players.Of(client))
            {
                var match = false;
                if (exactMatch)
                {
                    if (player.Username.Equals(query, StringComparison.OrdinalIgnoreCase))
                        match = true;
                }
                else if (regex)
                {
                    try
                    {
                        if (Regex.IsMatch(player.Username, query, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
                            match = true;
                    }
                    catch (ArgumentException ex)
                    {
                        throw new CommandException("Regex error: " + ex.Message);
                    }
                }
                else
                {
                    if (player.Username.StartsWith(query, StringComparison.OrdinalIgnoreCase) ||
                        player.GetTrimmedName().StartsWith(query, StringComparison.OrdinalIgnoreCase))
                        match = true;
                }
                if (match)
                    list.Add(player);
            }

            if (list.Count == 0)
                throw new UnknownPlayerCommandException("No player found!");
            if (!filter.HasFlag(PlayerSearchFilter.AllowMultiple) && list.GroupBy(i => i.Username).Count() > 1)
                throw new CommandException("Multiple players with different usernames found.");
            return list.ToArray();
        }
 public static Action<IInvokeSource, ParsedRequest> WrapTryCatch(
     BotBitsClient client, Func<IInvokeSource, ParsedRequest, Task> callback)
 {
     return (source, req) =>
     {
         callback(source, req).ContinueWith(task =>
             Scheduler.Of(client).Schedule(() =>
             {
                 if (task.Exception == null) return;
                 var ex = task.Exception.InnerExceptions.FirstOrDefault() as CommandException;
                 if (ex != null)
                 {
                     new CommandExceptionEvent(source, req, ex)
                         .RaiseIn(client);
                 }
                 else
                 {
                     throw task.Exception.InnerExceptions.FirstOrDefault() ?? task.Exception;
                 }
             }), TaskContinuationOptions.OnlyOnFaulted);
     };
 }
Example #14
0
        static void Main(string[] args)
        {
            var bot = new BotBitsClient();

            // Events
            EventLoader.Of(bot).LoadStatic<Program>();

            // Commands
            CommandsExtension.LoadInto(bot, '!', '.');
            CommandLoader
                .Of(bot)
                .LoadStatic<Program>();

            // Login
            //ConnectionManager.Of(bot)
            //    .EmailLogin("email", "pass")
            //    .CreateJoinRoom("world");

            // Console commands
            while (true) 
                CommandManager.Of(bot).ReadNextConsoleCommand();
        }
Example #15
0
        public void CountTest()
        {
            var botBits = new BotBitsClient();
            var callback = new EventRaiseHandler<TestEvent>(delegate { });

            TestEvent
                .Of(botBits)
                .Bind(callback);

            Assert.AreEqual(1,
                TestEvent
                    .Of(botBits)
                    .Count);

            TestEvent
                .Of(botBits)
                .Unbind(callback);

            Assert.AreEqual(0,
                TestEvent
                    .Of(botBits)
                    .Count);
        }
        public static string GetUsernameIn(this ParsedRequest request, BotBitsClient client, int index)
        {
            var query = request.Args[index];
            PlayerSearchFilter filter;
            query = MatchHelper.TrimFilterPrefix(query, out filter);

            try
            {
                var list = request
                    .GetPlayersIn(client, index)
                    .Select(p => p.Username)
                    .Distinct()
                    .ToArray();

                if (!filter.HasFlag(PlayerSearchFilter.FirstResult) && list.Length >= 2)
                    throw new CommandException("More than one username was found.");

                return list.First();
            }
            catch (UnknownPlayerCommandException)
            {
                return query;
            }
        }
Example #17
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="TeleportEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal TeleportEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.X = message.GetInteger(1);
     this.Y = message.GetInteger(2);
 }
Example #18
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="WorldReleasedEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal WorldReleasedEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
 }
Example #19
0
 internal UnknownMessageEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
 }
Example #20
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="WriteEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal WriteEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Title = message.GetString(0);
     this.Text  = message.GetString(1);
 }
Example #21
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="CrewAddRequestEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal CrewAddRequestEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Requester = message.GetString(0);
     this.CrewName  = message.GetString(1);
 }
Example #22
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="CrewAddRequestFailedEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal CrewAddRequestFailedEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Reason = message.GetString(0);
 }
Example #23
0
 internal MinimapEnabledEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Enabled = message.GetBoolean(0);
 }
Example #24
0
 internal Command(BotBitsClient client, Action <IInvokeSource, ParsedRequest> callback)
     : this(client, ExceptionHelper.WrapTryCatch(client, callback), callback.Method)
 {
 }
Example #25
0
 internal void OnRemove(BotBitsClient client)
 {
     this._command.OnRemove(client, this);
 }
Example #26
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="LikedEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal LikedEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
 }
Example #27
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="UnfavoritedEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal UnfavoritedEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
 }
Example #28
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="GiveMagicSmileyEvent" /> class.
 /// </summary>
 /// <param name="client">The EE message.</param>
 /// <param name="message">The message.</param>
 internal GiveMagicSmileyEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.MagicSmiley = message.GetString(0);
 }
Example #29
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ShowKeyEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal ShowKeyEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Key = (Key)Enum.Parse(typeof(Key), message.GetString(0), true);
 }
Example #30
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="RefreshShopEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal RefreshShopEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
 }
Example #31
0
 internal Command(BotBitsClient client, Action<IInvokeSource, ParsedRequest> callback)
     : this(ExceptionHelper.WrapTryCatch(client, callback), callback.Method)
 {
 }
Example #32
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="CrownEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal CrownEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
 }
 protected internal virtual Action<IInvokeSource, ParsedRequest> DoTransformations(BotBitsClient client, Command command, Action<IInvokeSource, ParsedRequest> request)
 {
     return request;
 }
Example #34
0
 internal HideLobbyEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Hidden = message.GetBoolean(0);
 }
 internal RoomDescriptionEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Description = message.GetString(0);
 }
 internal LobbyPreviewEnabledEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Enabled = message.GetBoolean(0);
 }
Example #37
0
 internal RoomVisibleEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Visible = message.GetBoolean(0);
 }
Example #38
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="TimeEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal TimeEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Data = message.GetDouble(0);
     this.Time = message.GetDouble(0);
 }
Example #39
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="EffectLimitsEvent" /> class.
 /// </summary>
 /// <param name="client">The EE message.</param>
 /// <param name="message">The message.</param>
 internal EffectLimitsEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.CurseLimit  = message.GetInt(0);
     this.ZombieLimit = message.GetInt(1);
 }
Example #40
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="GodRightsEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal GodRightsEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.AllowToggle = message.GetBoolean(1);
 }
Example #41
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="EditRightsEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal EditRightsEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.AllowEdit = message.GetBoolean(1);
 }
Example #42
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="KillEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal KillEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
 }
Example #43
0
        public void UnbindTest2()
        {
            var botBits = new BotBitsClient();
            var callback = new EventRaiseHandler<TestEvent>(delegate { });

            TestEvent
                .Of(botBits)
                .Bind(callback);

            TestEvent
                .Of(botBits)
                .Bind(callback);

            TestEvent
                .Of(botBits)
                .Bind(callback, EventPriority.High);

            TestEvent
                .Of(botBits)
                .Unbind(callback);

            Assert.AreEqual(2,
                TestEvent
                    .Of(botBits)
                    .Count);
        }
Example #44
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="LoseAccessEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal LoseAccessEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
 }
Example #45
0
 internal BackgroundColorEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.BackgroundColor = message.GetUInt(0);
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="GiveMagicBrickPackageEvent" /> class.
 /// </summary>
 /// <param name="message">The EE message.</param>
 /// <param name="client"></param>
 internal GiveMagicBrickPackageEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.MagicPack = message.GetString(0);
 }
Example #47
0
        public void RaiseTest()
        {
            var isCalled = false;
            var callback = new EventRaiseHandler<TestEvent>(e => isCalled = true);
            var botBits = new BotBitsClient();

            TestEvent
                .Of(botBits)
                .Bind(callback);

            new TestEvent()
                .RaiseIn(botBits);

            Assert.IsTrue(isCalled);
        }
Example #48
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ModModeEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal ModModeEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.Mod = message.GetBoolean(1);
 }
Example #49
0
 internal void OnRemove(BotBitsClient client)
 {
     this._command.OnRemove(client, this);
 }
Example #50
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="AdminModeEvent" /> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="client"></param>
 internal AdminModeEvent(BotBitsClient client, Message message)
     : base(client, message)
 {
     this.AdminMode = message.GetBoolean(1);
     this.StaffAura = (StaffAura)message.GetInt(2);
 }
 protected internal virtual void OnRemove(BotBitsClient client, Command command)
 {
 }
Example #52
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ReceiveEvent{T}" /> class.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="message">The message.</param>
 internal ReceiveEvent(BotBitsClient client, Message message)
 {
     this.PlayerIOMessage = message;
 }