public void MsgConnect(Connect connect, BaseProtocol Protocol) { int startIndex = 0; BytesProtocol bytesProtocol = (BytesProtocol)Protocol; string protocolName = bytesProtocol.GetString(startIndex, ref startIndex); string name = bytesProtocol.GetString(startIndex, ref startIndex); Console.WriteLine("[客户端 " + connect.GetAddress() + " ](连接消息) 以用户名:" + name + " 连接"); BytesProtocol bytesProtocolReturn = new BytesProtocol(); bytesProtocolReturn.SpliceString("Connect"); //名字已被使用,无法连接,返回-1,连接失败 if (Player.NameIsUsed(name)) { bytesProtocolReturn.SpliceInt(-1); connect.Send(bytesProtocolReturn); return; } //为连接初始化角色数据 connect.player = new Player(name, connect); //触发连接事件 Server.instance.handlePlayerEvent.OnConnect(connect.player); //返回0,即连接成功 bytesProtocolReturn.SpliceInt(0); connect.Send(bytesProtocolReturn); return; }
public BytesProtocol GetRoomList() { BytesProtocol protocol = new BytesProtocol(); protocol.SpliceString("GetRoomList"); protocol.SpliceInt(RoomList.Count); for (int i = 0; i < RoomList.Count; i++) { //房间人数 protocol.SpliceInt(RoomList[i].playerDict.Count); //房间状态 protocol.SpliceInt((int)RoomList[i].status); } return(protocol); }
/// <summary> /// 玩家获得有害状态,转发 /// </summary> public void MsgPlayerGetBuff(Player player, BaseProtocol baseProtocol) { //消息结构: (string)PlayerName + (int)BuffType +(float)buffTime int startIndex = 0; BytesProtocol get = baseProtocol as BytesProtocol; Room room = player.playerStatus.room; get.GetString(startIndex, ref startIndex); string PlayerName = get.GetString(startIndex, ref startIndex); int bufftype = get.GetInt(startIndex, ref startIndex); float bufftime = get.GetFloat(startIndex, ref startIndex); if (room.playerDict.ContainsKey(PlayerName)) { room.playerDict[PlayerName].playerStatus.buffType = bufftype; room.playerDict[PlayerName].playerStatus.buffTime = bufftime; } BytesProtocol p = new BytesProtocol(); p.SpliceString("PlayerGetBuff"); p.SpliceString(PlayerName); p.SpliceInt(bufftype); p.SpliceFloat(bufftime); foreach (Player pr in room.playerDict.Values) { p.SpliceString(pr.name); } player.Send(p); }
/// <summary> /// 玩家特殊魔法,带终点 /// </summary> public void MsgPlayerMagicEndpoint(Player player, BaseProtocol baseProtocol) { //玩家特殊魔法,终点生成 //消息结构: (string)PlayerMagic + (string)playerName + (int)magicItemID + (float)endposX+(float)endposY+(float)endposZ if (player.playerStatus.status != PlayerStatus.Status.Gaming) { return; } Room room = player.playerStatus.room; int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; p.GetString(startIndex, ref startIndex); string playerName = p.GetString(startIndex, ref startIndex); int magicItemID = p.GetInt(startIndex, ref startIndex); float posX = p.GetFloat(startIndex, ref startIndex); float posY = p.GetFloat(startIndex, ref startIndex); float posZ = p.GetFloat(startIndex, ref startIndex); //转发魔法消息 BytesProtocol p_broadcast = new BytesProtocol(); p_broadcast.SpliceString("PlayerMagicEndpoint"); p_broadcast.SpliceString(player.name); p_broadcast.SpliceInt(magicItemID); p_broadcast.SpliceFloat(posX); p_broadcast.SpliceFloat(posY); p_broadcast.SpliceFloat(posZ); room.Broadcast(p_broadcast); }
public BytesProtocol CirclefieldTimeProtocol() { BytesProtocol p = new BytesProtocol(); p.SpliceString("CirclefieldTime"); p.SpliceInt(circlefieldInfo.Circlefields[circlefieldIndex].Holdtime); return(p); }
public void MsgCreateRoom(Player player, BaseProtocol baseProtocol) { BytesProtocol bytesProtocol = new BytesProtocol(); bytesProtocol.SpliceString("CreateRoom"); //玩家的状态不是在房间中或战局中时,创建失败,返回-1 if (player.playerStatus.status != PlayerStatus.Status.Null) { bytesProtocol.SpliceInt(-1); player.Send(bytesProtocol); Console.WriteLine("MsgCreateRoom 失败,创建提出者:" + player.name); return; } RoomManager.instance.CreateRoom(player); //创建成功 返回0 bytesProtocol.SpliceInt(0); player.Send(bytesProtocol); Console.WriteLine("MsgCreateRoom 创建房间完成, 房主是: " + player.name); }
public void MsgJoinRoom(Player player, BaseProtocol baseProtocol) { int startIndex = 0; BytesProtocol Protocol = baseProtocol as BytesProtocol; string methodName = Protocol.GetString(startIndex, ref startIndex); int RoomIndex = Protocol.GetInt(startIndex, ref startIndex); Console.WriteLine("[客户端 " + player.name + " ]" + "请求加入房间(MsgJoinRoom):index:" + RoomIndex); Protocol = new BytesProtocol(); Protocol.SpliceString("JoinRoom"); if (RoomIndex < 0 || RoomIndex >= RoomManager.instance.RoomList.Count) { Console.WriteLine("[客户端 " + player.name + " ]" + "请求加入房间(MsgJoinRoom):index:" + RoomIndex + " 超出列表范围"); Protocol.SpliceInt(-1); player.Send(Protocol); return; } Room room = RoomManager.instance.RoomList[RoomIndex]; if (room.status != Room.Status.Preparing) { Console.WriteLine("[客户端 " + player.name + " ]" + "请求加入房间(MsgJoinRoom):index:" + RoomIndex + " 房间正在游玩"); Protocol.SpliceInt(-1); player.Send(Protocol); return; } if (room.AddPlayer(player)) { room.Broadcast(room.GetRoomInfo()); Protocol.SpliceInt(0); player.Send(Protocol); } else { Console.WriteLine("[客户端 " + player.name + " ]" + "请求加入房间(MsgJoinRoom):index:" + RoomIndex + " 房间人数已满"); Protocol.SpliceInt(-1); player.Send(Protocol); } }
/// <summary> /// 毒圈协议 /// </summary> /// <returns></returns> public BytesProtocol CirclefieldProtocol() { BytesProtocol p = new BytesProtocol(); p.SpliceString("Circlefield"); Point point = CalcCircleCenter(0, 0, R, circlefieldInfo.Circlefields[circlefieldIndex].ShrinkPercent); p.SpliceFloat(point.X); p.SpliceFloat(point.Y); p.SpliceFloat(circlefieldInfo.Circlefields[circlefieldIndex].ShrinkPercent); p.SpliceInt(circlefieldInfo.Circlefields[circlefieldIndex].Movetime); Console.WriteLine("pointX:{0} pointY:{1} per:{2} mt{3}", point.X, point.Y, circlefieldInfo.Circlefields[circlefieldIndex].ShrinkPercent, circlefieldInfo.Circlefields[circlefieldIndex].Movetime); return(p); }
/// <summary> /// 拾取物品 /// </summary> public void MsgPickItem(Player player, BaseProtocol baseProtocol) { //拾取物品 //消息结构: (string)PickItem + (int)GroundItemID int startIndex = 0; BytesProtocol get = baseProtocol as BytesProtocol; get.GetString(startIndex, ref startIndex); int GroundItemID = get.GetInt(startIndex, ref startIndex); BytesProtocol p = new BytesProtocol(); p.SpliceString("PickItem"); p.SpliceInt(GroundItemID); player.playerStatus.room.Broadcast(p); }
public void MsgLeaveRoom(Player player, BaseProtocol baseProtocol) { BytesProtocol protocol = new BytesProtocol(); protocol.SpliceString("LeaveRoom"); if (player.playerStatus.status != PlayerStatus.Status.InRoom) { Console.WriteLine("[客户端 " + player.name + " ]" + "请求离开房间(MsgLeaveRoom):玩家不在房间中,离开失败"); protocol.SpliceInt(-1); player.Send(protocol); return; } protocol.SpliceInt(0); player.Send(protocol); Room room = player.playerStatus.room; RoomManager.instance.LeaveRoom(player); //离开房间时,房间仍有人,则广播新的房间玩家信息 if (room != null) { room.Broadcast(room.GetRoomInfo()); } }
/// <summary> /// 获取房间信息协议 /// </summary> /// <returns></returns> public BytesProtocol GetRoomInfo() { //(int)playerNum PlayerInfo... BytesProtocol protocol = new BytesProtocol(); protocol.SpliceString("GetRoomInfo"); //玩家数量 protocol.SpliceInt(playerDict.Count); foreach (Player player in playerDict.Values) { //玩家名 protocol.SpliceString(player.name); //是房主则返回1 不是则返回0 if (player.playerStatus.isMaster) { protocol.SpliceInt(1); } else { protocol.SpliceInt(0); } } return(protocol); }
/// <summary> /// 房间门打开转发 /// </summary> /// <param name="player"></param> /// <param name="baseProtocol"></param> public void MsgDoorOpen(Player player, BaseProtocol baseProtocol) { //开门 //消息结构: (string)DoorOpen + (int)DoorID Room room = player.playerStatus.room; int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; p.GetString(startIndex, ref startIndex); int DoorID = p.GetInt(startIndex, ref startIndex); BytesProtocol doorProtocol = new BytesProtocol(); doorProtocol.SpliceString("DoorOpen"); doorProtocol.SpliceInt(DoorID); room.Broadcast(doorProtocol); }
public void MsgRequestStartGame(Player player, BaseProtocol baseProtocol) { BytesProtocol protocol = new BytesProtocol(); protocol.SpliceString("RequestStartGame"); /* if (!player.playerStatus.room.CanStart()) { protocol.SpliceInt(-1); player.Send(protocol); return; } */ protocol.SpliceInt(0); player.Send(protocol); player.playerStatus.room.StartGame(); }
/// <summary> /// 获取房间玩家信息 /// </summary> public void MsgGetPlayersInfo(Player player, BaseProtocol baseProtocol) { //消息结构: (string)GetPlayersInfo + (int)PlayerNum +(string)PlayerName1 + ... +(string)PlayerName# int startIndex = 0; BytesProtocol get = baseProtocol as BytesProtocol; get.GetString(startIndex, ref startIndex); Room room = player.playerStatus.room; BytesProtocol p = new BytesProtocol(); p.SpliceString("GetPlayersInfo"); p.SpliceInt(room.playerDict.Count); foreach (Player pr in room.playerDict.Values) { p.SpliceString(pr.name); } player.Send(p); }
public void MsgDisconnect(Connect connect, BaseProtocol Protocol) { BytesProtocol bytesProtocol = new BytesProtocol(); bytesProtocol.SpliceString("Disconnect"); bytesProtocol.SpliceInt(0); if (connect.player == null) { connect.Send(bytesProtocol); connect.Close(); } else { connect.Send(bytesProtocol); connect.player.Disconnect(); } }
/// <summary> /// 获取下落点 /// </summary> public void MsgDroppoint(Player player, BaseProtocol baseProtocol) { int startIndex = 0; BytesProtocol get = baseProtocol as BytesProtocol; get.GetString(startIndex, ref startIndex); Room room = player.playerStatus.room; BytesProtocol p = new BytesProtocol(); p.SpliceString("Droppoint"); Random rand = new Random(); lock (room.playerDroppoints) { int index = rand.Next(0, room.playerDroppoints.Count - 1); room.playerDroppoints.RemoveAt(index); p.SpliceInt(room.playerDroppoints[index]); } player.Send(p); }
/// <summary> /// 开始战局 /// </summary> public void StartGame() { BytesProtocol p = new BytesProtocol(); p.SpliceString("StartGame"); status = Status.Ongame; lock (playerDict) { //玩家数 p.SpliceInt(playerDict.Count); foreach (Player player in playerDict.Values) { player.playerStatus.HP = 100f; p.SpliceString(player.name); p.SpliceFloat(player.playerStatus.HP); } Broadcast(p); } }
public BytesProtocol CirclefieldProtocol2() { BytesProtocol p = new BytesProtocol(); p.SpliceString("Circlefield"); //Point point= CalcCircleCenter(0, 0, R, circlefieldInfo.Circlefields[circlefieldIndex].ShrinkPercent); if (CalcCenterOnce) { CalcCenterOnce = false; lastPoint.X = (float)Utility.NextDouble(new Random(), 20, 60); lastPoint.Y = (float)Utility.NextDouble(new Random(), 20, 60); } p.SpliceFloat(lastPoint.X); p.SpliceFloat(lastPoint.Y); p.SpliceFloat(circlefieldInfo.Circlefields[circlefieldIndex].ShrinkPercent); p.SpliceInt(circlefieldInfo.Circlefields[circlefieldIndex].Movetime); Console.WriteLine("pointX:{0} pointY:{1} Shrinkper:{2} movetime{3} Round:{4}", lastPoint.X, lastPoint.Y, circlefieldInfo.Circlefields[circlefieldIndex].ShrinkPercent, circlefieldInfo.Circlefields[circlefieldIndex].Movetime, circlefieldIndex); return(p); }
/// <summary> /// 获取地图物品数据 /// </summary> public void MsgGetMapItemData(Player player, BaseProtocol baseProtocol) { int startIndex = 0; BytesProtocol protocol = baseProtocol as BytesProtocol; protocol.GetString(startIndex, ref startIndex); Random ran = new Random((int)Utility.GetTimeStamp()); if (randomItemCode == 0) { randomItemCode = ran.Next(1, 999); } //Console.WriteLine("RandomItemCode Set: " + randomItemCode.ToString()); BytesProtocol p = new BytesProtocol(); p.SpliceString("GetMapItemData"); p.SpliceInt(randomItemCode); player.playerStatus.room.Broadcast(p); //player.Send(player.playerStatus.room.ItemsProtocol); }
/// <summary> /// 玩家扔物品 /// </summary> public void MsgDropItem(Player player, BaseProtocol baseProtocol) { //拾取物品 //消息结构: (string)PickItem + (int)GroundItemID int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; p.GetString(startIndex, ref startIndex); int GroundItemID = p.GetInt(startIndex, ref startIndex); float posX = p.GetFloat(startIndex, ref startIndex); float posY = p.GetFloat(startIndex, ref startIndex); float posZ = p.GetFloat(startIndex, ref startIndex); //转发魔法消息 BytesProtocol p_broadcast = new BytesProtocol(); p_broadcast.SpliceString("DropItem"); p_broadcast.SpliceInt(GroundItemID); p_broadcast.SpliceFloat(posX); p_broadcast.SpliceFloat(posY); p_broadcast.SpliceFloat(posZ); player.playerStatus.room.Broadcast(p); }