using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using GameData;
using GameMessage;
using MrWu.Time;
using Server;
using Debug = MrWu.Debug.Debug;
using GameData.Club;

namespace ObjectModel.Club
{
    /// <summary>
    /// 俱乐部比赛 表实体
    /// </summary>
    public class ClubMatch
    {
        /// <summary>
        /// 联赛的俱乐部比赛数据
        /// </summary>
        public ClubMatch LeagueClubMatch;

        /// <summary>
        /// 开启中
        /// </summary>
        public bool IsOpening;
        
        /// <summary>
        /// 自增ID  主键  0是新增  其他是修改
        /// </summary>
        public int ClubMatchId;

        /// <summary>
        /// 俱乐部Id   索引
        /// </summary>
        public int ClubId;

        /// <summary>
        /// 比赛名称
        /// </summary>
        public string ClubMatchName;

        private DateTime _MatchStartTime;

        /// <summary>
        /// 比赛开始时间
        /// </summary>
        public DateTime MatchStartTime
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch.MatchStartTime;
                }
                return _MatchStartTime;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("MatchStartTime 当前比赛数据属于联赛，不能修改！");
                }

                _MatchStartTime = value;
            }
        }

        private DateTime _MatchEndTime;

        /// <summary>
        /// 比赛结束时间
        /// </summary>
        public DateTime MatchEndTime
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch.MatchEndTime;
                }

                return _MatchEndTime;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("MatchEndTime 当前比赛数据属于联赛，不能修改！");
                }

                _MatchEndTime = value;
            }
        }

        private int _DieOutNum;

        /// <summary>
        /// 比赛淘汰分值
        /// </summary>
        public int DieOutNum
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch.DieOutNum;
                }

                return _DieOutNum;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("DieOutNum 当前比赛的数据属于联赛，不能修改！");
                }

                _DieOutNum = value;
            }
        }

        private DateTime _CreateTime;
        
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch.CreateTime;
                }

                return _CreateTime;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("CreateTime 当前比赛的数据属于联赛，不能修改!");
                }
                _CreateTime = value;
            }
        }

        private int _StatusFlag;

        /// <summary>
        /// 比赛状态 1比赛中 2结束
        /// </summary>
        public int StatusFlag
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch.StatusFlag;
                }
                return _StatusFlag;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("StatusFlag 当前比赛数据属于联赛，不能修改!");
                }

                _StatusFlag = value;
            }
        }

        private bool _AutoOpen;
        
        /// <summary>
        /// 是否自动开启下一场比赛
        /// </summary>
        public bool AutoOpen
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch.AutoOpen;
                }

                return _AutoOpen;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("AutoOpen 当前比赛数据属于联赛，不能修改!");
                }

                _AutoOpen = value;
            }
        }

        private int _HowTopUser;
        
        /// <summary>
        /// --前多少名正数玩家自动进入下一轮比赛
        /// 该值表示是否是联赛，小于0是联赛，该值存储了联赛的ClubId
        /// </summary>
        public int HowTopUser
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch.HowTopUser;
                }

                return _HowTopUser;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("HowTopUser 当前比赛数据属于联赛，不能修改!");
                }

                _HowTopUser = value;
            }
        }

        /// <summary>
        /// 第几场比赛
        /// </summary>
        public int Round;

        private int _MatchRate;
        
        /// <summary>
        /// 比赛频率    单位为天
        /// </summary>
        public int MatchRate
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch.MatchRate;
                }

                return _MatchRate;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("MatchRate 当前比赛数据属于联赛，不能修改!");
                }

                _MatchRate = value;
            }
        }

        private int _VisibleDesk;

        /// <summary>
        /// 普通成员显示的桌子数量
        /// </summary>
        public int VisibleDesk
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch._VisibleDesk;
                }

                return _VisibleDesk;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("VisibleDesk 当前比赛数据属于联赛，不能修改!");
                }

                _VisibleDesk = value;
            }
        }

        private byte _isOnlyVisibleMyClub;
        
        /// <summary>
        /// 是否只显示自己俱乐部的桌子--只有联赛才会能体现出这个设置
        /// 1不是 0是显示自己俱乐部的桌子
        /// </summary>
        public byte isOnlyVisibleMyClub
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch._isOnlyVisibleMyClub;
                }

                return _isOnlyVisibleMyClub;
            }
            set
            {
                _isOnlyVisibleMyClub = value;
            }
        }

        private byte _isVisibleScoreBak;

        /// <summary>
        /// 是否显示淘汰分 1显示 0不显示(默认)
        /// </summary>
        public byte isVisibleScoreBak
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch._isVisibleScoreBak;
                }
                return _isVisibleScoreBak;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("isVisibleScoreBak 当前比赛数据属于联赛，不能修改!");
                }
                _isVisibleScoreBak = value;
            }
        }

        private byte _isScoreReset;

        /// <summary>
        /// 每轮比赛开始积分是否清零 1清零 0不清零(默认)
        /// </summary>
        public byte isScoreReset
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch._isScoreReset;
                }
                return _isScoreReset;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("isScoreReset 当前比赛数据属于联赛，不能修改!");
                }
                _isScoreReset = value;
            }
        }

        private List<MatchPlayWay> _MatchPlayWays = new List<MatchPlayWay>();

        /// <summary>
        /// 比赛关联的玩法  数据库这个字段用string存储 MatchPlayWayId，用逗号分隔。
        /// </summary>
        public List<MatchPlayWay> MatchPlayWays
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch.MatchPlayWays;
                }
                return _MatchPlayWays;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("MatchPlayWays 当前比赛数据属于联赛，不能修改!");
                }
                _MatchPlayWays = value;
            }
        }

        public List<MatchPlayWay> _BakMatchPlayWays = new List<MatchPlayWay>();

        /// <summary>
        /// 备份的玩法，目的是用户删除玩法然后有的玩法该玩法已经开战了，用户开战玩家计算分。
        /// 不存数据库，只在内存中。
        /// </summary>
        public List<MatchPlayWay> BakMatchPlayWays
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch._BakMatchPlayWays;
                }
                return _BakMatchPlayWays;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("BakMatchPlayWays 当前比赛数据属于联赛，不能修改!");
                }
                _BakMatchPlayWays = value;
            }
        }

        /// <summary>
        /// 存储比赛其他设置的Json数据
        /// </summary>
        public string JsonData;

        /// <summary>
        /// 比赛账号转换联赛账号的基数
        /// </summary>
        const int ConstNum = 10000;

        private MatchOtherSetting _matchOtherSetting = new MatchOtherSetting();
        
        /// <summary>
        /// 比赛其他设置
        /// </summary>
        public MatchOtherSetting matchOtherSetting
        {
            get
            {
                if (LeagueClubMatch != null)
                {
                    return LeagueClubMatch._matchOtherSetting;
                }
                return _matchOtherSetting;
            }
            set
            {
                if (LeagueClubMatch != null)
                {
                    Debug.Error("matchOtherSetting 当前比赛数据属于联赛，不能修改!");
                }
                _matchOtherSetting = value;
            }
        }

        public void InitOtherSetting()
        {
            if (!string.IsNullOrEmpty(JsonData))
            {
                matchOtherSetting = JsonPack.GetData<MatchOtherSetting>(JsonData);
            }
        }

        public MatchOtherSetting CopyMatchOtherSetting()
        {
            string jsondata = GetJsonData();
            return JsonPack.GetData<MatchOtherSetting>(jsondata);
        }

        public string GetJsonData()
        {
            if (matchOtherSetting == null)
            {
                matchOtherSetting = new MatchOtherSetting();
            }
            return JsonPack.GetJsonNoIgnore(matchOtherSetting);
        }

        /// <summary>
        /// 获取联赛账号 5位数数字
        /// </summary>
        /// <returns></returns>
        public static int GetLeagueUserName(int clubMatchId)
        {
            return clubMatchId + ConstNum;
        }

        /// <summary>
        /// 根据联赛账号返回比赛ID
        /// </summary>
        /// <returns></returns>
        public static int GetClubMatchId(int leagueUserNameNumber)
        {
            return leagueUserNameNumber - ConstNum;
        }

        /// <summary>
        /// 返回对象
        /// </summary>
        /// <returns></returns>
        public static ClubMatch GetInstance(CreateClubMatchPack request, ClubMatch oldClubMatch, bool IsLeague = false)
        {
            DateTime endTime = DateTime.Now.Date.AddDays(request.MatchRate).AddHours(request.MatchStartTime.Hour).AddMinutes(request.MatchStartTime.Minute);
            return new ClubMatch()
            {
                ClubMatchId = 0,
                ClubId = request.ClubId,
                ClubMatchName = request.ClubMatchName ?? $"{(IsLeague ? "竞技联赛" : "竞技赛")}第{GetFromRound(oldClubMatch == null ? 1 : oldClubMatch.Round + 1)}轮",
                MatchStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, request.MatchStartTime.Hour, request.MatchStartTime.Minute, 0),
                MatchEndTime = endTime,
                DieOutNum = request.DieOutNum,
                CreateTime = DateTime.Now,
                StatusFlag = 1,
                AutoOpen = request.AutoOpen,
                HowTopUser = request.HowTopUser,
                Round = oldClubMatch == null ? 1 : oldClubMatch.Round + 1,
                MatchRate = request.MatchRate,
                MatchPlayWays = MatchPlayWay.GetInstances(request),
                VisibleDesk = request.VisibleDesk,
                isOnlyVisibleMyClub = request.isOnlyVisibleMyClub,
                isScoreReset = request.isScoreReset,
                isVisibleScoreBak = request.isVisibleScoreBak
            };
        }

        /// <summary>
        /// 获取界面上显示的比赛轮次
        /// </summary>
        /// <param name="round"></param>
        /// <returns></returns>
        public static int GetFromRound(int round)
        {
            return (round % 5) + 1;
        }

        private static string GetClubMatchName(bool isLeague,int round)
        {
            return $"{(isLeague ? "竞技联赛" : "竞技赛")}第{GetFromRound(round + 1)}轮";
        }

        /// <summary>
        /// 返回下一轮新比赛的对象
        /// </summary>
        /// <param name="oldClubMatch"></param>
        /// <returns></returns>
        public static ClubMatch GetNexMatchObject(ClubMatch oldClubMatch, bool isLeague = false)
        {
            if (oldClubMatch == null) return null;
            DateTime endTime = DateTime.Now.Date.AddDays(oldClubMatch.MatchRate).AddHours(oldClubMatch.MatchEndTime.Hour).AddMinutes(oldClubMatch.MatchEndTime.Minute);
            var result = new ClubMatch();
            result.ClubMatchId = 0;
            result.ClubId = oldClubMatch.ClubId;
            result.ClubMatchName = GetClubMatchName(isLeague, oldClubMatch.Round);
            result.MatchStartTime = DateTime.Now;
            result.MatchEndTime = endTime;
            result.DieOutNum = oldClubMatch.DieOutNum;
            result.CreateTime = DateTime.Now;
            result.StatusFlag = 1;
            result.AutoOpen = oldClubMatch.AutoOpen;
            result.HowTopUser = oldClubMatch.HowTopUser;
            result.Round = oldClubMatch.Round + 1;
            result.MatchRate = oldClubMatch.MatchRate;
            result.MatchPlayWays = oldClubMatch.MatchPlayWays;
            result.VisibleDesk = oldClubMatch.VisibleDesk;
            result.isOnlyVisibleMyClub = oldClubMatch.isOnlyVisibleMyClub;
            result.isVisibleScoreBak = oldClubMatch.isVisibleScoreBak;
            result.isScoreReset = oldClubMatch.isScoreReset;
            result.matchOtherSetting = oldClubMatch.CopyMatchOtherSetting();
            if (result.MatchPlayWays != null)
            {
                foreach (var item in result.MatchPlayWays)
                {
                    item.ClubMatchId = 0;
                    item.MatchPlayWayId = 0;
                    if (item.MaxWinnerScoreRanges != null)
                    {
                        foreach (var itemChid in item.MaxWinnerScoreRanges)
                        {
                            itemChid.MatchPlayWayId = 0;
                            itemChid.MaxWinnerScoreRangeId = 0;
                        }
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 根据联赛返回 比赛对象
        /// </summary>
        /// <returns></returns>
        public static ClubMatch GetClubMatchByLeagueMatch(ClubMatch leagueMatch,ClubMatch originMatch,int clubId, int round)
        {
            if (leagueMatch == null) return null;
            var result = new ClubMatch();
            result.ClubMatchName = leagueMatch.ClubMatchName; //比赛名称不能乱改，必须与联赛一致，使用了名字做逻辑
            result.LeagueClubMatch = leagueMatch;
            result.ClubMatchId = 0;
            result.ClubId = clubId;
            result.Round = round + 1;
            return result;
        }

        public static ClubMatchInfoResponse GetClubMatchInfoResponse(ClubMatch clubMatch)
        {
            ClubMatchInfoResponse response = new ClubMatchInfoResponse();

            response.ClubId = clubMatch.ClubId;
            response.MatchStartTime = DateTimeHelper.DateTime2TimeStamp(clubMatch.MatchStartTime);
            response.MatchInfoName = clubMatch.ClubMatchName;
            response.MatchEndTime = DateTimeHelper.DateTime2TimeStamp(clubMatch.MatchEndTime);
            response.MatchRate = clubMatch.MatchRate;
            response.DieOutNum = clubMatch.DieOutNum;
            response.AutoOpen = clubMatch.AutoOpen;
            response.LeagueClubId = clubMatch.HowTopUser;
            response.VisibleDesk = clubMatch.VisibleDesk;
            response.IsOnlyVisibleMyClub = clubMatch.isOnlyVisibleMyClub;
            response.IsScoreReset = clubMatch.isScoreReset;
            response.IsVisibleScoreBak = clubMatch.isVisibleScoreBak;
            response.LeagueUserNameNumber = ClubMatch.GetLeagueUserName(clubMatch.ClubMatchId);
            response.MatchPlayWayInfos = MatchPlayWay.GetMatchPlayWayInfos(clubMatch.MatchPlayWays);
            return response;
        }

        /// <summary>
        /// 返回输出对象
        /// </summary>
        /// <param name="oldClubMatch"></param>
        /// <returns></returns>
        public static CreateClubMatchPack GetCreateClubMatchResponse(ClubMatch oldClubMatch)
        {
            return new CreateClubMatchPack
            {
                ClubId = oldClubMatch.ClubId,
                MatchStartTime = oldClubMatch.MatchStartTime,
                ClubMatchName = oldClubMatch.ClubMatchName,
                MatchEndTime = oldClubMatch.MatchEndTime,
                MatchRate = oldClubMatch.MatchRate,
                DieOutNum = oldClubMatch.DieOutNum,
                AutoOpen = oldClubMatch.AutoOpen,
                HowTopUser = oldClubMatch.HowTopUser,
                VisibleDesk = oldClubMatch.VisibleDesk,
                isOnlyVisibleMyClub = oldClubMatch.isOnlyVisibleMyClub,
                isScoreReset = oldClubMatch.isScoreReset,
                isVisibleScoreBak = oldClubMatch.isVisibleScoreBak,
                LeagueUserNameNumber = ClubMatch.GetLeagueUserName(oldClubMatch.ClubMatchId),
                MatchPlayWays = MatchPlayWay.GetMatchPlayWayPacks(oldClubMatch.MatchPlayWays)
            };
        }

        /// <summary>
        /// 返回对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static ClubMatchInfo GetMatchInfoInstance(ClubMatch obj)
        {
            return new ClubMatchInfo
            {
                ClubMatchId = obj.ClubMatchId,
                ClubMatchName = obj.ClubMatchName,
                ClubId = obj.ClubId,
                StatusFlag = obj.StatusFlag,
                MatchStartTime = obj.MatchStartTime,
                MatchEndTime = obj.MatchEndTime
            };
        }

        public static ClubMatchData GetMatchDataInstance(ClubMatch obj)
        {
            ClubMatchData data = new ClubMatchData();
            data.ClubMatchId = obj.ClubMatchId;
            data.ClubId = obj.ClubId;
            data.ClubMatchName = obj.ClubMatchName;
            data.MatchStartTime = DateTimeHelper.DateTime2TimeStamp(obj.MatchStartTime);
            data.MatchEndTime = DateTimeHelper.DateTime2TimeStamp(obj.MatchEndTime);
            data.StatusFlag = obj.StatusFlag;
            return data;
        }

        public void Copy2LeagueMatch()
        {
            ClubMatch leagueClubMatch = this.LeagueClubMatch;
            this.LeagueClubMatch = null;
            this._MatchStartTime = leagueClubMatch._MatchStartTime;
            this._MatchEndTime = leagueClubMatch._MatchEndTime;
            this._DieOutNum = leagueClubMatch._DieOutNum;
            this._CreateTime = leagueClubMatch._CreateTime;
            this._StatusFlag = leagueClubMatch._StatusFlag;
            this._AutoOpen = leagueClubMatch._AutoOpen;
            this._HowTopUser = leagueClubMatch._HowTopUser;
            this._MatchRate = leagueClubMatch._MatchRate;
            this._VisibleDesk = leagueClubMatch._VisibleDesk;
            this._isOnlyVisibleMyClub = leagueClubMatch._isOnlyVisibleMyClub;
            this._isVisibleScoreBak = leagueClubMatch._isVisibleScoreBak;
            this._isScoreReset = leagueClubMatch._isScoreReset;
            this._MatchPlayWays = leagueClubMatch._MatchPlayWays;
            this._BakMatchPlayWays = leagueClubMatch._BakMatchPlayWays;
            this._matchOtherSetting = leagueClubMatch._matchOtherSetting;
        }
    }

    /// <summary>
    /// 比赛其他设置
    /// </summary>
    public class MatchOtherSetting
    {
        /// <summary>
        /// 重赛限制  --重赛审核中的用户无法在本联赛任何俱乐部参与比赛 false不限制 true限制 默认不限制
        /// </summary>
        public bool ReplayRestrictions;
        /// <summary>
        /// 生存任务限制 --下一轮比赛开始时生存任务调整为默认值
        /// </summary>
        public bool LiveTaskRestrictions;
    }

    /// <summary>
    /// 比赛玩法    表实体对象
    /// </summary>
    public class MatchPlayWay
    {
        /// <summary>
        /// 自增Id 主键
        /// </summary>
        public int MatchPlayWayId;

        /// <summary>
		/// 所属竞技比赛 组合索引I_MatchPlayWay_ClubIdClubMatchId   1
		/// </summary>
		public int ClubId;

        /// <summary>
        /// 比赛Id 外键 组合索引I_MatchPlayWay_ClubIdClubMatchId   2
        /// </summary>
        public int ClubMatchId;

        /// <summary>
        /// 玩法的唯一码
        /// </summary>
        public string weiyima;

        /// <summary>
        /// 最低分数限制，玩家低于此分不能进入桌子
        /// </summary>
        public int MinScore;

        /// <summary>
        /// 桌费数值    AA
        /// </summary>
        public int DeskCost;

        /// <summary>
        /// 消耗方式 1AA 2大赢家
        /// </summary>
        public int TypeMode;

        /// <summary>
        /// 最低分值免收桌费    AA
        /// </summary>
        public int MinFreeDeskCostNum;

        /// <summary>
        /// 是否第一局结束前解散不计房费  true是不计  false 否计算
        /// </summary>
        public bool IsFirstCalcDeskCost;

        /// <summary>
        /// 小局低于多少分解散桌子
        /// </summary>
        public int BuresMinScore;

        /// <summary>
        /// 是否开启小局低于多少分解散房间
        /// </summary>
        public bool IsBuresMin;

        /// <summary>
        /// 是否开启 解散房间按实际局数计算比赛消耗
        /// </summary>
        public bool IsRealCalcScore;

        /// <summary>
        /// 大赢家消耗规则
        /// </summary>
        public List<MaxWinnerScoreRange> MaxWinnerScoreRanges;

        /// <summary>
        /// 每次比赛联赛活跃积分
        /// </summary>
        public decimal LeagueDeskScore;

        /// <summary>
        /// 深复制对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T DeepCopy<T>(T obj)
        {
            object retval;
            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer xml = new XmlSerializer(typeof(T));
                xml.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                retval = xml.Deserialize(ms);
                ms.Close();
            }
            return (T)retval;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static List<MatchPlayWay> GetInstances(CreateClubMatchPack request)
        {
            var result = new List<MatchPlayWay>();
            if (request.MatchPlayWays == null || request.MatchPlayWays.Count < 1)
            {
                return result;
            }
            foreach (var item in request.MatchPlayWays)
            {
                result.Add(new MatchPlayWay
                {
                    MatchPlayWayId = 0,
                    ClubId = request.ClubId,
                    ClubMatchId = 0,//新增的时候要赋值
                    weiyima = item.weiyima,
                    MinScore = item.MinScore,
                    DeskCost = item.DeskCost,
                    TypeMode = item.TypeMode,
                    MinFreeDeskCostNum = item.MinFreeDeskCostNum,
                    IsBuresMin = item.IsBuresMin,
                    IsFirstCalcDeskCost = item.IsFirstCalcDeskCost,
                    IsRealCalcScore = item.IsRealCalcScore,
                    BuresMinScore = item.BuresMinScore,
                    MaxWinnerScoreRanges = item.MaxWinnerScoreRanges,
                    LeagueDeskScore = item.LeagueDeskScore
                });
            }
            return result;

        }

        /// <summary>
        /// 生成一个默认的玩法
        /// </summary>
        /// <param name="playWay"></param>
        /// <param name="clubMatch"></param>
        /// <returns></returns>
        public static MatchPlayWay GetInstanceByPlayWay(PlayWay playWay, ClubMatch clubMatch)
        {
            return new MatchPlayWay()
            {
                MatchPlayWayId = 0,
                ClubId = playWay.Clubid,
                ClubMatchId = clubMatch.ClubMatchId,
                weiyima = playWay.weiyima,
                MinScore = clubMatch.DieOutNum,
                DeskCost = 5,
                TypeMode = 2,
                MinFreeDeskCostNum = 10,
                IsBuresMin = true,
                IsFirstCalcDeskCost = true,
                IsRealCalcScore = true,
                BuresMinScore = clubMatch.DieOutNum,
                MaxWinnerScoreRanges = new List<MaxWinnerScoreRange>() {
                    new MaxWinnerScoreRange{ MatchPlayWayId=0, MaxWinnerScoreRangeId=0, DeskScore=5,
                    MaxWinnerMinScore=0}
                    }
            };
        }

        /// <summary>
        /// 获取玩法
        /// </summary>
        /// <param name="matchPlayWays"></param>
        /// <returns></returns>
        public static List<MatchPlayWayInfo> GetMatchPlayWayInfos(List<MatchPlayWay> matchPlayWays)
        {
            List<MatchPlayWayInfo> result = new List<MatchPlayWayInfo>();
            if (matchPlayWays == null || matchPlayWays.Count < 1)
            {
                return result;
            }

            foreach (var item in matchPlayWays)
            {
                result.Add(new MatchPlayWayInfo
                {
                    WeiYiMa = item.weiyima,
                    MinScore = item.MinScore,
                    DeskCost = item.DeskCost,
                    TypeMode = item.TypeMode,
                    MinFreeDeskCostNum = item.MinFreeDeskCostNum,
                    IsBuresMin = item.IsBuresMin,
                    IsFirstCalcDeskCost = item.IsFirstCalcDeskCost,
                    IsRealCalcScore = item.IsRealCalcScore,
                    BuresMinScore = item.BuresMinScore,
                    MaxWinnerScoreRanges = Convert2MatchMaxWinnerScoreRanges(item.MaxWinnerScoreRanges),
                    LeagueDeskScore = (double)item.LeagueDeskScore
                });
            }
            return result;
        }

        private static List<MatchMaxWinnerScoreRange> Convert2MatchMaxWinnerScoreRanges(List<MaxWinnerScoreRange> maxWinnerScoreRanges)
        {
            List<MatchMaxWinnerScoreRange> result = new List<MatchMaxWinnerScoreRange>();
            foreach (var item in maxWinnerScoreRanges)
            {
                result.Add(MaxWinnerScoreRange2MatchMaxWinnerScoreRange(item));
            }

            return result;
        }
        
        //todo 优化使用引用池
        private static MatchMaxWinnerScoreRange MaxWinnerScoreRange2MatchMaxWinnerScoreRange(MaxWinnerScoreRange maxWinnerScoreRange)
        {
            MatchMaxWinnerScoreRange range = new MatchMaxWinnerScoreRange();
            range.DeskScore = maxWinnerScoreRange.DeskScore;
            range.MaxWinnerMinScore = maxWinnerScoreRange.MaxWinnerMinScore;
            return range;
        }

        /// <summary>
        /// 获取玩法
        /// </summary>
        /// <param name="matchPlayWays"></param>
        /// <returns></returns>
        public static List<MatchPlayWayPack> GetMatchPlayWayPacks(List<MatchPlayWay> matchPlayWays)
        {
            var result = new List<MatchPlayWayPack>();
            if (matchPlayWays == null || matchPlayWays.Count < 1)
            {
                return result;
            }
            foreach (var item in matchPlayWays)
            {
                result.Add(new MatchPlayWayPack
                {
                    weiyima = item.weiyima,
                    MinScore = item.MinScore,
                    DeskCost = item.DeskCost,
                    TypeMode = item.TypeMode,
                    MinFreeDeskCostNum = item.MinFreeDeskCostNum,
                    IsBuresMin = item.IsBuresMin,
                    IsFirstCalcDeskCost = item.IsFirstCalcDeskCost,
                    IsRealCalcScore = item.IsRealCalcScore,
                    BuresMinScore = item.BuresMinScore,
                    MaxWinnerScoreRanges = item.MaxWinnerScoreRanges,
                    LeagueDeskScore = item.LeagueDeskScore
                });
            }
            return result;
        }

    }



    /// <summary>
    /// 积分记录
    /// </summary>
    public class ClubMatchScoreNotes
    {
        /// <summary>
        /// 自增Id 主键
        /// </summary>
        public int ScoreNotesId;
        /// <summary>
        /// 俱乐部ID   组合索引I_ScoreNotes_ClubIdClubMatchId  1
        /// </summary>
        public int ClubId;
        /// <summary>
        /// 比赛ID    组合索引I_ScoreNotes_ClubIdClubMatchId  2
        /// </summary>
        public int ClubMatchId;

        /// <summary>
        /// 玩家Id    组合索引I_ScoreNotes_ClubIdClubMatchIdUserId  3
        /// </summary>
        public int UserId;
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime;
        /// <summary>
        /// 类型 1游戏结算 2群主调整比赛积分  3比赛重置  4比赛结束 5调整淘汰分
        /// </summary>
        public int TypeId;
        /// <summary>
        /// 操作的分值
        /// </summary>
        public double Score;

        /// <summary>
        /// 这把玩家扣除的桌费--这个字段只有群主自己展示
        /// </summary>
        public double DeskScore;

        /// <summary>
        /// 操作以后的分值
        /// </summary>
        public double NewScore;

        /// <summary>
        /// 获取输出对象
        /// </summary>
        /// <param name="lists"></param>
        /// <returns></returns>
        public static List<UserScoreLog> GetUserScoreLogs(List<ClubMatchScoreNotes> lists)
        {
            var result = new List<UserScoreLog>();
            if (lists == null || lists.Count < 1)
            {
                return result;
            }

            for (int i = lists.Count - 1; i >= 0; i--)
            {
                var item = lists[i];
                result.Add(new UserScoreLog
                {
                    CreateTime = item.CreateTime,
                    TypeId = item.TypeId,
                    Score = item.Score,
                    DeskScore = item.DeskScore,
                    ScoreUI = item.TypeId == 1 ? item.Score - item.DeskScore : item.NewScore
                });
            }
            return result;
        }
    }

    /// <summary>
    /// 俱乐部退赛对象
    /// </summary>
    public class ExitMatchObj
    {
        public int ClubId;

        public List<ClubMatchUserScore> Datas;
    }

    /// <summary>
    /// 比赛玩家积分表
    /// </summary>
    public class ClubMatchUserScore
    {
        /// <summary>
        /// 自增Id 主键
        /// </summary>
        public int UserScoreId;
        /// <summary>
        /// 俱乐部ID   组合索引I_UserScore_ClubIdClubMatchId
        /// </summary>
        public int ClubId;
        /// <summary>
        /// 比赛ID    组合索引I_UserScore_ClubIdClubMatchId
        /// </summary>
        public int ClubMatchId;

        /// <summary>
        /// 玩家Id
        /// </summary>
        public int UserId;

        /// <summary>
        /// 比赛分
        /// </summary>
        public decimal Score;

        /// <summary>
        /// 玩家提供的桌费  竞技赛活跃积分  房费/人数
        /// </summary>
        public decimal DeskScore;

        /// <summary>
        /// 如果显示淘汰分那么就是淘汰分，如果不显示就是积分备注
        /// </summary>
        public decimal ScoreBak;

        /// <summary>
        /// 数据更新时间
        /// </summary>
        public DateTime UpdateTime;

        /// <summary>
        /// 获取玩家总积分
        /// </summary>
        /// <param name="isHaveScoreBak">是否显示淘汰分</param>
        /// <returns></returns>
        public decimal GetPlayScore(bool isHaveScoreBak)
        {
            if (isHaveScoreBak)
            {
                return this.Score + this.ScoreBak;
            }
            else
            {
                return this.Score;
            }
        }

        public ClubMatchUserScore Clone()
        {
            return new ClubMatchUserScore
            {
                UserScoreId = this.UserScoreId,
                ClubId = this.ClubId,
                ClubMatchId = this.ClubMatchId,
                UserId = this.UserId,
                Score = this.Score,
                DeskScore = this.DeskScore,
                ScoreBak = this.ScoreBak
            };
        }
    }

    /// <summary>
    /// 比赛记录
    /// </summary>
    public class ClubMatchNotes
    {
        /// <summary>
        /// 自增Id 主键
        /// </summary>
        public long ClubMatchNotesId;

        /// <summary>
        /// 日志类型 
        /// </summary>
        public MatchNotesEnum TypeId;

        /// <summary>
        /// 俱乐部ID   组合索引I_ClubMatchNotes_ClubIdClubMatchId
        /// </summary>
        public int ClubId;
        /// <summary>
        /// 比赛ID    组合索引I_ClubMatchNotes_ClubIdClubMatchId
        /// </summary>
        public int ClubMatchId;

        /// <summary>
        /// 产生时间
        /// </summary>
        public DateTime CreateTime;

        /// <summary>
        /// 内容
        /// </summary>
        public string Content;

        /// <summary>
        /// 获取输出对象
        /// </summary>
        /// <param name="lists"></param>
        /// <returns></returns>
        public static List<ClubMatchNotesLog> GetClubMatchNotes(List<ClubMatchNotes> lists)
        {
            var result = new List<ClubMatchNotesLog>();
            if (lists == null || lists.Count < 1)
            {
                return result;
            }

            for (int i=lists.Count - 1; i >= 0; i--)
            {
                var item = lists[i];
                result.Add(new ClubMatchNotesLog
                {
                    TypeId = item.TypeId,
                    CreateTime = item.CreateTime,
                    Content = item.Content
                });
                //Debug.Info($"发给客户端的日志:{item.Content}");
            }
            
            return result;
        }
    }
}



