public class Room
{
/// <summary>
/// 使用静态变量进行房间号标识
/// </summary>
public static int Count;
public int Id;
/// <summary>
/// 玩家信息的数据结构(它可不能通信啊)
/// </summary>
private PlayerModel[] models = new PlayerModel[3];
/// <summary>
/// 负责和远程玩家通信的分身(ServerAgent)
/// </summary>
private Client[] clients = new Client[3];
//定义房间状态(准备,开始)
public Room()
{
//Id = Math.Abs(System.DateTime.UtcNow.Millisecond);
Count++;
Id = Count;
}
/// <summary>
/// 匹配成功之后进入房间
/// </summary>
public void InRoom(Client[] clients)
{
this.clients = clients;
//1.广播匹配数据
NetResponse res = new NetResponse();
res.Type = "Match";
//构造客户端发来Mach请求响应的数据结构(NetResponse)
JsonData jd = new JsonData();
jd["RoomId"] = Id;
//数据初始化
for (int i = 0; i < clients.Length; i++)
{
//初始化房间里玩家的数据
models[i] = new PlayerModel(clients[i].Id, clients[i].Name);
//给房间ID赋值
clients[i].RoomId = Id;
}
jd["Player"] = JsonMapper.ToJson(models);
res.Data = JsonMapper.ToJson(jd);
Notify(res);
}
/// <summary>
/// 广播
/// </summary>
/// <param name="rep"></param>
public void Notify(NetResponse rep)
{
for (int i = 0; i < clients.Length; i++)
{
clients[i].Send(rep);
}
}
}