コード例 #1
0
        public void Subscribes()
        {
            var pb = new PubSub(_redis, "pb_t1,pb_t2");

            var source = new CancellationTokenSource(2_000);

            var count = 0;

            Task.Run(() => pb.SubscribeAsync((t, s) =>
            {
                count++;
                XTrace.WriteLine("Consume: [{0}] {1}", t, s);
            }, source.Token));

            Thread.Sleep(100);

            var pb1 = new PubSub(_redis, "pb_t1");
            var rs  = pb1.Publish("test");

            Assert.Equal(1, rs);

            Thread.Sleep(100);
            var pb2 = new PubSub(_redis, "pb_t2");

            pb2.Publish("test2");

            Thread.Sleep(100);
            pb2.Publish("test3");

            Thread.Sleep(100);

            Assert.Equal(3, count);
        }
コード例 #2
0
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == this.Player.gameObject)
        {
            return;
        }

        Debug.Log(string.Format("{0} is attacking {1}", this.Player, this.CurrentOpponent));

        switch (this.AttackType)
        {
        case AttackType.LightPunch:
            PubSub.Publish(new PlayerAttacking(this.Player, this.CurrentOpponent, AttackType.LightPunch));
            break;

        case AttackType.HeavyPunch:
            PubSub.Publish(new PlayerAttacking(this.Player, this.CurrentOpponent, AttackType.HeavyPunch));
            break;

        case AttackType.LightKick:
            PubSub.Publish(new PlayerAttacking(this.Player, this.CurrentOpponent, AttackType.LightKick));
            break;

        case AttackType.HeavyKick:
            PubSub.Publish(new PlayerAttacking(this.Player, this.CurrentOpponent, AttackType.HeavyKick));
            break;
        }
    }
コード例 #3
0
    private void SpawnPlayers(FightStart fightStart)
    {
        this.Player1Prefab.transform.position = this.PLAYER_1_START_POS;
        this.Player2Prefab.transform.position = this.PLAYER_2_START_POS;

        PubSub.Publish(new PlayersSpawned());
    }
コード例 #4
0
        public void Test1()
        {
            var pb = new PubSub(_redis, "pb_test1");
            var rs = pb.Publish("test");

            Assert.Equal(0, rs);
        }
コード例 #5
0
        private void Start()
        {
            if (!this.IsValidObject())
            {
                return;
            }

            // Setup bindings from view to events or model
            this.PlayerView.DecreaseHealth
            .OnClickAsObservable()
            .Subscribe(_ => PubSub.Publish(new DecreaseHealth(10)));

            this.PlayerView.Reset
            .OnClickAsObservable()
            .Subscribe(_ =>
            {
                this.PlayerModel.CurrentHealth.Value        = this.PlayerModel.MaxHealth.Value;
                this.PlayerView.DecreaseHealth.interactable = true;
            });

            // Setup bindings from model to view
            this.PlayerModel.Name
            .SubscribeToText(this.PlayerView.Name);

            this.PlayerModel.CurrentHealth
            .SubscribeToText(this.PlayerView.CurrentHealth);

            this.PlayerModel.IsDead
            .Where(x => x)
            .Subscribe(_ => this.PlayerView.DecreaseHealth.interactable = false);
        }
コード例 #6
0
        public void EverythingWorks()
        {
            int    topicOne       = 0;
            int    topicTwo       = 0;
            double sumTemperature = 0.0;
            double sumHumidity    = 0.0;

            EventHandler <WeatherEventArgs> ev1 = (sender, e) => {
                topicOne++;
                sumTemperature += e.Temperature;
                sumHumidity    += e.Humidity;
            };
            EventHandler <WeatherEventArgs> ev2 = (sender, e) => {
                topicTwo++;
                sumTemperature += e.Temperature;
                sumHumidity    += e.Humidity;
            };

            // First pass:
            // ev1 is subscribed to topicOne
            // ev2 is subscribed to topicTwo

            pubSub.Subscribe("topicOne", ev1);
            pubSub.Subscribe("topicTwo", ev2);

            pubSub.Publish("topicOne", new WeatherEventArgs(1.0, 5.5));
            pubSub.Publish("topicTwo", new WeatherEventArgs(2.5, 3.1));

            Assert.Equal(1, topicOne);
            Assert.Equal(1, topicTwo);
            Assert.Equal(3.5, sumTemperature);
            Assert.Equal(8.6, sumHumidity);

            // Second pass:
            // ev2 is subscribed to topicOne
            // ev2 is subscribed to topicTwo

            pubSub.Subscribe("topicOne", ev2);
            pubSub.Unsubscribe("topicOne", ev1);
            pubSub.Publish("topicOne", new WeatherEventArgs(1.0, 5.5));
            pubSub.Publish("topicTwo", new WeatherEventArgs(2.5, 3.1));

            Assert.Equal(1, topicOne);
            Assert.Equal(3, topicTwo);
            Assert.Equal(7.0, sumTemperature);
            Assert.Equal(17.2, sumHumidity);
        }
