public void MsgRegister(Conn conn, ProtocolBase protoBase) { int start = 0; ProtocolBytes protocol = (ProtocolBytes)protoBase; string protoName = protocol.GetString(start, ref start); string id = protocol.GetString(start, ref start); string pw = protocol.GetString(start, ref start); string strFormat = "[收到注册协议]" + conn.GetAdress(); Console.WriteLine(strFormat + " 用户名:" + id + " 密码:" + pw); //构建返回协议 protocol = new ProtocolBytes(); protocol.AddString("Register"); //注册 if (DataMgr.instance.Register(id, pw)) { protocol.AddInt(0); //创建角色 } else { protocol.AddInt(-1); } DataMgr.instance.CreatePlayer(id); //返回给客户端 conn.Send(protocol); }
public void StartFight() { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("Fight"); status = Status.Fight; int teampos1 = 1; int teampos2 = 1; lock (list) { protocol.AddInt(list.Count); foreach (Player p in list.Values) { p.tempData.hp = 200; protocol.AddString(p.id); protocol.AddInt(p.tempData.team); if (p.tempData.team == 1) { protocol.AddInt(teampos1++); } else { protocol.AddInt(teampos2++); } p.tempData.status = PlayerTempData.Status.Fight; } Broadcast(protocol); } }
//获取玩家信息 public void MsgGetAchieve(Player player, ProtocolBase protoBase) { ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("GetAchieve"); protocolRet.AddInt(player.data.win); protocolRet.AddInt(player.data.fail); player.Send(protocolRet); Console.WriteLine("MsgGetScore " + player.id + player.data.win); }
//登录 //Login str 用户名 str 密码 //-1失败,0成功 public void MsgLogin(Conn conn, ProtocolBase protobase) { //获取数值 int start = 0; ProtocolBytes protocol = (ProtocolBytes)protobase; string protoname = protocol.GetString(start, ref start); string id = protocol.GetString(start, ref start); string pw = protocol.GetString(start, ref start); string strFormat = "[收到登录协议]" + conn.GetAdress(); Console.WriteLine(strFormat + " 用户名:" + id + " 密码:" + pw); //返回协议 ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("Login"); //验证 if (!DataMgr.instance.CheckPassWord(id, pw)) { protocolRet.AddInt(-1); conn.Send(protocolRet); return; } //是否已经登录 ProtocolBytes protocolLogout = new ProtocolBytes(); protocolLogout.AddString("Logout"); if (!Player.KickOff(id, protocolLogout)) { protocolRet.AddInt(-1); conn.Send(protocolRet); return; } PlayerData playerData = DataMgr.instance.GettPlayerData(id); if (playerData == null) { protocolRet.AddInt(-1); conn.Send(protocolRet); return; } conn.player = new Player(id, conn); conn.player.data = playerData; //事件触发 ServNet.instance.handlePlayerEvent.OnLogin(conn.player); //返回 protocolRet.AddInt(0); conn.Send(protocolRet); return; }
public void UpdateWin() { int isWin = IsWin(); if (isWin == 0) { return; } lock (list) { status = Status.Prepare; foreach (Player player in list.Values) { player.tempData.status = PlayerTempData.Status.Room; if (player.tempData.team == isWin) { player.data.win++; } else { player.data.fail++; } } } ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("Result"); protocol.AddInt(isWin); Broadcast(protocol); }
public ProtocolBytes GetRoomInfo() { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("GetRoomInfo"); //房间信息 protocol.AddInt(list.Count); foreach (Player py in list.Values) { protocol.AddString(py.id); protocol.AddInt(py.tempData.team); protocol.AddInt(py.data.win); protocol.AddInt(py.data.fail); int isOwner = py.tempData.isOwner ? 1 : 0; protocol.AddInt(isOwner); } return(protocol); }
public ProtocolBytes GetRoomList() { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("GetRoomList"); //数量 int count = list.Count; protocol.AddInt(count); for (int i = 0; i < count; i++) { Room room = list[i]; protocol.AddInt(room.list.Count); protocol.AddInt((int)room.status); } return(protocol); }
//创建 public void MsgCreateRoom(Player player, ProtocolBase protoBase) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("CreateRoom"); //条件检测 if (player.tempData.status != PlayerTempData.Status.None) { protocol.AddInt(-1); player.Send(protocol); Console.WriteLine("MsgCreateRoom Fail" + player.id); return; } RoomMgr.instance.CreateRoom(player); protocol.AddInt(0); player.Send(protocol); Console.WriteLine("MsgCreateRoom ok" + player.id); }
//加入 public void MsgEnterRoom(Player player, ProtocolBase proto) { int start = 0; ProtocolBytes protocol = (ProtocolBytes)proto; string protoName = protocol.GetString(start, ref start); int index = protocol.GetInt(start, ref start); Console.WriteLine("[收到 MsetenterRoom] " + player.id + "" + index); protocol = new ProtocolBytes(); protocol.AddString("EnterRoom"); if (index < 0 || index >= RoomMgr.instance.list.Count) { Console.WriteLine("MsgEnterRoom Index error" + player.id); protocol.AddInt(-1); player.Send(protocol); return; } Room room = RoomMgr.instance.list[index]; //判断房间状态 if (room.status != Room.Status.Prepare) { Console.WriteLine("MsgEnterRoom statuserr" + player.id); protocol.AddInt(-1); player.Send(protocol); return; } if (room.AddPlayer(player)) { room.Broadcast(room.GetRoomInfo()); protocol.AddInt(0); player.Send(protocol); } else { Console.WriteLine("MsgEnterRoom maxPlayer erro " + player.id); protocol.AddInt(-1); player.Send(protocol); } }
//start public void MsgStartFight(Player player, ProtocolBase protoBase) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("StartFight"); if (player.tempData.status != PlayerTempData.Status.Room) { Console.WriteLine("MsgStartFight status err " + player.id); protocol.AddInt(-1); player.Send(protocol); return; } if (!player.tempData.isOwner) { Console.WriteLine("MsgStartFight owner err " + player.id); protocol.AddInt(-1); player.Send(protocol); return; } Room room = player.tempData.room; if (!room.CanStart()) { Console.WriteLine("MsgStartFight Canstart err " + player.id); protocol.AddInt(-1); player.Send(protocol); return; } protocol.AddInt(0); player.Send(protocol); room.StartFight(); }
//离开 public void MsgLeaveRoom(Player player, ProtocolBase protoBase) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("LeaveRoom"); //条件检测 if (player.tempData.status != PlayerTempData.Status.Room) { protocol.AddInt(-1); player.Send(protocol); Console.WriteLine("MsgLeaveRoom status err" + player.id); return; } protocol.AddInt(0); player.Send(protocol); Room room = player.tempData.room; RoomMgr.instance.LeaveRoom(player); if (room != null) { room.Broadcast(room.GetRoomInfo()); } }
//返回协议 : 0-正常下线 public void MsgLogout(Conn conn, ProtocolBase protoBase) { 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 MsgUpdateInfo(Player player, ProtocolBase protoBase) { //获取分数 int start = 0; ProtocolBytes protocol = (ProtocolBytes)protoBase; 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 = protocol.GetInt(start, ref start); Scene.Scener.instance.UpdateInfo(protoname, 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); }