public Player(int id, Team team, NetworkUser networkUser) { passed = false; this.id = id; cards = new List <Card>(); this.team = team; wonTricks = new List <Card>(); score = 0; this.networkUser = networkUser; }
public void SendPrivateTest() { Thread serverThread = new Thread(() => { try { Server.Network network = new Server.Network("127.0.0.1", 5009); NetworkUser user = null; while (user == null) { user = Server.Network.AddUser(); } Player player = new Player(0, Team.FIRST, user); Network.SendPrivate(player, "this is a test"); } catch (Exception) { Assert.False(true); } }); Thread clientThread = new Thread(() => { try { Server.NetworkUser server = new Server.NetworkUser((new TcpClient("127.0.0.1", 5009)).Client); String str = server.GetReader().ReadLine(); Assert.Equal(str, "this is a test"); } catch (Exception) { Assert.False(true); } }); try { serverThread.Start(); Thread.Sleep(5000); clientThread.Start(); } catch (UserQuit) { Console.WriteLine("Test ok"); } catch (Exception) { Console.WriteLine("Something happened..."); } }
public void AddUserTest() { Thread serverThread = new Thread(() => { try { Server.Network network = new Server.Network("127.0.0.1", 5006); NetworkUser user = null; while (user == null) { user = Server.Network.AddUser(); } Assert.NotNull(user); } catch (Exception) { Assert.False(true); } }); Thread clientThread = new Thread(() => { try { Server.NetworkUser server = new Server.NetworkUser((new TcpClient("127.0.0.1", 5006)).Client); } catch (Exception) { Assert.False(true); } }); try { serverThread.Start(); Thread.Sleep(5000); clientThread.Start(); } catch (UserQuit) { Console.WriteLine("Test ok"); } catch (Exception) { Console.WriteLine("Something happened..."); } }
public void Run() { int gamesId = 0; do { while (!Console.KeyAvailable) { if (waiters.Count < 4) { waiters = Network.SendNetworks(waiters, "alive"); NetworkUser user = Network.AddUser(); if (user != null) { waiters.Add(user); } else { Thread.Sleep(100); continue; } waiters = Network.SendNetworks(waiters, "A new user came!"); } if (waiters.Count >= 4) { List <NetworkUser> temp = GameLaunching(); if (temp.Count != 4) { temp = Network.SendNetworks(temp, "Somebody quitted during user collection..."); foreach (NetworkUser n in temp) { if (n.IsAlive()) { waiters.Add(n); } } temp.Clear(); continue; } Game game = null; try { gamesId += 1; game = new Game(gamesId, temp); } catch (UserQuit) { temp = Network.SendNetworks(temp, "Somebody quitted during game creation..."); foreach (NetworkUser n in temp) { if (n.IsAlive()) { waiters.Add(n); } } temp.Clear(); continue; } Thread thread = new Thread( () => { try { temp = Network.SendNetworks(temp, "Begining of the game!"); try { temp = game.runGame(); } catch (UserQuit) { temp = Network.SendNetworks(temp, "Somebody quitted during game..."); } if (temp.Count == 0) { Console.WriteLine("Nobody survived from the game..."); } else if (temp.Count == 4) { temp = Network.SendNetworks(temp, "Game over!"); } else { temp = Network.SendNetworks(temp, "Somebody quitted the game... Back to the lobby!"); } foreach (NetworkUser n in temp) { if (n.IsAlive()) { waiters.Add(n); } } temp.Clear(); } catch (Exception) { Console.Error.WriteLine("Something happened..."); } } ); thread.Start(); threads.Add(thread); } } } while (Console.ReadKey(true).Key != ConsoleKey.Escape); try { List <Thread> threads2 = threads; foreach (Thread t in threads2) { if (t.IsAlive) { t.Interrupt(); } } } catch (Exception) { Console.WriteLine("Something happened..."); } }
public void SendNetworksTest() { Thread serverThread = new Thread(() => { try { Server.Network network = new Server.Network("127.0.0.1", 5003); List <NetworkUser> users = new List <NetworkUser>(); NetworkUser user = null; while (user == null) { user = Server.Network.AddUser(); } users.Add(user); user = null; while (user == null) { user = Server.Network.AddUser(); } users.Add(user); Network.SendNetworks(users, "this is a test"); } catch (Exception) { Assert.False(true); } }); Thread clientThread = new Thread(() => { try { Server.NetworkUser server = new Server.NetworkUser((new TcpClient("127.0.0.1", 5003)).Client); Assert.Equal(server.GetReader().ReadLine(), "this is a test"); } catch (Exception) { Assert.False(true); } }); Thread clientThread2 = new Thread(() => { try { Server.NetworkUser server = new Server.NetworkUser((new TcpClient("127.0.0.1", 5003)).Client); Assert.Equal(server.GetReader().ReadLine(), "this is a test"); } catch (Exception) { Assert.False(true); } }); try { serverThread.Start(); Thread.Sleep(5000); clientThread.Start(); clientThread2.Start(); } catch (UserQuit) { Console.WriteLine("Test ok"); } catch (Exception) { Console.WriteLine("Something happened..."); } }
public void setNetworkUser(NetworkUser networkUser) { this.networkUser = networkUser; }