using System;
using Server.Data;
using System.Collections.Generic;
using MrWu.Debug;
using System.IO;
using System.Text;
using MrWu.Basic;
using Server;
namespace Game.Shadow
{
///
/// 影子池
///
public class ShadowPool
{
///
/// 初始化
///
public void Init()
{
ShadowManager.instance.LoadShadows();
}
///
/// 随机获取一个名字
///
///
public Shadow GetRandomShadow()
{
return ShadowManager.instance.GetRandomName();
}
///
/// 放入影子
///
///
public void PutShadows(Shadow shadow)
{
ShadowManager.instance.PutShadows(shadow);
}
///
/// 放入影子
///
///
public void PutShadows(IEnumerable sds)
{
ShadowManager.instance.PutShadows(sds);
}
///
/// 当玩家上桌的时候 使用
///
public virtual bool SetPlayerShadow(PlayerDataAsyc pl, int roomid, int roomCnt, RoomManager[] rooms)
{
Shadow[] shadows = null;
PlayerDataAsyc[] sds = null;
if (pl.shadowsInfo == null)
{
//没影子就去取影子
shadows = ShadowManager.instance.GetShadows(roomCnt);
sds = new PlayerDataAsyc[roomCnt];
}
else
{
shadows = pl.shadowsInfo;
sds = pl.shadows;
}
//取新的
if (shadows == null)
{
Debug.Error("未获取到影子!!!!");
return false;
}
int maxMoney = 0;
bool isNew = false;
//为影子创建玩家
for (int i = 0; i < roomCnt; i++)
{
isNew = false; //重置状态
if (sds[i] != null &&
(sds[i].money < rooms[i].config.MinMoney || sds[i].money >= rooms[i].config.MaxMoney))
{
//影子的钱不在此房间范围,换影子
ShadowManager.instance.PutShadows(shadows[i]);
sds[i] = null;
var tmp = ShadowManager.instance.GetShadows(1);
if (tmp == null)
{
Debug.Error("第二次取。。。。未取到影子?????");
return false;
}
shadows[i] = tmp[0];
}
if (sds[i] == null)
{
//影子没人
sds[i] = new PlayerDataAsyc();
sds[i].nickname = shadows[i].name;
sds[i].sex = shadows[i].sex;
sds[i].avatar = shadows[i].photo;
sds[i].area = "江西南昌";
isNew = true;
}
if (isNew)
{
//新的影子, 重新设置金额
maxMoney = rooms[i].config.MaxMoney;
if (maxMoney > 1000000000) //最大值大于10亿, 应该是最后一个房间
{
if (rooms[i].config.MinMoney < 200000000) //如果房间的最小金额小于2亿
maxMoney = 200000000; //让鬼的金额不至于太大
}
//矫正金额
sds[i].money = BaseFun.random.Next(rooms[i].config.MinMoney, maxMoney);
}
}
pl.shadows = sds;
pl.shadowsInfo = shadows;
return true;
}
///
/// 影子管理
///
protected class ShadowManager
{
///
/// 实例
///
public static ShadowManager instance { get; private set; }
private ShadowManager()
{
}
static ShadowManager()
{
instance = new ShadowManager();
}
///
/// 所有的影子
///
protected readonly List shadows = new List();
///
/// 影子剩余数量
///
public int count
{
get { return shadows.Count; }
}
///
/// 加载影子
///
public void LoadShadows()
{
string[] lines = File.ReadAllLines(TableManager.GetRobotInfoPath(), Encoding.UTF8);
int len = lines.Length;
//没开金币场
if (ServerConfigManager.Instance.OpenGold == 0)
{
len = Math.Min(len, 10);
}
Debug.Info($"读取到替身名字{len}个");
for (int i = 0; i < len; i++)
{
string[] items = lines[i].Split(',');
string phtoto = null;
if (items.Length > 1)
{
phtoto = $"https://cs1.jxhappy.top/GameRes/Avatar/{items[1]}";
}
Shadow shadow = new Shadow(items[0], phtoto, i % 2);
shadows.Add(shadow);
}
}
static int namecnt;
///
/// 随机一个影子的名字
///
///
public Shadow GetRandomName()
{
int cnt = 10;
for (int i = 0; i < cnt; i++)
{
int idx = BaseFun.random.Next(0, count);
var sd = shadows[idx];
if (!sd.isGetName) //名字没被使用过
{
sd.isGetName = true;
return sd;
}
}
return null;
}
///
/// 获取影子
///
///
///
public Shadow[] GetShadows(int count)
{
Debug.Log("影子的数量:" + shadows.Count + "," + count);
if (shadows.Count < count)
return null;
Shadow[] sds = new Shadow[count];
for (int i = 0; i < count; i++)
{
int idx = BaseFun.random.Next(0, shadows.Count);
sds[i] = shadows[idx];
shadows.RemoveAt(idx);
}
// Debug.Log("剩余的替身数量:" + shadows.Count);
return sds;
}
///
/// 放入影子
///
///
public void PutShadows(IEnumerable sds)
{
shadows.AddRange(sds);
}
///
/// 放入影子
///
///
public void PutShadows(Shadow shadow)
{
shadows.Add(shadow);
}
}
}
}