public void OnSyncNewPlayer(MyGameClient peer, OperationRequest operationRequest, SendParameters sendParameters) { Dictionary <byte, object> dictionary = operationRequest.Parameters; object o_name; dictionary.TryGetValue((byte)DataType.PlayerName, out o_name); //先进行一个防止重复的操作 foreach (MyGameClient cp in MyGameServer.instance.peerlist) { if (peer.ClientName == o_name.ToString()) { return; } } peer.ClientName = o_name.ToString(); //取得所有在线玩家的用户名 List <string> usernameList = new List <string>(); foreach (MyGameClient cp in MyGameServer.instance.peerlist) { //如果连接过来的客户端已经登录了并且这个客户端不是当前客户端 if (!string.IsNullOrEmpty(cp.ClientName) && cp != peer) { //把客户端的用户添加到集合里面 usernameList.Add(cp.ClientName); } } //通过xml序列化进行数据传输,传输给客户端 StringWriter sw = new StringWriter(); XmlSerializer serializer = new XmlSerializer(typeof(List <string>)); serializer.Serialize(sw, usernameList); sw.Close(); string usernameListString = sw.ToString(); //告诉当前客户端其他客户端的名字 Dictionary <byte, object> data = new Dictionary <byte, object>(); data.Add((byte)DataType.PlayerName, usernameListString); OperationResponse response = new OperationResponse((byte)OperationCode.SyncPlayer); response.Parameters = data; peer.SendOperationResponse(response, sendParameters); //告诉其他客户端有新的客户端加入 foreach (MyGameClient cp in MyGameServer.instance.peerlist) { if (!string.IsNullOrEmpty(cp.ClientName) && cp != peer) { EventData ed = new EventData((byte)EventCode.NewPlayer); Dictionary <byte, object> newplayerData = new Dictionary <byte, object>(); newplayerData.Add((byte)DataType.PlayerName, peer.ClientName); //把新加入的用户名传递给其他客户端 ed.Parameters = newplayerData; cp.SendEvent(ed, sendParameters); //发送事件 } } }
static void Main(string[] args) { using (MyGameClient gc = new MyGameClient()) { Console.WriteLine("Ask a question"); string answer = gc.GetAnswer(Console.ReadLine()); Console.WriteLine(answer); } }
static void Main(string[] args) { // MEX - metadata exchange - point using (MyGameClient client = new MyGameClient()) { Console.WriteLine("Put your question"); string answer = client.GetAnswer(Console.ReadLine()); Console.WriteLine(answer); } }
public void OnSyncJoinRoom(MyGameClient peer, OperationRequest operationRequest, SendParameters sendParameters) { int PlayerNum = MyGameServer.instance.peerlist.Count; Dictionary <byte, object> dictionary = new Dictionary <byte, object>(); dictionary.Add((byte)DataType.PlayerNum, PlayerNum); OperationResponse response = new OperationResponse((byte)OperationCode.SyncJoinRoom); response.Parameters = dictionary; peer.SendOperationResponse(response, sendParameters); }
public void OnSyncRotationRecevied(MyGameClient peer, OperationRequest operationRequest, SendParameters sendParameters) { Dictionary <byte, object> dictionary = operationRequest.Parameters; object o_rx, o_ry, o_rz; dictionary.TryGetValue((byte)DataType.RX, out o_rx); dictionary.TryGetValue((byte)DataType.RY, out o_ry); dictionary.TryGetValue((byte)DataType.RZ, out o_rz); peer.rx = float.Parse(o_rx.ToString()); peer.ry = float.Parse(o_ry.ToString()); peer.rz = float.Parse(o_rz.ToString()); }
public void OnSyncPositionRecevied(MyGameClient peer, OperationRequest operationRequest, SendParameters sendParameters) { Dictionary <byte, object> data = operationRequest.Parameters; object o_x, o_y, o_z; data.TryGetValue((byte)DataType.X, out o_x); data.TryGetValue((byte)DataType.Y, out o_y); data.TryGetValue((byte)DataType.Z, out o_z); peer.x = float.Parse(o_x.ToString()); peer.y = float.Parse(o_y.ToString()); peer.z = float.Parse(o_z.ToString()); }
public void OnSyncPlayerLeaveHandler(MyGameClient peer, OperationRequest operationRequest, SendParameters sendParameters) { Dictionary <byte, object> dictionary = operationRequest.Parameters; object o_name; dictionary.TryGetValue((byte)DataType.PlayerName, out o_name); foreach (MyGameClient cp in MyGameServer.instance.peerlist) { if (!string.IsNullOrEmpty(cp.ClientName) && cp != peer) { EventData ed = new EventData((byte)EventCode.SyncPlayerLeave); Dictionary <byte, object> playerLeave = new Dictionary <byte, object>(); playerLeave.Add((byte)DataType.PlayerName, o_name); ed.Parameters = playerLeave; cp.SendEvent(ed, sendParameters); } } }
public void OnSyncDeathHandler(MyGameClient peer, OperationRequest operationRequest, SendParameters sendParameters) { Dictionary <byte, object> dictionary = operationRequest.Parameters; object o_username, o_killer; dictionary.TryGetValue((byte)DataType.PlayerName, out o_username); dictionary.TryGetValue((byte)DataType.Killers, out o_killer); foreach (MyGameClient cp in MyGameServer.instance.peerlist) { if (!string.IsNullOrEmpty(cp.ClientName)) { EventData ed = new EventData((byte)EventCode.SyncDeath); Dictionary <byte, object> eventdictionary = new Dictionary <byte, object>(); eventdictionary.Add((byte)DataType.PlayerName, o_username.ToString()); eventdictionary.Add((byte)DataType.Killers, o_killer.ToString()); ed.Parameters = eventdictionary; cp.SendEvent(ed, sendParameters); } } }
public void OnSyncHealthHandler(MyGameClient peer, OperationRequest operationRequest, SendParameters sendParameters) { Dictionary <byte, object> dictionary = operationRequest.Parameters; object o_username, o_damage; dictionary.TryGetValue((byte)DataType.Damage, out o_damage); dictionary.TryGetValue((byte)DataType.PlayerName, out o_username); //在当前在线用户列表找到受伤害的玩家 并发送伤害信息 foreach (MyGameClient cp in MyGameServer.instance.peerlist) { if (cp.ClientName == o_username.ToString()) { MyGameServer.instance.Display(o_damage.ToString()); Dictionary <byte, object> peerdictionary = new Dictionary <byte, object>(); peerdictionary.Add((byte)DataType.Damage, o_damage.ToString()); peerdictionary.Add((byte)DataType.PlayerName, peer.ClientName.ToString()); OperationResponse response = new OperationResponse((byte)OperationCode.SyncHealth); response.Parameters = peerdictionary; cp.SendOperationResponse(response, new SendParameters()); break; } } }