refactor a bit
This commit is contained in:
parent
149812b669
commit
f76816fd09
|
@ -14,7 +14,26 @@ type env =
|
||||||
| Floor
|
| Floor
|
||||||
| Trap
|
| Trap
|
||||||
| Player
|
| Player
|
||||||
|
|
||||||
|
type cast = {
|
||||||
|
spell: string
|
||||||
|
direction: direction
|
||||||
|
}
|
||||||
|
type command =
|
||||||
|
| Move of direction
|
||||||
|
| Refresh
|
||||||
|
| Cast of cast
|
||||||
|
|
||||||
|
type location =
|
||||||
|
{
|
||||||
|
x: int
|
||||||
|
y: int
|
||||||
|
}
|
||||||
|
type state =
|
||||||
|
{
|
||||||
|
board: string
|
||||||
|
playerLocation: location
|
||||||
|
}
|
||||||
|
|
||||||
let envToString e =
|
let envToString e =
|
||||||
match e with
|
match e with
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
module Server.Controllers
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
module Server.App
|
module Server.App
|
||||||
|
|
||||||
|
open Views
|
||||||
|
|
||||||
open System
|
open System
|
||||||
open System.IO
|
open System.IO
|
||||||
open Microsoft.AspNetCore.Builder
|
open Microsoft.AspNetCore.Builder
|
||||||
|
@ -7,77 +9,41 @@ 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 Microsoft.AspNetCore.Http
|
||||||
open FSharp.Control.Tasks.V2.ContextInsensitive
|
|
||||||
open Microsoft.Extensions.DependencyInjection
|
open Microsoft.Extensions.DependencyInjection
|
||||||
|
open FSharp.Control.Tasks.V2.ContextInsensitive
|
||||||
open Giraffe
|
open Giraffe
|
||||||
|
|
||||||
open Game.Game
|
open Game.Game
|
||||||
|
|
||||||
// ---------------------------------
|
|
||||||
// Models
|
|
||||||
// ---------------------------------
|
|
||||||
|
|
||||||
type Message =
|
|
||||||
{
|
|
||||||
Text: string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------
|
|
||||||
// Views
|
|
||||||
// ---------------------------------
|
|
||||||
|
|
||||||
module Views =
|
|
||||||
open GiraffeViewEngine
|
|
||||||
|
|
||||||
let layout (content: XmlNode list) =
|
|
||||||
html [] [
|
|
||||||
head [] [
|
|
||||||
title [] [ encodedText "Server" ]
|
|
||||||
link [ _rel "stylesheet"
|
|
||||||
_type "text/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
|
|
||||||
]
|
|
||||||
|
|
||||||
let partial() =
|
|
||||||
h1 [] [ encodedText "Dis my dumb game" ]
|
|
||||||
|
|
||||||
let display() =
|
|
||||||
div [ _id "game" ] []
|
|
||||||
|
|
||||||
let index () =
|
|
||||||
[
|
|
||||||
partial()
|
|
||||||
display()
|
|
||||||
] |> layout
|
|
||||||
|
|
||||||
// ---------------------------------
|
// ---------------------------------
|
||||||
// Web app
|
// Web app
|
||||||
// ---------------------------------
|
// ---------------------------------
|
||||||
|
|
||||||
let indexHandler (name: string) =
|
let indexHandler (name: string) =
|
||||||
let view = Views.index ()
|
let view = Views.index()
|
||||||
htmlView view
|
htmlView view
|
||||||
|
|
||||||
type mapResponse =
|
|
||||||
{
|
|
||||||
view: string
|
|
||||||
}
|
|
||||||
|
|
||||||
let mapHandler() =
|
let mapHandler() =
|
||||||
let floor = [
|
let floor = [
|
||||||
[ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ];
|
[ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ];
|
||||||
[ Wall NS; Floor; Floor; Floor; Wall NS ];
|
[ Wall NS; Floor; Floor; Floor; Wall NS ];
|
||||||
[ Wall NS; Floor; Player; Floor; Wall NS ];
|
[ Wall NS; Floor; Floor; Floor; Wall NS ];
|
||||||
[ Wall NS; Floor; Floor; Floor; Wall NS ];
|
[ Wall NS; Floor; Floor; Floor; Wall NS ];
|
||||||
[ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ]
|
[ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ]
|
||||||
]
|
]
|
||||||
json (floorToString floor)
|
let state = {
|
||||||
|
board = floorToString floor
|
||||||
|
playerLocation = { x = 1; y = 1 }
|
||||||
|
}
|
||||||
|
json state
|
||||||
|
|
||||||
|
type Direction = string
|
||||||
|
|
||||||
|
let moveHandler (next: HttpFunc) (ctx: HttpContext) =
|
||||||
|
task {
|
||||||
|
let! direction = ctx.BindJsonAsync<Direction>()
|
||||||
|
return! (mapHandler()) next ctx
|
||||||
|
}
|
||||||
|
|
||||||
let webApp =
|
let webApp =
|
||||||
choose [
|
choose [
|
||||||
|
@ -87,6 +53,10 @@ let webApp =
|
||||||
routef "/hello/%s" indexHandler
|
routef "/hello/%s" indexHandler
|
||||||
route "/map" >=> mapHandler()
|
route "/map" >=> mapHandler()
|
||||||
]
|
]
|
||||||
|
POST >=>
|
||||||
|
choose [
|
||||||
|
route "/move" >=> moveHandler
|
||||||
|
]
|
||||||
setStatusCode 404 >=> text "Not Found" ]
|
setStatusCode 404 >=> text "Not Found" ]
|
||||||
|
|
||||||
// ---------------------------------
|
// ---------------------------------
|
||||||
|
|
|
@ -15,6 +15,8 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="Views.fs" />
|
||||||
|
<Compile Include="Controllers.fs" />
|
||||||
<Compile Include="Program.fs" />
|
<Compile Include="Program.fs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
namespace Server
|
||||||
|
|
||||||
|
open Giraffe
|
||||||
|
open GiraffeViewEngine
|
||||||
|
|
||||||
|
// ---------------------------------
|
||||||
|
// Views
|
||||||
|
// ---------------------------------
|
||||||
|
|
||||||
|
module Views =
|
||||||
|
let layout (content: XmlNode list) =
|
||||||
|
html [] [
|
||||||
|
head [] [
|
||||||
|
title [] [ encodedText "Server" ]
|
||||||
|
link [ _rel "stylesheet"
|
||||||
|
_type "text/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
|
||||||
|
]
|
||||||
|
|
||||||
|
let partial() =
|
||||||
|
h1 [] [ encodedText "Dis my dumb game" ]
|
||||||
|
|
||||||
|
let display() =
|
||||||
|
div [ _id "app" ] [
|
||||||
|
div [ _id "game" ] []
|
||||||
|
]
|
||||||
|
|
||||||
|
let index() =
|
||||||
|
[
|
||||||
|
partial()
|
||||||
|
display()
|
||||||
|
] |> layout
|
|
@ -1,32 +1,45 @@
|
||||||
let app = new Vue({
|
let app = new Vue({
|
||||||
el: "#game" ,
|
el: "#app",
|
||||||
data: {
|
data: {
|
||||||
display: null,
|
display: null,
|
||||||
board: [],
|
state: {board: ""},
|
||||||
err: ""
|
err: ""
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
let options = {
|
let options = {
|
||||||
forceSquareRatio:true
|
forceSquareRatio: true
|
||||||
};
|
};
|
||||||
this.display = new ROT.Display(options);
|
this.display = new ROT.Display(options);
|
||||||
document.getElementById("game").appendChild(this.display.getContainer());
|
document.getElementById("game").replaceWith(this.display.getContainer());
|
||||||
this.getData();
|
this.getData();
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
draw: function(board) {
|
draw: function (board) {
|
||||||
console.log("drawing board:\n", this.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.state.board);
|
||||||
this.display.drawText(0, 0, this.board);
|
if (this.state.playerLocation) {
|
||||||
|
this.display.drawText(this.state.playerLocation.x, this.state.playerLocation.y, "@")
|
||||||
|
}
|
||||||
},
|
},
|
||||||
getData: function() {
|
getData: function () {
|
||||||
axios.get("/map")
|
axios.get("/map")
|
||||||
.then(resp => {
|
.then(resp => {
|
||||||
console.log("Got data:\n", resp);
|
console.log("Got data:\n", resp);
|
||||||
this.board = resp.data;
|
this.state = resp.data;
|
||||||
this.draw();
|
this.draw();
|
||||||
})
|
})
|
||||||
.catch(err => this.err = err);
|
.catch(err => this.err = err);
|
||||||
|
},
|
||||||
|
// sendCommand will be wired to the various keybinds
|
||||||
|
// each binding should send the game command expected
|
||||||
|
sendCommand: function (cmd) {
|
||||||
|
axios.post("/cmd")
|
||||||
|
.then(resp => {
|
||||||
|
console.log("Got data from cmd:\n", resp)
|
||||||
|
this.board = resp.data;
|
||||||
|
this.draw()
|
||||||
|
})
|
||||||
|
.catch(err => this.err = err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue