/// <summary>
        /// 创建房间
        /// </summary>
        /// <param name="self"></param>
        public static async void CreateRoom(this MatchComponent self)
        {
            if (self.CreateRoomLock)
            {
                return;
            }

            //消息加锁,避免因为延迟重复发多次创建消息
            self.CreateRoomLock = true;

            //发送创建房间消息
            string        mapAddress   = Game.Scene.GetComponent <RealmMapAddressComponent>().GetAddress().GetComponent <InnerConfig>().Address;
            Session       mapSession   = Game.Scene.GetComponent <NetInnerComponent>().Get(mapAddress);
            CreateRoom_RE createRoomRE = await mapSession.Call <CreateRoom_RE>(new CreateRoom_RT()
            {
                Level = RoomLevel.Lv100
            });

            Room room = EntityFactory.CreateWithId <Room>(createRoomRE.RoomID);

            Game.Scene.GetComponent <RoomManagerComponent>().Add(room);

            //解锁
            self.CreateRoomLock = false;
        }
        /// <summary>
        /// 加入房间
        /// </summary>
        /// <param name="self"></param>
        /// <param name="room"></param>
        /// <param name="matcher"></param>
        public static async void JoinRoom(this MatchComponent self, Room room, Matcher matcher)
        {
            //玩家加入房间,移除匹配队列
            self.Playing[matcher.UserID] = room.Id;
            self.MatchSuccessQueue.Enqueue(matcher);

            //向房间服务器发送玩家进入请求
            ActorProxy actorProxy = Game.Scene.GetComponent <ActorProxyComponent>().Get(room.Id);
            Actor_PlayerEnterRoom_Ack actor_PlayerEnterRoom_Ack = await actorProxy.Call(new Actor_PlayerEnterRoom_Req()
            {
                PlayerID  = matcher.PlayerID,
                UserID    = matcher.UserID,
                SessionID = matcher.GateSessionID
            }) as Actor_PlayerEnterRoom_Ack;

            Gamer gamer = GamerFactory.Create(matcher.PlayerID, matcher.UserID, actor_PlayerEnterRoom_Ack.GamerID);

            room.Add(gamer);

            //向玩家发送匹配成功消息
            ActorProxyComponent actorProxyComponent = Game.Scene.GetComponent <ActorProxyComponent>();
            ActorProxy          gamerActorProxy     = actorProxyComponent.Get(gamer.PlayerID);

            gamerActorProxy.Send(new Actor_MatchSucess_Ntt()
            {
                GamerID = gamer.Id
            });
        }
Exemplo n.º 3
0
        void ToggleButton_Changed(object sender, System.EventArgs e)
        {
            Skill.Editor.UI.ToggleButton tb = (Skill.Editor.UI.ToggleButton)sender;
            MatchComponent match            = (MatchComponent)tb.UserData;

            match.Copy = tb.IsChecked;
        }
        /// <summary>
        /// 加入房间
        /// </summary>
        /// <param name="self"></param>
        /// <param name="room"></param>
        /// <param name="matcher"></param>
        public static async void JoinRoom(this MatchComponent self, Room room, Matcher matcher)
        {
            //玩家加入房间,移除匹配队列
            self.Playing.Add(matcher.UserID, room.Id);
            self.MatchSuccessQueue.Enqueue(matcher);
            room.Add(EntityFactory.CreateWithId <Gamer, long>(matcher.PlayerID, matcher.UserID));

            //发送获取加入房间密匙消息
            ActorProxy        actorProxy       = Game.Scene.GetComponent <ActorProxyComponent>().Get(room.Id);
            GetJoinRoomKey_RE playerJoinRoomRE = await actorProxy.Call <GetJoinRoomKey_RE>(new GetJoinRoomKey_RT()
            {
                PlayerID      = matcher.PlayerID,
                UserID        = matcher.UserID,
                GateSeesionID = matcher.GateSessionID
            });

            //发送匹配成功消息
            string  gateAddress = Game.Scene.GetComponent <StartConfigComponent>().Get(matcher.GateAppID).GetComponent <InnerConfig>().Address;
            Session gateSession = Game.Scene.GetComponent <NetInnerComponent>().Get(gateAddress);

            gateSession.Send(new MatchSuccess()
            {
                PlayerID = matcher.PlayerID, RoomID = room.Id, Key = playerJoinRoomRE.Key
            });
        }
