Commit 8df93a2

Richard Luby <richluby@gmail.com>
2016-11-01 09:59:52
client now calculates score
score is stored as float32, calculated by the client, and then verified by the server if necessary
1 parent 3407834
command.go
@@ -86,11 +86,16 @@ func init() {
 	commandArray = append(commandArray, helpCommand)
 }
 
+// calculateScore returns the client score as a float32
+func calculateScore(numCorrect int, numQuestions int) float32 {
+	return float32(numCorrect) / float32(numQuestions) * 100
+}
+
 // walks the user through the test questions
 // this function updates the records with the user responses
-func runTest(clientTest ClientTest) []ClientRecord {
+func runTest(clientTest *ClientTest) {
 	reader := bufio.NewReader(os.Stdin)
-	var responseArray []ClientRecord
+	numCorrect := 0
 	for i, record := range clientTest.Records {
 		fmt.Printf("%d) %s\n", i+1, record.Question)
 		input, _ := reader.ReadString('\n')
@@ -98,14 +103,17 @@ func runTest(clientTest ClientTest) []ClientRecord {
 		if strings.Compare(input, record.Answer) == 0 {
 			fmt.Println("Correct.")
 			clientTest.Records[i].AnsweredCorrectly = true
+			numCorrect++
 		} else if strings.Compare(input, "exit") == 0 {
-			return responseArray
+			clientTest.Score = calculateScore(numCorrect, len(clientTest.Records))
+			return
 		} else {
 			fmt.Printf("Incorrect. Correct answer was: %s\n", record.Answer)
 			clientTest.Records[i].ClientAnswer = input
 		}
 	}
-	return responseArray
+	clientTest.Score = calculateScore(numCorrect, len(clientTest.Records))
+	return
 }
 
 // postRecordsToServer sends the client responses back to the server
@@ -160,6 +168,7 @@ func executeTest(args []string) error {
 		return fmt.Errorf("Error while requesting test from server: %+v", err)
 	}
 	clientTest.Username = clientConfig.USER
-	runTest(clientTest)
+	runTest(&clientTest)
+	fmt.Printf("You scored: %.2f%%.\n", clientTest.Score)
 	return postRecordsToServer(client, &clientTest)
 }
serverHandlers.go
@@ -41,7 +41,7 @@ func writeTestFile(clientTest ClientTest, data []byte) error {
 	}
 	resultsFilePath = filepath.Join(resultsFilePath,
 		strconv.Itoa(len(clientTest.Records))+
-			"-"+strconv.Itoa(clientTest.Score))
+			"-"+fmt.Sprintf("%.2f", clientTest.Score))
 	id := fmt.Sprintf("%05d", rand.Intn(99999)) // generate random 5-digit id
 	for fileNeedsWrite := true; fileNeedsWrite; {
 		if err = ioutil.WriteFile(resultsFilePath+"-"+id, data, 0400); err != nil {
@@ -56,14 +56,14 @@ func writeTestFile(clientTest ClientTest, data []byte) error {
 // validateScore verifies that the user test score is valid
 // if not, the score is recalculated using integer math
 func validateScore(clientTest *ClientTest) {
-	if clientTest.Score < 0 || clientTest.Score > 100 {
+	if clientTest.Score <= 0 || clientTest.Score > 100 {
 		clientTest.Score = 0
 		for _, record := range clientTest.Records {
 			if record.AnsweredCorrectly == true {
 				clientTest.Score++
 			}
 		}
-		clientTest.Score = 1.0 * clientTest.Score / len(clientTest.Records) * 100
+		clientTest.Score = clientTest.Score / float32(len(clientTest.Records)) * 100
 	}
 }
 
structures.go
@@ -34,6 +34,6 @@ type ClientRecord struct {
 // ClientTest stores a full test and its results for the client
 type ClientTest struct {
 	Records  []ClientRecord
-	Score    int
+	Score    float32
 	Username string
 }