view 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
line wrap: on
line source

#{

	strJoin <- :str arr {
		acc <- ""
		arr foreach: :i el {
			if: i = 0 {
				acc <- (string: el)
			} else: {
				acc <- acc . ", " . (string: el)
			}
		}
		acc
		//arr fold: "" with: :acc el {acc . el}
	}

	quote <- :str {
		"\"" . str . "\""
	}

	println <- :str {
		print: str . "\n"
	}

	evalId <- :id args {
		#{
			string <- {
				idStr <- (quote: "id") . ":" . (quote: id)
				argsStr <- (quote: "arguments") . ":" . (quote: (strJoin: "," args))
				"{" . idStr . "," . argsStr . "}"
			}
		}
	}

	evalProgram <- :program args {
		args <- args map: :el {
			"\"0x" . (hex: el) . "\""
		}
		#{
			string <- {
				progStr <- (quote: "program") . ":" . (quote: program)
				argsStr <- (quote: "arguments") . ":[" . (strJoin: "," args) . "]"
				"{" . progStr . "," . argsStr . "}"
			}
			sendWithKey <- :key {
				print: "Sending: " . string . "\n"
				cli <- http client: "icfpc2013.cloudapp.net"
				resp <- cli post: string toPath: "/eval?auth=" . key withType: "application/json"
				evalResponse: resp
			}
		}
	}

	guessRequest <- :id :prog {
		#{
			string <- {
				idStr <- "\"id\":\"" . id . "\""
				progStr <- "\"program\":\"" . prog . "\""
				"{" . idStr . "," . progStr . "}"
			}
		}
	}

	evalResponse <- :httpResp {
		_code <- httpResp statusCode
		bod <- httpResp body
		print: "Response code: " . (string: _code) . "\n"
		print: bod . "\n"
		decoded <- json decode: bod
		_status <- "error"
		if: _code = 200 {
			_status <- decoded get: "status" withDefault: "error"
		} else: {
			print: "http response gave error!, code was: " . _code
		}
		if: _status = "ok" {
			_outputs <- (decoded get: "outputs" withDefault: #[]) map: :num {
				(num from: 2) parseHex64
			}
			#{
				status <- { "ok" }
				outputs <- { _outputs }
				string <- {
					str <- "OK:"
					foreach: _outputs :idx val {
						str <- str . "\n" . (string: idx) . ": 0x" . (hex: val)
					}
					str
				}
				print <- {
					print: string . "\n"
				}
			}
		} else: {
			_message <- decoded get: "message" withDefault: ""
			#{
				status <- { "error" }
				message <- { _message }
				string <- {
					"Error: " . _message
				}
				print <- {
					print: string . "\n"
				}
			}
		}
	}

	guessResponse <- :httpResp {
		_code <- httpResp statusCode
		bod <- httpResp body
		print: "Response code: " . (string: _code) . "\n"
		print: bod . "\n"
		decoded <- json decode: bod
		_status <- "error"
		if: _code = 200 {
			_status <- decoded get: "status" withDefault: "error"
		} else: {
			print: "http response gave error!, code was: " . _code
		}
		if: _status = "win" {
			#{
				status <- { "win" }
				string <- { "OK: win" }
				print <- {
					print: string . "\n"
				}
			}
		} else: { if: _status = "mismatch" {

			_values <- (decoded get: "values" withDefault: #[]) map: :num {
				(num from: 2) parseHex64
			}
			#{
				status <- { "mismatch" }
				values <- { _values }
				string <- {
					str <- "OK:"
					foreach: _values :idx val {
						str <- str . "\n" . (string: idx) . ": 0x" . (hex: val)
					}
					str
				}
				print <- {
					print: string . "\n"
				}
			}
		} else: {
			_message <- decoded get: "message" withDefault: ""
			#{
				status <- { "error" }
				message <- { _message }
				string <- {
					"Error: " . _message
				}
				print <- {
					print: string . "\n"
				}
			}
		}} // end if
	}

	main <- :args {
		print: ((evalId: "someId" #[1u64 2u64 3u64]) string) . "\n"
		print: ((guessRequest: "someId" "someProg") string) . "\n"

		if: (args length) > 1 {
			key <- args get: 1
			print: ((evalProgram: "(lambda (input) (shl1 input))" #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key)
		}
	}
}