-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
64 lines (52 loc) · 1.47 KB
/
api.go
File metadata and controls
64 lines (52 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"net/http"
"vaas/structs"
"github.com/gin-gonic/gin"
)
/*
The following code is a result of the collaboration of Team Vaas and
Peter Kelly from team Lelkolopher (Dominion)
*/
// Stats API's main method.
func main() {
router := gin.Default()
router.GET("/", api_home)
router.GET("/getStats/:userId", api_getStats)
router.Run("0.0.0.0:5001")
}
// Default home endpoint for checking if the stats container is running.
func api_home(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "stats is running"})
}
// Gets relevant stats for the given user.
func api_getStats(c *gin.Context) {
user, err := mongo_getUser(c.Param("userId"))
if err != nil {
c.JSON(http.StatusInternalServerError, structs.Message{Message: err.Error()})
return
}
words, err := mongo_getCommonWords(user.Id)
if err != nil {
c.JSON(http.StatusInternalServerError, structs.Message{Message: err.Error()})
return
}
mostCommonWord := ""
highestFrequency := 0
for _, word := range words {
if word.Count > highestFrequency {
mostCommonWord = word.Word
highestFrequency = word.Count
}
}
winPercentage := float32(0)
if user.NumGamesFinished != 0 {
winPercentage = float32(user.NumGamesWon) / float32(user.NumGamesFinished)
}
c.JSON(http.StatusOK, &structs.IndividualUserStats{
GamesPlayed: user.NumGamesFinished,
WinPercentage: winPercentage,
MostGuessedWord: mostCommonWord,
MostGuessedWordFrequency: highestFrequency,
})
}