コード例 #7
0
    private IEnumerator EndCharacterSelect()
    {
        yield return(new WaitForSeconds(0.25f));

        PubSub.Publish <EndCharacterSelect>(
            new EndCharacterSelect(
                playerSelections[PLAYER_1_INDEX],
                playerSelections[PLAYER_2_INDEX]
                )
            );
    }
コード例 #8
0
ファイル: Program.cs プロジェクト: llenroc/Cqs
        private static void DoPubsubTests()
        {
            System.Console.WriteLine("\n-- Pub/Sub tests --");
            var broker = new PubSub();

            broker.WorkInAsync = true;

            broker.Subscribe("NewsPosted", (data) =>
            {
                System.Console.WriteLine("Topic: 'NewsPosted' Data = '{0}'", data);
            });
            broker.Publish("NewsPosted", "yup");
        }
コード例 #9
0
    private void UnlockSelection(int playerID)
    {
        int       playerIndex = GetPlayerIndexFromID(playerID);
        Transform selection   = GetSelectionForPlayer(playerID);

        // Set the border color back to black
        Image border = selection.GetComponentInChildren <Image>();

        border.color = new Color(0f, 0f, 0f);

        playerReady[playerIndex] = false;

        PubSub.Publish(new CharacterSelectReadyChange(playerID, this.playerSelections[playerIndex], false));
    }
コード例 #10
0
        public void TestSubscriberShouldBeCollected()
        {
            new Action(() =>
            {
                Subscriber sub = new Subscriber();

                PubSub.Subscribe <Subscriber, bool>(sub, sub.Listener);
            })();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            PubSub.Publish(true);
        }
コード例 #11
0
    private void LockInSelection(int playerID)
    {
        int       playerIndex = GetPlayerIndexFromID(playerID);
        Transform selection   = GetSelectionForPlayer(playerID);

        // Set the border color for the player
        Image border = selection.GetComponentInChildren <Image>();

        border.color = GetPlayerColor(playerID);

        playerReady[playerIndex] = true;

        PubSub.Publish(new CharacterSelectReadyChange(playerID, this.playerSelections[playerIndex], true));
    }
コード例 #12
0
    private void MoveSelection(int playerID, int delta)
    {
        int playerIndex      = GetPlayerIndexFromID(playerID);
        int otherPlayerIndex = (playerIndex == 0 ? 1 : 0);

        ClearPortraitSelection(this.playerSelections[playerIndex]);
        this.playerSelections[playerIndex] = (this.playerSelections[playerIndex] + this.characterCount + delta) % this.characterCount;
        while (this.playerSelections[playerIndex] == this.playerSelections[otherPlayerIndex] || this.portraits.GetChild(this.playerSelections[playerIndex]).CompareTag("Locked"))
        {
            this.playerSelections[playerIndex] = (this.playerSelections[playerIndex] + this.characterCount + delta) % this.characterCount;
        }
        SelectPortrait(playerID, this.playerSelections[playerIndex]);

        PubSub.Publish(new CharacterSelectChange(playerID, this.playerSelections[playerIndex], delta));
    }
コード例 #13
0
        public void SubscribeTest()
        {
            var hub       = new PubSub();
            int callCount = 0;

            var sender1 = new object();
            var sender2 = new object();

            hub.Subscribe(sender1, new Action <string>(a => callCount++));
            hub.Subscribe(sender2, new Action <string>(a => callCount++));

            hub.Publish(null, default(string));

            Assert.AreEqual(2, callCount);
        }
コード例 #14
0
    private void OnCollisionEnter(Collision collision)
    {
        switch (collision.collider.tag)
        {
        case "Floor":
            if (!this.CanJump && this.IsJumping)
            {
                this.CanJump   = true;
                this.IsJumping = false;

                PubSub.Publish <PlayerJumpEnd>(new PlayerJumpEnd(this.Player.Id));
            }
            break;
        }
    }