Exemplo n.º 5
0
    private IEnumerator AnimateDie(GameObject die, int sides, int outcome, MatchComponent mc)
    {
        _animatingCounter++;
        // TODO: Use target transform and bounce to it
        float counter    = 0;
        int   randomSide = -1;

        while (counter < _maxAnimationTimes)
        {
            int nextRandomSide = Utils.RandomInt(sides);
            while (nextRandomSide == randomSide)
            {
                nextRandomSide = Utils.RandomInt(sides);
            }
            randomSide = nextRandomSide;
            die.GetComponent <Dice>().SetFace(nextRandomSide + 1);
            yield return(new WaitForSeconds(counter * _animationRate));

            counter++;
        }
        die.GetComponent <Dice>().SetFace(outcome);
        die.GetComponent <Dice>().SetOutcomeColors(mc.win);
        _animatingCounter--;
        if (_animatingCounter == 0)
        {
            OnComplete(mc);
        }
    }
Exemplo n.º 6
0
 public override void OnComponentAdded(BaseComponent c)
 {
     if (c is MatchComponent)
     {
         MatchComponent mc = c as MatchComponent;
         DetermineMatch(mc);
         AnimateDice(mc);
     }
 }
Exemplo n.º 7
0
        private void Rebuild()
        {
            _SourceComponents.Controls.Clear();
            _DestinationComponents.Controls.Clear();

            if (_Source.Object != null && _Destination.Object != null)
            {
                EnableControls(true);

                Component[] sourceComponents = _Source.Object.GetComponents <Component>();
                Component[] destComponents   = _Destination.Object.GetComponents <Component>();

                _MatchedComponents.Clear();
                for (int i = 0; i < sourceComponents.Length; i++)
                {
                    MatchComponent match = new MatchComponent();
                    match.Source      = sourceComponents[i];
                    match.Destination = FindMatch(destComponents, match.Source.GetType());
                    match.Copy        = true;
                    if (match.Source is Transform)
                    {
                        match.Copy = false;
                    }
                    _MatchedComponents.Add(match);
                }

                Skill.Framework.UI.Thickness margin = new Skill.Framework.UI.Thickness(2, 2, 2, 6);
                foreach (var item in _MatchedComponents)
                {
                    Skill.Editor.UI.ToggleButton tb = new Skill.Editor.UI.ToggleButton()
                    {
                        Margin = margin, Left = true
                    };
                    tb.IsChecked  = item.Copy;
                    tb.UserData   = item;
                    tb.Label.text = item.Source.GetType().Name;
                    _SourceComponents.Controls.Add(tb);

                    Skill.Framework.UI.Label lbl = new Skill.Framework.UI.Label()
                    {
                        Margin = margin
                    };
                    if (item.Destination != null)
                    {
                        lbl.Text = item.Destination.GetType().Name;
                    }
                    _DestinationComponents.Controls.Add(lbl);

                    tb.Changed += ToggleButton_Changed;
                }
            }
            else
            {
                EnableControls(false);
            }
        }
Exemplo n.º 8
0
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        GameController.Instance.opponentLuck.luck    = opponentLuck;
        GameController.Instance.opponentLuck.maxLuck = opponentMaxLuck;

        MatchComponent mc = GameController.Instance.gameObject.AddComponent <MatchComponent>();

        mc.dice      = dice;
        mc.threshold = threshold;
    }
