Beispiel #1
0
        private async Task TestRequestResponse(MultiPlayerServer server, ServerInfo serverInfo, MultiPlayerClient client)
        {
            await server.OpenForNewConnections().AsAwaitable();

            Console.WriteLine("server is listening");
            await client.Connect(serverInfo).AsAwaitable();


            try
            {
                var sw       = Stopwatch.StartNew();
                var response = await client.SendRequest(new PingMessage(), timeout : TimeSpan.FromDays(1)).AsAwaitable();

                sw.Stop();
                Console.WriteLine("ping took " + sw.ElapsedMilliseconds + " ms");
            }
            catch (Exception ex)
            {
                throw;
            }



            try
            {
                await client.SendRequest(new PingMessage()
                {
                    Delay = 300
                }, timeout : TimeSpan.FromSeconds(.1)).AsAwaitable();

                Assert.Fail("A timeout exception should have been thrown");
            }
            catch (PromiseWaitException ex)
            {
                Assert.AreEqual(1, ex.InnerExceptions.Count);
                Assert.IsTrue(ex.InnerException is TimeoutException);
            }

            client.Dispose();
            server.Dispose();
        }
        private async Task ConnectToRemoteServer(ServerInfo serverInfo)
        {
            var delayedConnectingDialog = new DelayedWaitingPrompt();
            var connectingDialogTask    = ShowConnectingDialogDelayed(serverInfo, delayedConnectingDialog);
            Task <StartGameMessage> gameStartSignal;

            try
            {
                client          = InitializeClient();
                gameStartSignal = client.EventRouter.GetAwaitable <StartGameMessage>();
                await client.Connect(serverInfo).AsAwaitable();

                delayedConnectingDialog.PromptIsStillNeeded = false;
                await connectingDialogTask;
                await client.SendRequest(new UserInfoMessage()
                {
                    DisplayName = Environment.UserName
                }).AsAwaitable();
            }
            catch (Exception ex)
            {
                QueueAction(() => Dialog.ShowMessage("Failed to connect".ToRed()).Then(Cleanup));
                return;
            }
            finally
            {
                await delayedConnectingDialog.Cleanup();
            }

            var delayedWaitingForOtherPlayersDialogSignal = new DelayedWaitingPrompt();
            var waitingForOtherPlayersDialogTask          = ShowWaitingMessage("Waiting for other players...".ToConsoleString(), delayedWaitingForOtherPlayersDialogSignal);

            // If we got here then we are connected. Now we wait for the game to start
            var gameStartInfo = await gameStartSignal;

            delayedWaitingForOtherPlayersDialogSignal.PromptIsStillNeeded = false;
            await waitingForOtherPlayersDialogTask;
            await delayedWaitingForOtherPlayersDialogSignal.Cleanup();

            await RevealPlayers(gameStartInfo);

            QueueAction(() => ScenePanel.IsVisible = true);
            Sound.Play("Reload");
            var bgMusicHandle = await Sound.Play("BackgroundMusic").AsAwaitable();

            gameLifetime.OnDisposed(bgMusicHandle.Dispose);
            // wait for the game to end
            var gameOverMessage = await client.EventRouter.GetAwaitable <GameOverMessage>();

            ShowWinnerAndCleanup(gameOverMessage);
        }
Beispiel #3
0
        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);
        }