protected override async Task Run(Gamer gamer, Actor_GamerApplyRoomDismiss message)
        {
            try
            {
                Log.Info($"玩家{gamer.UserID}申请解散房间");
                RoomComponent roomComponent = Game.Scene.GetComponent <RoomComponent>();
                Room          room          = roomComponent.Get(gamer.RoomID);
                if (room == null)
                {
                    return;
                }
                if (!room.IsFriendRoom)
                {
                    return;
                }

                GameControllerComponent gameControllerComponent = room.GetComponent <GameControllerComponent>();
                if (room.State == RoomState.Idle)
                {
                    if (gamer.UserID != gameControllerComponent.RoomConfig.MasterUserId)
                    {
                        Log.Warning($"准备阶段只有房主才能解散,房主:{gameControllerComponent.RoomConfig.MasterUserId},gamer:{gamer.UserID}");
                        return;
                    }
                    else
                    {
                        if (room.CurrentJuCount > 0)
                        {
                            return;
                        }
                        room.Broadcast(new Actor_GamerReadyTimeOut()
                        {
                            Message = "房主解散房间"
                        });
                        GameHelp.RoomDispose(room);
                        return;
                    }
                }

                room.Broadcast(new Actor_GamerApplyRoomDismiss());
                room.WaitDismiss(60);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

            await Task.CompletedTask;
        }
Example #2
0
        public static async Task GamerAgreeRoomDismiss(Gamer gamer)
        {
            try
            {
                RoomComponent roomComponent = Game.Scene.GetComponent <RoomComponent>();
                Room          room          = roomComponent.Get(gamer.RoomID);
                if (room == null || !room.IsFriendRoom || gamer.DismissState != DismissState.None)
                {
                    return;
                }

                gamer.DismissState = DismissState.Agree;
                room.Broadcast(new Actor_GamerAgreeRoomDismiss()
                {
                    UserId = gamer.UserID
                });

                Gamer[] gamers = room.GetAll();

                if (gamers.Count(g => g.DismissState == DismissState.Agree) == 3)
                {
                    room.roomDismissTokenSource?.Cancel();
                    room.Broadcast(new Actor_GamerReadyTimeOut()
                    {
                        Message = "房间已解散"
                    });
                    GameHelp.RoomDispose(room);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

            await Task.CompletedTask;
        }
        public static async Task GamerContinue(Gamer gamer)
        {
            try
            {
                Log.Info($"玩家{gamer.UserID}继续游戏");

                RoomComponent roomComponent = Game.Scene.GetComponent <RoomComponent>();
                Room          room          = roomComponent.Get(gamer.RoomID);
                if (room == null)
                {
                    return;
                }

                GameControllerComponent  gameControllerComponent  = room.GetComponent <GameControllerComponent>();
                OrderControllerComponent orderControllerComponent = room.GetComponent <OrderControllerComponent>();

                gamer.ReadyTimeOut = 0;
                List <GamerInfo> Gamers = new List <GamerInfo>();
                for (int i = 0; i < room.GetAll().Length; i++)
                {
                    Gamer _gamer = room.GetAll()[i];
                    if (_gamer == null)
                    {
                        continue;
                    }
                    GamerInfo gamerInfo = new GamerInfo();
                    gamerInfo.UserID    = _gamer.UserID;
                    gamerInfo.SeatIndex = room.GetGamerSeat(_gamer.UserID);
                    gamerInfo.IsReady   = _gamer.IsReady;
                    PlayerBaseInfo playerBaseInfo = await DBCommonUtil.getPlayerBaseInfo(gamerInfo.UserID);

                    //判断金币是否不够
                    if (!room.IsFriendRoom)
                    {
                        if (playerBaseInfo.GoldNum < gameControllerComponent.RoomConfig.MinThreshold)
                        {
                            room.GamerBroadcast(_gamer, new Actor_GamerReadyTimeOut()
                            {
                                Message = "金币不足"
                            });
                            room.Remove(_gamer.UserID);
                            _gamer.Dispose();
                            if (room.Count == 0)
                            {
                                GameHelp.RoomDispose(room);
                                return;
                            }
                            continue;
                        }
                    }

                    PlayerInfo playerInfo = PlayerInfoFactory.Create(playerBaseInfo);
                    gamerInfo.playerInfo = playerInfo;

                    Gamers.Add(gamerInfo);
                }

                Actor_GamerEnterRoom actorGamerEnterRoom = new Actor_GamerEnterRoom()
                {
                    RoomType = (int)gameControllerComponent.RoomConfig.Id,
                    Gamers   = Gamers
                };
                if (room.IsFriendRoom)
                {
                    actorGamerEnterRoom.RoomId         = gameControllerComponent.RoomConfig.FriendRoomId;
                    actorGamerEnterRoom.MasterUserId   = gameControllerComponent.RoomConfig.MasterUserId;
                    actorGamerEnterRoom.JuCount        = gameControllerComponent.RoomConfig.JuCount;
                    actorGamerEnterRoom.Multiples      = gameControllerComponent.RoomConfig.Multiples;
                    actorGamerEnterRoom.CurrentJuCount = room.CurrentJuCount;
                }

                room.Broadcast(actorGamerEnterRoom);

//                if (room.IsFriendRoom)
//                {
                await Actor_GamerReadyHandler.GamerReady(gamer, new Actor_GamerReady()
                {
                });

//                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

            await Task.CompletedTask;
        }
Example #4
0
        /// <summary>
        /// 游戏结束
        /// </summary>
        /// <param name="self"></param>
        /// <param name="huaCount"></param>
        public static async Task GameOver(this GameControllerComponent self, int huaCount)
        {
            RoomComponent roomComponent = null;
            Room          room          = null;

            try
            {
                room            = self.GetParent <Room>();
                room.IsGameOver = true;
                room.tokenSource.Cancel();
                roomComponent = Game.Scene.GetComponent <RoomComponent>();
                DeskComponent           deskComponent       = room.GetComponent <DeskComponent>();
                GameControllerComponent controllerComponent = room.GetComponent <GameControllerComponent>();

                deskComponent.RestLibrary.Clear();

                if (huaCount == 0)
                {
                    //没牌
                    room.huPaiUid         = 0;
                    room.fangPaoUid       = 0;
                    room.LiangZhuangCount = 0;
                    room.Broadcast(new Actor_GameFlow());
                    //流局
                    room.LastBiXiaHu = true;
                }

                await ChangeWeath(self, huaCount, room);

                if (room.huPaiUid != 0)
                {
                    await DBCommonUtil.RecordWeekRankLog(room.huPaiUid, 0, 1);
                }
                Log.Info(
                    $"当前{room.CurrentJuCount}局,改变了{room.GetAll()[0].ChangeGold},{room.GetAll()[1].ChangeGold},{room.GetAll()[2].ChangeGold},{room.GetAll()[3].ChangeGold}");
                //更新任务
                List <Task> tasks            = new List <Task>();
                Task        updateTask       = UpdateTask(room);
                Task        updateChengjiu   = UpdateChengjiu(room);
                Task        updatePlayerInfo = UpdatePlayerInfo(room, huaCount);
                //记录对局
                Task logGame = DBCommonUtil.Log_Game(
                    controllerComponent.RoomConfig.Name,
                    room.GetAll()[0].UserID,
                    room.GetAll()[1].UserID,
                    room.GetAll()[2].UserID,
                    room.GetAll()[3].UserID,
                    room.huPaiUid,
                    self.RoomConfig.FriendRoomId,
                    room.GetAll()[0].UserID + ";" + room.GetAll()[0].playerBaseInfo.Name + ";" + room.GetAll()[0].ChangeGold,
                    room.GetAll()[1].UserID + ";" + room.GetAll()[1].playerBaseInfo.Name + ";" + room.GetAll()[1].ChangeGold,
                    room.GetAll()[2].UserID + ";" + room.GetAll()[2].playerBaseInfo.Name + ";" + room.GetAll()[2].ChangeGold,
                    room.GetAll()[3].UserID + ";" + room.GetAll()[3].playerBaseInfo.Name + ";" + room.GetAll()[3].ChangeGold
                    );
                tasks.Add(updateTask);
                tasks.Add(updateChengjiu);
                tasks.Add(updatePlayerInfo);
                tasks.Add(logGame);
                await Task.WhenAll(tasks);

                //设置在线时长
                foreach (var gamer in room.GetAll())
                {
                    //在线
                    if (!gamer.isOffline)
                    {
                        gamer.EndTime = DateTime.Now;
                        TimeSpan span         = gamer.EndTime - gamer.StartTime;
                        int      totalSeconds = (int)span.TotalSeconds;
                        //await DBCommonUtil.RecordGamerTime(gamer.EndTime, false, gamer.UserID);
                        await DBCommonUtil.RecordGamerInfo(gamer.UserID, totalSeconds);
                    }
                }

                foreach (var gamer in room.GetAll())
                {
                    if (gamer == null)
                    {
                        continue;
                    }
                    gamer.RemoveComponent <HandCardsComponent>();
                    gamer.IsReady           = false;
                    gamer.ReadyTimeOut      = 0;
                    gamer.isGangFaWanPai    = false;
                    gamer.isFaWanPaiTingPai = false;
                    gamer.isGangEndBuPai    = false;
                    gamer.isGetYingHuaBuPai = false;
                    gamer.IsCanPeng         = false;
                    gamer.IsCanGang         = false;
                    gamer.IsCanHu           = false;
                    gamer.IsWinner          = false;
                    gamer.IsTrusteeship     = false;
                    gamer.ChangeGold        = 0;
                    //离线踢出
                    if (gamer.isOffline && !room.IsFriendRoom)
                    {
                        Log.Info($"玩家{gamer.UserID}结束游戏后离线踢出,移除玩家");
                        room.Remove(gamer.UserID);
                        gamer.isOffline = !gamer.isOffline;
                    }
                }

                #region 好友房设置

                if (room.IsFriendRoom)
                {
                    //打完啦。可以解散了
                    if (room.CurrentJuCount == self.RoomConfig.JuCount)
                    {
                        //等待结算界面结束
                        await Game.Scene.GetComponent <TimerComponent>().WaitAsync(5000);

                        Log.Debug("好友房间打完了");
                        room.Broadcast(new Actor_GamerReadyTimeOut()
                        {
                            Message = "房间解散"
                        });
                        GameHelp.RoomDispose(room);
                        return;
                    }
                    else
                    {
                        Log.Debug("还没打完");
                        room.StartReady();
                    }
                }

                #endregion


                room.State        = RoomState.Idle;
                room.IsLianZhuang = true;

                //游戏房间进入准备房间
                roomComponent.gameRooms.Remove(room.Id);
                roomComponent.idleRooms.Add(room.Id, room);

                //房间没人就释放
                if (room.seats.Count == 0)
                {
                    Log.Info($"房间释放:{room.Id}");
                    roomComponent.RemoveRoom(room);
                    room?.Dispose();
                }
            }
            catch (Exception e)
            {
                Log.Error("房间结算:" + e);
                //游戏房间进入准备房间
                roomComponent?.gameRooms.Remove(room.Id);
                roomComponent?.idleRooms.Add(room.Id, room);
            }
        }
Example #5
0
        protected override async Task Run(Gamer gamer, Actor_GamerExitRoom message)
        {
            try
            {
                Log.Info($"玩家{gamer.UserID}退出房间");
                RoomComponent roomComponent = Game.Scene.GetComponent <RoomComponent>();
                Room          room          = roomComponent.Get(gamer.RoomID);
                if (room == null)
                {
                    return;
                }

                if (room.State == RoomState.Game)
                {
                    if (gamer.isOffline)
                    {
                        return;
                    }
                    gamer.isOffline = true;
                    //玩家断开添加自动出牌组件
                    //if (gamer.GetComponent<TrusteeshipComponent>() == null)
                    //gamer.AddComponent<TrusteeshipComponent>();
                    gamer.EndTime = DateTime.Now;
                    TimeSpan span         = gamer.EndTime - gamer.StartTime;
                    int      totalSeconds = (int)span.TotalSeconds;
                    //await DBCommonUtil.RecordGamerTime(gamer.EndTime, false,gamer.UserID);
                    await DBCommonUtil.RecordGamerInfo(gamer.UserID, totalSeconds);

                    Log.Info($"玩家{gamer.UserID}断开,切换为自动模式");
                }
                else
                {
                    GameControllerComponent gameControllerComponent = room.GetComponent <GameControllerComponent>();

                    //好友房还没开局房主掉线,房间解散
                    if (room.IsFriendRoom && room.State == RoomState.Idle && room.CurrentJuCount == 0)
                    {
                        if (gameControllerComponent.RoomConfig.MasterUserId == gamer.UserID)
                        {
                            room.Broadcast(new Actor_GamerReadyTimeOut()
                            {
                                Message = "房主解散房间"
                            });
                            GameHelp.RoomDispose(room);
                            return;
                        }
                    }
                    //好友房开局后,掉线后不能退出
                    if (room.IsFriendRoom && room.CurrentJuCount > 0 && room.CurrentJuCount < gameControllerComponent.RoomConfig.JuCount)
                    {
                        gamer.isOffline = true;
                        Log.Info($"{gamer.UserID} 好友房开局后,掉线后不能退出");
                        return;
                    }

                    //玩家主动退出 通知gate
                    if (message.IsFromClient)
                    {
                        ActorMessageSenderComponent actorMessageSenderComponent = Game.Scene.GetComponent <ActorMessageSenderComponent>();
                        ActorMessageSender          actorMessageSender          = actorMessageSenderComponent.Get(gamer.PlayerID);
                        actorMessageSender.Send(new M2G_Actor_GamerExitRoom());

                        //消息广播给其他人
                        room.Broadcast(new Actor_GamerExitRoom()
                        {
                            Uid = gamer.UserID
                        });
                        //房间移除玩家
                        Log.Info($"{gamer.UserID}主动退出,移除玩家");
                        room.Remove(gamer.UserID);
                    }
                    else     //游戏崩溃
                    {
                        //房间移除玩家
                        Log.Info($"{gamer.UserID}崩溃退出,移除玩家");
                        room.Remove(gamer.UserID);
                        //消息广播给其他人
                        room.Broadcast(new Actor_GamerExitRoom()
                        {
                            Uid = gamer.UserID
                        });
                    }
                    gamer.Dispose();
                    //房间没人就释放
                    if (room.seats.Count == 0)
                    {
                        roomComponent.RemoveRoom(room);
                        room?.Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

            await Task.CompletedTask;
        }