This commit is contained in:
Chris Sexton 2019-10-05 12:08:08 -04:00
parent 028ee0bf10
commit 149812b669
3 changed files with 91 additions and 26 deletions

View File

@ -3,12 +3,17 @@
type direction = type direction =
| NS | NS
| EW | EW
| NE
| NW
| SE
| SW
| Corner | Corner
type env = type env =
| Wall of direction | Wall of direction
| Floor | Floor
| Trap | Trap
| Player
let envToString e = let envToString e =
@ -17,10 +22,19 @@ let envToString e =
match d with match d with
| NS -> "|" | NS -> "|"
| EW -> "-" | EW -> "-"
| NE -> "\\"
| NW -> "/"
| SE -> "/"
| SW -> "\\"
| Corner -> "+" | Corner -> "+"
| Floor -> "#" | Floor -> "."
| Trap -> "!" | Trap -> "!"
| Player -> "@"
let rowToString = List.fold (fun r e -> r + envToString e) "" let rowToString = List.fold (fun r e -> r + envToString e) ""
let floorToString = List.fold (fun f r -> f + rowToString r + "\n") "" let floorToString = List.fold (fun f r -> f + rowToString r + "\n") ""
let rowToStringList = List.map (fun e -> envToString e)
let floorToStringLists = List.map rowToStringList

View File