Exemplo n.º 9
0
        protected override async void Run(Session session, JoinMatch_RT message, Action <JoinMatch_RE> reply)
        {
            JoinMatch_RE response = new JoinMatch_RE();

            try
            {
                MatchComponent matchComponent = Game.Scene.GetComponent <MatchComponent>();
                if (matchComponent.Playing.ContainsKey(message.UserID))
                {
                    //重连房间
                    long roomId = matchComponent.Playing[message.UserID];
                    RoomManagerComponent roomManager = Game.Scene.GetComponent <RoomManagerComponent>();
                    Room room = roomManager.Get(roomId);
                    foreach (var gamer in room.GetAll())
                    {
                        if (gamer.UserID == message.UserID)
                        {
                            long pastId = gamer.Id;
                            gamer.Id = message.PlayerID;
                            room.Replace(pastId, gamer);
                            break;
                        }
                    }
                    ActorProxy actorProxy = Game.Scene.GetComponent <ActorProxyComponent>().Get(roomId);
                    actorProxy.Send(new PlayerReconnect()
                    {
                        PlayerID = message.PlayerID, UserID = message.UserID, GateSessionID = message.GateSessionID
                    });

                    response.ActorID = roomId;
                    reply(response);
                    return;
                }

                //创建匹配玩家
                Matcher matcher = EntityFactory.Create <Matcher, long>(message.PlayerID);
                matcher.UserID        = message.UserID;
                matcher.GateSessionID = message.GateSessionID;
                matcher.GateAppID     = message.GateAppID;

                await matcher.AddComponent <ActorComponent>().AddLocation();

                //加入匹配队列
                Game.Scene.GetComponent <MatcherComponent>().Add(matcher);
                Log.Info($"玩家{message.PlayerID}加入匹配队列");

                response.ActorID = matcher.Id;
                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemplo n.º 10
0
 private void AnimateDice(MatchComponent mc)
 {
     for (int i = 0; i < mc.dice.Count; i++)
     {
         int        sides   = mc.dice[i];
         int        outcome = mc.rolledDice[i];
         GameObject prefab  = GameController.Instance.dicePrefabs[sides];
         GameObject die     = GameObject.Instantiate(prefab, GameController.Instance.diceHolder.transform);
         GameController.Instance.HandleCoroutine(AnimateDie(die, sides, outcome, mc));
     }
 }
Exemplo n.º 11
0
    private void DetermineMatch(MatchComponent mc)
    {
        float playerSkew   = GameController.Instance.playerLuck.luck * LuckSystem.LUCKPOINT;
        float opponentSkew = GameController.Instance.opponentLuck.luck * LuckSystem.LUCKPOINT;
        float totalSkew    = playerSkew - opponentSkew;

        int   totalSides = mc.dice.Sum();
        int   threshold  = mc.threshold - 1;
        float winPercent = Mathf.Max(Mathf.Min(((float)threshold) / ((float)totalSides) + totalSkew, 1.0f), 0.0f);

        //Debug.Log(string.Format("Dice:"));
        //foreach(int n in mc.dice) {
        //    Debug.Log(string.Format(" {0}", n));
        //}
        //Debug.Log(string.Format("Threshold: {0}", threshold));
        //Debug.Log(string.Format("Win %: {0}", winPercent));

        mc.win = Utils.RandomFloat(1.0f) <= winPercent;

        /*
         *
         * 6 6, threshold 3, mc.threshold is 4
         * 2-3 win, 4-12 lose
         *
         */
        int number = mc.win ?
                     mc.dice.Count + Utils.RandomInt(mc.threshold - mc.dice.Count):
                     mc.threshold + Utils.RandomInt(totalSides - mc.threshold + 1);

        Debug.Log(string.Format("Number: {0}", number));
        mc.rolledDice = new List <int>(mc.dice);
        for (int i = 0; i < mc.rolledDice.Count; i++)
        {
            mc.rolledDice[i] = 1;
        }
        for (int i = 0; i < number - mc.rolledDice.Count; i++)
        {
            int index = Utils.RandomInt(mc.rolledDice.Count);
            while (mc.rolledDice[index] == mc.dice[index])
            {
                index = Utils.RandomInt(mc.rolledDice.Count);
            }
            mc.rolledDice[index]++;
        }
        Debug.Log(string.Format("Win: {0}", mc.win));
        Debug.Log("Dice:");
        foreach (int n in mc.rolledDice)
        {
            Debug.Log(string.Format(" {0}", n));
        }
    }
Exemplo n.º 12
0
        protected override async void Run(Session session, G2M_PlayerEnterMatch message, Action <M2G_PlayerEnterMatch> reply)
        {
            M2G_PlayerEnterMatch response = new M2G_PlayerEnterMatch();

            try
            {
                MatchComponent      matchComponent      = Game.Scene.GetComponent <MatchComponent>();
                ActorProxyComponent actorProxyComponent = Game.Scene.GetComponent <ActorProxyComponent>();

                if (matchComponent.Playing.ContainsKey(message.UserID))
                {
                    //todo 重连逻辑
                    MatchRoomComponent matchRoomComponent = Game.Scene.GetComponent <MatchRoomComponent>();
                    long  roomId = matchComponent.Playing[message.UserID];
                    Room  room   = matchRoomComponent.Get(roomId);
                    Gamer gamer  = room.Get(message.UserID);

                    //重置GateActorID
                    gamer.PlayerID = message.PlayerID;

                    //重连房间
                    ActorProxy actorProxy = actorProxyComponent.Get(roomId);
                    await actorProxy.Call(new MH2MP_PlayerEnterRoom()
                    {
                        PlayerID  = message.PlayerID,
                        UserID    = message.UserID,
                        SessionID = message.SessionID
                    });

                    //向玩家发送匹配成功消息
                    ActorProxy gamerActorProxy = actorProxyComponent.Get(gamer.PlayerID);
                    gamerActorProxy.Send(new M2G_MatchSucess()
                    {
                        GamerID = gamer.Id
                    });
                }
                else
                {
                    //创建匹配玩家
                    Matcher matcher = MatcherFactory.Create(message.PlayerID, message.UserID, message.SessionID);
                }

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemplo n.º 13
0
        public static void Update(this MatchComponent self)
        {
            while (true)
            {
                MatcherComponent   matcherComponent = Game.Scene.GetComponent <MatcherComponent>();
                Queue <Matcher>    matchers         = new Queue <Matcher>(matcherComponent.GetAll());
                MatchRoomComponent roomManager      = Game.Scene.GetComponent <MatchRoomComponent>();
                Room room = roomManager.GetReadyRoom();

                if (matchers.Count == 0)
                {
                    //当没有匹配玩家时直接结束
                    break;
                }

                if (room == null && matchers.Count >= 3)
                {
                    //当还有一桌匹配玩家且没有可加入房间时使用空房间
                    room = roomManager.GetIdleRoom();
                }

                if (room != null)
                {
                    //当有准备状态房间且房间还有空位时匹配玩家直接加入填补空位
                    while (matchers.Count > 0 && room.Count < 3)
                    {
                        self.JoinRoom(room, matcherComponent.Remove(matchers.Dequeue().UserID));
                    }
                }
                else if (matchers.Count >= 3)
                {
                    //当还有一桌匹配玩家且没有空房间时创建新房间
                    self.CreateRoom();
                    break;
                }
                else
                {
                    break;
                }

                //移除匹配成功玩家
                while (self.MatchSuccessQueue.Count > 0)
                {
                    matcherComponent.Remove(self.MatchSuccessQueue.Dequeue().UserID);
                }
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// 创建房间
        /// </summary>
        /// <param name="self"></param>
        public static async void CreateRoom(this MatchComponent self)
        {
            if (self.CreateRoomLock)
            {
                return;
            }

            //消息加锁,避免因为延迟重复发多次创建消息
            self.CreateRoomLock = true;

            //发送创建房间消息
            IPEndPoint           mapIPEndPoint = Game.Scene.GetComponent <AllotMapComponent>().GetAddress().GetComponent <InnerConfig>().IPEndPoint;
            Session              mapSession    = Game.Scene.GetComponent <NetInnerComponent>().Get(mapIPEndPoint);
            MP2MH_CreateRoom_Ack createRoomRE  = await mapSession.Call(new MH2MP_CreateRoom_Req()) as MP2MH_CreateRoom_Ack;

            Room room = ComponentFactory.CreateWithId <Room>(createRoomRE.RoomID);

            Game.Scene.GetComponent <MatchRoomComponent>().Add(room);

            //解锁
            self.CreateRoomLock = false;
        }
Exemplo n.º 15
0
 public override void OnComponentRemoved(BaseComponent c)
 {
     if (c is MatchComponent)
     {
         MatchComponent mc = c as MatchComponent;
         if (mc.win)
         {
             GameController.Instance.playerLuck.maxLuck += GameController.Instance.opponentLuck.maxLuck; // TODO: Maybe its more fair to use luck
             PlayerPrefs.SetInt("MaxLuck", GameController.Instance.playerLuck.maxLuck);
             Debug.Log(string.Format("Player luck gained {0} to {1}", GameController.Instance.opponentLuck.maxLuck, GameController.Instance.playerLuck.maxLuck));
             GameController.Instance.dialogStateMachine.SetTrigger("GoodTrigger");
         }
         else if (GameController.Instance.playerLuck.maxLuck < 1)
         {
             GameController.Instance.dialogStateMachine.SetTrigger("OutOfLuckTrigger");
             GameController.Instance.ClearProgress();
         }
         else
         {
             GameController.Instance.dialogStateMachine.SetTrigger("BadTrigger");
         }
     }
 }
Exemplo n.º 16
0
 private void OnComplete(MatchComponent mc)
 {
     GameObject.Destroy(mc);
 }