コード例 #1
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);
            }
        }