/// <summary> /// 准备开始游戏 /// </summary> /// <param name="self"></param> public static void ReadyStartGame(this GameControllerComponent self) { Room room = self.GetParent <Room>(); Gamer[] gamers = room.GetAll(); //房间内有3名玩家且全部准备则开始游戏 if (room.Count == 3 && gamers.Where(g => g.IsReady).Count() == 3) { //同步匹配服务器开始游戏 room.State = RoomState.Game; MapHelper.SendMessage(new MP2MH_SyncRoomState_Ntt() { RoomID = room.InstanceId, State = room.State }); //初始玩家开始状态 foreach (var _gamer in gamers) { if (_gamer.GetComponent <HandCardsComponent>() == null) { _gamer.AddComponent <HandCardsComponent>(); } _gamer.IsReady = false; } GameControllerComponent gameController = room.GetComponent <GameControllerComponent>(); //洗牌发牌 gameController.DealCards(); List <GamerCardNum> gamersCardNum = new List <GamerCardNum>(); Array.ForEach(gamers, (g) => { HandCardsComponent handCards = g.GetComponent <HandCardsComponent>(); //重置玩家身份 handCards.AccessIdentity = Identity.None; //记录玩家手牌数 gamersCardNum.Add(new GamerCardNum() { UserID = g.UserID, Num = g.GetComponent <HandCardsComponent>().GetAll().Length }); }); //发送玩家手牌和其他玩家手牌数 foreach (var _gamer in gamers) { ActorMessageSender actorProxy = _gamer.GetComponent <UnitGateComponent>().GetActorMessageSender(); Actor_GameStart_Ntt gameStartMessage = new Actor_GameStart_Ntt(); gameStartMessage.HandCards.AddRange(_gamer.GetComponent <HandCardsComponent>().GetAll()); gameStartMessage.GamersCardNum.AddRange(gamersCardNum); actorProxy.Send(gameStartMessage); } //随机先手玩家 gameController.RandomFirstAuthority(); Log.Info($"房间{room.InstanceId}开始游戏"); } }
protected override async Task Run(Room room, Actor_PlayerEnterRoom_Req message, Action <Actor_PlayerEnterRoom_Ack> reply) { Actor_PlayerEnterRoom_Ack response = new Actor_PlayerEnterRoom_Ack(); try { Gamer gamer = room.Get(message.UserID); if (gamer == null) { //创建房间玩家对象 gamer = GamerFactory.Create(message.PlayerID, message.UserID); await gamer.AddComponent <MailBoxComponent>().AddLocation(); gamer.AddComponent <UnitGateComponent, long>(message.SessionID); //加入到房间 room.Add(gamer); Actor_GamerEnterRoom_Ntt broadcastMessage = new Actor_GamerEnterRoom_Ntt(); foreach (Gamer _gamer in room.GetAll()) { if (_gamer == null) { //添加空位 broadcastMessage.Gamers.Add(null); continue; } //添加玩家信息 GamerInfo info = new GamerInfo() { UserID = _gamer.UserID, IsReady = _gamer.IsReady }; broadcastMessage.Gamers.Add(info); } //广播房间内玩家消息 room.Broadcast(broadcastMessage); Log.Info($"玩家{message.UserID}进入房间"); } else { //玩家重连 gamer.isOffline = false; gamer.PlayerID = message.PlayerID; gamer.GetComponent <UnitGateComponent>().GateSessionActorId = message.SessionID; //玩家重连移除托管组件 gamer.RemoveComponent <TrusteeshipComponent>(); Actor_GamerEnterRoom_Ntt broadcastMessage = new Actor_GamerEnterRoom_Ntt(); foreach (Gamer _gamer in room.GetAll()) { if (_gamer == null) { //添加空位 broadcastMessage.Gamers.Add(null); continue; } //添加玩家信息 GamerInfo info = new GamerInfo() { UserID = _gamer.UserID, IsReady = _gamer.IsReady }; broadcastMessage.Gamers.Add(info); } //发送房间玩家信息 ActorMessageSender actorProxy = gamer.GetComponent <UnitGateComponent>().GetActorMessageSender(); actorProxy.Send(broadcastMessage); List <GamerCardNum> gamersCardNum = new List <GamerCardNum>(); List <GamerState> gamersState = new List <GamerState>(); GameControllerComponent gameController = room.GetComponent <GameControllerComponent>(); OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>(); DeskCardsCacheComponent deskCardsCache = room.GetComponent <DeskCardsCacheComponent>(); foreach (Gamer _gamer in room.GetAll()) { HandCardsComponent handCards = _gamer.GetComponent <HandCardsComponent>(); gamersCardNum.Add(new GamerCardNum() { UserID = _gamer.UserID, Num = _gamer.GetComponent <HandCardsComponent>().GetAll().Length }); gamersState.Add(new GamerState() { UserID = _gamer.UserID, Identity = (byte)handCards.AccessIdentity, GrabLandlordState = orderController.GamerLandlordState.ContainsKey(_gamer.UserID) ? orderController.GamerLandlordState[_gamer.UserID] : false }); } //发送游戏开始消息 Actor_GameStart_Ntt gameStartNotice = new Actor_GameStart_Ntt() { HandCards = gamer.GetComponent <HandCardsComponent>().GetAll(), GamersCardNum = gamersCardNum }; actorProxy.Send(gameStartNotice); Card[] lordCards = null; if (gamer.GetComponent <HandCardsComponent>().AccessIdentity == Identity.None) { //广播先手玩家 actorProxy.Send(new Actor_AuthorityGrabLandlord_Ntt() { UserID = orderController.CurrentAuthority }); } else { if (gamer.UserID == orderController.CurrentAuthority) { //发送可以出牌消息 bool isFirst = gamer.UserID == orderController.Biggest; actorProxy.Send(new Actor_AuthorityPlayCard_Ntt() { UserID = orderController.CurrentAuthority, IsFirst = isFirst }); } lordCards = deskCardsCache.LordCards.ToArray(); } //发送重连数据 Actor_GamerReconnect_Ntt reconnectNotice = new Actor_GamerReconnect_Ntt() { Multiples = room.GetComponent <GameControllerComponent>().Multiples, GamersState = gamersState, DeskCards = new KeyValuePair <long, Card[]>(orderController.Biggest, deskCardsCache.library.ToArray()), LordCards = lordCards, }; actorProxy.Send(reconnectNotice); Log.Info($"玩家{message.UserID}重连"); } response.GamerID = gamer.Id; reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
protected override void Run(Gamer gamer, Actor_GamerGrabLandlordSelect_Ntt message) { Room room = Game.Scene.GetComponent <RoomComponent>().Get(gamer.RoomID); OrderControllerComponent orderController = room.GetComponent <OrderControllerComponent>(); GameControllerComponent gameController = room.GetComponent <GameControllerComponent>(); if (orderController.CurrentAuthority == gamer.UserID) { //保存抢地主状态 orderController.GamerLandlordState[gamer.UserID] = message.IsGrab; if (message.IsGrab) { orderController.Biggest = gamer.UserID; gameController.Multiples *= 2; room.Broadcast(new Actor_SetMultiples_Ntt() { Multiples = gameController.Multiples }); } //转发消息 Actor_GamerGrabLandlordSelect_Ntt transpond = new Actor_GamerGrabLandlordSelect_Ntt(); transpond.IsGrab = message.IsGrab; transpond.UserID = gamer.UserID; room.Broadcast(transpond); if (orderController.SelectLordIndex >= room.Count) { /* * 地主:√ 农民1:× 农民2:× * 地主:× 农民1:√ 农民2:√ * 地主:√ 农民1:√ 农民2:√ 地主:√ * * */ if (orderController.Biggest == 0) { //没人抢地主则重新发牌 gameController.BackToDeck(); gameController.DealCards(); //发送玩家手牌 Gamer[] gamers = room.GetAll(); List <GamerCardNum> gamersCardNum = new List <GamerCardNum>(); Array.ForEach(gamers, _gamer => gamersCardNum.Add(new GamerCardNum() { UserID = _gamer.UserID, Num = _gamer.GetComponent <HandCardsComponent>().GetAll().Length })); Array.ForEach(gamers, _gamer => { ActorMessageSender actorProxy = _gamer.GetComponent <UnitGateComponent>().GetActorMessageSender(); Actor_GameStart_Ntt actorMessage = new Actor_GameStart_Ntt(); actorMessage.HandCards.AddRange(_gamer.GetComponent <HandCardsComponent>().GetAll()); actorMessage.GamersCardNum.AddRange(gamersCardNum); }); //随机先手玩家 gameController.RandomFirstAuthority(); return; } else if ((orderController.SelectLordIndex == room.Count && ((orderController.Biggest != orderController.FirstAuthority.Key && !orderController.FirstAuthority.Value) || orderController.Biggest == orderController.FirstAuthority.Key)) || orderController.SelectLordIndex > room.Count) { gameController.CardsOnTable(orderController.Biggest); return; } } //当所有玩家都抢地主时先手玩家还有一次抢地主的机会 if (gamer.UserID == orderController.FirstAuthority.Key && message.IsGrab) { orderController.FirstAuthority = new KeyValuePair <long, bool>(gamer.UserID, true); } orderController.Turn(); orderController.SelectLordIndex++; room.Broadcast(new Actor_AuthorityGrabLandlord_Ntt() { UserID = orderController.CurrentAuthority }); } }