Game list should now be sorted, a new endpoint with the game list in random order have been added.

This commit is contained in:
2021-12-03 23:28:23 +01:00
parent ecec5fae9b
commit 0a73134381
5 changed files with 50 additions and 6 deletions

View File

@@ -10,6 +10,7 @@ import (
"net/http"
"os"
"strconv"
"time"
)
var currentSong = -1
@@ -161,6 +162,20 @@ func getAllGames() []string {
return jsonArray
}
func getAllGamesRandom() []string {
if games == nil || len(games) == 0 {
games = findAllGames()
}
var jsonArray []string
for _, game := range games {
jsonArray = append(jsonArray, game.gameName)
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(jsonArray), func(i, j int) { jsonArray[i], jsonArray[j] = jsonArray[j], jsonArray[i] })
return jsonArray
}
func setPlayed(songNumber int) {
if songQue == nil || len(songQue) == 0 || songNumber >= len(songQue) {
return
@@ -248,6 +263,10 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(getAllGames())
} else if r.URL.Path == "/music/all/random" && r.Method == http.MethodGet {
w.Header().Add("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(getAllGamesRandom())
} else if r.URL.Path == "/music/played" && r.Method == http.MethodPut {
var p Played
err := json.NewDecoder(r.Body).Decode(&p)