@ -6,6 +6,8 @@ open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Cors.Infrastructure open Microsoft.AspNetCore.Cors.Infrastructure
open Microsoft.AspNetCore.Hosting open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Logging open Microsoft.Extensions.Logging
open Microsoft.AspNetCore.Http
open FSharp.Control.Tasks.V2.ContextInsensitive
open Microsoft.Extensions.DependencyInjection open Microsoft.Extensions.DependencyInjection
open Giraffe open Giraffe
@ -17,7 +19,7 @@ open Game.Game
type Message = type Message =
{ {
Text : string Text: string
} }
// --------------------------------- // ---------------------------------
@ -30,43 +32,60 @@ module Views =
let layout (content: XmlNode list) = let layout (content: XmlNode list) =
html [] [ html [] [
head [] [ head [] [
title [] [ encodedText "Server" ] title [] [ encodedText "Server" ]
link [ _rel "stylesheet" link [ _rel "stylesheet"
_type "text/css" _type "text/css"
_href "/main.css" ] _href "/main.css" ]
script [ _src "//cdnjs.cloudflare.com/ajax/libs/rot.js/0.6.0/rot.js" ] []
script [ _src "//unpkg.com/vue" ] []
script [ _src "//unpkg.com/axios/dist/axios.min.js" ] []
script [ _src "/main.js"; _async ] []
] ]
body [] content body [] content
] ]
let partial () = let partial() =
h1 [] [ encodedText "Server" ] h1 [] [ encodedText "Dis my dumb game" ]
let index (model : Message) = let display() =
div [ _id "game" ] []
let index () =
[ [
partial() partial()
pre [] [ encodedText model.Text ] display()
] |> layout ] |> layout
// --------------------------------- // ---------------------------------
// Web app // Web app
// --------------------------------- // ---------------------------------
let indexHandler (name : string) = let indexHandler (name: string) =
let floor = [ let view = Views.index ()
[ Wall Corner; Wall EW; Wall Corner ];
[ Wall NS; Floor; Wall NS ];
[ Wall Corner; Wall EW; Wall Corner ]
]
let model = { Text = floorToString floor }
let view = Views.index model
htmlView view htmlView view
type mapResponse =
{
view: string
}
let mapHandler() =
let floor = [
[ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ];
[ Wall NS; Floor; Floor; Floor; Wall NS ];
[ Wall NS; Floor; Player; Floor; Wall NS ];
[ Wall NS; Floor; Floor; Floor; Wall NS ];
[ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ]
]
json (floorToString floor)
let webApp = let webApp =
choose [ choose [
GET >=> GET >=>
choose [ choose [
route "/" >=> indexHandler "world" route "/" >=> indexHandler "world"
routef "/hello/%s" indexHandler routef "/hello/%s" indexHandler
route "/map" >=> mapHandler()
] ]
setStatusCode 404 >=> text "Not Found" ] setStatusCode 404 >=> text "Not Found" ]
@ -74,7 +93,7 @@ let webApp =
// Error handler // Error handler
// --------------------------------- // ---------------------------------
let errorHandler (ex : Exception) (logger : ILogger) = let errorHandler (ex: Exception) (logger: ILogger) =
logger.LogError(ex, "An unhandled exception has occurred while executing the request.") logger.LogError(ex, "An unhandled exception has occurred while executing the request.")
clearResponse >=> setStatusCode 500 >=> text ex.Message clearResponse >=> setStatusCode 500 >=> text ex.Message
@ -82,27 +101,27 @@ let errorHandler (ex : Exception) (logger : ILogger) =
// Config and Main // Config and Main
// --------------------------------- // ---------------------------------
let configureCors (builder : CorsPolicyBuilder) = let configureCors (builder: CorsPolicyBuilder) =
builder.WithOrigins("http://localhost:8080") builder.WithOrigins("http://localhost:8080")
.AllowAnyMethod() .AllowAnyMethod()
.AllowAnyHeader() .AllowAnyHeader()
|> ignore |> ignore
let configureApp (app : IApplicationBuilder) = let configureApp (app: IApplicationBuilder) =
let env = app.ApplicationServices.GetService<IHostingEnvironment>() let env = app.ApplicationServices.GetService<IHostingEnvironment>()
(match env.IsDevelopment() with (match env.IsDevelopment() with
| true -> app.UseDeveloperExceptionPage() | true -> app.UseDeveloperExceptionPage()
| false -> app.UseGiraffeErrorHandler errorHandler) | false -> app.UseGiraffeErrorHandler errorHandler)
.UseHttpsRedirection() .UseHttpsRedirection()
.UseCors(configureCors) .UseCors(configureCors)
.UseStaticFiles() .UseStaticFiles()
.UseGiraffe(webApp) .UseGiraffe(webApp)
let configureServices (services : IServiceCollection) = let configureServices (services: IServiceCollection) =
services.AddCors() |> ignore services.AddCors() |> ignore
services.AddGiraffe() |> ignore services.AddGiraffe() |> ignore
let configureLogging (builder : ILoggingBuilder) = let configureLogging (builder: ILoggingBuilder) =
builder.AddFilter(fun l -> l.Equals LogLevel.Error) builder.AddFilter(fun l -> l.Equals LogLevel.Error)
.AddConsole() .AddConsole()
.AddDebug() |> ignore .AddDebug() |> ignore
@ -110,7 +129,7 @@ let configureLogging (builder : ILoggingBuilder) =
[<EntryPoint>] [<EntryPoint>]
let main _ = let main _ =
let contentRoot = Directory.GetCurrentDirectory() let contentRoot = Directory.GetCurrentDirectory()
let webRoot = Path.Combine(contentRoot, "WebRoot") let webRoot = Path.Combine(contentRoot, "WebRoot")
WebHostBuilder() WebHostBuilder()
.UseKestrel() .UseKestrel()
.UseContentRoot(contentRoot) .UseContentRoot(contentRoot)
@ -121,4 +140,4 @@ let main _ =
.ConfigureLogging(configureLogging) .ConfigureLogging(configureLogging)
.Build() .Build()
.Run() .Run()
0 0

View File

@ -0,0 +1,32 @@
let app = new Vue({
el: "#game" ,
data: {
display: null,
board: [],
err: ""
},
mounted() {
let options = {
forceSquareRatio:true
};
this.display = new ROT.Display(options);
document.getElementById("game").appendChild(this.display.getContainer());
this.getData();
},
methods: {
draw: function(board) {
console.log("drawing board:\n", this.board);
// let str = "Using a regular grid\n@....%b{blue}#%b{}##.%b{red}.%b{}.$$$";
this.display.drawText(0, 0, this.board);
},
getData: function() {
axios.get("/map")
.then(resp => {
console.log("Got data:\n", resp);
this.board = resp.data;
this.draw();
})
.catch(err => this.err = err);
}
}
});