Пример #1
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseDefaultFiles();

            app.UseWebSockets();

            // ICI on fonctionne en THREAD !
            app.Use(async(context, next) =>
            {
                Console.WriteLine("New WebSocket connection");
                // ouverture d'une websocket, un nouveau bot se connecte
                if (context.Request.Path == "/bot")
                {
                    Console.WriteLine("WebSocket /bot");
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        //Console.WriteLine("AcceptWebSocketAsync");
                        // on l'ajoute à notre simulation !
                        WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
                        //await Echo(context, webSocket);
                        Console.WriteLine("A new BOT in arena!");
                        // Démarrage d'un nouveau bot. Si on revient c'est qu'il est mort !
                        await MainGame.AddBot(webSocket);
                        Console.WriteLine($"#BOTS: {MainGame.AllBot.Count}");
                    }
                    else
                    {
                        context.Response.StatusCode = 400;
                        Console.WriteLine("WebSocket ERROR : Not a WebSocket establishment request.");
                    }
                }
                if (context.Request.Path == "/display")
                {
                    Console.WriteLine("[SOCKET] WebSocket /display");
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        // on l'ajoute à notre simulation !
                        WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
                        Console.WriteLine("[DISPLAY] New DISPLAY!");
                        await MainGame.AddViewer(webSocket);
                        Console.WriteLine($"[DISPLAY] number= {MainGame.AllViewer.Count}");
                    }
                    else
                    {
                        context.Response.StatusCode = 400;
                        Console.WriteLine("WebSocket ERROR : Not a WebSocket establishment request.");
                    }
                }
                if (context.Request.Path == "/cockpit")
                {
                    Console.WriteLine("WebSocket /cockpit");
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        // on l'ajoute à notre simulation !
                        WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
                        Console.WriteLine("New COCKPIT!");
                        await MainGame.AddCockpit(webSocket);
                        Console.WriteLine($"#COCKPIT: {MainGame.AllCockpit.Count}");
                    }
                    else
                    {
                        context.Response.StatusCode = 400;
                        Console.WriteLine("WebSocket ERROR : Not a WebSocket establishment request.");
                    }
                }

                if (context.Request.Path == "/startsim")
                {
                    Console.WriteLine("WebSocket /startsim");
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        MainGame.RunSimulator();
                        Console.WriteLine($"[SIM]: Start simulation");
                        context.Response.StatusCode = 200;
                    }
                    else
                    {
                        context.Response.StatusCode = 400;
                        Console.WriteLine("WebSocket ERROR : Not a WebSocket establishment request.");
                    }
                }

                if (context.Request.Path == "/stopsim")
                {
                    Console.WriteLine("WebSocket /stopsim");
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        MainGame.StopSimulator();
                        Console.WriteLine($"[SIM]: Stop simulation");
                        context.Response.StatusCode = 200;
                    }
                    else
                    {
                        context.Response.StatusCode = 400;
                        Console.WriteLine("WebSocket ERROR : Not a WebSocket establishment request.");
                    }
                }
                if (context.Request.Path == "/statsim")
                {
                    Console.WriteLine("WebSocket /statsim");
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        MainGame.StopSimulator();
                        Console.WriteLine($"[SIM]: Ask server status ");
                        context.Response.StatusCode = 200;
                        var buffer          = new byte[4];
                        buffer[0]           = MainGame.isRunning();
                        buffer[1]           = (byte)MainGame.AllBot.Count;
                        buffer[2]           = (byte)MainGame.TheMap.Length;
                        buffer[3]           = (byte)MainGame.TheMap.Length;
                        WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
                        await webSocket.SendAsync(new ArraySegment <byte>(buffer, 0, buffer.Length), WebSocketMessageType.Binary, true, CancellationToken.None);
                        await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
                        Console.WriteLine($"[SIM]: Status sent ! ");
                    }

                    else
                    {
                        context.Response.StatusCode = 400;
                        Console.WriteLine("WebSocket ERROR : Not a WebSocket establishment request.");
                    }
                }
            });
        }
Пример #2
0
        static void Main(string[] args)
        {
            var currentDir = Directory.GetCurrentDirectory();
            var theFile    = Path.Combine(currentDir, "settings.json");

            // création du fichier settings.json avec les valeurs par défaut
            if (!File.Exists(theFile))
            {
                MainGame.Settings = new Settings();
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(MainGame.Settings, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText(theFile, json);
            }
            var prm = Newtonsoft.Json.JsonConvert.DeserializeObject <Settings>(File.ReadAllText(theFile));

            MainGame.Settings = prm;
            if (MainGame.Settings.MapName != "")
            {
                MainGame.LoadMap(MainGame.Settings.MapName);
            }
            else
            {
                MainGame.InitNewMap();
            }



            var host = new WebHostBuilder()
                       .UseKestrel()
                       .UseStartup <Startup>()
                       .ConfigureKestrel((context, options) => { options.ListenAnyIP(MainGame.Settings.ServerPort); })
                       .Build();

            host.Start();                     //Start server non-blocking

            ShowHelp();
            bool exit = false;

            while (!exit)
            {
                Console.Write(">");
                var key = Console.ReadKey(true);
                switch (key.KeyChar.ToString().ToLower())
                {
                case "h":
                    ShowHelp();
                    break;

                case "e":
                    Console.WriteLine("Exit program");
                    if (MainGame.AllBot.Count > 0)
                    {
                        Console.WriteLine("Not possible, at least 1 BOT is in arena.");
                    }
                    else
                    {
                        if (MainGame.AllViewer.Count > 0)
                        {
                            Console.WriteLine("Not possible, at least 1 VIEWER is working.");
                        }
                        else
                        {
                            exit = true;
                        }
                    }
                    break;

                case "g":
                    Console.WriteLine("GO!");
                    MainGame.RunSimulator();
                    break;

                case "s":
                    Console.WriteLine("Stop");
                    MainGame.StopSimulator();
                    break;

                case "x":     // debug stuff to view shield
                    foreach (OneBot x in MainGame.AllBot)
                    {
                        x.bot.ShieldLevel++;
                        if (x.bot.ShieldLevel > 10)
                        {
                            x.bot.ShieldLevel = 0;
                        }
                        MainGame.ViewerPlayerShield(x.bot.X, x.bot.Y, x.bot.ShieldLevel);
                    }
                    break;

                case "w":     // debug stuff to view cloak
                    foreach (OneBot x in MainGame.AllBot)
                    {
                        x.bot.CloakLevel++;
                        if (x.bot.CloakLevel > 10)
                        {
                            x.bot.CloakLevel = 0;
                        }
                        MainGame.ViewerPlayerCloak(x.bot.X, x.bot.Y, x.bot.CloakLevel);
                    }
                    break;
                }
            }
            host.StopAsync();
        }