Mercurial > repos > icfp2013
annotate src/requests.tp @ 26:18a043613dae
added guess response.
author | William Morgan <bill@mrgn.org> |
---|---|
date | Sat, 10 Aug 2013 18:39:45 -0700 |
parents | bb80f86c5048 |
children | a4bffcd381cd |
rev | line source |
---|---|
10
79c1db5e7ebd
sending broken file for mike to check error
William Morgan <bill@mrgn.org>
parents:
diff
changeset
|
1 #{ |
18 | 2 |
20 | 3 strJoin <- :str arr { |
4 acc <- "" | |
5 arr foreach: :i el { | |
6 if: i = 0 { | |
7 acc <- (string: el) | |
8 } else: { | |
9 acc <- acc . ", " . (string: el) | |
10 } | |
11 } | |
12 acc | |
13 //arr fold: "" with: :acc el {acc . el} | |
14 } | |
15 | |
18 | 16 quote <- :str { |
24
e1109e33b796
some work that probably needs to be trashed on requests.tp
Mike Pavone <pavone@retrodev.com>
parents:
23
diff
changeset
|
17 "\"" . str . "\"" |
18 | 18 } |
19 | |
24
e1109e33b796
some work that probably needs to be trashed on requests.tp
Mike Pavone <pavone@retrodev.com>
parents:
23
diff
changeset
|
20 println <- :str { |
23 | 21 print: str . "\n" |
22 } | |
23 | |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
24 evalId <- :id args { |
12
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
25 #{ |
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
26 string <- { |
20 | 27 idStr <- (quote: "id") . ":" . (quote: id) |
28 argsStr <- (quote: "arguments") . ":" . (quote: (strJoin: "," args)) | |
18 | 29 "{" . idStr . "," . argsStr . "}" |
12
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
30 } |
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
31 } |
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
32 } |
10
79c1db5e7ebd
sending broken file for mike to check error
William Morgan <bill@mrgn.org>
parents:
diff
changeset
|
33 |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
34 evalProgram <- :program args { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
35 args <- args map: :el { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
36 "\"0x" . (hex: el) . "\"" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
37 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
38 #{ |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
39 string <- { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
40 progStr <- (quote: "program") . ":" . (quote: program) |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
41 argsStr <- (quote: "arguments") . ":[" . (strJoin: "," args) . "]" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
42 "{" . progStr . "," . argsStr . "}" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
43 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
44 sendWithKey <- :key { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
45 print: "Sending: " . string . "\n" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
46 cli <- http client: "icfpc2013.cloudapp.net" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
47 resp <- cli post: string toPath: "/eval?auth=" . key withType: "application/json" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
48 evalResponse: resp |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
49 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
50 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
51 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
52 |
15
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
53 guessRequest <- :id :prog { |
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
54 #{ |
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
55 string <- { |
17
566960135ea1
Small bit of cleanup now that the string escaping bug is fixed.
Mike Pavone <pavone@retrodev.com>
parents:
16
diff
changeset
|
56 idStr <- "\"id\":\"" . id . "\"" |
566960135ea1
Small bit of cleanup now that the string escaping bug is fixed.
Mike Pavone <pavone@retrodev.com>
parents:
16
diff
changeset
|
57 progStr <- "\"program\":\"" . prog . "\"" |
566960135ea1
Small bit of cleanup now that the string escaping bug is fixed.
Mike Pavone <pavone@retrodev.com>
parents:
16
diff
changeset
|
58 "{" . idStr . "," . progStr . "}" |
15
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
59 } |
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
60 } |
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
61 } |
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
62 |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
63 evalResponse <- :httpResp { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
64 _code <- httpResp statusCode |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
65 bod <- httpResp body |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
66 print: "Response code: " . (string: _code) . "\n" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
67 print: bod . "\n" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
68 decoded <- json decode: bod |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
69 _status <- "error" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
70 if: _code = 200 { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
71 _status <- decoded get: "status" withDefault: "error" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
72 } else: { |
26 | 73 print: "http response gave error!, code was: " . _code |
24
e1109e33b796
some work that probably needs to be trashed on requests.tp
Mike Pavone <pavone@retrodev.com>
parents:
23
diff
changeset
|
74 } |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
75 if: _status = "ok" { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
76 _outputs <- (decoded get: "outputs" withDefault: #[]) map: :num { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
77 (num from: 2) parseHex64 |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
78 } |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
79 #{ |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
80 status <- { "ok" } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
81 outputs <- { _outputs } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
82 string <- { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
83 str <- "OK:" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
84 foreach: _outputs :idx val { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
85 str <- str . "\n" . (string: idx) . ": 0x" . (hex: val) |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
86 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
87 str |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
88 } |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
89 print <- { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
90 print: string . "\n" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
91 } |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
92 } |
24
e1109e33b796
some work that probably needs to be trashed on requests.tp
Mike Pavone <pavone@retrodev.com>
parents:
23
diff
changeset
|
93 } else: { |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
94 _message <- decoded get: "message" withDefault: "" |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
95 #{ |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
96 status <- { "error" } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
97 message <- { _message } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
98 string <- { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
99 "Error: " . _message |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
100 } |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
101 print <- { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
102 print: string . "\n" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
103 } |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
104 } |
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
105 } |
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
106 } |
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
107 |
26 | 108 guessResponse <- :httpResp { |
109 _code <- httpResp statusCode | |
110 bod <- httpResp body | |
111 print: "Response code: " . (string: _code) . "\n" | |
112 print: bod . "\n" | |
113 decoded <- json decode: bod | |
114 _status <- "error" | |
115 if: _code = 200 { | |
116 _status <- decoded get: "status" withDefault: "error" | |
117 } else: { | |
118 print: "http response gave error!, code was: " . _code | |
119 } | |
120 if: _status = "win" { | |
121 #{ | |
122 status <- { "win" } | |
123 string <- { "OK: win" } | |
124 print <- { | |
125 print: string . "\n" | |
126 } | |
127 } | |
128 } else: { if: _status = "mismatch" { | |
129 | |
130 _values <- (decoded get: "values" withDefault: #[]) map: :num { | |
131 (num from: 2) parseHex64 | |
132 } | |
133 #{ | |
134 status <- { "mismatch" } | |
135 values <- { _values } | |
136 string <- { | |
137 str <- "OK:" | |
138 foreach: _values :idx val { | |
139 str <- str . "\n" . (string: idx) . ": 0x" . (hex: val) | |
140 } | |
141 str | |
142 } | |
143 print <- { | |
144 print: string . "\n" | |
145 } | |
146 } | |
147 } else: { | |
148 _message <- decoded get: "message" withDefault: "" | |
149 #{ | |
150 status <- { "error" } | |
151 message <- { _message } | |
152 string <- { | |
153 "Error: " . _message | |
154 } | |
155 print <- { | |
156 print: string . "\n" | |
157 } | |
158 } | |
159 }} // end if | |
160 } | |
161 | |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
162 main <- :args { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
163 print: ((evalId: "someId" #[1u64 2u64 3u64]) string) . "\n" |
15
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
164 print: ((guessRequest: "someId" "someProg") string) . "\n" |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
165 |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
166 if: (args length) > 1 { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
167 key <- args get: 1 |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
168 print: ((evalProgram: "(lambda (input) (shl1 input))" #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key) |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
169 } |
12
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
170 } |
10
79c1db5e7ebd
sending broken file for mike to check error
William Morgan <bill@mrgn.org>
parents:
diff
changeset
|
171 } |