Пример #1
0
func homeHandler(s *session, w io.Writer, r *request) {
	// check the args
	if len(r.args) > 1 {
		usageHandler(fmt.Sprintf("%s takes no arguments", r.command), w, r)
		return
	}
	// output the current puzzle summary
	fmt.Fprintf(w, "Session %q with current puzzle:\n", s.sid)
	fmt.Fprintf(w, "  %s [%s, %dx%d] (id: %s)\n\tSolved: %d; Remaining: %d\n",
		s.ss.Info.Name,
		s.ss.Info.Geometry, s.ss.Info.SideLength, s.ss.Info.SideLength,
		s.ss.Info.PuzzleId,
		len(s.ss.Info.Choices), s.ss.Info.Remaining)
	fmt.Fprintf(w, "\nOther puzzles (being solved first, most recent first):\n")
	// output the rest of the session puzzles
	infos := s.ss.GetInactivePuzzles()
	sort.Sort(storage.ByLatestSolutionView(infos))
	for _, info := range infos {
		fmt.Fprintf(w, "  %s [%s, %dx%d] (id: %s)\n\tSolved: %d; Remaining: %d\n",
			info.Name,
			info.Geometry, info.SideLength, info.SideLength,
			info.PuzzleId,
			len(info.Choices), info.Remaining)
	}
}
Пример #2
0
func TestHomePage(t *testing.T) {
	session0 := "httpx-Test0"
	info0 := &storage.PuzzleInfo{
		PuzzleId:   "test-0-id",
		Name:       "test-0",
		Geometry:   puzzle.StandardGeometryName,
		SideLength: 9,
		Choices:    []puzzle.Choice{{1, 1}},
		Remaining:  0,
	}
	others0 := []*storage.PuzzleInfo{
		&storage.PuzzleInfo{
			PuzzleId:   "ps1",
			Name:       "pseudo-puzzle-1",
			Geometry:   puzzle.StandardGeometryName,
			SideLength: 9,
			Choices:    nil,
			Remaining:  1,
			LastView:   time.Now(),
		},
		&storage.PuzzleInfo{
			PuzzleId:   "ps2",
			Name:       "pseudo-puzzle-2",
			Geometry:   puzzle.StandardGeometryName,
			SideLength: 16,
			Choices:    []puzzle.Choice{{2, 2}},
			Remaining:  2,
			LastView:   time.Now().Add(-time.Second),
		},
		&storage.PuzzleInfo{
			PuzzleId:   "ps3",
			Name:       "pseudo-puzzle-3",
			Geometry:   puzzle.RectangularGeometryName,
			SideLength: 6,
			Choices:    []puzzle.Choice{{2, 2}, {3, 3}},
			Remaining:  3,
			LastView:   time.Now().Add(-time.Hour),
		},
		&storage.PuzzleInfo{
			PuzzleId:   "ps4",
			Name:       "pseudo-puzzle-4",
			Geometry:   puzzle.RectangularGeometryName,
			SideLength: 12,
			Choices:    []puzzle.Choice{{2, 2}, {3, 3}, {4, 4}},
			Remaining:  4,
			LastView:   time.Now().Add(-time.Minute),
		},
	}
	sort.Sort(storage.ByLatestSolutionView(others0))
	body := HomePage(session0, info0, others0)
	err := sameAsResultFile(body, "TestHomePage0.html")
	if err != "" {
		t.Errorf("Test Home 0: got unexpected result body:\n%s:\n%v\n", err, body)
	}
}
Пример #3
0
func (s *session) homeHandler(w http.ResponseWriter, r *http.Request) {
	infos := s.ss.GetInactivePuzzles()
	sort.Sort(storage.ByLatestSolutionView(infos))
	body := client.HomePage(s.sid, s.ss.Info, infos)
	hs := w.Header()
	hs.Add("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write([]byte(body))
	log.Printf("Returned home page for %s:%q step %d.",
		s.sid, s.name(), s.step())
}