Commit 3f97761

Richard Luby <richluby@gmail.com>
2016-12-24 05:23:22
server tracks used questions
server keeps track of which questions have already been used, restting them if no more are available
1 parent ee90299
serverHandlers.go
@@ -30,13 +30,18 @@ func getRecordForCategory(categoryIndex int) Record {
 	defer mapLock.Unlock()
 	var record Record
 	for record.Answer == "" {
-		if len(categories[categoryIndex].RecordArray) > 0 {
+		if len(categories[categoryIndex].RecordArray) > 0 { // if there are unused records
 			recordIndex := rand.Intn(len(categories[categoryIndex].RecordArray))
-			//TODO: remove category from array
-			return categories[categoryIndex].RecordArray[recordIndex]
-		} else if len(categories[categoryIndex].UsedRecordArray) > 0 {
+			lastRecordIndex := len(categories[categoryIndex].RecordArray)-1
+			record = categories[categoryIndex].RecordArray[recordIndex]
+			categories[categoryIndex].UsedRecordArray = append(categories[categoryIndex].UsedRecordArray,
+				record)
+			categories[categoryIndex].RecordArray[recordIndex] = categories[categoryIndex].RecordArray[lastRecordIndex]
+			categories[categoryIndex].RecordArray = categories[categoryIndex].RecordArray[0:lastRecordIndex]
+		} else if len(categories[categoryIndex].UsedRecordArray) > 0 { // else reset the unused records
 			categories[categoryIndex].RecordArray = categories[categoryIndex].UsedRecordArray[0:]
-		} else { // choose random sub-category because this is not a leaf
+			categories[categoryIndex].UsedRecordArray = []Record{}
+		} else { // or choose random sub-category because this is not a leaf
 			if len(categories[categoryIndex].Children) > 0 {
 				childChosen := rand.Intn(len(categories[categoryIndex].Children))
 				categoryIndex = categories[categoryIndex].Children[childChosen]
@@ -45,6 +50,7 @@ func getRecordForCategory(categoryIndex int) Record {
 			}
 		}
 	}
+	log.Printf("unused: %+v\nused: %+v", categories[categoryIndex].RecordArray, categories[categoryIndex].UsedRecordArray)
 	return record
 }
 
@@ -55,13 +61,11 @@ func buildRecordArray(numQuestions int, categoryIndices []int) []Record {
 	totalCats := len(categories)
 	for len(recordArray) < numQuestions {
 		var catChosen int
-		log.Printf("numCats: %d\ttotalCats: %d", numCats, totalCats)
 		if numCats > 0 {
 			catChosen = categoryIndices[rand.Intn(numCats)]
 		} else {
 			catChosen = rand.Intn(totalCats)
 		}
-		log.Printf("catChosen: %d", catChosen)
 		record := getRecordForCategory(catChosen)
 		if record.Answer != "" {
 			recordArray = append(recordArray, record)
serverHandlers_test.go
@@ -23,7 +23,7 @@ func TestBuildRecordArray(t *testing.T) {
 	if len(categories[categories[recordCat].Parent].Children) != 1 {
 		log.Printf("Wrong children: %s", categories[categories[recordCat].Parent].Children)
 	}
-	if len(categories[recordCat].RecordArray) != 1 {
+	if len(categories[recordCat].RecordArray) != 1 && len(categories[recordCat].UsedRecordArray) != 1 {
 		log.Printf("Record not added: %+v", categories[recordCat])
 	}
 	log.Printf("records: %v", recordArray)