public void InitClient(string key) { var fighter = new StarFighter(Color.Red, new SizeF(10f, 10f), new Vector2F(200, 200), 0); fighter.Init(); gameObjects.Add(key, fighter); Console.WriteLine("sending " + key + " objects"); try { foreach (var obj in gameObjects) { string typeName = obj.Value.GetType().Name.ToString(); Console.WriteLine("sending to " + key + " " + " of type" + typeName + " " + obj.ToString()); var item = ((IGame2DProperties)obj.Value); var dat = key + "," + item.Location.X + "," + item.Location.Y; var type = (GameAction)Enum.Parse(typeof(GameAction), typeName); ServerSide.SendData(type, dat, PlayingClients[key].Ns); } ServerSide.SendCommand(GameAction.Done, PlayingClients[key].Ns); } catch { } PlayingClients[key].status = "loaded"; foreach (var obj in gameObjects) { obj.Value.Init(); } }
public override void Update(GameTime gameTime) { foreach (var client in PlayingClients) { if (client.Value.status == "loaded") { if (client.Value.Ns.DataAvailable) { try { //var sizeData = new byte[4]; //client.Value.Ns.Read(sizeData, 0, 4); //var message = Encoding.ASCII.GetString(sizeData); byte[] b = new byte[sizeof(short)]; client.Value.Ns.Read(b, 0, b.Length); var message = (GameAction)BitConverter.ToInt16(b, 0); // if (String.Compare(message, "Acce") == 0) if (message == GameAction.Accelerate) { ((StarFighter)gameObjects[client.Value.key]).Accelerate(); } if (message == GameAction.Deaccelerate) { ((StarFighter)gameObjects[client.Value.key]).Deaccelerate(); } if (message == GameAction.TurnLeft) { ((StarFighter)gameObjects[client.Value.key]).TurnLeft(); } if (message == GameAction.TurnRight) { ((StarFighter)gameObjects[client.Value.key]).TurnRight(); } } catch { } } } foreach (var obj in gameObjects) { obj.Value.Update(gameTime); } var fighters = gameObjects.Values.OfType <StarFighter>(); if (client.Value.status == "loaded") { foreach (var fighter in fighters) { // client.Value.Ns.Read(new byte[4096], 0, 4096); //short id = 0;// BitConverter.ToInt16(Encoding.ASCII.GetBytes(client.Value.key),0); var datString = client.Value.key + "," + fighter.Angle + "," + fighter.Location.X + "," + fighter.Location.Y; // ServerSide.SendData(GameAction.Update, new short[] { id, (short)fighter.Angle, (short)fighter.Location.X, (short)fighter.Location.Y }, client.Value.Ns); // SendString("upfi", client.Value.key, ); ServerSide.SendData(GameAction.Update, datString, client.Value.Ns); //SendString(client.Value.key, client.Value.key, null); // SendObject("upfi", client.Value.key, client.Value.key + "," + fighter.Angle + "," + fighter.Location.X + "," + fighter.Location.Y); } } } //foreach (var player in gameObjects.Values.OfType<StarFighter>()) //{ // foreach (var bullet in gameObjects.Values.OfType<Projectile>()) // { // var delx = player.Size.Width; // var dely = player.Size.Height; // if (bullet.InBounds(new BoundF(player.Location.X - delx, player.Location.Y - dely, player.Location.X + delx, player.Location.Y + dely))) // { // if (!bullet.ID.Contains(player.ID)) // { // var id = bullet.ID.Split(',').Last(); // bullet.ToBeDestroyed = true; // if (player.IsDead.IsNotTrue()) // { // player.IsDead = true; // scores[id]++; // } // } // } // } //} //var keys = gameObjects.Keys.ToArray<string>(); //for (int i = 0; i < keys.Count<string>(); i++) //{ // var ass = gameObjects[keys[i]]; // if (ass.GetType() == typeof(Asteroid)) // { // // ((IGame2DProperties)ass).Location = ((IGame2DProperties)ass).KeepInBounds(bound).Location; // } //} //var fighters = gameObjects.Values.OfType<StarFighter>(); //foreach (var client in PlayingClients) //{ // if (client.Value.status == "loaded") // { // foreach (var fighter in fighters) // { // SendString("upfi", client.Value.key, client.Value.key + "," + fighter.Angle + "," + fighter.Location.X + "," + fighter.Location.Y); // // SendObject("upfi", client.Value.key, client.Value.key + "," + fighter.Angle + "," + fighter.Location.X + "," + fighter.Location.Y); // } // } //} if (delClient) { var lst = new List <string>(); foreach (var client in PlayingClients) { if (!client.Value.Client.Connected) { lst.Add(client.Key); } } foreach (var key in lst) { PlayingClients.Remove(key); if (gameObjects.ContainsKey(key)) { gameObjects.Remove(key); } } delClient = false; } if (GameInput.isKeyDown(Keys.Escape)) { RunAgain = false; GameWindow.AudioPlayer.Item1.StopTrack("background"); GameWindow.CurrentScene = NextScene; GameWindow.View.Init(); gameObjects.Clear(); } }
public override void Update(GameTime gameTime) { foreach (var client in ServerSide.ConnectClients.ConnectedClients.Values) { if (client.Connected) { try { NetworkStream ns = client.GetStream(); var message = ServerSide.RecvCommand(ns); if (message == GameAction.Ready) { var key = ServerSide.RecvShortString(ns); if (!PlayingClients.ContainsKey(key)) { client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); Console.WriteLine("client address: " + client.Client.RemoteEndPoint.ToString() + "name:" + key + " playing"); PlayingClients.TryAdd(key, new ClientMember(client, key, client.GetStream())); //, sw, sr)); Console.WriteLine("initing " + key + " background transfer"); InitClient(key); var pt = Parallel.ForEach <ClientMember>(PlayingClients.Values, (cl) => { if (fighter.Value != null) { var datString = key + "," + fighter.Value.Angle + "," + fighter.Value.Location.X + "," + fighter.Value.Location.Y + ","; gameObjects.TryAdd(key, fighter.Value); ServerSide.SendData(GameAction.NewPlayer, datString, cl.Ns); } }); while (!pt.IsCompleted) { } break; } } // ns.Close(); } catch { Console.WriteLine("problem"); } } } var p = Parallel.ForEach <ClientMember>(PlayingClients.Values, (client) => { if (client.status == "loaded") { try { client.Ns = client.Client.GetStream(); } catch { } if (client.Ns.DataAvailable) { try { var message = ServerSide.RecvCommand(client.Ns); var key = ServerSide.RecvShortString(client.Ns); if (message == GameAction.Accelerate) { ((StarFighter)gameObjects[key]).Accelerate(); } if (message == GameAction.Deaccelerate) { ((StarFighter)gameObjects[key]).Deaccelerate(); } if (message == GameAction.TurnLeft) { ((StarFighter)gameObjects[key]).TurnLeft(); } if (message == GameAction.TurnRight) { ((StarFighter)gameObjects[key]).TurnRight(); } client.ns.Read(new byte[ClientSide.Client.ReceiveBufferSize], 0, ClientSide.Client.ReceiveBufferSize); } catch { } } var fighters = gameObjects.Keys.OfType <StarFighter>(); foreach (var fighter in fighters) { var datString = fighter.ID + "," + fighter.Angle + "," + fighter.Location.X + "," + fighter.Location.Y + ","; ServerSide.SendData(GameAction.Update, datString, client.Ns); } } }); while (!p.IsCompleted) { } newPlayer = false; foreach (var obj in gameObjects) { obj.Value.Update(gameTime); } //foreach (var player in gameObjects.Values.OfType<StarFighter>()) //{ // foreach (var bullet in gameObjects.Values.OfType<Projectile>()) // { // var delx = player.Size.Width; // var dely = player.Size.Height; // if (bullet.InBounds(new BoundF(player.Location.X - delx, player.Location.Y - dely, player.Location.X + delx, player.Location.Y + dely))) // { // if (!bullet.ID.Contains(player.ID)) // { // var id = bullet.ID.Split(',').Last(); // bullet.ToBeDestroyed = true; // if (player.IsDead.IsNotTrue()) // { // player.IsDead = true; // scores[id]++; // } // } // } // } //} //var keys = gameObjects.Keys.ToArray<string>(); //for (int i = 0; i < keys.Count<string>(); i++) //{ // var ass = gameObjects[keys[i]]; // if (ass.GetType() == typeof(Asteroid)) // { // // ((IGame2DProperties)ass).Location = ((IGame2DProperties)ass).KeepInBounds(bound).Location; // } //} //var fighters = gameObjects.Values.OfType<StarFighter>(); //foreach (var client in PlayingClients) //{ // if (client.Value.status == "loaded") // { // foreach (var fighter in fighters) // { // SendString("upfi", client.Value.key, client.Value.key + "," + fighter.Angle + "," + fighter.Location.X + "," + fighter.Location.Y); // // SendObject("upfi", client.Value.key, client.Value.key + "," + fighter.Angle + "," + fighter.Location.X + "," + fighter.Location.Y); // } // } //} // if (delClient) { var lst = new List <string>(); foreach (var client in PlayingClients) { if (!client.Value.Client.Connected) { lst.Add(client.Key); client.Value.status = "disconnected"; delClient = true; } } if (delClient) { foreach (var key in lst) { var c = new ClientMember(null, "", null); var f = default(IGameBehavior); PlayingClients.TryRemove(key, out c); if (gameObjects.ContainsKey(key)) { gameObjects.TryRemove(key, out f); } } // delClient = false; // tasks[1] = Task.Run(() =>{ for (int i = 0; i < ServerSide.ConnectClients.ConnectedClients.Count; i++) { try { if (!ServerSide.ConnectClients.ConnectedClients[i].Connected) { // disconnect clients var key = ServerSide.ConnectClients.ConnectedClients[i].Client.RemoteEndPoint.ToString(); var t = new TcpClient(); ServerSide.ConnectClients.ConnectedClients.TryRemove(i, out t);//.TryTake(out ServerSide.ConnectClients.ConnectedClients.ElementAt(i) ); i--; Console.WriteLine("client " + key + " disconnected"); } } catch { } } delClient = false; } // Console.WriteLine("ConnectReadyCheck Close.."); // }); } if (GameInput.isKeyDown(Keys.Escape)) { RunAgain = false; GameWindow.AudioPlayer.Item1.StopTrack("background"); GameWindow.CurrentScene = NextScene; GameWindow.View.Init(); gameObjects.Clear(); } }