コード例 #15
0
ファイル: PublishSubscribe.cs プロジェクト: fthoms/jiminy
        public void Run()
        {
            var wgReady = new WaitGroup(2);
            var wgDone  = new WaitGroup(2);

            using (var publisher = new PubSub <int>()) {
                Subscribe(publisher, wgReady, wgDone, "subscriber 1");
                Subscribe(publisher, wgReady, wgDone, "subscriber 2");
                wgReady.Wait();
                for (var i = 0; i < 5; i++)
                {
                    publisher.Publish(i);
                }
            }
            wgDone.Wait();
        }
コード例 #16
0
    private void SetHealth(HealthChange healthChange)
    {
        if (!this.IsActive)
        {
            return;
        }

        this.CurrentHealth.OnNext(new CurrentPlayerHealth(this.Player.Id, this.CurrentHealth.Value.Health + healthChange.Amount));

        if (this.CurrentHealth.Value.Health <= 0)
        {
            PubSub.Publish <PlayerKnockedOut>(new PlayerKnockedOut(this.Player.Id));
        }

        this.CurrentHealth.OnNext(new CurrentPlayerHealth(this.Player.Id, this.CurrentHealth.Value.Health + healthChange.Amount));
    }
コード例 #17
0
    private void Jump(PlayerJump playerJump)
    {
        if (!this.canReceiveInput)
        {
            return;
        }

        if (this.CanJump)
        {
            this.Rigidbody.velocity = Vector3.zero;
            this.Rigidbody.AddForce(new Vector3(0.0f, this.Player.JumpForce, 0.0f), ForceMode.Impulse);
            this.CanJump   = false;
            this.IsJumping = true;

            PubSub.Publish <PlayerJumpStart>(new PlayerJumpStart(this.Player.Id));
        }
    }
コード例 #18
0
        public void TestShouldBeCollectedIfCallbackTargetIsSubscriber()
        {
            WeakReference wr = null;

            new Action(() =>
            {
                Subscriber sub = new Subscriber();

                wr = new WeakReference(sub);

                PubSub.Subscribe <Subscriber, bool>(sub, sub.Listener);
            })();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            PubSub.Publish(true);

            Assert.IsFalse(wr.IsAlive);
        }
コード例 #19
0
    private void TakeDamage(PlayerHit playerHit)
    {
        switch (playerHit.AttackType)
        {
        case AttackType.LightPunch:
            PubSub.Publish <HealthChange>(new HealthChange(this.Player.Id, (playerHit.Blocked ? -3 : -6)));
            break;

        case AttackType.HeavyPunch:
            PubSub.Publish <HealthChange>(new HealthChange(this.Player.Id, (playerHit.Blocked ? -5 : -10)));
            break;

        case AttackType.LightKick:
            PubSub.Publish <HealthChange>(new HealthChange(this.Player.Id, (playerHit.Blocked ? -3 : -6)));
            break;

        case AttackType.HeavyKick:
            PubSub.Publish <HealthChange>(new HealthChange(this.Player.Id, (playerHit.Blocked ? -5 : -10)));
            break;
        }
    }
コード例 #20
0
    // Update is called once per frame
    void Update()
    {
        if (!active || paused)
        {
            return;
        }
        if (roundTimeRemaining > 0f)
        {
            roundTimeRemaining -= Time.deltaTime;
        }

        if (roundTimeRemaining <= this.ROUND_DURATION)
        {
            if (!this.roundStarted)
            {
                PubSub.Publish <RoundStarted>(new RoundStarted());
                this.roundStarted = true;
            }

            label.SetText(roundTimeRemaining.ToString("0"));
        }
    }
コード例 #21
0
    private void PlayerHit(PlayerAttacking playerAttacking)
    {
        Debug.Log(string.Format("{0} was hit by {1}", this.Player, playerAttacking.Player));

        if (this.IsBlocking && this.Player.Chest != null)
        {
            var blockImpact = GameObject.Instantiate(
                this.BlockImpact,
                this.Player.Chest.transform.position + this.BlockImpact.transform.position,
                Quaternion.identity);

            GameObject.Destroy(blockImpact, this.EFFECT_DURATION);
        }
        else if (this.Player.Chest != null)
        {
            var hitImpact = GameObject.Instantiate(this.HitImpact,
                                                   this.Player.Chest.transform.position + this.HitImpact.transform.position,
                                                   Quaternion.identity);

            GameObject.Destroy(hitImpact, this.EFFECT_DURATION);
        }

        PubSub.Publish(new PlayerHit(this.Player.Id, playerAttacking.AttackType, this.IsBlocking));
    }
