-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
145 lines (122 loc) · 4.44 KB
/
Program.fs
File metadata and controls
145 lines (122 loc) · 4.44 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Learn more about F# at http://fsharp.org
open System
open System.IO
open FSharp.Data
open ChartPrinter
type SSCAIT = JsonProvider<"https://sscaitournament.com/api/games.php?future=false">
let getDataPage page pageSize =
async {
let url = sprintf "https://sscaitournament.com/api/games.php?future=false&page=%d&count=%d" page pageSize;
let! doc = SSCAIT.AsyncLoad(url)
return doc
}
let getResultsPage page pageSize count =
async {
let! doc = getDataPage page pageSize
return doc |> Seq.take(count)
}
let getResults count =
let pageSize = 1000
let pagesCount = (count + pageSize - 1) / pageSize
let f page =
if page = pagesCount then
getResultsPage page pageSize (count % pageSize)
else
getResultsPage page pageSize pageSize
[1..pagesCount]
|> Seq.map f
|> Async.Parallel
|> Async.RunSynchronously
|> Seq.concat
type ModeOption =
| ModeGetData
| ModePrintChart
| ModePrintPrediction
| ModeBotStats
| ModeLostGames
| ModeInvalid
type CommandLineOptions = {
mode: ModeOption
gamesProcess: int
filterThreshold: int
botName: string
}
// create the "helper" recursive function
let rec parseCommandLineRec args optionsSoFar =
match args with
// empty list means we're done.
| [] ->
optionsSoFar
| "get"::xs ->
match xs with
| [] ->
optionsSoFar
| games::xss ->
let newOptionsSoFar = { optionsSoFar with gamesProcess = (games|> int); mode=ModeGetData }
parseCommandLineRec xss newOptionsSoFar
| "print"::xs ->
let newOptionsSoFar = { optionsSoFar with mode=ModePrintChart}
parseCommandLineRec xs newOptionsSoFar
| "botStats"::xs ->
match xs with
| [] ->
optionsSoFar
| botName::xss ->
let newOptionsSoFar = { optionsSoFar with botName = botName; mode=ModeBotStats }
parseCommandLineRec xss newOptionsSoFar
| "predict"::xs ->
match xs with
| [] ->
optionsSoFar
| threshold::xss ->
let newOptionsSoFar = { optionsSoFar with filterThreshold = (threshold|> int); mode=ModePrintPrediction }
parseCommandLineRec xss newOptionsSoFar
| "lostGames"::xs ->
match xs with
| [] ->
optionsSoFar
| botName::xss ->
let newOptionsSoFar = { optionsSoFar with botName = botName; mode=ModeLostGames }
parseCommandLineRec xss newOptionsSoFar
// handle unrecognized option and keep looping
| x::xs ->
printfn "Option '%s' is unrecognized" x
parseCommandLineRec xs optionsSoFar
// create the "public" parse function
let parseCommandLine args =
// create the defaults
let defaultOptions = {
mode = ModeInvalid;
gamesProcess = 0;
filterThreshold = 0;
botName = "<<Not specified>>"
}
// call the recursive one with the initial options
parseCommandLineRec args defaultOptions
[<EntryPoint>]
let main argv =
let commandArgs = parseCommandLine (argv |> Seq.toList)
match commandArgs.mode with
| ModeGetData ->
let doc = getResults commandArgs.gamesProcess
use streamWriter = new StreamWriter("results.txt", false)
streamWriter.WriteLine "Timestamp,Host,Guest,Result,Replay"
for record in doc do
let line = sprintf "%d,%s,%s,%d,%s" record.Timestamp record.Host record.Guest record.Result record.Replay
streamWriter.WriteLine line
| ModePrintChart ->
printfn "Printing chart"
let sourceFile = Path.Combine(Directory.GetCurrentDirectory(), "results.txt")
printChart sourceFile
| ModeBotStats ->
let sourceFile = Path.Combine(Directory.GetCurrentDirectory(), "results.txt")
printBotStats sourceFile commandArgs.botName
| ModePrintPrediction ->
let sourceFile = Path.Combine(Directory.GetCurrentDirectory(), "results.txt")
printPrediction sourceFile commandArgs.filterThreshold
| ModeLostGames ->
let sourceFile = Path.Combine(Directory.GetCurrentDirectory(), "results.txt")
printLostGames sourceFile commandArgs.botName
| ModeInvalid ->
printfn "Invalid arguments"
0 // return an integer exit code