about summary refs log tree commit diff
path: root/src/seed.go
diff options
context:
space:
mode:
authormaride <maride@darknebu.la>2019-10-11 14:29:01 +0200
committermaride <maride@darknebu.la>2019-10-11 14:29:01 +0200
commit92d20710d7cbad0781d04d45ee859a1d66b30b0d (patch)
tree4361cdc19a54d6026aefc3dff3b8ea5aa2002cc4 /src/seed.go
parent8cb39e5fea15b6401a9a0e9229a8d86c0c3d1601 (diff)
Add points per challenge and overall scoring
Diffstat (limited to 'src/seed.go')
-rw-r--r--src/seed.go17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/seed.go b/src/seed.go
index 805ab20..d876940 100644
--- a/src/seed.go
+++ b/src/seed.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"io/ioutil"
 	"log"
+	"strconv"
 	"time"
 
 	"github.com/pkg/errors"
@@ -51,8 +52,10 @@ func readSeedFile(path string) ([]Challenge, error) {
 		flag, flagOK := challenge["flag"].(string)
 		cont, contOK := challenge["container"].(string)
 		category, categoryOK := challenge["category"].(string)
+		pointsStr, pointsOK := challenge["points"].(string)
+		points, _ := strconv.Atoi(pointsStr)
 
-		if nameOK && descOK && flagOK && contOK && categoryOK {
+		if nameOK && descOK && flagOK && contOK && categoryOK && pointsOK {
 			tmpChallenges = append(tmpChallenges, Challenge{
 				Name:        name,
 				Description: desc,
@@ -61,9 +64,10 @@ func readSeedFile(path string) ([]Challenge, error) {
 				FlagTries:   0,
 				Container:   cont,
 				Category:    category,
+				Points:		 points,
 			})
 		} else {
-			log.Printf("Ignoring challenge at position %d: Not all values are parseable (name: %b, desc: %b, flag: %b, container: %b, category: %b", index, nameOK, descOK, flagOK, contOK, categoryOK)
+			log.Printf("Ignoring challenge at position %d: Not all values are parseable (name: %b, desc: %b, flag: %b, container: %b, category: %b, points: %b", index, nameOK, descOK, flagOK, contOK, categoryOK, pointsOK)
 		}
 	}
 
@@ -116,18 +120,25 @@ func generateJSONFromChallenges() (string, error) {
 func generateJSONFromChallengesForStats() (string, error) {
 	// To include only required information for statistics, we need to strip the challenge
 	var strippedChallenges []StatsStrippedChallenge
+	score := 0
 	categories := map[string]int{}
 
 	for _, challenge := range challenges {
 		// Append challenge to list
 		strippedChallenges = append(strippedChallenges, stripChallengeForStatistics(challenge))
-
+		// Raise category counter
 		categories[challenge.Category]++
+
+		// Raise score if flag was found
+		if challenge.FoundFlag.Unix() > 0 {
+			score = score + challenge.Points
+		}
 	}
 
 	marshalled, marshalError := json.Marshal(map[string]interface{}{
 		"user":       user,
 		"challenges": strippedChallenges,
+		"score":	  score,
 	})
 	return string(marshalled), marshalError
 }