// See https://aka.ms/new-console-template for more information using Lobbies; using LobbyClientTest; using LobbyServerDto; using System.Net; Console.WriteLine("Starting lobby client!"); var lobbyClient = new LobbyClient(); var cancellationTokenSource = new CancellationTokenSource(); List openLobbies = new List(); lobbyClient.Connect("localhost", 8088, cancellationTokenSource.Token); FakeGameHost fakeGameHost = new FakeGameHost(); int myPort = fakeGameHost.Server(0); IPEndPoint? hostInfo = null; bool running = true; _ = Task.Run(() => { while (running) { foreach (var lobbyEvent in lobbyClient.ReadEvents(20)) { switch (lobbyEvent.EventType) { case LobbyClientEventTypes.Connected: { var p = Console.GetCursorPosition(); Console.SetCursorPosition(0, p.Top); Console.WriteLine("Lobby client connected!"); Console.Write(">"); lobbyClient.ObserveLobbies(GameGuids.NFS); } break; case LobbyClientEventTypes.LobbyAdd: case LobbyClientEventTypes.LobbyUpdate: { var lobbyInfo = lobbyEvent.EventData as LobbyInfo; openLobbies.Add(lobbyInfo!); var p = Console.GetCursorPosition(); Console.SetCursorPosition(0, p.Top); Console.WriteLine($"LobbyInfo: {lobbyInfo!.Id}, name: {lobbyInfo.Name}, mode: {lobbyInfo.GameMode}, maxplayercount: {lobbyInfo.MaxPlayerCount}, playercount: {lobbyInfo.PlayerCount}, password: {lobbyInfo.PasswordProtected}"); Console.Write(">"); } break; case LobbyClientEventTypes.LobbyDelete: { var lobbyDelete = lobbyEvent.EventData as LobbyDelete; var existingLobby = openLobbies.FirstOrDefault(l => l.Id == lobbyDelete!.Id); if (existingLobby != null) openLobbies.Remove(existingLobby); var p = Console.GetCursorPosition(); Console.SetCursorPosition(0, p.Top); Console.WriteLine($"LobbyDelete: {lobbyDelete!.Id}"); Console.Write(">"); } break; case LobbyClientEventTypes.Failed: { var reason = lobbyEvent.EventData as LobbyClientDisconnectReason; var p = Console.GetCursorPosition(); Console.SetCursorPosition(0, p.Top); Console.WriteLine($"Lobby connection failed! WasError: {reason!.WasError}, error: {reason.ErrorMessage}"); running = false; } break; case LobbyClientEventTypes.LobbyHostInfo: { var lobbyHostInfo = lobbyEvent.EventData as LobbyHostInfo; var p = Console.GetCursorPosition(); Console.SetCursorPosition(0, p.Top); Console.WriteLine($"Host info for lobby {lobbyHostInfo!.LobbyId} is {lobbyHostInfo.HostIp}:{lobbyHostInfo.HostPort}!"); hostInfo = new IPEndPoint(IPAddress.Parse(lobbyHostInfo.HostIp!), lobbyHostInfo.HostPort); Console.WriteLine($"Requesting nat punch!"); lobbyClient.RequestLobbyNatPunch(lobbyHostInfo.LobbyId, null, null, myPort); Console.Write(">"); } break; case LobbyClientEventTypes.LobbyRequestNatPunch: { var lobbyRequestNatPunch = lobbyEvent.EventData as LobbyRequestNatPunch; var p = Console.GetCursorPosition(); Console.SetCursorPosition(0, p.Top); Console.WriteLine($"Nat punch requested to {lobbyRequestNatPunch!.ClientIp}:{lobbyRequestNatPunch.ClientPort}!"); //NatPuncher.NatPunch(new IPEndPoint(IPAddress.Any, myPort), new IPEndPoint(IPAddress.Parse(lobbyRequestNatPunch.ClientIp!), lobbyRequestNatPunch.ClientPort)); var ep = new IPEndPoint(IPAddress.Parse(lobbyRequestNatPunch.ClientIp!), lobbyRequestNatPunch.ClientPort); for (int z = 0; z < 16; z++) { fakeGameHost.Send(ep, "Nat Falcon Punch!"); } lobbyClient.NotifyLobbyNatPunchDone(lobbyRequestNatPunch.NatPunchId); Console.Write(">"); } break; case LobbyClientEventTypes.LobbyNatPunchDone: { var lobbyNatPunchDone = lobbyEvent.EventData as LobbyNatPunchDone; var p = Console.GetCursorPosition(); Console.SetCursorPosition(0, p.Top); Console.WriteLine($"Nat punch request done!"); Console.WriteLine($"Connecting game client!"); fakeGameHost.Send(hostInfo!, "Hello from Game Client!"); Console.Write(">"); } break; case LobbyClientEventTypes.Disconnected: { var p = Console.GetCursorPosition(); Console.SetCursorPosition(0, p.Top); Console.WriteLine($"Lobby disonnected!"); running = false; } break; } } Thread.Sleep(10); } }); while (running) { Console.Write(">"); var line = Console.ReadLine(); if (line != null) { switch (line) { case "host": { Console.WriteLine("Hosting game ..."); lobbyClient.HostLobby(GameGuids.NFS, "Hallo, Welt!", 1, 8, null, null, myPort); fakeGameHost.isHost = true; } break; case "host stop": { Console.WriteLine("Stop hosting game ..."); lobbyClient.CloseLobby(); fakeGameHost.isHost = false; } break; case "join": { Console.WriteLine("Trying to join first lobby ..."); var firstLobby = openLobbies.FirstOrDefault(); if (firstLobby != null) { lobbyClient.RequestLobbyHostInfo(firstLobby.Id, null); } else { Console.WriteLine("Seeing no open lobby!"); } } break; case "observe": { Console.WriteLine("Observing lobby list ..."); lobbyClient.ObserveLobbies(GameGuids.NFS); } break; case "observe stop": { Console.WriteLine("Stop observing lobby list ..."); lobbyClient.StopObservingLobbies(); } break; case "exit": running = false; break; } } } lobbyClient.Stop(); lobbyClient.Dispose();