-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathApp.fs
114 lines (100 loc) · 4.91 KB
/
App.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
module App
open Suave
open Suave.Successful
open Suave.Filters
open Suave.Operators
open System
open System.IO
open ItemRandomizer
type Context =
{
Error: bool;
Info: string;
Mode: string;
}
type TestLocation =
{
Location: string;
Count: int;
}
type TestItem =
{
Item: string;
Locations: TestLocation list;
}
type TestContext =
{
TestItems: TestItem list;
}
let index =
DotLiquid.page "index.html" ()
let information =
DotLiquid.page "information.html" ()
let contact =
DotLiquid.page "contact.html" ()
let testGet =
DotLiquid.page "test.html" ()
let testPost (r:HttpRequest) =
let itemLocations = Randomizer.TestRandomize
let mutable (testItems:TestItem list) = []
for i in List.filter (fun (j:Types.Item) -> j.Class = Types.ItemClass.Major) ItemRandomizer.Items.Items do
let mutable (testLocations:TestLocation list) = []
for l in List.filter (fun (k:Types.Location) -> k.Class = Types.ItemClass.Major) TournamentLocations.AllLocations do
let name = l.Name
let count = List.length (List.filter (fun (f:Types.ItemLocation) -> f.Item.Type = i.Type && f.Location.Address = l.Address) itemLocations)
let newLocation = { Location = name; Count = count }
testLocations <- (newLocation :: testLocations)
let newTestItem = { Item = i.Name; Locations = testLocations }
testItems <- newTestItem :: testItems
DotLiquid.page "test.html" { TestItems = testItems }
let randomizeGet =
DotLiquid.page "randomize.html" ()
let randomizerPost (r:HttpRequest) =
match r.files.Length with
| 0 -> DotLiquid.page "randomize.html" { Error = true; Info = "No file uploaded"; Mode = "POST" }
| _ ->
let file = r.files.Head
let (_, inputSeed) = r.multiPartFields.Head
let (_, inputDifficulty) = r.multiPartFields.Tail.Head;
let bytes =
use binaryReader = new BinaryReader(File.Open(file.tempFilePath, FileMode.Open))
binaryReader.ReadBytes(int binaryReader.BaseStream.Length)
try
let difficulty = enum<Types.Difficulty>(Int32.Parse inputDifficulty)
let parsedSeed = if inputSeed.StartsWith("0x") then Convert.ToInt32(inputSeed, 16) else Convert.ToInt32(inputSeed)
let ipsPatches = List.filter (fun (p:Types.IpsPatch) -> ((p.Difficulty = difficulty || p.Difficulty = Types.Difficulty.Any) && p.Default)) Patches.IpsPatches
let patches = List.filter (fun (p:Types.Patch) -> ((p.Difficulty = difficulty || p.Difficulty = Types.Difficulty.Any) && p.Default)) Patches.RomPatches
let (seed, binaryData) = Randomizer.Randomize parsedSeed difficulty true "" bytes ipsPatches patches
let newFileName = sprintf "Item Randomizer %s%d.sfc" (match difficulty with
| Types.Difficulty.Casual -> "CX"
| Types.Difficulty.Normal -> "X"
| Types.Difficulty.Hard -> "HX"
| Types.Difficulty.Tournament -> "TX"
| Types.Difficulty.Open -> "OX"
| Types.Difficulty.Full -> "FX"
| _ -> "X") seed
if File.Exists(file.tempFilePath) then File.Delete(file.tempFilePath) else ()
Writers.setMimeType("application/octet-stream")
>=> Writers.setHeader "Content-Disposition" (sprintf "attachment; filename=\"%s\"" newFileName)
>=> (Successful.ok <| binaryData)
with
| _ -> DotLiquid.page "randomize.html" { Error = true; Info = "Couldn't patch the ROM, did you upload the correct file?"; Mode = "POST" }
let Router =
choose [
path "/" >=> index
path "/randomize" >=> choose
[
POST >=> request randomizerPost
GET >=> randomizeGet
]
path "/information" >=> information
path "/contact" >=> contact
path "/test" >=> choose
[
POST >=> request testPost
GET >=> testGet
]
GET >=> path "/favicon.ico" >=> Files.browseFile __SOURCE_DIRECTORY__ "favicon.ico"
GET >=> Files.browse (Path.GetFullPath "./static")
RequestErrors.NOT_FOUND "Page not found"
]