//------------------- // Finally, the tests... func TestSize(t *testing.T) { u := universe.LoadFromCanonicalString("3,2,f|0,0,") if u.Width != 3 || u.Height != 2 { t.Error("Size does not match") } u = universe.LoadFromString("..\n..\n..\n") if u.Width != 2 || u.Height != 3 { t.Error("Size does not match") } }
//// Protocol: // width,height,[true/false]|i1,j1,i2,j2.... // where i = col # for a living cell // and j = row # for living cell // t/f for toroidal or not func main() { log.Print("Starting webserver.") http.HandleFunc("/next", func(w http.ResponseWriter, r *http.Request) { log.Print("Responding to ", r.URL.Path) val := r.FormValue("state") u := universe.LoadFromCanonicalString(val) u.Next() fmt.Fprintf(w, "%s\n", u.CanonicalString()) }) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./web/")))) http.HandleFunc("/maps", func(w http.ResponseWriter, r *http.Request) { log.Print("Responding to ", r.URL.Path) mapName := r.FormValue("mapName") u, _ := universe.LoadFromFile("./maps/" + mapName + ".txt") fmt.Fprintf(w, "%s\n", u.CanonicalString()) }) http.Handle("/stream", websocket.Handler(wsHandler)) http.HandleFunc("/mapslist", func(w http.ResponseWriter, r *http.Request) { files, _ := ioutil.ReadDir("maps") filenames := make([]string, len(files)) for i, file := range files { parts := strings.Split(file.Name(), ".") filenames[i] = parts[0] } b, _ := json.Marshal(filenames) fmt.Fprintf(w, string(b)) }) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { serveHTML(w, "./web/main.html") }) go h.run() log.Fatal(http.ListenAndServe(":8080", nil)) }
func (c *connection) writer() { ticker := time.NewTicker(100 * time.Millisecond) log.Println("starting to write to clients") var u *universe.Universe for _ = range ticker.C { if c.Initial == "" { // Wait for a start state before sending the data at it log.Print("No start state received") continue } if h.connections[c] { // use start state, then mark it to not be used again h.connections[c] = false u = universe.LoadFromCanonicalString(c.Initial) } err := websocket.Message.Send(c.ws, u.CanonicalString()) if err != nil { c.ws.Close() break } u.Next() } log.Println("stopped") }
func (c canonicalToCanonical) Check() (bool, string) { u := universe.LoadFromCanonicalString(c.Input) u.Next() message := "Answer:\n" + c.Answer + "\nGuess:\n" + u.CanonicalString() + "\nInput:\n" + c.Input + "\n" return u.CanonicalString() == c.Answer, message }