/********************************
 *
 * 作者:吴隆健
 * 创建时间: 2019/6/28 16:51:21
 *
 ********************************/

using System;
using Server.Config;
using System.Text;
using Game.Player;
using MrWu.Basic;
using MrWu.Debug;
using Game.Robot;
using MrWu.Time;

namespace Server
{
    /// <summary>
    /// 游戏信息
    /// </summary>
    public class GameSerInfo
    {
        /// <summary>
        /// 游戏名称
        /// </summary>
        public string name;

        /// <summary>
        /// 服务器id
        /// </summary>
        public int gameid;

        /// <summary>
        /// 客户端id
        /// </summary>
        public int clientid;

        /// <summary>
        /// 模块id
        /// </summary>
        public int moduleid;

        /// <summary>
        /// 玩家数量
        /// </summary>
        public int playerCount;

        /// <summary>
        /// 真人数量
        /// </summary>
        public int personCount;

        /// <summary>
        /// 房间数量
        /// </summary>
        public int roomCnt;

        /// <summary>
        /// 厅数量
        /// </summary>
        public int tingCnt;

        /// <summary>
        /// 桌子数量
        /// </summary>
        public int deskCnt;

        /// <summary>
        /// 开战次数
        /// </summary>
        public long warCnt;

        /// <summary>
        /// 运行的桌子
        /// </summary>
        public int runDeskCnt;

        /// <summary>
        /// 服务器赢的钱
        /// </summary>
        public long winMoney;

        /// <summary>
        /// 税收
        /// </summary>
        public long taxMoney;

        /// <summary>
        /// 总机器人数量
        /// </summary>
        public int RobotCnt;

        /// <summary>
        /// 闲置的机器人数量
        /// </summary>
        public int idleRobotCnt;

        /// <summary>
        /// 是否开启朋友房
        /// </summary>
        public bool openFriend;

        /// <summary>
        /// 是否开启金币房
        /// </summary>
        public bool openGrid;

        /// <summary>
        /// 是否开启回放
        /// </summary>
        public bool openhuifang;

        /// <summary>
        /// 消耗的房卡数量
        /// </summary>
        public long deFangKa;

        /// <summary>
        /// 登录次数
        /// </summary>
        public long loginCnt;

        /// <summary>
        /// 重连次数
        /// </summary>
        public long reConnectionCnt;

        /// <summary>
        /// 影子数量
        /// </summary>
        public int shadowCnt;

        /// <summary>
        /// 更新信息
        /// </summary>
        public virtual void Update()
        {
            GameConfig gameConfig = ServerConfigManager.Instance.GameConfig;
            name = gameConfig.GameName;
            openFriend = gameConfig.OpenFriend == 1;
            openGrid = gameConfig.OpenGold == 1;
            gameid = gameConfig.GameId;
            clientid = gameConfig.ClientId;
            moduleid = ServerConfigManager.Instance.NodeId;
            RobotCnt = RobotController.instance.RobotCnt;
            idleRobotCnt = RobotController.instance.idleRobotCnt;
            
            playerCount = PlayerCollection.Instance.PlayerCnt;
            RobotCnt = PlayerCollection.Instance.RobotCnt;
            roomCnt = GameManager.instance.roomCnt;
            tingCnt = GameManager.instance.tingCnt;
            winMoney = GameManager.instance.winmoney;
            taxMoney = GameManager.instance.taxmoney;
            deFangKa = GameManager.instance.DelFangKa;
            loginCnt = GameManager.instance.loginCnt;
            reConnectionCnt = GameManager.instance.reConnection;

            deskCnt = GameManager.instance.deskCnt;
            runDeskCnt = deskCnt - GameManager.instance.idleDeskCnt;
            warCnt = GameManager.instance.warCnt;
            personCount = playerCount - RobotCnt;
        }

