1.0 of Go version
This commit is contained in:
243
musicFacade.go
Normal file
243
musicFacade.go
Normal file
@@ -0,0 +1,243 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var currentSong = -1
|
||||
var games []GameData
|
||||
var songQue []SongData
|
||||
|
||||
func getSoundCheckSong() string {
|
||||
reset()
|
||||
|
||||
files, err := ioutil.ReadDir("songs")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fileInfo := files[rand.Intn(len(files))]
|
||||
return "songs/" + fileInfo.Name()
|
||||
}
|
||||
|
||||
func reset() {
|
||||
songQue = nil
|
||||
currentSong = -1
|
||||
games = findAllGames()
|
||||
}
|
||||
|
||||
func getRandomSong() string {
|
||||
if games == nil || len(games) == 0 {
|
||||
games = findAllGames()
|
||||
}
|
||||
|
||||
game := getRandomGame(games)
|
||||
|
||||
songs := findSongsFromGame(game.id)
|
||||
|
||||
song := songs[rand.Intn(len(songs))]
|
||||
|
||||
currentSong = len(songQue)
|
||||
songQue = append(songQue, song)
|
||||
return song.path
|
||||
}
|
||||
|
||||
func getRandomSongLowChance() string {
|
||||
gameList := findAllGames()
|
||||
|
||||
var listOfGames []GameData
|
||||
|
||||
var averagePlayed = getAveragePlayed(gameList)
|
||||
|
||||
for _, data := range gameList {
|
||||
var timesToAdd = averagePlayed - data.timesPlayed
|
||||
if timesToAdd <= 0 {
|
||||
listOfGames = append(listOfGames, data)
|
||||
} else {
|
||||
for i := 0; i < timesToAdd; i++ {
|
||||
listOfGames = append(listOfGames, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
game := getRandomGame(listOfGames)
|
||||
|
||||
songs := findSongsFromGame(game.id)
|
||||
|
||||
song := songs[rand.Intn(len(songs))]
|
||||
|
||||
currentSong = len(songQue)
|
||||
songQue = append(songQue, song)
|
||||
return song.path
|
||||
|
||||
}
|
||||
|
||||
func getSongInfo() SongInfo {
|
||||
var currentSongData = songQue[currentSong]
|
||||
|
||||
currentGameData := getCurrentGame(currentSongData)
|
||||
|
||||
return SongInfo{
|
||||
Game: currentGameData.gameName,
|
||||
GamePlayed: currentGameData.timesPlayed,
|
||||
Song: currentSongData.songName,
|
||||
SongPlayed: currentSongData.timesPlayed,
|
||||
CurrentlyPlaying: true,
|
||||
SongNo: currentSong,
|
||||
}
|
||||
}
|
||||
|
||||
func getPlayedSongs() []SongInfo {
|
||||
var songList []SongInfo
|
||||
|
||||
for i, song := range songQue {
|
||||
gameData := getCurrentGame(song)
|
||||
songList = append(songList, SongInfo{
|
||||
Game: gameData.gameName,
|
||||
GamePlayed: gameData.timesPlayed,
|
||||
Song: song.songName,
|
||||
SongPlayed: song.timesPlayed,
|
||||
CurrentlyPlaying: i == currentSong,
|
||||
SongNo: i,
|
||||
})
|
||||
}
|
||||
return songList
|
||||
}
|
||||
|
||||
func getSong(song string) string {
|
||||
currentSong, _ = strconv.Atoi(song)
|
||||
if currentSong >= len(songQue) {
|
||||
currentSong = len(songQue) - 1
|
||||
} else if currentSong < 0 {
|
||||
currentSong = 0
|
||||
}
|
||||
var songData = songQue[currentSong]
|
||||
return songData.path
|
||||
}
|
||||
|
||||
func getAllGames() []string {
|
||||
if games == nil || len(games) == 0 {
|
||||
games = findAllGames()
|
||||
}
|
||||
|
||||
var jsonArray []string
|
||||
for _, game := range games {
|
||||
jsonArray = append(jsonArray, game.gameName)
|
||||
}
|
||||
return jsonArray
|
||||
}
|
||||
|
||||
func setPlayed(songNumber int) {
|
||||
if songQue == nil || len(songQue) == 0 || songNumber >= len(songQue) {
|
||||
return
|
||||
}
|
||||
var songData = songQue[songNumber]
|
||||
addGamePlayed(songData.gameId)
|
||||
addSongPlayed(songData.gameId, songData.songName)
|
||||
}
|
||||
|
||||
func getNextSong() string {
|
||||
if currentSong == len(songQue)-1 || currentSong == -1 {
|
||||
return getRandomSong()
|
||||
} else {
|
||||
currentSong = currentSong + 1
|
||||
var songData = songQue[currentSong]
|
||||
return songData.path
|
||||
}
|
||||
}
|
||||
|
||||
func getPreviousSong() string {
|
||||
if currentSong == -1 || currentSong == 0 {
|
||||
var songData = songQue[0]
|
||||
return songData.path
|
||||
} else {
|
||||
currentSong = currentSong - 1
|
||||
var songData = songQue[currentSong]
|
||||
return songData.path
|
||||
}
|
||||
}
|
||||
|
||||
func musicHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/music" && r.Method == http.MethodGet {
|
||||
song := r.URL.Query().Get("song")
|
||||
if song == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, err := fmt.Fprint(w, "song can't be empty")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
} else {
|
||||
http.ServeFile(w, r, getSong(song))
|
||||
}
|
||||
} else if r.URL.Path == "/music/first" && r.Method == http.MethodGet {
|
||||
http.ServeFile(w, r, getSoundCheckSong())
|
||||
|
||||
} else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet {
|
||||
reset()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
} else if r.URL.Path == "/music/rand" && r.Method == http.MethodGet {
|
||||
http.ServeFile(w, r, getRandomSong())
|
||||
|
||||
} else if r.URL.Path == "/music/rand/low" && r.Method == http.MethodGet {
|
||||
http.ServeFile(w, r, getRandomSongLowChance())
|
||||
|
||||
} else if r.URL.Path == "/music/info" && r.Method == http.MethodGet {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(getSongInfo())
|
||||
|
||||
} else if r.URL.Path == "/music/list" && r.Method == http.MethodGet {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(getPlayedSongs())
|
||||
|
||||
} else if r.URL.Path == "/music/next" {
|
||||
http.ServeFile(w, r, getNextSong())
|
||||
|
||||
} else if r.URL.Path == "/music/previous" {
|
||||
http.ServeFile(w, r, getPreviousSong())
|
||||
|
||||
} else if r.URL.Path == "/music/all" && r.Method == http.MethodGet {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(getAllGames())
|
||||
|
||||
} else if r.URL.Path == "/music/played" && r.Method == http.MethodPut {
|
||||
var p Played
|
||||
err := json.NewDecoder(r.Body).Decode(&p)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
setPlayed(p.song)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
type Played struct {
|
||||
song int
|
||||
}
|
||||
|
||||
func getCurrentGame(currentSongData SongData) GameData {
|
||||
for _, game := range games {
|
||||
if game.id == currentSongData.gameId {
|
||||
return game
|
||||
}
|
||||
}
|
||||
return GameData{}
|
||||
}
|
||||
|
||||
func getAveragePlayed(gameList []GameData) int {
|
||||
var sum int
|
||||
for _, data := range gameList {
|
||||
sum += data.timesPlayed
|
||||
}
|
||||
return sum / len(gameList)
|
||||
}
|
||||
|
||||
func getRandomGame(listOfGames []GameData) GameData {
|
||||
return listOfGames[rand.Intn(len(listOfGames))]
|
||||
}
|
||||
Reference in New Issue
Block a user