예제 #1
0
파일: GameConfig.cs 프로젝트: rfHu/poker
    private GameData()
    {
        disposable = new CompositeDisposable();

        RxSubjects.TakeSeat.Subscribe((e) => {
            var index      = e.Data.Int("where");
            var playerInfo = e.Data.Dict("who");
            var player     = new Player(playerInfo, index);

            GameData.Shared.Players[index] = player;
        });

        RxSubjects.Paused.AsObservable().Subscribe((e) => {
            Paused.OnNext(e.Data.Int("type"));
        });

        RxSubjects.Started.AsObservable().Subscribe((e) => {
            GameStarted.OnNext(true);
            LeftTime.OnNext(e.Data.Int("left_time"));
            Paused.OnNext(0);
        });

        RxSubjects.UnSeat.AsObservable().Subscribe((e) => {
            var index = e.Data.Int("where");
            GameData.Shared.Players.Remove(index);

            var uid = e.Data.String("uid");
            // 清空数据
            if (uid == Uid)
            {
                GameData.Shared.MaxFiveRank.Value = 0;
            }
        });

        RxSubjects.GameStart.AsObservable().Subscribe((e) => {
            GameStarted.OnNext(true);

            var json            = e.Data.Dict("room");
            PublicCardAnimState = true;

            byJson(json, true);
        });

        RxSubjects.Look.Subscribe((e) => {
            PublicCardAnimState = false;
            byJson(e.Data);
        });

        RxSubjects.GameEnd.Subscribe((e) => {
            GameEnd = true;
        });

        RxSubjects.Deal.Subscribe((e) => {
            Pot.Value = e.Data.Int("pot");

            if (e.Data.ContainsKey("pots"))
            {
                Pots.Value = e.Data.DL("pots");
            }

            // 引入一个变量记录动画状态
            PublicCardAnimState = true;

            var state = e.Data.Int("state");

            // 发下一张牌的时候,重置所有prchips
            foreach (Player player in Players.Values)
            {
                player.PrChips.Value = 0;
                player.ChipsChange   = false;
            }

            var data = e.Data.Dict("deals");
            foreach (KeyValuePair <string, object> item in data)
            {
                var list = item.Value.GetIL();

                if (list == null || list.Count <= 0)
                {
                    continue;
                }

                if (item.Key == "-1")
                {
                    setPbCards(list, state);
                }
                else
                {
                    var k = Convert.ToInt16(item.Key);
                    if (!Players.ContainsKey(k))
                    {
                        continue;
                    }

                    var player         = Players[k];
                    player.SeeCardAnim = true;
                    player.Cards.OnNext(list);
                }
            }

            var pbList = data.IL("-1");
            var delay  = 0.5f;

            if (pbList.Count >= 3)               // 等待亮牌动画
            {
                delay += pbList.Count * Card.TurnCardDuration;
            }

            Observable.Timer(TimeSpan.FromSeconds(delay)).AsObservable().Subscribe((_) => {
                if (e.Data.ContainsKey("maxFiveRank"))
                {
                    MaxFiveRank.Value = e.Data.Int("maxFiveRank");
                }

                if (e.Data.ContainsKey("maxFive"))
                {
                    HighlightIndex.Value = Card.ExtractHighlightCards(e.Data.IL("maxFive"), MaxFiveRank.Value);
                }
            }).AddTo(disposable);
        });

        RxSubjects.GamePause.Subscribe((pause) => {
            if (pause)
            {
                disposable.Clear();
            }
        });

        Action <RxData> updateCoins = (e) => {
            var index    = e.Data.Int("where");
            var bankroll = e.Data.Int("bankroll");

            if (Players.ContainsKey(index))
            {
                var player = Players[index];
                player.Bankroll.Value = bankroll;
            }
        };

        RxSubjects.Ready.Subscribe(updateCoins);
        RxSubjects.TakeMore.Subscribe(updateCoins);

        RxSubjects.Pass.Subscribe((e) => {
            var index    = e.Data.Int("where");
            var inGame   = e.Data.Bool("is_ingame");
            var bankroll = e.Data.Int("bankroll");
            var coins    = e.Data.Int("coins");
            var uid      = e.Data.String("uid");

            Coins = coins;

            if (uid == GameData.Shared.Uid)
            {
                GameData.Shared.Bankroll.Value = bankroll;
            }

            if (index < 0 || inGame || bankroll <= 0)
            {
                return;
            }

            if (Players.ContainsKey(index))
            {
                Players[index].Bankroll.Value = bankroll;
            }
        });

        RxSubjects.Fold.Subscribe((e) => {
            var index = e.Data.Int("seat");

            if (Players.ContainsKey(index))
            {
                var player    = Players[index];
                player.InGame = false;
                player.ActState.OnNext(ActionState.Fold);

                G.PlaySound("foldpai");
            }
        });

        Action <RxData> act = (e) => {
            var userAction = e.Data.ToObject <UserActionModel>();
            Pot.Value = userAction.pot;

            var index = userAction.seat;
            if (!Players.ContainsKey(index))
            {
                return;
            }

            var player = Players[index];
            player.ChipsChange    = true;
            player.PrChips.Value  = userAction.pr_chips;
            player.Bankroll.Value = userAction.bankroll;

            var state = e.E.ToActionEnum();

            // @TODO: 播放声音的地方应该统一一下
            switch (state)
            {
            case ActionState.Allin:
                G.PlaySound("allin");
                break;

            case ActionState.Check:
                G.PlaySound("check");
                break;

            default:
                break;
            }

            player.ActState.OnNext(state);
        };

        RxSubjects.Call.Subscribe(act);
        RxSubjects.AllIn.Subscribe(act);
        RxSubjects.Check.Subscribe(act);
        RxSubjects.Raise.Subscribe(act);

        RxSubjects.SeeCard.Subscribe((e) => {
            var cards = e.Data.IL("cards");
            var index = e.Data.Int("seat");
            if (Players.ContainsKey(index))
            {
                var player         = Players[index];
                player.SeeCardAnim = true;
                player.Cards.OnNext(cards);
            }
        });

        RxSubjects.GameOver.Subscribe((e) => {
            InGame = false;
            if (NeedInsurance.Value)
            {
                InsuranceState.Value = false;
            }

            var data = e.Data.Dict("scorelist");

            foreach (KeyValuePair <string, object> item in data)
            {
                var dict  = (Dictionary <string, object>)item.Value;
                var json  = new GameOverJson(dict);
                var index = Convert.ToInt32(item.Key);

                if (Players.ContainsKey(index))
                {
                    Players[index].OverData.Value = json;
                }
            }

            var room   = e.Data.Dict("room");
            Pot.Value  = room.Int("pot");
            Pots.Value = room.DL("pots");
        });

        RxSubjects.UnAuditCD.AsObservable().Where((e) => {
            return(e.Data.String("uid") == GameData.Shared.Uid);
        }).Subscribe(
            (e) => {
            var sec = e.Data.Int("sec");
            if (sec <= 0)
            {
                return;
            }

            AuditCD.OnNext(sec);
        }
            );

        RxSubjects.Audit.Subscribe((e) => {
            var array       = e.Data.List("ids");
            ShowAudit.Value = array.Count > 0;
        });

        // 倒计时
        Observable.Interval(TimeSpan.FromSeconds(1)).AsObservable().Subscribe((_) => {
            // 游戏已暂停,不需要修改
            if (GameStarted.Value && Paused.Value > 0)
            {
                return;
            }

            var value = Math.Max(0, LeftTime.Value - 1);
            LeftTime.OnNext(value);
        });

        RxSubjects.GamerState.Subscribe((e) => {
            var uid   = e.Data.String("uid");
            var state = e.Data.Int("state");
            var ucd   = e.Data.Int("unseat_countdown");
            setState(uid, state, ucd);
        });

        RxSubjects.RaiseBlind.Subscribe((e) => {
            BlindLv = e.Data.Int("blind_lv");
        });

        RxSubjects.CurrentRank.Subscribe((e) => {
            Rank.Value = e.Data.Int("rank");
        });

        RxSubjects.NoTalking.Subscribe((e) => {
            bool type  = e.Data.Int("type") == 1;
            string uid = e.Data.String("uid");
            if (uid == GameData.Shared.Uid)
            {
                GameData.Shared._talkLimit.Value = type;
            }
        });

        TalkLimit = _talkLimit.CombineLatest(InsuranceState, (x, y) => x || y).ToReadOnlyReactiveProperty();

        RxSubjects.HalfBreak.Subscribe((e) => {
            MatchData.MatchRoomStatus.OnNext(MatchRoomStat.Rest);
            LeftTime.OnNext(e.Data.Int("ct"));
        });
    }