        /// <summary>
        /// 输出
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            int wight = -15;
            sb.AppendLine(BaseFun.Align("名称:", name, wight));
            sb.AppendLine(BaseFun.Align("服务器id:", gameid, wight));
            sb.AppendLine(BaseFun.Align("客户端id:", clientid, wight));
            sb.AppendLine(BaseFun.Align("模块id:", moduleid, wight));
            sb.AppendLine(BaseFun.Align("朋友房:", openFriend ? "开启" : "关闭", wight));
            sb.AppendLine(BaseFun.Align("金币房:", openGrid ? "开启" : "关闭", wight));
            sb.AppendLine(BaseFun.Align("玩家数量:", playerCount, wight));
            sb.AppendLine(BaseFun.Align("真人数量:", personCount, wight));
            sb.AppendLine(BaseFun.Align("房间数量:", roomCnt, wight));
            sb.AppendLine(BaseFun.Align("厅数量:", tingCnt, wight));
            sb.AppendLine(BaseFun.Align("桌子数量:", deskCnt, wight));
            sb.AppendLine(BaseFun.Align("运行的桌子数:", runDeskCnt, wight));
            sb.AppendLine(BaseFun.Align("赢的钱:", winMoney, wight));
            sb.AppendLine(BaseFun.Align("税收:", taxMoney, wight));
            sb.AppendLine(BaseFun.Align("机器人数量:", RobotCnt, wight));
            sb.AppendLine(BaseFun.Align("闲置机器人数量:", idleRobotCnt, wight));
            sb.AppendLine(BaseFun.Align("消耗的房卡:", deFangKa, wight));
            sb.AppendLine(BaseFun.Align("开战次数:", warCnt, wight));
            sb.AppendLine(BaseFun.Align("登录人次:", loginCnt, wight));
            sb.AppendLine(BaseFun.Align("重连人次:", reConnectionCnt, wight));
            sb.AppendLine(BaseFun.Align("影子数量:", shadowCnt, wight));
            return sb.ToString();
        }
    }

    /// <summary>
    /// 城堡信息
    /// </summary>
    public class CastleSerInfo
    {
        /// <summary>
        /// 是否有这个城堡
        /// </summary>
        public bool isHave;

        /// <summary>
        /// id
        /// </summary>
        public int id;

        /// <summary>
        /// 最小可进游戏币
        /// </summary>
        public int minMoney;

        /// <summary>
        /// 最大可进游戏币
        /// </summary>
        public int maxMoney;

        /// <summary>
        /// 税收
        /// </summary>
        public int tax;

        /// <summary>
        /// 赔率
        /// </summary>
        public int peilv;

        /// <summary>
        /// 经验
        /// </summary>
        public int exp;

        /// <summary>
        /// 赢的钱
        /// </summary>
        public long winMoney;

        /// <summary>
        /// 税收的钱
        /// </summary>
        public long taxMoney;

        /// <summary>
        /// 玩家数量
        /// </summary>
        public int playerCnt;

        /// <summary>
        /// 真人数量
        /// </summary>
        public int personCnt;

        /// <summary>
        /// 机器人数量
        /// </summary>
        public int robotCnt;

        /// <summary>
        /// 档数/等级
        /// </summary>
        public int level;

        /// <summary>
        /// 更新
        /// </summary>
        public virtual void Update(int id)
        {
            isHave = id >= 0 && id < GameManager.instance.roomCnt;

            if (isHave)
            {
                this.id = id;
                var room = GameManager.instance.FindRoom(id);
                minMoney = room.config.MinMoney;
                maxMoney = room.config.MaxMoney;
                tax = room.config.Tax;
                peilv = room.config.BeiLv;
                exp = room.config.Exp;
                winMoney = room.winmoney;
                taxMoney = room.taxmoney;
                playerCnt = room.playerCount;
                robotCnt = room.robotCount;
                personCnt = playerCnt - robotCnt;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            int wight = -15;
            if (!isHave)
                sb.Append("没有城堡");
            else
            {
                sb.AppendLine(BaseFun.Align("城堡id:", id, wight));
                sb.AppendLine(BaseFun.Align("最低游戏币:", minMoney, wight));
                sb.AppendLine(BaseFun.Align("最高游戏币:", maxMoney, wight));
                sb.AppendLine(BaseFun.Align("税收:", tax, wight));
                sb.AppendLine(BaseFun.Align("经验值:", exp, wight));
                sb.AppendLine(BaseFun.Align("赔率:", peilv, wight));
                sb.AppendLine(BaseFun.Align("赢的钱:", winMoney, wight));
                sb.AppendLine(BaseFun.Align("税收的钱", taxMoney, wight));
                sb.AppendLine(BaseFun.Align("玩家数量:", playerCnt, wight));
                sb.AppendLine(BaseFun.Align("真人数量:", personCnt, wight));
                sb.AppendLine(BaseFun.Align("机器人数量:", robotCnt, wight));
            }

            return sb.ToString();
        }
    }

    /// <summary>
    /// 服务器厅信息
    /// </summary>
    public class TingSerInfo
    {
        /// <summary>
        /// 是否存在厅
        /// </summary>
        public bool isHave;

        /// <summary>
        /// 厅id
        /// </summary>
        public int id;

        /// <summary>
        /// 玩家数量
        /// </summary>
        public int playerCnt;

        /// <summary>
        /// 桌子数量
        /// </summary>
        public int deskCnt;

        /// <summary>运行中的桌子数量
        /// </summary>
        public int runDeskCnt;

        /// <summary>
        /// 机器人数量
        /// </summary>
        public int robotCnt;

        /// <summary>
        /// 真人数量
        /// </summary>
        public int personCnt;

        /// <summary>
        /// 最小开战人数
        /// </summary>
        public int warCnt;

        /// <summary>
        /// 最大开战人数
        /// </summary>
        public int maxCnt;

        /// <summary>
        /// 总的开战次数
        /// </summary>
        public long allWarCnt;

        /// <summary>
        /// 机器人胜利次数
        /// </summary>
        public long allRobotWinCnt;

        /// <summary>
        /// 总的开战时间
        /// </summary>
        public long totalWarTime;

        /// <summary>
        /// 平均开战时间
        /// </summary>
        public long avgWarTime;

        /// <summary>
        /// 最长战斗时间
        /// </summary>
        public long maxWarTime;

        /// <summary>
        /// 最短战斗时间
        /// </summary>
        public long minWarTime;

        /// <summary>
        /// 税收的金额
        /// </summary>
        public long taxMoney;

        /// <summary>
        /// 赢取的金额
        /// </summary>
        public long winMoney;

        /// <summary>
        /// 更新厅信息
        /// </summary>
        public virtual void Update(int id)
        {
            isHave = id >= 0 && id < GameManager.instance.tingCnt;

            if (isHave)
            {
                this.id = id;
                var ting = GameManager.instance.FindTing(id);
                deskCnt = ting.Count;
                warCnt = ting.config.MinPlayerCnt;
                maxCnt = ting.config.MaxPlayerCnt;
                playerCnt = ting.PlayerCnt;
                robotCnt = ting.RobotCnt;
                personCnt = playerCnt - robotCnt;
                runDeskCnt = ting.WarCount;
                allWarCnt = ting.warCnt;
                totalWarTime = ting.totalwarTime;
                avgWarTime = ting.avgWarTime;
                taxMoney = ting.taxmoney;
                winMoney = ting.winmoney;
                minWarTime = ting.minWarTime;
                maxWarTime = ting.maxWarTime;
                allRobotWinCnt = ting.robotWinCnt;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            int wight = -15;
            if (!isHave)
                sb.Append("没有厅");
            else
            {
                sb.AppendLine(BaseFun.Align("厅id:", id, wight));
                sb.AppendLine(BaseFun.Align("玩家数量:", playerCnt, wight));
                sb.AppendLine(BaseFun.Align("桌子数量:", deskCnt, wight));
                sb.AppendLine(BaseFun.Align("运行的桌子数量:", runDeskCnt, wight));
                sb.AppendLine(BaseFun.Align("机器人数量:", robotCnt, wight));
                sb.AppendLine(BaseFun.Align("真人数量:", personCnt, wight));
                sb.AppendLine(BaseFun.Align("最小开战人数:", warCnt, wight));
                sb.AppendLine(BaseFun.Align("最大开战人数:", maxCnt, wight));
                sb.AppendLine(BaseFun.Align("战斗次数:", allWarCnt, wight));
                sb.AppendLine(BaseFun.Align("机器人胜利次数:", allRobotWinCnt, wight));
                sb.AppendLine(BaseFun.Align("税收:", taxMoney, wight));
                sb.AppendLine(BaseFun.Align("赢取:", winMoney, wight));
                sb.AppendLine(BaseFun.Align("总开战时间", totalWarTime, wight));
                sb.AppendLine(BaseFun.Align("平均战斗时间", avgWarTime, wight));
                sb.AppendLine(BaseFun.Align("最短战斗时间", minWarTime, wight));
                sb.AppendLine(BaseFun.Align("最长战斗时间", maxWarTime, wight));
            }

            return sb.ToString();
        }
    }

    /// <summary>
    /// 桌子服务器信息
    /// </summary>
    public class DeskSerInfo
    {
        /// <summary>
        /// 是否存在
        /// </summary>
        public bool isHave;

        /// <summary>
        /// 厅id
        /// </summary>
        public int tingid;

        /// <summary>
        /// 桌子id
        /// </summary>
        public int id;

        /// <summary>
        /// 房号
        /// </summary>
        public string roomNum;

        /// <summary>
        /// 玩家数量
        /// </summary>
        public int PlayerCnt;

        /// <summary>
        /// 桌子状态
        /// </summary>
        public DeskState state;

        /// <summary>
        /// 开战时间
        /// </summary>
        public DateTime warTime;

        /// <summary>
        /// 玩家信息
        /// </summary>
        public PlayerInfo[] pls;

        /// <summary>
        /// 赢取的金额
        /// </summary>
        public long winmoney;

        /// <summary>
        /// 税收金额
        /// </summary>
        public long taxmoney;

        /// <summary>
        /// 玩家信息
        /// </summary>
        public class PlayerInfo
        {
            /// <summary>
            /// 玩家的id
            /// </summary>
            public int id;

            /// <summary>
            /// 玩家的userid
            /// </summary>
            public int userid;

            /// <summary>
            /// 玩家的昵称
            /// </summary>
            public string nickname;

            /// <summary>
            /// 是否存在玩家
            /// </summary>
            public bool isHave;
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual void Update(int tingid, int deskid)
        {
            if (tingid < 0 || tingid >= GameManager.instance.tingCnt)
            {
                Debug.Log("update_无此厅id:" + tingid + "," + GameManager.instance.tingCnt);
                isHave = false;
                return;
            }

            var ting = GameManager.instance.FindTing(tingid);
            if (deskid < 0 || deskid >= ting.Count)
            {
                Debug.Log("update_无此桌子:" + deskid + "," + ting.Count);
                isHave = false;
                return;
            }

            isHave = true;
            var desk = ting.desks[deskid];
            if (desk.deskinfo != null)
            {
                this.roomNum = desk.deskinfo.roomNum;
            }
            else
            {
                this.roomNum = null;
            }

            this.id = deskid;
            this.PlayerCnt = desk.PlayerCnt;
            this.tingid = tingid;
            this.state = desk.state;
            int len = desk.Count;
            this.warTime = desk.warTime;
            this.taxmoney = desk.taxmoney;
            this.winmoney = desk.winmoney;
            this.pls = new PlayerInfo[desk.Count];
            for (int i = 0; i < len; i++)
            {
                PlayerInfo pl = new PlayerInfo();
                pl.isHave = desk[i] != null;
                if (pl.isHave)
                {
                    pl.id = desk[i].id;
                    pl.nickname = desk[i].nickname;
                    pl.userid = desk[i].userid;
                }

                pls[i] = pl;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            int wight = -15;
            if (!isHave)
                sb.Append("没有桌子");
            else
            {
                if (!string.IsNullOrEmpty(roomNum))
                {
                    sb.AppendLine(BaseFun.Align("桌子号:", roomNum, wight));
                }
                
                sb.AppendLine(BaseFun.Align("桌子id:", id, wight));
                sb.AppendLine(BaseFun.Align("厅id:", tingid, wight));
                sb.AppendLine(BaseFun.Align("人数:", PlayerCnt, wight));
                sb.AppendLine(BaseFun.Align("开战时间", warTime.TimeToString(), wight));
                sb.AppendLine(BaseFun.Align("税收:", taxmoney, wight));
                sb.AppendLine(BaseFun.Align("赢取:", winmoney, wight));
                string statestr = string.Empty;
                if ((state & DeskState.Idle) > 0)
                    statestr += "闲置中/";
                if ((state & DeskState.Queue) > 0)
                    statestr += "排队中/";
                if ((state & DeskState.War) > 0)
                    statestr += "开战中/";
                sb.AppendLine(BaseFun.Align("状态:", statestr, wight));
                sb.AppendLine("所有玩家:");
                int len = pls.Length;
                for (int i = 0; i < len; i++)
                {
                    sb.AppendLine("位置" + i + ":");
                    if (pls[i].isHave)
                    {
                        // 这个位置有人
                        sb.AppendLine(BaseFun.Align("id:", pls[i].id, wight));
                        sb.AppendLine(BaseFun.Align("userid:", pls[i].userid, wight));
                        sb.AppendLine(BaseFun.Align("nickname:", pls[i].nickname, wight));
                    }
                }
            }

            return sb.ToString();
        }
    }
}