Esempio n. 1
0
        protected override async void Run(Session session, C2G_JoinRoom message, Action <G2C_JoinRoom> reply)
        {
            G2C_JoinRoom response = new G2C_JoinRoom();

            try
            {
                //验证合法性
                if (!GateHelper.SignSession(session))
                {
                    response.Error = ErrorCode.ERR_SignError;
                    reply(response);
                    return;
                }
                //获取房间信息
                Room room = Game.Scene.GetComponent <RoomComponent>().Get(message.RoomId);
                if (room == null)
                {
                    Log.Info($"房间不存在:{message.RoomId}");
                    response.Error = ErrorCode.ERR_NotFoundRoom;
                    reply(response);
                    return;
                }
                if (room.IsCanAddPlayer())
                {
                    Log.Info($"房间已经满人:{message.RoomId}");
                    response.Error = ErrorCode.ERR_RoomIsFull;
                    reply(response);
                    return;
                }

                //设置用户的房间ID
                User user = session.GetUser();
                user.RoomID = room.RoomId;
                //返回参数设置
                response.Room = new RoomInfo
                {
                    PlayerCount = room.roomConfig.PlayerCount,
                    GameCount   = room.roomConfig.GameCount,
                    GameScore   = room.roomConfig.GameScore
                };
                response.RoomId  = room.RoomId;
                response.ChairID = room.GetNullChair();
                reply(response);
                Log.Info("成功申请加入房间!!!");


                //向游戏服务器发送玩家进入请求
                ActorMessageSender actorProxy = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(room.Id);
                actorProxy.Send(new Actor_PlayerEnterRoom_Ntt()
                {
                    PlayerID  = user.Id,
                    UserID    = user.UserID,
                    SessionID = session.Id,
                });
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
        protected override async Task Run(Room room, Actor_PlayerEnterRoom_Ntt message)
        {
            try
            {
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();
                Gamer            gamer   = room.Get(message.UserID);
                if (gamer == null)
                {
                    //创建房间玩家对象
                    gamer = GamerFactory.Create(message.PlayerID, message.UserID, room.RoomId, (ushort)room.Count);
                    await gamer.AddComponent <MailBoxComponent>().AddLocation();

                    gamer.AddComponent <UnitGateComponent, long>(message.SessionID);

                    //设置转接ID
                    Session session           = Game.Scene.GetComponent <NetOuterComponent>().Get(message.SessionID);
                    session.GetUser().ActorID = gamer.Id;

                    room.Add(gamer);

                    Actor_GamerEnterRoom_Ntt broadcastMessage = new Actor_GamerEnterRoom_Ntt();
                    foreach (Gamer item in room.GetAll())
                    {
                        if (item == null)
                        {
                            //添加空位
                            broadcastMessage.users.Add(null);
                            continue;
                        }
                        UserInfo userInfo = await dbProxy.Query <UserInfo>(item.UserID, false);

                        GamerInfo info = new GamerInfo()
                        {
                            ChairID = item.uChairID, NickName = userInfo.NickName, Loses = userInfo.Loses, Money = userInfo.Money, Wins = userInfo.Wins
                        };
                        broadcastMessage.users.Add(info);
                    }
                    room.Broadcast(broadcastMessage);

                    Log.Info($"玩家{message.UserID}进入房间");
                }
                else
                {
                    Log.Info("断线重连?????");
                }
            }
            catch (Exception e)
            {
                Log.Info($"创建房间错误:{e.Message}");
            }
        }
Esempio n. 3
0
        public static async Task <Room> Create(byte PlayerCount, byte GameCount, int GameScore, Session client)
        {
            Room       room   = ComponentFactory.Create <Room, byte>(PlayerCount);
            RoomConfig config = new RoomConfig
            {
                PlayerCount = PlayerCount,
                GameCount   = GameCount,
                GameScore   = GameScore
            };

            room.roomConfig = config;
            await room.AddComponent <MailBoxComponent>().AddLocation();

            room.AddComponent <GameControllerComponent, RoomConfig>(config);
            Game.Scene.GetComponent <RoomComponent>().Add(room);
            client.GetUser().RoomID = room.RoomId;
            room.AddComponent <DeckComponent>();
            Log.Info($"创建房间{room.Id}");
            return(room);
        }
Esempio n. 4
0
        protected override async void Run(Session session, C2G_CreateRoom message, Action <G2C_CreateRoom> reply)
        {
            G2C_CreateRoom response = new G2C_CreateRoom();

            try
            {
                //验证合法性
                if (!GateHelper.SignSession(session))
                {
                    response.Error = ErrorCode.ERR_SignError;
                    reply(response);
                    return;
                }
                DBProxyComponent dbProxy = Game.Scene.GetComponent <DBProxyComponent>();

                User     user     = session.GetUser();
                UserInfo userInfo = await dbProxy.Query <UserInfo>(user.UserID, false);

                if (userInfo.Money < message.Room.GameScore)
                {
                    response.Error = ErrorCode.ERR_UserMoneyLessError;
                    reply(response);
                    return;
                }

                //创建房间
                Room room = await RoomFactory.Create(message.Room.PlayerCount, message.Room.GameCount,
                                                     message.Room.GameScore, session);

                //创建房间玩家对象
                Gamer gamer = GamerFactory.Create(user.Id, user.UserID, room.RoomId, (ushort)room.Count);
                await gamer.AddComponent <MailBoxComponent>().AddLocation();

                gamer.AddComponent <UnitGateComponent, long>(session.Id);

                //房间添加玩家
                room.Add(gamer);

                //网关服务器和玩家对应
                user.ActorID = gamer.Id;

                //返回房间信息给玩家
                response.RoomId  = room.RoomId;
                response.Room    = message.Room;
                response.ChairID = (ushort)(room.Count - 1);
                Log.Info($"创建房间成功:--- {userInfo.NickName} ,房号:{room.RoomId}");
                //返回给客户端
                reply(response);

                //存储该用户房间的创建次数
                RoomHistory roomInfo = await dbProxy.Query <RoomHistory>(user.UserID, false);

                if (roomInfo == null)
                {
                    RoomHistory roomHistory = ComponentFactory.CreateWithId <RoomHistory>(userInfo.Id);
                    roomHistory.CreateCount = 1;
                    roomHistory.NickName    = userInfo.NickName;
                    await dbProxy.Save(roomHistory, false);
                }
                else
                {
                    roomInfo.CreateCount += 1;
                    await dbProxy.Save(roomInfo, false);
                }



                //连接游戏服务器
                //StartConfigComponent config = Game.Scene.GetComponent<StartConfigComponent>();
                //IPEndPoint GameIPEndPoint= config.GameConfig.GetComponent<InnerConfig>().IPEndPoint;
                //Session GameSession = Game.Scene.GetComponent<NetInnerComponent>().Get(GameIPEndPoint);
                //GS2G_EnterRoom gs2g_EnterRoom =await GameSession.Call(new G2GS_EnterRoom()
                //{
                //    PlayerId = user.Id,
                //    UserID = user.UserID,
                //    GateSessionId = session.Id
                //}) as GS2G_EnterRoom;
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }