Commit 2dc3fd8

Richard Luby <richluby@gmail.com>
2016-12-20 13:49:40
changed category storage to be array-based
1 parent 34ecc3a
command.go
@@ -234,7 +234,7 @@ func runTest(clientTest *ClientTest) {
 	displayStringToMainScreen("\n")
 	for i, record := range clientTest.Records {
 		displayStringToMainScreen(fmt.Sprintf("%d) [%s] %s\n", i+1,
-			COLOR_BLUE+categoricalData.CategoryPath[record.Category]+COLOR_RESET,
+			COLOR_BLUE+categories[record.Category].FullCategoryPath()+COLOR_RESET,
 			record.Question))
 		updateMainView()
 		readUserInputSemaphore.P() // released when user presses 'Enter'
@@ -309,8 +309,8 @@ func getCategoriesFromServer(command Command) error {
 	if err != nil {
 		return err
 	}
-	err = json.Unmarshal(data, &categoricalData.CategoryPath)
-	log.Printf("Pulled %d categories from server.", len(categoricalData.CategoryPath))
+	err = json.Unmarshal(data, &categories)
+	log.Printf("Pulled %d categories from server.", len(categories))
 	return nil
 }
 
@@ -340,10 +340,10 @@ func buildBluePrint(useBlueprint bool) (string, error) {
 	}
 	var blueprint string
 	displayStringToMainScreen("\nCategories\n----------\n")
-	for i := 0; i < len(categoricalData.CategoryPath)-1; i += 2 {
+	for i := 0; i < len(categories)-1; i += 2 {
 		displayStringToMainScreen(fmt.Sprintf("%d) %-15s\t%d) %-15s\n",
-			i, categoricalData.CategoryPath[i],
-			i+1, categoricalData.CategoryPath[i+1]))
+			i, categories[i],
+			i+1, categories[i+1]))
 	}
 	displayStringToMainScreen("Enter the category numbers, separated by a comma: ")
 	updateMainView()
question.go
@@ -51,6 +51,8 @@ func LoadFile(file *os.File) error {
 	for scanner.Scan() {
 		if err := parseLine(scanner.Text()); err != nil {
 			log.Printf("Error while parsing line: %+v.", err)
+		} else {
+			recordsArray[len(recordsArray)-1].Category = catIndex
 		}
 	}
 	return nil
server.go
@@ -105,7 +105,10 @@ func ExecuteServer() {
 		log.Printf("Failed to load questions due to error: %+v", err)
 		os.Exit(EXIT_CODE.FILE_IO_ERROR)
 	}
-	log.Printf("Loaded %d questions from %s with %d categories.", len(recordsArray), serverConfig.QUESTIONS, len(categoricalData.CategoryPath))
+	log.Printf("Loaded %d questions from %s with %d categories.", len(recordsArray), serverConfig.QUESTIONS, len(categories))
+	for _, cat := range categories {
+		log.Printf("Cat: %s", cat.FullCategoryPath())
+	}
 	rand.Seed(time.Now().Unix())
 	Listen(serverConfig)
 }
serverHandlers.go
@@ -31,20 +31,7 @@ func getRecordForCategory(category string) Record {
 
 // buildRecordArray builds an array of Records based on the parameter criteria
 func buildRecordArray(numQuestions int, categories []int) []Record {
-	var keyIndex int
-	var recordArray = []Record{}
-	for i := 0; i < numQuestions; i++ {
-		if categories == nil {
-			keyIndex = rand.Intn(len(categoricalData.CategoryPath))
-		} else {
-			keyIndex = categories[rand.Intn(len(categories))]
-			if keyIndex >= len(categoricalData.CategoryPath) {
-				return nil
-			}
-		}
-		recordArray = append(recordArray, getRecordForCategory(categoricalData.CategoryPath[keyIndex]))
-	}
-	return recordArray
+	return nil
 }
 
 // handleRequestForTest provides a JSON formatted test for the client
@@ -164,7 +151,7 @@ func handleTestQueries(writer http.ResponseWriter, request *http.Request) {
 // handleRequestForCategories returns the JSON-encoded array
 // of categories available on the server
 func handleRequestForCategories(writer http.ResponseWriter, request *http.Request) error {
-	data, err := json.Marshal(categoricalData.CategoryPath)
+	data, err := json.Marshal(categories)
 	if err != nil {
 		return err
 	}