//增加分数 //协议参数: public void MsgAddScore(Player player, ProtocolBase protocolBase) { //获取数值 int start = 0; ProtocolBytes protocol = (ProtocolBytes)protocolBase; string protoName = protocol.GetString(start, ref start); //处理 player.data.score += 1; Console.WriteLine("MsgAddScore()-> " + player.id + " " + player.data.score.ToString()); }
//同步坦克单元 public void MsgUpdateUnitInfo(Player player, ProtocolBase protoBase) { //获取数值 int start = 0; ProtocolBytes protocol = (ProtocolBytes)protoBase; string protoName = protocol.GetString(start, ref start); float posX = protocol.GetFloat(start, ref start); float posY = protocol.GetFloat(start, ref start); float posZ = protocol.GetFloat(start, ref start); float rotX = protocol.GetFloat(start, ref start); float rotY = protocol.GetFloat(start, ref start); float rotZ = protocol.GetFloat(start, ref start); float gunRot = protocol.GetFloat(start, ref start); float gunRoll = protocol.GetFloat(start, ref start); //获取房间 if (player.tempData.status != PlayerTempData.Status.Fight) { return; } Room room = player.tempData.room; //作弊校验 略 player.tempData.posX = posX; player.tempData.posY = posY; player.tempData.posZ = posZ; player.tempData.lastUpdateTime = Sys.GetTimeStamp(); //广播 ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("UpdateUnitInfo"); protocolRet.AddString(player.id); protocolRet.AddFloat(posX); protocolRet.AddFloat(posY); protocolRet.AddFloat(posZ); protocolRet.AddFloat(rotX); protocolRet.AddFloat(rotY); protocolRet.AddFloat(rotZ); protocolRet.AddFloat(gunRot); protocolRet.AddFloat(gunRoll); room.Broadcast(protocolRet); }
/// <summary> /// 加入房间 /// </summary> /// <param name="player"></param> /// <param name="protocolBase"></param> public void MsgEnterRoom(Player player, ProtocolBase protocolBase) { //获取数值 int start = 0; ProtocolBytes protocol = (ProtocolBytes)protocolBase; string protoName = protocol.GetString(start, ref start); int index = protocol.GetInt(start, ref start); Console.WriteLine("[收到 MsgEnterRoom] " + player.id + " 加入房间号: " + index); //构建进入房间的返回协议 protocol = new ProtocolBytes(); protocol.AddString("EnterRoom"); //判断房间是否存在 if (index < 0 || index >= RoomMgr.instance.roomList.Count) { Console.WriteLine("MsgEnterRoom index error " + player.id); protocol.AddInt(-1); player.Send(protocol); return; } //判断房间的状态,只有处于“准备中”的房间才允许玩家加入 Room room = RoomMgr.instance.roomList[index]; if (room.status != Room.Status.Prepare) { Console.WriteLine("MsgEnterRoom status error " + player.id); protocol.AddInt(-1); player.Send(protocol); return; } //把玩家添加到房间,加入成功返回 0,加入失败返回 -1 if (room.AddPlayer(player)) { room.Broadcast(room.GetRoomInfo()); protocol.AddInt(0); player.Send(protocol); } else { Console.WriteLine("MsgEnterRoom maxPlayer error " + player.id); protocol.AddInt(-1); player.Send(protocol); } }
//更新信息 public void UpdateInfo(string id, float x, float y, float z, int score) { int count = playerList.Count; ProtocolBytes protocol = new ProtocolBytes(); ScenePlayer p = GetScenePlayer(id); if (p == null) { return; } p.x = x; p.y = y; p.z = z; p.score = score; }
//删除玩家 public void DeletePlayer(string id) { lock (playerList) { ScenePlayer p = GetScenePlayer(id); if (p != null) { playerList.Remove(p); } } ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("PlayerLeave"); protocol.AddString(id); ServNet.instance.Broadcast(protocol); }
//下线 //协议参数:无 //返回协议:0 正常下线 public void MsgLogout(Conn conn, ProtocolBase protocolBase) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("Logout"); protocol.AddInt(0); if (conn.player == null) { conn.Send(protocol); conn.Close(); } else { conn.Send(protocol); conn.player.Logout(); } }
//发送列表 public void SendPlayerList(Player player) { int count = playerList.Count; ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("GetList"); protocol.AddInt(count); for (int i = 0; i < count; i++) { ScenePlayer p = playerList[i]; protocol.AddString(p.id); protocol.AddFloat(p.x); protocol.AddFloat(p.y); protocol.AddFloat(p.z); protocol.AddInt(p.score); } player.Send(protocol); }
/// <summary> /// 构建返回房间列表协议GetRoomList, /// 第一个参数为房间数量count,紧接着附带count个房间数据,依次为房间里的玩家数量count,房间状态status /// </summary> /// <returns></returns> public ProtocolBytes GetRoomList() { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("GetRoomList"); int count = roomList.Count; //往协议添加房间数量 protocol.AddInt(count); //循环添加每个房间的信息 for (int i = 0; i < count; i++) { Room room = roomList[i]; protocol.AddInt(room.playerList.Count); protocol.AddInt((int)room.status); } return(protocol); }
//开始战斗 public void MsgStartFight(Player player, ProtocolBase protocolBase) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("StartFight"); //条件判断 //玩家是否在房间里 if (player.tempData.status != PlayerTempData.Status.Room) { Console.WriteLine("MsgStartFight status error " + player.id); protocol.AddInt(-1); player.Send(protocol); return; } //玩家是否为房主 if (!player.tempData.isOwner) { Console.WriteLine("MsgStartFigth owner error " + player.id); protocol.AddInt(-1); player.Send(protocol); return; } //房间能否开启战斗 Room room = player.tempData.room; if (!room.IsCanStart()) { Console.WriteLine("MsgStartFight Can Start Error" + player.id); protocol.AddInt(-1); player.Send(protocol); return; } //开始战斗 protocol.AddInt(0); player.Send(protocol); room.StartFight(); }
/// <summary> /// 创建房间 /// </summary> /// <param name="player"></param> /// <param name="protocolBase"></param> public void MsgCreateRoom(Player player, ProtocolBase protocolBase) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("CreateRoom"); //条件检测,如果玩家的状态是在房间中或是战斗中,则不能创建房间,返回创建房间失败 if (player.tempData.status != PlayerTempData.Status.None) { Console.WriteLine("MsgCreateRoom Fail" + player.id); protocol.AddInt(-1); player.Send(protocol); return; } //创建房间并返回成功信息 RoomMgr.instance.CreateRoom(player); protocol.AddInt(0); player.Send(protocol); Console.WriteLine("MsgCreateRoom OK" + player.id); }
//房间信息 public ProtocolBytes GetRoomInfo() { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("GetRoomInfo"); //添加房间玩家数量 protocol.AddInt(playerList.Count); //循环添加每个玩家的信息 foreach (Player p in playerList.Values) { protocol.AddString(p.id); protocol.AddInt(p.tempData.team); protocol.AddInt(p.data.win); protocol.AddInt(p.data.fail); int isOwner = p.tempData.isOwner ? 1 : 0; protocol.AddInt(isOwner); } return(protocol); }
//更新信息 public void MsgUpdateInfo(Player player, ProtocolBase protocolBase) { //获取数值 int start = 0; ProtocolBytes protocol = (ProtocolBytes)protocolBase; string protoName = protocol.GetString(start, ref start); float x = protocol.GetFloat(start, ref start); float y = protocol.GetFloat(start, ref start); float z = protocol.GetFloat(start, ref start); int score = player.data.score; Scene.instance.UpdateInfo(player.id, x, y, z, score); //广播 ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("UpdateInfo"); protocolRet.AddString(player.id); protocolRet.AddFloat(x); protocolRet.AddFloat(y); protocolRet.AddFloat(z); protocolRet.AddInt(score); ServNet.instance.Broadcast(protocolRet); }