コード例 #22
0
        public void TestNotCollectedIfSubscriberIsNotTheCallbackTarget()
        {
            WeakReference wr = null;

            new Action(() =>
            {
                Subscriber sub = new Subscriber();

                wr = new WeakReference(sub);

                PubSub.Subscribe <Subscriber, bool>(sub, (_) => sub.MakeSuccessful());
            })();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Assert.IsTrue(wr.IsAlive);
            Assert.IsNotNull(wr.Target as Subscriber);
            Assert.IsFalse(((Subscriber)wr.Target).Successful);

            PubSub.Publish(true);

            Assert.IsTrue(((Subscriber)wr.Target).Successful);
        }
コード例 #23
0
ファイル: Player.cs プロジェクト: joemicmc/GlobalGameJam2019
 private void Start()
 {
     PubSub.Publish(new PlayerSpawned(this));
 }
コード例 #24
0
 private void PlayerKnockedOut(PlayerKnockedOut playerKnockedOut)
 {
     PubSub.Publish(new FightOver());
 }
コード例 #25
0
 public async Task Publish(object eventData)
 {
     _logger?.Log($"{Tag}, Publish event: {eventData.GetType().Name}, {eventData}");
     await _pubSub.Publish(eventData);
 }
コード例 #26
0
 private void Start()
 {
     PubSub.Publish(new StartCharacterSelect());
 }
コード例 #27
0
 private void EndCharacterSelect(EndCharacterSelect endCharacterSelect)
 {
     PubSub.Publish(new FightStart(endCharacterSelect));
 }
コード例 #28
0
 private void HealthChange()
 {
     // Publishes the HealthChange event which notifies subscribers.
     PubSub.Publish(new HealthChange(amount));
 }
コード例 #29
0
 private void GameOver(FightOver gameOver)
 {
     PubSub.Publish(new StartCharacterSelect());
 }
コード例 #30
0
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            PubSub.Publish <PlayerAttack>(new PlayerAttack(1, AttackType.HeavyPunch));
        }

        if (Input.GetKeyDown(KeyCode.G))
        {
            PubSub.Publish(new FightOver());
        }

        // AXIS INPUT
        if (this.AxisSets != null)
        {
            foreach (var axisSet in this.AxisSets)
            {
                var moveVector = new Vector3(Input.GetAxis(axisSet.HorizontalAxis), Input.GetAxis(axisSet.VerticalAxis));

                // MOVEMENT
                if (moveVector == Vector3.zero)
                {
                    PubSub.Publish <PlayerMove>(new PlayerMove(axisSet.JoystickID, Vector3.zero));
                }
                else if (moveVector.x != 0.0f)
                {
                    PubSub.Publish <PlayerMove>(new PlayerMove(axisSet.JoystickID, moveVector));
                }

                // JUMPING
                if (moveVector.y > 0.0f)
                {
                    PubSub.Publish <PlayerJump>(new PlayerJump(axisSet.JoystickID, moveVector));
                }
            }
        }

        // BUTTON INPUT
        if (this.InputSets != null)
        {
            foreach (var inputSet in this.InputSets)
            {
                if (Input.GetButtonDown(inputSet.LightPunch))
                {
                    PubSub.Publish <PlayerAttack>(new PlayerAttack(inputSet.JoystickID, AttackType.LightPunch));
                }

                if (Input.GetButtonDown(inputSet.HeavyPunch))
                {
                    PubSub.Publish <PlayerAttack>(new PlayerAttack(inputSet.JoystickID, AttackType.HeavyPunch));
                }

                if (Input.GetButtonDown(inputSet.LightKick))
                {
                    PubSub.Publish <PlayerAttack>(new PlayerAttack(inputSet.JoystickID, AttackType.LightKick));
                }

                if (Input.GetButtonDown(inputSet.HeavyKick))
                {
                    PubSub.Publish <PlayerAttack>(new PlayerAttack(inputSet.JoystickID, AttackType.HeavyKick));
                }
            }
        }
    }