changeset 34:732d9739356e

Merge
author Mike Pavone <pavone@retrodev.com>
date Sat, 10 Aug 2013 22:26:05 -0700
parents b00904b36aca (current diff) 8a2925ab0d4a (diff)
children 9d5c3b078c78
files
diffstat 1 files changed, 100 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/src/requests.tp	Sat Aug 10 22:25:49 2013 -0700
+++ b/src/requests.tp	Sat Aug 10 22:26:05 2013 -0700
@@ -21,20 +21,32 @@
 		print: str . "\n"
 	}
 
+	fixArgs <- :args {
+		args map: :el {
+			"\"0x" . (hex: el) . "\""
+		}
+	}
+
+
 	evalId <- :id args {
+		args <- fixArgs: args
 		#{
 			string <- {
 				idStr <- (quote: "id") . ":" . (quote: id)
-				argsStr <- (quote: "arguments") . ":" . (quote: (strJoin: "," args))
+				argsStr <- (quote: "arguments") . ":[" . (strJoin: "," args) . "]"
 				"{" . idStr . "," . 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
+			}
 		}
 	}
 
 	evalProgram <- :program args {
-		args <- args map: :el {
-			"\"0x" . (hex: el) . "\""
-		}
+		args <- fixArgs: args
 		#{
 			string <- {
 				progStr <- (quote: "program") . ":" . (quote: program)
@@ -50,13 +62,19 @@
 		}
 	}
 
-	guessRequest <- :id :prog {
+	guess <- :id :prog {
 		#{
 			string <- {
 				idStr <- "\"id\":\"" . id . "\""
 				progStr <- "\"program\":\"" . prog . "\""
 				"{" . idStr . "," . progStr . "}"
 			}
+			sendWithKey <- :key {
+				print: "Sending: " . string . "\n"
+				cli <- http client: "icfpc2013.cloudapp.net"
+				resp <- cli post: string toPath: "/guess?auth=" . key withType: "application/json"
+				guessResponse: resp
+			}
 		}
 	}
 
@@ -69,39 +87,40 @@
 		_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)
+			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
 					}
-					str
+					print <- {
+						print: string . "\n"
+					}
 				}
-				print <- {
-					print: string . "\n"
+			} else: {
+				_message <- decoded get: "message" withDefault: ""
+				#{
+					status <- { "error" }
+					message <- { _message }
+					string <- {
+						"Error: " . _message
+					}
+					print <- {
+						print: string . "\n"
+					}
 				}
 			}
 		} else: {
-			_message <- decoded get: "message" withDefault: ""
-			#{
-				status <- { "error" }
-				message <- { _message }
-				string <- {
-					"Error: " . _message
-				}
-				print <- {
-					print: string . "\n"
-				}
-			}
+			#{print <- print: "HTTP response gave error! code was: " . _code . "\n"}
+			
 		}
 	}
 
@@ -114,58 +133,66 @@
 		_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"
+			if: _status = "win" {
+				#{
+					status <- { "win" }
+					string <- { "OK: win" }
+					print <- {
+						print: string . "\n"
+					}
 				}
-			}
-		} else: { if: _status = "mismatch" {
+			} 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)
+				_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
 					}
-					str
+					print <- {
+						print: string . "\n"
+					}
 				}
-				print <- {
-					print: string . "\n"
+			} else: {
+				_message <- decoded get: "message" withDefault: ""
+				#{
+					status <- { "error" }
+					message <- { _message }
+					string <- {
+						"Error: " . _message
+					}
+					print <- {
+						print: string . "\n"
+					}
 				}
-			}
+			}} // end if
 		} else: {
-			_message <- decoded get: "message" withDefault: ""
-			#{
-				status <- { "error" }
-				message <- { _message }
-				string <- {
-					"Error: " . _message
-				}
-				print <- {
-					print: string . "\n"
-				}
-			}
-		}} // end if
+			#{print <- print: "HTTP response gave error! code was: " . _code . "\n"}
+		}
 	}
 
 	main <- :args {
-		print: ((evalId: "someId" #[1u64 2u64 3u64]) string) . "\n"
-		print: ((guessRequest: "someId" "someProg") string) . "\n"
+		//print: ((evalId: "someId" #[1u64 2u64 3u64]) string) . "\n"
+		//print: ((guess: "someId" "someProg") string) . "\n"
 
 		if: (args length) > 1 {
 			key <- args get: 1
-			print: ((evalProgram: "(lambda (input) (shl1 input))" #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key)
+			testId <- "wdThP1AgVrS2rp7q6qt9mLqp" // my first training problem
+			someId <- "QwhG7ZpaVsfXiLRvbJfIfxl8" // TRAINING PROGRAM IDs!!!  not real ones.
+
+			someProgram <- "(lambda (input) (shl1 input))"
+
+			//print: ((evalProgram: "(lambda (input) (shl1 input))" #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key)
+			//print: ((evalId: testId #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key)
+			print: ((guess: testId someProgram) sendWithKey: key)
+		
 		}
 	}
 }