private async Task OrchestrateServerMode() { var numPlayers = await PromptForNumberOfPlayers(); // first initialize the server var socketServer = new SocketServerNetworkProvider(8080); var server = new MultiPlayerServer(socketServer); await QueueAction(() => LayoutRoot.Add(new MultiPlayerServerInfoControl(server) { Width = 50 }).DockToRight().FillVertically()).AsAwaitable(); this.OnDisposed(server.Dispose); var deathmatch = new Deathmatch(new MultiPlayerContestOptions() { MaxPlayers = numPlayers, Server = server }); deathmatch.OrchestrationFailed.SubscribeOnce((ex) => { QueueAction(() => { Dialog.ShowMessage("Fatal error: " + ex.ToString().ToRed()).Then(Cleanup); }); }); await deathmatch.Start(); // then connect to it as a client await ConnectToRemoteServer(socketServer.ServerInfo); }
private async Task TestDeathmatch(MultiPlayerServer server, ServerInfo serverInfo, MultiPlayerClient client1, MultiPlayerClient client2, int delayMs) { Exception undeliverableException = null; Exception deathmatchException = null; server.Undeliverable.SubscribeForLifetime((args) => undeliverableException = args.Exception, server); var deathmatch = new Deathmatch(new MultiPlayerContestOptions() { MaxPlayers = 2, Server = server }); deathmatch.OrchestrationFailed.SubscribeOnce((ex) => deathmatchException = ex); // the game starts await deathmatch.Start(); // both clients start waiting for the start of the game var client1StartTask = client1.EventRouter.GetAwaitable <StartGameMessage>(); var client2StartTask = client2.EventRouter.GetAwaitable <StartGameMessage>(); // both clients get notified of each other's presence var client1SeesClient2Task = client1.EventRouter.GetAwaitable <NewUserMessage>(); var client2SeesClient1Task = client2.EventRouter.GetAwaitable <NewUserMessage>(); // both clients connect, which should trigger the start of the game await client1.Connect(serverInfo).AsAwaitable(); Console.WriteLine("client 1 connected"); await client2.Connect(serverInfo).AsAwaitable(); Console.WriteLine("client 2 connected"); // make sure both clients got the start event await client1StartTask; await client2StartTask; await client1SeesClient2Task; await client2SeesClient1Task; Assert.AreEqual(client2.ClientId, client1SeesClient2Task.Result.NewUserId); Assert.AreEqual(client1.ClientId, client2SeesClient1Task.Result.NewUserId); var client1GameOverTask = client1.EventRouter.GetAwaitable <GameOverMessage>(); var client2GameOverTask = client2.EventRouter.GetAwaitable <GameOverMessage>(); var response = await client1.SendRequest(new DamageMessage() { DamagedClient = client2.ClientId, NewHP = 0 }, timeout : TimeSpan.FromDays(1)).AsAwaitable(); // make sure both clients got the game over event event await Task.WhenAll(client1GameOverTask, client2GameOverTask); Assert.AreEqual(client1.ClientId, client1GameOverTask.Result.WinnerId); Assert.AreEqual(client1.ClientId, client2GameOverTask.Result.WinnerId); client1.Dispose(); client2.Dispose(); server.Dispose(); Assert.IsTrue(deathmatch.IsExpired); Assert.IsNull(undeliverableException); Assert.IsNull(deathmatchException); }