/// <summary> /// 注册协议处理 /// </summary> /// <param name="conn"></param> /// <param name="protoBase"></param> public void MsgRegister(Conn conn, ProtocolBase protoBase) { //开始读取的位置 int start = 0; ProtocolBytes protoBytes = (ProtocolBytes)protoBase; string protoName = protoBytes.GetString(start, ref start); string id = protoBytes.GetString(start, ref start); string pw = protoBytes.GetString(start, ref start); //构建返回协议 protoBytes = new ProtocolBytes(); protoBytes.AddString(protoName); //注册成功返回0,失败返回-1 if (DataHelper.Instance.Register(id, pw)) { protoBytes.AddInt(0); } else { protoBytes.AddInt(-1); } //创建角色 DataHelper.Instance.CreatePlayer(id); //返回协议给客户端 conn.Send(protoBytes); //测试用 //Console.WriteLine(protoBytes.GetDesc()); }
/// <summary> /// 开始战斗 /// </summary> /// <param name="player"></param> /// <param name="protoBase"></param> 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(); }
//注册 //协议参数:str用户名,str密码 //返回协议:-1表示失败 0表示成功 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 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 strFormat = "【收到登陆协议】" + conn.GetAdress(); Console.WriteLine(strFormat + " 用户名:" + id); ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("Login"); ProtocolBytes protocolLogout = new ProtocolBytes(); protocolLogout.AddString("Logout"); if (!Player.KickOff(id, protocolLogout)) { protocolRet.AddInt(-1); conn.Send(protocolRet); return; } conn.player = new Player(id, conn); ServNet.instance.handlePlayerEvent.OnLogin(conn.player); protocolRet.AddInt(0); conn.Send(protocolRet); return; }
//开始战斗,可用, room.StartGame()开始战斗 //发送协议: StartGame, int 0/1 public void MsgStartGame(Player player, ProtocolBase protoBase) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("StartGame"); if (!player.tempData.isOwner) { Logger.Default.Info("MsgStartGame owner err " + player.id); protocol.AddInt(-1); player.Send(protocol); return; } Room room = player.tempData.room; if (!room.CanStart()) { Logger.Default.Info("MsgStartGame CanStart err " + player.id); protocol.AddInt(-1); player.Send(protocol); return; } //开始战斗 protocol.AddInt(0); player.Send(protocol); room.EnterGame(); }
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); } }
//登录 //协议参数: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 strFormat = "[收到请求匹配协议]" + conn.GetAdress(); Console.WriteLine(strFormat + " 用户名:" + id); //构建返回协议 ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("Login"); //是否已经请求匹配 ProtocolBytes protocolLogout = new ProtocolBytes(); protocolLogout.AddString("Logout"); if (!Player.KickOff(id, protocolLogout)) { protocolRet.AddInt(-1); conn.Send(protocolRet); return; } conn.player = new Player(id, conn); //返回 protocolRet.AddInt(0); conn.Send(protocolRet); return; }
/// <summary> /// 登陆消息处理 /// </summary> /// <param name="conn"></param> /// <param name="protoBase"></param> public void MsgLogin(Conn conn, ProtocolBase protoBase) { //开始读取的位置 int start = 0; ProtocolBytes protoBytes = (ProtocolBytes)protoBase; string protoName = protoBytes.GetString(start, ref start); string id = protoBytes.GetString(start, ref start); string pw = protoBytes.GetString(start, ref start); //构建返回协议 protoBytes = new ProtocolBytes(); protoBytes.AddString(protoName); //验证密码 if (!DataHelper.Instance.LoginCheck(id, pw)) { protoBytes.AddInt(-1); conn.Send(protoBytes); return; } //将在线的该玩家踢下线 ProtocolBytes protoLogout = new ProtocolBytes(); protoLogout.AddString("Logout"); //如果踢下线失败 if (!Player.KickOff(id, protoLogout)) { protoBytes.AddInt(-1); conn.Send(protoBytes); return; } //获取玩家数据 PlayerData playerData = DataHelper.Instance.GetPlayerData(id); //获取数据失败 if (playerData == null) { protoBytes.AddInt(-1); conn.Send(protoBytes); return; } //conn与player互有 conn.player = new Player(id, conn); conn.player.data = playerData; //触发登陆事件 ServNet.Instance.handlePlayerEvent.OnLogin(conn.player); //登陆成功返回 protoBytes.AddInt(1); conn.Send(protoBytes); //测试用 //Console.WriteLine(protoBytes.GetDesc()); return; }
// 排行榜插入 // 协议参数:无 // 返回协议:-1表示失败,0表示成功 public void MsgInsertLeaderBoard(Conn conn, ProtocolBase protoBase) { ProtocolBytes protocol = (ProtocolBytes)protoBase; string strFormat = "[收到请求插入排行榜协议] " + conn.GetAdress(); Console.WriteLine(strFormat); // 构建返回协议 ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("InsertLeaderBoard"); // 获取数值 int start = 0; string protoName = protocol.GetString(start, ref start); string time = protocol.GetString(start, ref start); // 插入操作 bool flag = DataMgr.instance.SaveRank(time); // 返回 if (flag) { protocolRet.AddInt(0); } else { protocolRet.AddInt(-1); } // 发送 conn.Send(protocolRet); }
//重置密码 public void MsgReset(Conn conn, ProtocolBase protoBase) { //重置数值 int start = 0; ProtocolBytes protocol = (ProtocolBytes)protoBase; string protoName = protocol.GetString(start, ref start); //解码协议名 string pw = protocol.GetString(start, ref start); //解码新密码 string strFormat = "[收到重置密码协议]" + conn.GetAdress(); Console.WriteLine(strFormat + " 密码:" + pw);//打印新密码 //构建返回协议 protocol = new ProtocolBytes(); protocol.AddString("Reset"); //添加协议名 //重置密码 if (DataMgr.instance.Reset(pw)) //修改数据库中的用户密码并返回结果 { protocol.AddInt(0); //添加成功信息 } else { protocol.AddInt(-1);//添加失败信息 } //返回协议给客户端 conn.Send(protocol); }
//注册 //协议参数:str用户名,str密码 //返回协议:-1表示失败 0表示成功 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 sex = protocol.GetString(start, ref start); string adr = protocol.GetString(start, ref start); string que = protocol.GetString(start, ref start); string ans = protocol.GetString(start, ref start); string phone = protocol.GetString(start, ref start); string strFormat = "[收到注册协议]" + conn.GetAdress(); Console.WriteLine(strFormat + " 用户名:" + id + " 密码:" + pw + " 性别:" + sex + " 住址:" + adr + " 密保问题:" + que + " 密保答案:" + phone + " 联系方式:" + phone); //构建返回协议 protocol = new ProtocolBytes(); protocol.AddString("Register"); //注册 if (DataMgr.instance.Register(id, pw, sex, adr, que, ans, phone)) { protocol.AddInt(0); } else { protocol.AddInt(-1); } //创建角色 DataMgr.instance.CreatePlayer(id); //返回协议给客户端 conn.Send(protocol); }
public void SendPlayerTurn(int forward, bool stand, Vector3 pos) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("PlayerTurn"); protocol.AddString(playerid); protocol.AddInt(forward); protocol.AddBool(stand); protocol.AddFloat(pos.x); protocol.AddFloat(pos.y); protocol.AddFloat(pos.z); ProtocolBytes UDPprotocol = new ProtocolBytes(); UDPprotocol.AddInt(conn_id); UDPprotocol.AddString("U"); UDPprotocol.AddString(playerid); UDPprotocol.AddInt(forward); UDPprotocol.AddBool(stand); UDPprotocol.AddFloat(pos.x); UDPprotocol.AddFloat(pos.y); UDPprotocol.AddFloat(pos.z); UDPSend(UDPprotocol); Send(protocol); }
//离开房间 public void MsgLeaveRoom(Player player, ProtocolBase protoBase) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("LeaveRoom"); //条件检测 if (player.tempData.status != PlayerTempData.Status.Room) { Console.WriteLine("MsgLeaveRoom status err " + player.id); protocol.AddInt(-1); player.Send(protocol); return; } //处理 protocol.AddInt(0); player.Send(protocol); Room room = player.tempData.room; RoomMgr.instance.LeaveRoom(player); //广播 if (room != null) { room.Broadcast(room.GetRoomInfo()); } }
/// <summary> /// 获得物品同步,监听获得物品事件 /// </summary> /// <param name="characterMono"></param> /// <param name="itemGrid"></param> public void GetItemSynchronize(CharacterMono characterMono, ItemGrid itemGrid) { if (itemGrid.item == null) { return; } // 获得物品ID int itemId = itemGrid.item.ItemId; // 获得用户名 string userName = NetWorkManager.Instance.NowPlayerID; // 构造获得物品协议 ProtocolBytes protocolBytes = new ProtocolBytes(); // 协议名 protocolBytes.AddString("AddItem"); // 参数 protocolBytes.AddString(userName); protocolBytes.AddInt(itemId); protocolBytes.AddInt(itemGrid.ItemCount); // 发送协议 NetWorkManager.Instance.Send(protocolBytes); }
public void MsgSetMyBattleCardList(Player player, ProtocolBase protoBase) { int start = 0; ProtocolBytes protocol = (ProtocolBytes)protoBase; string protoName = protocol.GetString(start, ref start); List <int> MyPlayerBattleCardList = protocol.GetIntList(start, ref start); bool isScuss = DataMgr.instance.SetBattleCardList(player.id, MyPlayerBattleCardList); Console.WriteLine("MyPlayerBattleCardList :" + MyPlayerBattleCardList.Count); ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("SetMyBattleCardList"); if (isScuss) { protocolRet.AddInt(0); player.data.BattleCardList = MyPlayerBattleCardList; } else { protocolRet.AddInt(-1); } protocolRet.AddIntList(MyPlayerBattleCardList); player.conn.Send(protocolRet); }
//获取展厅列表 public ProtocolBytes GetRoomList(Player player) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("GetRoomList"); int count = 0; //初始化展厅数量 //展厅临时列表 Dictionary <string, Room> templist = new Dictionary <string, Room>(); //遍历字典添加每个展厅信息 foreach (Room room in list.Values) { //判断是否为用户当前选择的展厅 if (player.tempData.mapadress == room.Adress) { count++; templist.Add(room.Name, room); } } //添加展厅数量 protocol.AddInt(count); //遍历字典添加每个展厅信息 foreach (Room room in templist.Values) { protocol.AddString(room.Name); //展厅名称 protocol.AddInt(room.list.Count); //目前展厅中的人数 protocol.AddString(room.Author); //展厅作者 } return(protocol); //返回协议 }
//获取用户选择展厅的资源信息列表 public void MsgGetResoureList(Player player, ProtocolBase protoBase) { ProtocolBytes protocol = (ProtocolBytes)protoBase; int start = 0; string protoName = protocol.GetString(start, ref start); string RoomName = protocol.GetString(start, ref start);//展厅名称 ProtocolBytes protoRet = new ProtocolBytes(); protoRet.AddString("GetResoureList"); protoRet.AddString(RoomName); //判断是否存在该展厅 if (!RoomMgr.instance.list.ContainsKey(RoomName)) { protoRet.AddInt(-1);//不存在,返回失败信息 player.Send(protoRet); return; } //根据展厅名称获取展厅实体类 Room room = RoomMgr.instance.list[RoomName]; protoRet.AddInt(room.resouredata.Count);//添加展厅中资源数量 //遍历展厅中的资源 foreach (string str in room.resouredata.Keys) { protoRet.AddString(str); //添加资源名称 protoRet.AddString(room.resouredata[str].resouresort); //添加资源类型 } player.Send(protoRet); //返回资源列表给用户 Console.WriteLine("MsgGetResoureList Ok " + player.id); //打印 }
//增加资源 //协议参数: public void MsgAddResoure(Player player, ProtocolBase protoBase) { //获取数值 int start = 0; ProtocolBytes protocol = (ProtocolBytes)protoBase; string protoName = protocol.GetString(start, ref start); string RoomName = protocol.GetString(start, ref start); string ResoureName = protocol.GetString(start, ref start); string ResoureIns = protocol.GetString(start, ref start); string ResoureSort = protocol.GetString(start, ref start); string ResoureAdress = protocol.GetString(start, ref start); ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("AddResoure"); //判断是否存在该房间 if (!RoomMgr.instance.list.ContainsKey(RoomName)) { protocolRet.AddInt(-1); player.Send(protocolRet); return; } //判断资源名称是否存重复 if (RoomMgr.instance.list[RoomName].resouredata.ContainsKey(ResoureName)) { protocolRet.AddInt(-1); player.Send(protocolRet); return; } Room room = RoomMgr.instance.list[RoomName]; Resoure resoure = new Resoure(); resoure.resourename = ResoureName; resoure.resoureins = ResoureIns; resoure.resouresort = ResoureSort; resoure.resoureadress = ResoureAdress; room.resouredata.Add(ResoureName, resoure); player.tempData.tempadress = ResoureAdress; RoomMgr.instance.ReFlashPlayData(player, ResoureSort, 1); //处理 protocolRet.AddInt(0); protocolRet.AddString(ResoureAdress); player.Send(protocolRet); Console.WriteLine("MsgAddResoure " + player.id); //判断资源是否上传完成 //Thread t = new Thread(new ParameterizedThreadStart(Judge)); //t.Start(player); //ProtocolBytes protocolRet1 = new ProtocolBytes(); //protocolRet1.AddString("UpLoadCompleted"); ////Player player = (Player)obj; //while (true) //{ // //Thread.Sleep(200);//休眠 // if (File.Exists(@"C:" + player.tempData.tempadress)) break; //} //player.Send(protocolRet1); }
//登录 //协议参数: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.GetAddress(); WbeService.instance.WriteLine(strFormat + " 用户名:" + id + " 密码:" + pw); //构建返回协议 ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("Login"); //验证 if (!DataMgr.instance.CheckPassword(id, pw)) { protocolRet.AddInt(-1); Console.WriteLine("-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.GetPlayerData(id); //Console.WriteLine(playerData.name); if (playerData == null) { protocolRet.AddInt(-1); Console.WriteLine("-2"); conn.Send(protocolRet); return; } conn.player = new Player(id, conn); conn.player.data = playerData; //事件触发 ServiceNet.instance.handlePlayerEvent.OnLogin(conn.player); //返回 protocolRet.AddInt(0); Console.WriteLine("0"); conn.Send(protocolRet); return; }
/// <summary> /// 获取玩家战绩查询 /// </summary> /// <param name="player"></param> /// <param name="protoBase"></param> 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); }
public void SendID(Conn conn) { ProtocolBytes proto = new ProtocolBytes(); proto.AddString("ID"); proto.AddInt(conn.idinroom); proto.AddInt(conn.roomid); proto.AddInt(conn.id); Send(conn, proto); }
public void MsgGetNumber(Player player, ProtocolBase protoBase) { ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("GetNumber"); protocolRet.AddInt(player.number.maigcNum); protocolRet.AddInt(player.number.DiamondNum); player.Send(protocolRet); Console.WriteLine("MsgGetNumber" + player.id + player.number.maigcNum + player.number.DiamondNum); }
//获取玩家信息 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); }
//获取玩家信息 public void MsgGetAchieve(Player player, ProtocolBase protoBase) { ProtocolBytes protocolRet = new ProtocolBytes(); protocolRet.AddString("GetAchieve"); protocolRet.AddInt(player.data.picturecount); protocolRet.AddInt(player.data.videocount); protocolRet.AddInt(player.data.modelcount); player.Send(protocolRet); Console.WriteLine("MsgGetAchieve " + player.id); }
public void SendHit(string id, int isPlayer, int damage) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("Hit"); protocol.AddString(id); protocol.AddInt(isPlayer); protocol.AddInt(damage); Send(protocol); }
public override void OnShowing() { base.OnShowing(); //Transform Transform skinTrans = skin.transform; Transform listTrans = skinTrans.Find("ListImage"); Transform winTrans = skinTrans.Find("WinImage"); //获取成绩栏部件 idText = winTrans.Find("IDText").GetComponent <Text>(); winText = winTrans.Find("WinText").GetComponent <Text> (); lostText = winTrans.Find("LostText").GetComponent <Text> (); //获取列表栏部件 Transform scroolRect = listTrans.Find("ScrollRect"); content = scroolRect.Find("Content"); roomPrefab = content.Find("RoomPrefab").gameObject; roomPrefab.SetActive(false); closeBtn = listTrans.Find("CloseBtn").GetComponent <Button> (); newBtn = listTrans.Find("NewBtn").GetComponent <Button> (); reflashBtn = listTrans.Find("ReflashBtn").GetComponent <Button> (); reflashBtn.onClick.AddListener(OnReflashClick); newBtn.onClick.AddListener(OnNewClick); closeBtn.onClick.AddListener(OnCloseClick); NetMgr.srvConn.msgDist.AddListener("GetAchieve", RecvGetAchieve); NetMgr.srvConn.msgDist.AddListener("GetRoomList", RecvGetRoomList); ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("GetRoomList"); NetMgr.srvConn.Send(protocol); protocol = new ProtocolBytes(); protocol.AddString("GetAchieve"); NetMgr.srvConn.Send(protocol); ProtocolBytes a = new ProtocolBytes(); a.AddString("GetRoomList"); a.AddInt(2); a.AddInt(2); a.AddInt(1); a.AddInt(4); a.AddInt(2); RecvGetRoomList(a); }
/// <summary> /// 构造伤害协议 /// </summary> /// <param name="attacker"></param> /// <param name="victim"></param> /// <param name="baseDamage"></param> /// <param name="plusDamage"></param> private ProtocolBytes CreateDamageProtocol(string attacker, string victim, int baseDamage, int plusDamage) { ProtocolBytes protocol = new ProtocolBytes(); // 协议名 protocol.AddString("Damage"); // 参数 protocol.AddString(attacker); protocol.AddString(victim); protocol.AddInt(baseDamage); protocol.AddInt(plusDamage); return(protocol); }
public void DelPlayer(string id, int status) { lock (list) { if (!list.ContainsKey(id)) { return; } bool isOwner = list[id].tempData.isOwner; list[id].tempData.status = PlayerTempData.Status.None; list[id].tempData.isOwner = false; if (status == 1) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("BeKicked"); protocol.AddInt(0); list[id].Send(protocol); } else if (status == 2) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("BeKicked"); protocol.AddInt(1); list[id].Send(protocol); } list.Remove(id); if (isOwner) { UpdateOwner(); } } }
//判断用户是否含有已创建的房间 public void MsgHaveRoom(Player player, ProtocolBase protoBase) { ProtocolBytes protocol = new ProtocolBytes(); protocol.AddString("HaveRoom"); if (RoomMgr.instance.HaveRoom(player)) { protocol.AddInt(0); } else { protocol.AddInt(-1); } player.Send(protocol); Console.WriteLine("MsgHaveRoom" + player.id); }
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); }