This commit is contained in:
+50
-50
@@ -12,8 +12,8 @@ import (
|
||||
)
|
||||
|
||||
type SongInfo struct {
|
||||
Game string `json:"Game"`
|
||||
GamePlayed int32 `json:"GamePlayed"`
|
||||
Soundtrack string `json:"Soundtrack"`
|
||||
SoundtrackPlayed int32 `json:"SoundtrackPlayed"`
|
||||
Song string `json:"Song"`
|
||||
SongPlayed int32 `json:"SongPlayed"`
|
||||
CurrentlyPlaying bool `json:"CurrentlyPlaying"`
|
||||
@@ -22,7 +22,7 @@ type SongInfo struct {
|
||||
|
||||
var currentSong = -1
|
||||
|
||||
var gamesNew []repository.Soundtrack
|
||||
var soundtracksNew []repository.Soundtrack
|
||||
|
||||
var songQueNew []repository.Song
|
||||
|
||||
@@ -37,12 +37,12 @@ func initRepo() {
|
||||
}
|
||||
}
|
||||
|
||||
func getAllGames() []repository.Soundtrack {
|
||||
if len(gamesNew) == 0 {
|
||||
func getAllSoundtracks() []repository.Soundtrack {
|
||||
if len(soundtracksNew) == 0 {
|
||||
initRepo()
|
||||
gamesNew, _ = BackendRepo().FindAllSoundtracks(BackendCtx())
|
||||
soundtracksNew, _ = BackendRepo().FindAllSoundtracks(BackendCtx())
|
||||
}
|
||||
return gamesNew
|
||||
return soundtracksNew
|
||||
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func Reset() {
|
||||
songQueNew = nil
|
||||
currentSong = -1
|
||||
initRepo()
|
||||
gamesNew, _ = BackendRepo().FindAllSoundtracks(BackendCtx())
|
||||
soundtracksNew, _ = BackendRepo().FindAllSoundtracks(BackendCtx())
|
||||
}
|
||||
|
||||
func AddLatestToQue() {
|
||||
@@ -92,34 +92,34 @@ func SetPlayed(songNumber int) {
|
||||
}
|
||||
|
||||
func GetRandomSong() string {
|
||||
getAllGames()
|
||||
if len(gamesNew) == 0 {
|
||||
getAllSoundtracks()
|
||||
if len(soundtracksNew) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
song := getSongFromList(gamesNew)
|
||||
song := getSongFromList(soundtracksNew)
|
||||
lastFetchedNew = song
|
||||
return song.Path
|
||||
}
|
||||
|
||||
func GetRandomSongLowChance() string {
|
||||
getAllGames()
|
||||
getAllSoundtracks()
|
||||
|
||||
var listOfGames []repository.Soundtrack
|
||||
var listOfSoundtracks []repository.Soundtrack
|
||||
|
||||
var averagePlayed = getAveragePlayed()
|
||||
|
||||
for _, data := range gamesNew {
|
||||
for _, data := range soundtracksNew {
|
||||
timesToAdd := averagePlayed - data.TimesPlayed
|
||||
if timesToAdd <= 0 {
|
||||
listOfGames = append(listOfGames, data)
|
||||
listOfSoundtracks = append(listOfSoundtracks, data)
|
||||
} else {
|
||||
for i := int32(0); i < timesToAdd; i++ {
|
||||
listOfGames = append(listOfGames, data)
|
||||
listOfSoundtracks = append(listOfSoundtracks, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
song := getSongFromList(listOfGames)
|
||||
song := getSongFromList(listOfSoundtracks)
|
||||
|
||||
lastFetchedNew = song
|
||||
return song.Path
|
||||
@@ -127,11 +127,11 @@ func GetRandomSongLowChance() string {
|
||||
}
|
||||
|
||||
func GetRandomSongClassic() string {
|
||||
getAllGames()
|
||||
getAllSoundtracks()
|
||||
|
||||
var listOfAllSongs []repository.Song
|
||||
for _, game := range gamesNew {
|
||||
songList, _ := BackendRepo().FindSongsFromSoundtrack(BackendCtx(), game.ID)
|
||||
for _, soundtrack := range soundtracksNew {
|
||||
songList, _ := BackendRepo().FindSongsFromSoundtrack(BackendCtx(), soundtrack.ID)
|
||||
listOfAllSongs = append(listOfAllSongs, songList...)
|
||||
}
|
||||
|
||||
@@ -139,25 +139,25 @@ func GetRandomSongClassic() string {
|
||||
var song repository.Song
|
||||
for !songFound {
|
||||
song = listOfAllSongs[rand.Intn(len(listOfAllSongs))]
|
||||
gameData, err := BackendRepo().GetSoundtrackById(BackendCtx(), song.SoundtrackID)
|
||||
soundtrackData, err := BackendRepo().GetSoundtrackById(BackendCtx(), song.SoundtrackID)
|
||||
|
||||
if err != nil {
|
||||
BackendRepo().RemoveBrokenSong(BackendCtx(), repository.RemoveBrokenSongParams{SoundtrackID: song.SoundtrackID, Path: song.Path})
|
||||
logging.GetLogger().Warn("Song not found, removed from database",
|
||||
zap.String("song", song.SongName),
|
||||
zap.String("game", gameData.SoundtrackName),
|
||||
zap.String("soundtrack", soundtrackData.SoundtrackName),
|
||||
zap.String("filename", *song.FileName))
|
||||
continue
|
||||
}
|
||||
|
||||
//Check if file exists and open
|
||||
openFile, err := os.Open(song.Path)
|
||||
if err != nil || (song.FileName != nil && gameData.Path+*song.FileName != song.Path) {
|
||||
if err != nil || (song.FileName != nil && soundtrackData.Path+*song.FileName != song.Path) {
|
||||
//File not found
|
||||
BackendRepo().RemoveBrokenSong(BackendCtx(), repository.RemoveBrokenSongParams{SoundtrackID: song.SoundtrackID, Path: song.Path})
|
||||
logging.GetLogger().Warn("Song not found, removed from database",
|
||||
zap.String("song", song.SongName),
|
||||
zap.String("game", gameData.SoundtrackName),
|
||||
zap.String("soundtrack", soundtrackData.SoundtrackName),
|
||||
zap.String("filename", *song.FileName))
|
||||
} else {
|
||||
songFound = true
|
||||
@@ -177,11 +177,11 @@ func GetSongInfo() SongInfo {
|
||||
}
|
||||
var currentSongData = songQueNew[currentSong]
|
||||
|
||||
currentGameData := getCurrentGame(currentSongData)
|
||||
currentSoundtrackData := getCurrentSoundtrack(currentSongData)
|
||||
|
||||
return SongInfo{
|
||||
Game: currentGameData.SoundtrackName,
|
||||
GamePlayed: currentGameData.TimesPlayed,
|
||||
Soundtrack: currentSoundtrackData.SoundtrackName,
|
||||
SoundtrackPlayed: currentSoundtrackData.TimesPlayed,
|
||||
Song: currentSongData.SongName,
|
||||
SongPlayed: currentSongData.TimesPlayed,
|
||||
CurrentlyPlaying: true,
|
||||
@@ -193,10 +193,10 @@ func GetPlayedSongs() []SongInfo {
|
||||
var songList []SongInfo
|
||||
|
||||
for i, song := range songQueNew {
|
||||
gameData := getCurrentGame(song)
|
||||
soundtrackData := getCurrentSoundtrack(song)
|
||||
songList = append(songList, SongInfo{
|
||||
Game: gameData.SoundtrackName,
|
||||
GamePlayed: gameData.TimesPlayed,
|
||||
Soundtrack: soundtrackData.SoundtrackName,
|
||||
SoundtrackPlayed: soundtrackData.TimesPlayed,
|
||||
Song: song.SongName,
|
||||
SongPlayed: song.TimesPlayed,
|
||||
CurrentlyPlaying: i == currentSong,
|
||||
@@ -218,21 +218,21 @@ func GetSong(song string) string {
|
||||
}
|
||||
|
||||
func GetAllSoundtracks() []string {
|
||||
getAllGames()
|
||||
getAllSoundtracks()
|
||||
|
||||
var jsonArray []string
|
||||
for _, game := range gamesNew {
|
||||
jsonArray = append(jsonArray, game.SoundtrackName)
|
||||
for _, soundtrack := range soundtracksNew {
|
||||
jsonArray = append(jsonArray, soundtrack.SoundtrackName)
|
||||
}
|
||||
return jsonArray
|
||||
}
|
||||
|
||||
func GetAllSoundtracksRandom() []string {
|
||||
getAllGames()
|
||||
getAllSoundtracks()
|
||||
|
||||
var jsonArray []string
|
||||
for _, game := range gamesNew {
|
||||
jsonArray = append(jsonArray, game.SoundtrackName)
|
||||
for _, soundtrack := range soundtracksNew {
|
||||
jsonArray = append(jsonArray, soundtrack.SoundtrackName)
|
||||
}
|
||||
rand.Shuffle(len(jsonArray), func(i, j int) { jsonArray[i], jsonArray[j] = jsonArray[j], jsonArray[i] })
|
||||
return jsonArray
|
||||
@@ -266,12 +266,12 @@ func GetPreviousSong() string {
|
||||
}
|
||||
}
|
||||
|
||||
func getSongFromList(games []repository.Soundtrack) repository.Song {
|
||||
func getSongFromList(soundtracks []repository.Soundtrack) repository.Song {
|
||||
songFound := false
|
||||
var song repository.Song
|
||||
for !songFound {
|
||||
game := getRandomGame(games)
|
||||
songs, _ := BackendRepo().FindSongsFromSoundtrack(BackendCtx(), game.ID)
|
||||
soundtrack := getRandomSoundtrack(soundtracks)
|
||||
songs, _ := BackendRepo().FindSongsFromSoundtrack(BackendCtx(), soundtrack.ID)
|
||||
if len(songs) == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -280,12 +280,12 @@ func getSongFromList(games []repository.Soundtrack) repository.Song {
|
||||
|
||||
//Check if file exists and open
|
||||
openFile, err := os.Open(song.Path)
|
||||
if err != nil || (song.FileName != nil && game.Path+*song.FileName != song.Path) || (song.FileName != nil && strings.HasSuffix(*song.FileName, ".wav")) {
|
||||
if err != nil || (song.FileName != nil && soundtrack.Path+*song.FileName != song.Path) || (song.FileName != nil && strings.HasSuffix(*song.FileName, ".wav")) {
|
||||
//File not found
|
||||
BackendRepo().RemoveBrokenSong(BackendCtx(), repository.RemoveBrokenSongParams{SoundtrackID: song.SoundtrackID, Path: song.Path})
|
||||
logging.GetLogger().Warn("Song not found, removed from database",
|
||||
zap.String("song", song.SongName),
|
||||
zap.String("game", game.SoundtrackName),
|
||||
zap.String("soundtrack", soundtrack.SoundtrackName),
|
||||
zap.Any("filename", song.FileName))
|
||||
} else {
|
||||
songFound = true
|
||||
@@ -299,24 +299,24 @@ func getSongFromList(games []repository.Soundtrack) repository.Song {
|
||||
return song
|
||||
}
|
||||
|
||||
func getCurrentGame(currentSongData repository.Song) repository.Soundtrack {
|
||||
for _, game := range gamesNew {
|
||||
if game.ID == currentSongData.SoundtrackID {
|
||||
return game
|
||||
func getCurrentSoundtrack(currentSongData repository.Song) repository.Soundtrack {
|
||||
for _, soundtrack := range soundtracksNew {
|
||||
if soundtrack.ID == currentSongData.SoundtrackID {
|
||||
return soundtrack
|
||||
}
|
||||
}
|
||||
return repository.Soundtrack{}
|
||||
}
|
||||
|
||||
func getAveragePlayed() int32 {
|
||||
getAllGames()
|
||||
getAllSoundtracks()
|
||||
var sum int32
|
||||
for _, data := range gamesNew {
|
||||
for _, data := range soundtracksNew {
|
||||
sum += data.TimesPlayed
|
||||
}
|
||||
return sum / int32(len(gamesNew))
|
||||
return sum / int32(len(soundtracksNew))
|
||||
}
|
||||
|
||||
func getRandomGame(listOfGames []repository.Soundtrack) repository.Soundtrack {
|
||||
return listOfGames[rand.Intn(len(listOfGames))]
|
||||
func getRandomSoundtrack(listOfSoundtracks []repository.Soundtrack) repository.Soundtrack {
|
||||
return listOfSoundtracks[rand.Intn(len(listOfSoundtracks))]
|
||||
}
|
||||
|
||||
@@ -9,17 +9,17 @@ import (
|
||||
|
||||
// Test the average calculation logic directly without database access
|
||||
func TestCalculateAverage(t *testing.T) {
|
||||
games := []repository.Soundtrack{
|
||||
{SoundtrackName: "Game1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Game2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Game3", TimesPlayed: 30},
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{SoundtrackName: "Soundtrack1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Soundtrack2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Soundtrack3", TimesPlayed: 30},
|
||||
}
|
||||
|
||||
var sum int32
|
||||
for _, data := range games {
|
||||
for _, data := range soundtracks {
|
||||
sum += data.TimesPlayed
|
||||
}
|
||||
result := sum / int32(len(games))
|
||||
result := sum / int32(len(soundtracks))
|
||||
expected := int32(20)
|
||||
|
||||
if result != expected {
|
||||
@@ -28,9 +28,9 @@ func TestCalculateAverage(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCalculateAverageEmpty(t *testing.T) {
|
||||
games := []repository.Soundtrack{}
|
||||
soundtracks := []repository.Soundtrack{}
|
||||
|
||||
if len(games) == 0 {
|
||||
if len(soundtracks) == 0 {
|
||||
result := int32(0)
|
||||
expected := int32(0)
|
||||
if result != expected {
|
||||
@@ -40,10 +40,10 @@ func TestCalculateAverageEmpty(t *testing.T) {
|
||||
}
|
||||
|
||||
var sum int32
|
||||
for _, data := range games {
|
||||
for _, data := range soundtracks {
|
||||
sum += data.TimesPlayed
|
||||
}
|
||||
result := sum / int32(len(games))
|
||||
result := sum / int32(len(soundtracks))
|
||||
expected := int32(0)
|
||||
|
||||
if result != expected {
|
||||
@@ -52,107 +52,107 @@ func TestCalculateAverageEmpty(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCalculateAverageSingle(t *testing.T) {
|
||||
games := []repository.Soundtrack{
|
||||
{SoundtrackName: "Game1", TimesPlayed: 42},
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{SoundtrackName: "Soundtrack1", TimesPlayed: 42},
|
||||
}
|
||||
|
||||
var sum int32
|
||||
for _, data := range games {
|
||||
for _, data := range soundtracks {
|
||||
sum += data.TimesPlayed
|
||||
}
|
||||
result := sum / int32(len(games))
|
||||
result := sum / int32(len(soundtracks))
|
||||
expected := int32(42)
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("Average calculation with single game = %v, want %v", result, expected)
|
||||
t.Errorf("Average calculation with single soundtrack = %v, want %v", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandomGame(t *testing.T) {
|
||||
games := []repository.Soundtrack{
|
||||
{SoundtrackName: "Game1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Game2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Game3", TimesPlayed: 30},
|
||||
func TestGetRandomSoundtrack(t *testing.T) {
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{SoundtrackName: "Soundtrack1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Soundtrack2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Soundtrack3", TimesPlayed: 30},
|
||||
}
|
||||
|
||||
// Set seed for reproducible tests
|
||||
rand.Seed(42)
|
||||
|
||||
result := games[rand.Intn(len(games))]
|
||||
result := soundtracks[rand.Intn(len(soundtracks))]
|
||||
|
||||
if result.SoundtrackName == "" {
|
||||
t.Error("random game selection returned empty game")
|
||||
t.Error("random soundtrack selection returned empty soundtrack")
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, g := range games {
|
||||
if g.SoundtrackName == result.SoundtrackName {
|
||||
for _, s := range soundtracks {
|
||||
if s.SoundtrackName == result.SoundtrackName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Errorf("random game selection returned game not in list: %v", result.SoundtrackName)
|
||||
t.Errorf("random soundtrack selection returned soundtrack not in list: %v", result.SoundtrackName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindGameByID(t *testing.T) {
|
||||
games := []repository.Soundtrack{
|
||||
{ID: 1, SoundtrackName: "Game1", TimesPlayed: 10},
|
||||
{ID: 2, SoundtrackName: "Game2", TimesPlayed: 20},
|
||||
{ID: 3, SoundtrackName: "Game3", TimesPlayed: 30},
|
||||
func TestFindSoundtrackByID(t *testing.T) {
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{ID: 1, SoundtrackName: "Soundtrack1", TimesPlayed: 10},
|
||||
{ID: 2, SoundtrackName: "Soundtrack2", TimesPlayed: 20},
|
||||
{ID: 3, SoundtrackName: "Soundtrack3", TimesPlayed: 30},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
games []repository.Soundtrack
|
||||
gameID int32
|
||||
expected repository.Soundtrack
|
||||
name string
|
||||
soundtracks []repository.Soundtrack
|
||||
soundtrackID int32
|
||||
expected repository.Soundtrack
|
||||
}{
|
||||
{
|
||||
name: "existing game",
|
||||
games: games,
|
||||
gameID: 2,
|
||||
expected: repository.Soundtrack{ID: 2, SoundtrackName: "Game2", TimesPlayed: 20},
|
||||
name: "existing soundtrack",
|
||||
soundtracks: soundtracks,
|
||||
soundtrackID: 2,
|
||||
expected: repository.Soundtrack{ID: 2, SoundtrackName: "Soundtrack2", TimesPlayed: 20},
|
||||
},
|
||||
{
|
||||
name: "non-existing game",
|
||||
games: games,
|
||||
gameID: 99,
|
||||
expected: repository.Soundtrack{},
|
||||
name: "non-existing soundtrack",
|
||||
soundtracks: soundtracks,
|
||||
soundtrackID: 99,
|
||||
expected: repository.Soundtrack{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var result repository.Soundtrack
|
||||
for _, game := range tt.games {
|
||||
if game.ID == tt.gameID {
|
||||
result = game
|
||||
for _, s := range tt.soundtracks {
|
||||
if s.ID == tt.soundtrackID {
|
||||
result = s
|
||||
break
|
||||
}
|
||||
}
|
||||
if result.ID != tt.expected.ID || result.SoundtrackName != tt.expected.SoundtrackName {
|
||||
t.Errorf("findGameByID() = %v, want %v", result, tt.expected)
|
||||
t.Errorf("findSoundtrackByID() = %v, want %v", result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractSoundtrackNames(t *testing.T) {
|
||||
games := []repository.Soundtrack{
|
||||
{SoundtrackName: "Game1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Game2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Game3", TimesPlayed: 30},
|
||||
soundtracks := []repository.Soundtrack{
|
||||
{SoundtrackName: "Soundtrack1", TimesPlayed: 10},
|
||||
{SoundtrackName: "Soundtrack2", TimesPlayed: 20},
|
||||
{SoundtrackName: "Soundtrack3", TimesPlayed: 30},
|
||||
}
|
||||
|
||||
var result []string
|
||||
for _, game := range games {
|
||||
result = append(result, game.SoundtrackName)
|
||||
for _, s := range soundtracks {
|
||||
result = append(result, s.SoundtrackName)
|
||||
}
|
||||
|
||||
expected := []string{"Game1", "Game2", "Game3"}
|
||||
expected := []string{"Soundtrack1", "Soundtrack2", "Soundtrack3"}
|
||||
|
||||
if len(result) != len(expected) {
|
||||
t.Errorf("extractSoundtrackNames() length = %d, want %d", len(result), len(expected))
|
||||
@@ -167,29 +167,29 @@ func TestExtractSoundtrackNames(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestShuffleSoundtrackNames(t *testing.T) {
|
||||
games := []string{"Game1", "Game2", "Game3"}
|
||||
soundtracks := []string{"Soundtrack1", "Soundtrack2", "Soundtrack3"}
|
||||
|
||||
// Test that shuffle doesn't lose any elements
|
||||
// We can't test the order since it's random, but we can test length and contents
|
||||
original := make([]string, len(games))
|
||||
copy(original, games)
|
||||
original := make([]string, len(soundtracks))
|
||||
copy(original, soundtracks)
|
||||
|
||||
// Simple shuffle implementation for testing
|
||||
for i := range games {
|
||||
for i := range soundtracks {
|
||||
j := i // In real code this would be random
|
||||
games[i], games[j] = games[j], games[i]
|
||||
soundtracks[i], soundtracks[j] = soundtracks[j], soundtracks[i]
|
||||
}
|
||||
|
||||
if len(games) != len(original) {
|
||||
t.Errorf("shuffleSoundtrackNames() changed length from %d to %d", len(original), len(games))
|
||||
if len(soundtracks) != len(original) {
|
||||
t.Errorf("shuffleSoundtrackNames() changed length from %d to %d", len(original), len(soundtracks))
|
||||
return
|
||||
}
|
||||
|
||||
// Check all original elements are still present
|
||||
for _, orig := range original {
|
||||
found := false
|
||||
for _, g := range games {
|
||||
if g == orig {
|
||||
for _, s := range soundtracks {
|
||||
if s == orig {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
+108
-108
@@ -9,34 +9,34 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// GameWithSongs represents a game with its songs for statistics
|
||||
type GameWithSongs struct {
|
||||
SoundtrackID int32 `json:"game_id"`
|
||||
SoundtrackName string `json:"game_name"`
|
||||
SoundtrackPlayed int32 `json:"game_played"`
|
||||
SoundtrackLastPlayed *time.Time `json:"game_last_played,omitempty"`
|
||||
Songs []SongInfoForStats `json:"songs"`
|
||||
// SoundtrackWithSongs represents a soundtrack with its songs for statistics
|
||||
type SoundtrackWithSongs struct {
|
||||
SoundtrackID int32 `json:"soundtrack_id"`
|
||||
SoundtrackName string `json:"soundtrack_name"`
|
||||
SoundtrackPlayed int32 `json:"soundtrack_played"`
|
||||
SoundtrackLastPlayed *time.Time `json:"soundtrack_last_played,omitempty"`
|
||||
Songs []SongInfoForStats `json:"songs"`
|
||||
}
|
||||
|
||||
// SongInfoForStats represents a song with game info for statistics
|
||||
// SongInfoForStats represents a song with soundtrack info for statistics
|
||||
type SongInfoForStats struct {
|
||||
SoundtrackID int32 `json:"game_id"`
|
||||
SoundtrackName string `json:"game_name"`
|
||||
SongName string `json:"song_name"`
|
||||
Path string `json:"path"`
|
||||
TimesPlayed int32 `json:"times_played"`
|
||||
FileName *string `json:"file_name,omitempty"`
|
||||
SoundtrackID int32 `json:"soundtrack_id"`
|
||||
SoundtrackName string `json:"soundtrack_name"`
|
||||
SongName string `json:"song_name"`
|
||||
Path string `json:"path"`
|
||||
TimesPlayed int32 `json:"times_played"`
|
||||
FileName *string `json:"file_name,omitempty"`
|
||||
}
|
||||
|
||||
// StatisticsSummary holds overall statistics
|
||||
type StatisticsSummary struct {
|
||||
TotalGames int64 `json:"total_games"`
|
||||
PlayedGames int64 `json:"played_games"`
|
||||
NeverPlayedGames int64 `json:"never_played_games"`
|
||||
TotalGamePlays int64 `json:"total_game_plays"`
|
||||
AvgGamePlays float64 `json:"avg_game_plays"`
|
||||
MaxGamePlays int64 `json:"max_game_plays"`
|
||||
MinGamePlays int64 `json:"min_game_plays"`
|
||||
TotalSoundtracks int64 `json:"total_soundtracks"`
|
||||
PlayedSoundtracks int64 `json:"played_soundtracks"`
|
||||
NeverPlayedSoundtracks int64 `json:"never_played_soundtracks"`
|
||||
TotalSoundtrackPlays int64 `json:"total_soundtrack_plays"`
|
||||
AvgSoundtrackPlays float64 `json:"avg_soundtrack_plays"`
|
||||
MaxSoundtrackPlays int64 `json:"max_soundtrack_plays"`
|
||||
MinSoundtrackPlays int64 `json:"min_soundtrack_plays"`
|
||||
}
|
||||
|
||||
// StatisticsHandler manages statistics operations
|
||||
@@ -49,19 +49,19 @@ func NewStatisticsHandler() *StatisticsHandler {
|
||||
return &StatisticsHandler{}
|
||||
}
|
||||
|
||||
// GetMostPlayedGamesWithSongs returns the top N most played games with their songs
|
||||
func (h *StatisticsHandler) GetMostPlayedGamesWithSongs(limit int32) ([]GameWithSongs, error) {
|
||||
// GetMostPlayedSoundtracksWithSongs returns the top N most played soundtracks with their songs
|
||||
func (h *StatisticsHandler) GetMostPlayedSoundtracksWithSongs(limit int32) ([]SoundtrackWithSongs, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
|
||||
// Get raw results
|
||||
rows, err := queries.GetMostPlayedGamesWithSongs(ctx, limit)
|
||||
rows, err := queries.GetMostPlayedSoundtracksWithSongs(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert to GameWithSongs
|
||||
var result []GameWithSongs
|
||||
|
||||
// Convert to SoundtrackWithSongs
|
||||
var result []SoundtrackWithSongs
|
||||
for _, row := range rows {
|
||||
var songs []SongInfoForStats
|
||||
if row.Songs != nil {
|
||||
@@ -71,28 +71,28 @@ func (h *StatisticsHandler) GetMostPlayedGamesWithSongs(limit int32) ([]GameWith
|
||||
songs = make([]SongInfoForStats, 0)
|
||||
}
|
||||
}
|
||||
result = append(result, GameWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
result = append(result, SoundtrackWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
SoundtrackLastPlayed: row.SoundtrackLastPlayed,
|
||||
Songs: songs,
|
||||
Songs: songs,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetLeastPlayedGamesWithSongs returns the top N least played games with their songs
|
||||
func (h *StatisticsHandler) GetLeastPlayedGamesWithSongs(limit int32) ([]GameWithSongs, error) {
|
||||
// GetLeastPlayedSoundtracksWithSongs returns the top N least played soundtracks with their songs
|
||||
func (h *StatisticsHandler) GetLeastPlayedSoundtracksWithSongs(limit int32) ([]SoundtrackWithSongs, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetLeastPlayedGamesWithSongs(ctx, limit)
|
||||
|
||||
rows, err := queries.GetLeastPlayedSoundtracksWithSongs(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []GameWithSongs
|
||||
|
||||
var result []SoundtrackWithSongs
|
||||
for _, row := range rows {
|
||||
var songs []SongInfoForStats
|
||||
if row.Songs != nil {
|
||||
@@ -100,76 +100,76 @@ func (h *StatisticsHandler) GetLeastPlayedGamesWithSongs(limit int32) ([]GameWit
|
||||
songs = make([]SongInfoForStats, 0)
|
||||
}
|
||||
}
|
||||
result = append(result, GameWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
result = append(result, SoundtrackWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
SoundtrackLastPlayed: row.SoundtrackLastPlayed,
|
||||
Songs: songs,
|
||||
Songs: songs,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetMostPlayedSongsWithGame returns the top N most played songs with their game info
|
||||
func (h *StatisticsHandler) GetMostPlayedSongsWithGame(limit int32) ([]SongInfoForStats, error) {
|
||||
// GetMostPlayedSongsWithSoundtrack returns the top N most played songs with their soundtrack info
|
||||
func (h *StatisticsHandler) GetMostPlayedSongsWithSoundtrack(limit int32) ([]SongInfoForStats, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetMostPlayedSongsWithGame(ctx, limit)
|
||||
|
||||
rows, err := queries.GetMostPlayedSongsWithSoundtrack(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
var result []SongInfoForStats
|
||||
for _, row := range rows {
|
||||
result = append(result, SongInfoForStats{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SongName: row.SongName,
|
||||
Path: row.Path,
|
||||
TimesPlayed: row.TimesPlayed,
|
||||
FileName: row.FileName,
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SongName: row.SongName,
|
||||
Path: row.Path,
|
||||
TimesPlayed: row.TimesPlayed,
|
||||
FileName: row.FileName,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetLeastPlayedSongsWithGame returns the top N least played songs with their game info
|
||||
func (h *StatisticsHandler) GetLeastPlayedSongsWithGame(limit int32) ([]SongInfoForStats, error) {
|
||||
// GetLeastPlayedSongsWithSoundtrack returns the top N least played songs with their soundtrack info
|
||||
func (h *StatisticsHandler) GetLeastPlayedSongsWithSoundtrack(limit int32) ([]SongInfoForStats, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetLeastPlayedSongsWithGame(ctx, limit)
|
||||
|
||||
rows, err := queries.GetLeastPlayedSongsWithSoundtrack(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
var result []SongInfoForStats
|
||||
for _, row := range rows {
|
||||
result = append(result, SongInfoForStats{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SongName: row.SongName,
|
||||
Path: row.Path,
|
||||
TimesPlayed: row.TimesPlayed,
|
||||
FileName: row.FileName,
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SongName: row.SongName,
|
||||
Path: row.Path,
|
||||
TimesPlayed: row.TimesPlayed,
|
||||
FileName: row.FileName,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetNeverPlayedGames returns games that have never been played
|
||||
func (h *StatisticsHandler) GetNeverPlayedGames() ([]GameWithSongs, error) {
|
||||
// GetNeverPlayedSoundtracks returns soundtracks that have never been played
|
||||
func (h *StatisticsHandler) GetNeverPlayedSoundtracks() ([]SoundtrackWithSongs, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetNeverPlayedGames(ctx)
|
||||
|
||||
rows, err := queries.GetNeverPlayedSoundtracks(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []GameWithSongs
|
||||
|
||||
var result []SoundtrackWithSongs
|
||||
for _, row := range rows {
|
||||
var songs []SongInfoForStats
|
||||
if row.Songs != nil {
|
||||
@@ -177,28 +177,28 @@ func (h *StatisticsHandler) GetNeverPlayedGames() ([]GameWithSongs, error) {
|
||||
songs = make([]SongInfoForStats, 0)
|
||||
}
|
||||
}
|
||||
result = append(result, GameWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
result = append(result, SoundtrackWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
SoundtrackLastPlayed: nil,
|
||||
Songs: songs,
|
||||
Songs: songs,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetLastPlayedGames returns the most recently played games
|
||||
func (h *StatisticsHandler) GetLastPlayedGames(limit int32) ([]GameWithSongs, error) {
|
||||
// GetLastPlayedSoundtracks returns the most recently played soundtracks
|
||||
func (h *StatisticsHandler) GetLastPlayedSoundtracks(limit int32) ([]SoundtrackWithSongs, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetLastPlayedGames(ctx, limit)
|
||||
|
||||
rows, err := queries.GetLastPlayedSoundtracks(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []GameWithSongs
|
||||
|
||||
var result []SoundtrackWithSongs
|
||||
for _, row := range rows {
|
||||
var songs []SongInfoForStats
|
||||
if row.Songs != nil {
|
||||
@@ -206,28 +206,28 @@ func (h *StatisticsHandler) GetLastPlayedGames(limit int32) ([]GameWithSongs, er
|
||||
songs = make([]SongInfoForStats, 0)
|
||||
}
|
||||
}
|
||||
result = append(result, GameWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
result = append(result, SoundtrackWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
SoundtrackLastPlayed: row.SoundtrackLastPlayed,
|
||||
Songs: songs,
|
||||
Songs: songs,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetOldestPlayedGames returns the least recently played games
|
||||
func (h *StatisticsHandler) GetOldestPlayedGames(limit int32) ([]GameWithSongs, error) {
|
||||
// GetOldestPlayedSoundtracks returns the least recently played soundtracks
|
||||
func (h *StatisticsHandler) GetOldestPlayedSoundtracks(limit int32) ([]SoundtrackWithSongs, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
rows, err := queries.GetOldestPlayedGames(ctx, limit)
|
||||
|
||||
rows, err := queries.GetOldestPlayedSoundtracks(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []GameWithSongs
|
||||
|
||||
var result []SoundtrackWithSongs
|
||||
for _, row := range rows {
|
||||
var songs []SongInfoForStats
|
||||
if row.Songs != nil {
|
||||
@@ -235,12 +235,12 @@ func (h *StatisticsHandler) GetOldestPlayedGames(limit int32) ([]GameWithSongs,
|
||||
songs = make([]SongInfoForStats, 0)
|
||||
}
|
||||
}
|
||||
result = append(result, GameWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
result = append(result, SoundtrackWithSongs{
|
||||
SoundtrackID: row.SoundtrackID,
|
||||
SoundtrackName: row.SoundtrackName,
|
||||
SoundtrackPlayed: row.SoundtrackPlayed,
|
||||
SoundtrackLastPlayed: row.SoundtrackLastPlayed,
|
||||
Songs: songs,
|
||||
Songs: songs,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
@@ -250,20 +250,20 @@ func (h *StatisticsHandler) GetOldestPlayedGames(limit int32) ([]GameWithSongs,
|
||||
func (h *StatisticsHandler) GetStatisticsSummary() (*StatisticsSummary, error) {
|
||||
queries := BackendRepo()
|
||||
ctx := BackendCtx()
|
||||
|
||||
|
||||
row, err := queries.GetStatisticsSummary(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
return &StatisticsSummary{
|
||||
TotalGames: int64(row.TotalSoundtracks),
|
||||
PlayedGames: int64(row.PlayedSoundtracks),
|
||||
NeverPlayedGames: int64(row.NeverPlayedSoundtracks),
|
||||
TotalGamePlays: int64(row.TotalSoundtrackPlays),
|
||||
AvgGamePlays: float64(row.AvgSoundtrackPlays),
|
||||
MaxGamePlays: int64(row.MaxSoundtrackPlays),
|
||||
MinGamePlays: int64(row.MinSoundtrackPlays),
|
||||
TotalSoundtracks: int64(row.TotalSoundtracks),
|
||||
PlayedSoundtracks: int64(row.PlayedSoundtracks),
|
||||
NeverPlayedSoundtracks: int64(row.NeverPlayedSoundtracks),
|
||||
TotalSoundtrackPlays: int64(row.TotalSoundtrackPlays),
|
||||
AvgSoundtrackPlays: float64(row.AvgSoundtrackPlays),
|
||||
MaxSoundtrackPlays: int64(row.MaxSoundtrackPlays),
|
||||
MinSoundtrackPlays: int64(row.MinSoundtrackPlays),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
+131
-131
@@ -30,14 +30,14 @@ var start time.Time
|
||||
var totalTime time.Duration
|
||||
var timeSpent time.Duration
|
||||
|
||||
var allGames []repository.Soundtrack
|
||||
var gamesBeforeSync []repository.Soundtrack
|
||||
var gamesAfterSync []repository.Soundtrack
|
||||
var gamesAdded []string
|
||||
var gamesReAdded []string
|
||||
var gamesChangedTitle map[string]string
|
||||
var gamesChangedContent []string
|
||||
var gamesRemoved []string
|
||||
var allSoundtracks []repository.Soundtrack
|
||||
var soundtracksBeforeSync []repository.Soundtrack
|
||||
var soundtracksAfterSync []repository.Soundtrack
|
||||
var soundtracksAdded []string
|
||||
var soundtracksReAdded []string
|
||||
var soundtracksChangedTitle map[string]string
|
||||
var soundtracksChangedContent []string
|
||||
var soundtracksRemoved []string
|
||||
var catchedErrors []string
|
||||
|
||||
type brokenSong struct {
|
||||
@@ -50,13 +50,13 @@ var pool *ants.Pool
|
||||
var poolSong *ants.Pool
|
||||
|
||||
type SyncResponse struct {
|
||||
GamesAdded []string `json:"games_added"`
|
||||
GamesReAdded []string `json:"games_re_added"`
|
||||
GamesChangedTitle map[string]string `json:"games_changed_title"`
|
||||
GamesChangedContent []string `json:"games_changed_content"`
|
||||
GamesRemoved []string `json:"games_removed"`
|
||||
CatchedErrors []string `json:"catched_errors"`
|
||||
TotalTime string `json:"total_time"`
|
||||
SoundtracksAdded []string `json:"soundtracks_added"`
|
||||
SoundtracksReAdded []string `json:"soundtracks_re_added"`
|
||||
SoundtracksChangedTitle map[string]string `json:"soundtracks_changed_title"`
|
||||
SoundtracksChangedContent []string `json:"soundtracks_changed_content"`
|
||||
SoundtracksRemoved []string `json:"soundtracks_removed"`
|
||||
CatchedErrors []string `json:"catched_errors"`
|
||||
TotalTime string `json:"total_time"`
|
||||
}
|
||||
|
||||
type ProgressResponse struct {
|
||||
@@ -64,24 +64,24 @@ type ProgressResponse struct {
|
||||
TimeSpent string `json:"time_spent"`
|
||||
}
|
||||
|
||||
type GameStatus int
|
||||
type SoundtrackStatus int
|
||||
|
||||
const (
|
||||
NotChanged GameStatus = iota
|
||||
NotChanged SoundtrackStatus = iota
|
||||
TitleChanged
|
||||
GameChanged
|
||||
NewGame
|
||||
SoundtrackChanged
|
||||
NewSoundtrack
|
||||
)
|
||||
|
||||
var statusName = map[GameStatus]string{
|
||||
NotChanged: "Not changed",
|
||||
TitleChanged: "Title changed",
|
||||
GameChanged: "Game changed",
|
||||
NewGame: "New game",
|
||||
var statusName = map[SoundtrackStatus]string{
|
||||
NotChanged: "Not changed",
|
||||
TitleChanged: "Title changed",
|
||||
SoundtrackChanged: "Soundtrack changed",
|
||||
NewSoundtrack: "New soundtrack",
|
||||
}
|
||||
|
||||
func (gs GameStatus) String() string {
|
||||
return statusName[gs]
|
||||
func (ss SoundtrackStatus) String() string {
|
||||
return statusName[ss]
|
||||
}
|
||||
|
||||
func ResetDB() {
|
||||
@@ -107,54 +107,54 @@ func SyncProgress() ProgressResponse {
|
||||
|
||||
func SyncResult() SyncResponse {
|
||||
logging.GetLogger().Info("Sync completed",
|
||||
zap.Int("games_before", len(gamesBeforeSync)),
|
||||
zap.Int("games_after", len(gamesAfterSync)))
|
||||
zap.Int("soundtracks_before", len(soundtracksBeforeSync)),
|
||||
zap.Int("soundtracks_after", len(soundtracksAfterSync)))
|
||||
|
||||
if len(gamesAdded) > 0 {
|
||||
logging.GetLogger().Debug("Games added", zap.Strings("games", gamesAdded))
|
||||
if len(soundtracksAdded) > 0 {
|
||||
logging.GetLogger().Debug("Soundtracks added", zap.Strings("soundtracks", soundtracksAdded))
|
||||
}
|
||||
|
||||
if len(gamesReAdded) > 0 {
|
||||
logging.GetLogger().Debug("Games readded", zap.Strings("games", gamesReAdded))
|
||||
if len(soundtracksReAdded) > 0 {
|
||||
logging.GetLogger().Debug("Soundtracks readded", zap.Strings("soundtracks", soundtracksReAdded))
|
||||
}
|
||||
|
||||
if len(gamesChangedTitle) > 0 {
|
||||
logging.GetLogger().Debug("Games with changed title", zap.Any("changes", gamesChangedTitle))
|
||||
if len(soundtracksChangedTitle) > 0 {
|
||||
logging.GetLogger().Debug("Soundtracks with changed title", zap.Any("changes", soundtracksChangedTitle))
|
||||
}
|
||||
|
||||
if len(gamesChangedContent) > 0 {
|
||||
logging.GetLogger().Debug("Games with changed content", zap.Strings("games", gamesChangedContent))
|
||||
if len(soundtracksChangedContent) > 0 {
|
||||
logging.GetLogger().Debug("Soundtracks with changed content", zap.Strings("soundtracks", soundtracksChangedContent))
|
||||
}
|
||||
|
||||
var gamesRemovedTemp []string
|
||||
for _, beforeGame := range gamesBeforeSync {
|
||||
var soundtracksRemovedTemp []string
|
||||
for _, beforeSoundtrack := range soundtracksBeforeSync {
|
||||
var found = false
|
||||
for _, afterGame := range gamesAfterSync {
|
||||
if beforeGame.SoundtrackName == afterGame.SoundtrackName {
|
||||
for _, afterSoundtrack := range soundtracksAfterSync {
|
||||
if beforeSoundtrack.SoundtrackName == afterSoundtrack.SoundtrackName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
gamesRemovedTemp = append(gamesRemovedTemp, beforeGame.SoundtrackName)
|
||||
soundtracksRemovedTemp = append(soundtracksRemovedTemp, beforeSoundtrack.SoundtrackName)
|
||||
}
|
||||
}
|
||||
|
||||
for _, game := range gamesRemovedTemp {
|
||||
for _, soundtrack := range soundtracksRemovedTemp {
|
||||
var found bool = false
|
||||
for key := range gamesChangedTitle {
|
||||
if game == key {
|
||||
for key := range soundtracksChangedTitle {
|
||||
if soundtrack == key {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
gamesRemoved = append(gamesRemoved, game)
|
||||
soundtracksRemoved = append(soundtracksRemoved, soundtrack)
|
||||
}
|
||||
}
|
||||
|
||||
if len(gamesRemoved) > 0 {
|
||||
logging.GetLogger().Debug("Games removed", zap.Strings("games", gamesRemoved))
|
||||
if len(soundtracksRemoved) > 0 {
|
||||
logging.GetLogger().Debug("Soundtracks removed", zap.Strings("soundtracks", soundtracksRemoved))
|
||||
}
|
||||
|
||||
if len(catchedErrors) > 0 {
|
||||
@@ -165,27 +165,27 @@ func SyncResult() SyncResponse {
|
||||
logging.GetLogger().Info("Sync completed", zap.String("total_time", out.Format("15:04:05.00000")))
|
||||
|
||||
return SyncResponse{
|
||||
GamesAdded: gamesAdded,
|
||||
GamesReAdded: gamesReAdded,
|
||||
GamesChangedTitle: gamesChangedTitle,
|
||||
GamesChangedContent: gamesChangedContent,
|
||||
GamesRemoved: gamesRemoved,
|
||||
CatchedErrors: catchedErrors,
|
||||
TotalTime: out.Format("15:04:05"),
|
||||
SoundtracksAdded: soundtracksAdded,
|
||||
SoundtracksReAdded: soundtracksReAdded,
|
||||
SoundtracksChangedTitle: soundtracksChangedTitle,
|
||||
SoundtracksChangedContent: soundtracksChangedContent,
|
||||
SoundtracksRemoved: soundtracksRemoved,
|
||||
CatchedErrors: catchedErrors,
|
||||
TotalTime: out.Format("15:04:05"),
|
||||
}
|
||||
}
|
||||
|
||||
func SyncSoundtracksNewFull() {
|
||||
syncGamesNew(true)
|
||||
syncSoundtracksNew(true)
|
||||
Reset()
|
||||
}
|
||||
|
||||
func SyncSoundtracksNewOnlyChanges() {
|
||||
syncGamesNew(false)
|
||||
syncSoundtracksNew(false)
|
||||
Reset()
|
||||
}
|
||||
|
||||
func syncGamesNew(full bool) {
|
||||
func syncSoundtracksNew(full bool) {
|
||||
musicPath := os.Getenv("MUSIC_PATH")
|
||||
fmt.Printf("dir: %s\n", musicPath)
|
||||
logging.GetLogger().Debug("Folder to sync", zap.String("MUSIC_PATH", musicPath))
|
||||
@@ -201,19 +201,19 @@ func syncGamesNew(full bool) {
|
||||
logging.GetLogger().Debug("Folders to skip during sync", zap.Strings("folders", foldersToSkip))
|
||||
|
||||
var err error
|
||||
gamesAdded = nil
|
||||
gamesReAdded = nil
|
||||
gamesChangedTitle = nil
|
||||
gamesChangedContent = nil
|
||||
gamesRemoved = nil
|
||||
soundtracksAdded = nil
|
||||
soundtracksReAdded = nil
|
||||
soundtracksChangedTitle = nil
|
||||
soundtracksChangedContent = nil
|
||||
soundtracksRemoved = nil
|
||||
catchedErrors = nil
|
||||
brokenSongs = nil
|
||||
|
||||
gamesBeforeSync, err = repo.FindAllSoundtracks(BackendCtx())
|
||||
soundtracksBeforeSync, err = repo.FindAllSoundtracks(BackendCtx())
|
||||
handleError("FindAllSoundtracks Before", err, "")
|
||||
logging.GetLogger().Info("Starting sync", zap.Int("games_before", len(gamesBeforeSync)))
|
||||
logging.GetLogger().Info("Starting sync", zap.Int("soundtracks_before", len(soundtracksBeforeSync)))
|
||||
|
||||
allGames, err = repo.GetAllSoundtracksIncludingDeleted(BackendCtx())
|
||||
allSoundtracks, err = repo.GetAllSoundtracksIncludingDeleted(BackendCtx())
|
||||
handleError("GetAllSoundtracksIncludingDeleted", err, "")
|
||||
err = repo.SetSoundtrackDeletionDate(BackendCtx())
|
||||
handleError("SetSoundtrackDeletionDate", err, "")
|
||||
@@ -233,13 +233,13 @@ func syncGamesNew(full bool) {
|
||||
for _, dir := range directories {
|
||||
pool.Submit(func() {
|
||||
defer syncWg.Done()
|
||||
syncGameNew(dir, foldersToSkip, musicPath, full)
|
||||
syncSoundtrackNew(dir, foldersToSkip, musicPath, full)
|
||||
})
|
||||
}
|
||||
syncWg.Wait()
|
||||
checkBrokenSongsNew()
|
||||
|
||||
gamesAfterSync, err = repo.FindAllSoundtracks(BackendCtx())
|
||||
soundtracksAfterSync, err = repo.FindAllSoundtracks(BackendCtx())
|
||||
handleError("FindAllSoundtracks After", err, "")
|
||||
|
||||
finished := time.Now()
|
||||
@@ -286,48 +286,48 @@ func checkBrokenSongNew(song repository.Song) {
|
||||
}
|
||||
}
|
||||
|
||||
func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full bool) {
|
||||
func syncSoundtrackNew(file os.DirEntry, foldersToSkip []string, baseDir string, full bool) {
|
||||
if file.IsDir() && !contains(foldersToSkip, file.Name()) {
|
||||
logging.GetLogger().Debug("Syncing game", zap.String("game", file.Name()))
|
||||
gameDir := baseDir + file.Name() + "/"
|
||||
dirHash := getHashForDir(gameDir)
|
||||
logging.GetLogger().Debug("Syncing soundtrack", zap.String("soundtrack", file.Name()))
|
||||
soundtrackDir := baseDir + file.Name() + "/"
|
||||
dirHash := getHashForDir(soundtrackDir)
|
||||
|
||||
var status GameStatus = NewGame
|
||||
var oldGame repository.Soundtrack
|
||||
var status SoundtrackStatus = NewSoundtrack
|
||||
var oldSoundtrack repository.Soundtrack
|
||||
var id int32 = -1
|
||||
|
||||
//fmt.Printf("Games before: %d\n", len(gamesBeforeSync))
|
||||
//fmt.Printf("Soundtracks before: %d\n", len(soundtracksBeforeSync))
|
||||
|
||||
for _, currentGame := range allGames {
|
||||
oldGame = currentGame
|
||||
//fmt.Printf("%s | %s\n", oldGame.SoundtrackName, oldGame.Hash)
|
||||
if oldGame.SoundtrackName == file.Name() && oldGame.Hash == dirHash {
|
||||
for _, currentSoundtrack := range allSoundtracks {
|
||||
oldSoundtrack = currentSoundtrack
|
||||
//fmt.Printf("%s | %s\n", oldSoundtrack.SoundtrackName, oldSoundtrack.Hash)
|
||||
if oldSoundtrack.SoundtrackName == file.Name() && oldSoundtrack.Hash == dirHash {
|
||||
status = NotChanged
|
||||
id = oldGame.ID
|
||||
//fmt.Printf("Game not changed\n")
|
||||
id = oldSoundtrack.ID
|
||||
//fmt.Printf("Soundtrack not changed\n")
|
||||
break
|
||||
} else if oldGame.SoundtrackName == file.Name() && oldGame.Hash != dirHash {
|
||||
status = GameChanged
|
||||
id = oldGame.ID
|
||||
//fmt.Printf("Game changed\n")
|
||||
} else if oldSoundtrack.SoundtrackName == file.Name() && oldSoundtrack.Hash != dirHash {
|
||||
status = SoundtrackChanged
|
||||
id = oldSoundtrack.ID
|
||||
//fmt.Printf("Soundtrack changed\n")
|
||||
break
|
||||
} else if oldGame.SoundtrackName != file.Name() && oldGame.Hash == dirHash {
|
||||
} else if oldSoundtrack.SoundtrackName != file.Name() && oldSoundtrack.Hash == dirHash {
|
||||
status = TitleChanged
|
||||
id = oldGame.ID
|
||||
id = oldSoundtrack.ID
|
||||
//fmt.Printf("SoundtrackName changed\n")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if full && status != NewGame {
|
||||
if full && status != NewSoundtrack {
|
||||
status = TitleChanged
|
||||
}
|
||||
entries, err := os.ReadDir(gameDir)
|
||||
entries, err := os.ReadDir(soundtrackDir)
|
||||
if err != nil {
|
||||
logging.GetLogger().Error("Failed to read game directory", zap.String("path", gameDir), zap.String("error", err.Error()))
|
||||
logging.GetLogger().Error("Failed to read soundtrack directory", zap.String("path", soundtrackDir), zap.String("error", err.Error()))
|
||||
}
|
||||
switch status {
|
||||
case NewGame:
|
||||
case NewSoundtrack:
|
||||
if id != -1 {
|
||||
for _, entry := range entries {
|
||||
fileInfo, err := entry.Info()
|
||||
@@ -340,13 +340,13 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
|
||||
break
|
||||
}
|
||||
}
|
||||
err = repo.InsertSoundtrackWithExistingId(BackendCtx(), repository.InsertSoundtrackWithExistingIdParams{ID: id, SoundtrackName: file.Name(), Path: gameDir, Hash: dirHash})
|
||||
err = repo.InsertSoundtrackWithExistingId(BackendCtx(), repository.InsertSoundtrackWithExistingIdParams{ID: id, SoundtrackName: file.Name(), Path: soundtrackDir, Hash: dirHash})
|
||||
handleError("InsertSoundtrackWithExistingId", err, "")
|
||||
if err != nil {
|
||||
logging.GetLogger().Debug("Game already exists, removing old ID file",
|
||||
logging.GetLogger().Debug("Soundtrack already exists, removing old ID file",
|
||||
zap.Int32("id", id),
|
||||
zap.String("game_dir", gameDir))
|
||||
fileName := gameDir + "/." + strconv.Itoa(int(id)) + ".id"
|
||||
zap.String("soundtrack_dir", soundtrackDir))
|
||||
fileName := soundtrackDir + "/." + strconv.Itoa(int(id)) + ".id"
|
||||
logging.GetLogger().Debug("Removing ID file", zap.String("filename", fileName))
|
||||
|
||||
err := os.Remove(fileName)
|
||||
@@ -354,50 +354,50 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
|
||||
logging.GetLogger().Error("Failed to remove ID file", zap.String("filename", fileName), zap.String("error", err.Error()))
|
||||
}
|
||||
|
||||
newDirHash := getHashForDir(gameDir)
|
||||
newDirHash := getHashForDir(soundtrackDir)
|
||||
|
||||
id = insertGameNew(file.Name(), gameDir, newDirHash)
|
||||
id = insertSoundtrackNew(file.Name(), soundtrackDir, newDirHash)
|
||||
}
|
||||
} else {
|
||||
id = insertGameNew(file.Name(), gameDir, dirHash)
|
||||
id = insertSoundtrackNew(file.Name(), soundtrackDir, dirHash)
|
||||
}
|
||||
logging.GetLogger().Debug("New game detected",
|
||||
logging.GetLogger().Debug("New soundtrack detected",
|
||||
zap.Int32("id", id),
|
||||
zap.String("game", file.Name()),
|
||||
zap.String("soundtrack", file.Name()),
|
||||
zap.String("hash", dirHash),
|
||||
zap.String("status", status.String()))
|
||||
gamesAdded = append(gamesAdded, file.Name())
|
||||
newCheckSongs(entries, gameDir, id)
|
||||
case GameChanged:
|
||||
logging.GetLogger().Debug("Game changed",
|
||||
soundtracksAdded = append(soundtracksAdded, file.Name())
|
||||
newCheckSongs(entries, soundtrackDir, id)
|
||||
case SoundtrackChanged:
|
||||
logging.GetLogger().Debug("Soundtrack changed",
|
||||
zap.Int32("id", id),
|
||||
zap.String("game", file.Name()),
|
||||
zap.String("soundtrack", file.Name()),
|
||||
zap.String("hash", dirHash),
|
||||
zap.String("status", status.String()))
|
||||
err = repo.UpdateSoundtrackHash(BackendCtx(), repository.UpdateSoundtrackHashParams{Hash: dirHash, ID: id})
|
||||
handleError("UpdateSoundtrackHash", err, "")
|
||||
gamesChangedContent = append(gamesChangedContent, file.Name())
|
||||
newCheckSongs(entries, gameDir, id)
|
||||
soundtracksChangedContent = append(soundtracksChangedContent, file.Name())
|
||||
newCheckSongs(entries, soundtrackDir, id)
|
||||
case TitleChanged:
|
||||
logging.GetLogger().Debug("Game title changed",
|
||||
logging.GetLogger().Debug("Soundtrack title changed",
|
||||
zap.Int32("id", id),
|
||||
zap.String("oldName", oldGame.SoundtrackName),
|
||||
zap.String("oldName", oldSoundtrack.SoundtrackName),
|
||||
zap.String("newName", file.Name()),
|
||||
zap.String("hash", dirHash),
|
||||
zap.String("status", status.String()))
|
||||
err = repo.UpdateSoundtrackName(BackendCtx(), repository.UpdateSoundtrackNameParams{Name: file.Name(), Path: gameDir, ID: id})
|
||||
err = repo.UpdateSoundtrackName(BackendCtx(), repository.UpdateSoundtrackNameParams{Name: file.Name(), Path: soundtrackDir, ID: id})
|
||||
handleError("UpdateSoundtrackName", err, "")
|
||||
newCheckSongs(entries, gameDir, id)
|
||||
if gamesChangedTitle == nil {
|
||||
gamesChangedTitle = make(map[string]string)
|
||||
newCheckSongs(entries, soundtrackDir, id)
|
||||
if soundtracksChangedTitle == nil {
|
||||
soundtracksChangedTitle = make(map[string]string)
|
||||
}
|
||||
gamesChangedTitle[oldGame.SoundtrackName] = file.Name()
|
||||
soundtracksChangedTitle[oldSoundtrack.SoundtrackName] = file.Name()
|
||||
case NotChanged:
|
||||
var found bool = false
|
||||
for _, beforeGame := range gamesBeforeSync {
|
||||
if dirHash == beforeGame.Hash {
|
||||
for _, beforeSoundtrack := range soundtracksBeforeSync {
|
||||
if dirHash == beforeSoundtrack.Hash {
|
||||
found = true
|
||||
logging.GetLogger().Debug("Game not changed",
|
||||
logging.GetLogger().Debug("Soundtrack not changed",
|
||||
zap.Int32("id", id),
|
||||
zap.String("newName", file.Name()),
|
||||
zap.String("hash", dirHash),
|
||||
@@ -405,9 +405,9 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
newCheckSongs(entries, gameDir, id)
|
||||
gamesReAdded = append(gamesReAdded, file.Name())
|
||||
logging.GetLogger().Debug("Game added again",
|
||||
newCheckSongs(entries, soundtrackDir, id)
|
||||
soundtracksReAdded = append(soundtracksReAdded, file.Name())
|
||||
logging.GetLogger().Debug("Soundtrack added again",
|
||||
zap.Int32("id", id),
|
||||
zap.String("newName", file.Name()),
|
||||
zap.String("hash", dirHash),
|
||||
@@ -415,9 +415,9 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
|
||||
|
||||
}
|
||||
}
|
||||
logging.GetLogger().Debug("Game sync status",
|
||||
logging.GetLogger().Debug("Soundtrack sync status",
|
||||
zap.Int32("id", id),
|
||||
zap.String("game", file.Name()),
|
||||
zap.String("soundtrack", file.Name()),
|
||||
zap.String("hash", dirHash),
|
||||
zap.String("status", status.String()))
|
||||
err = repo.RemoveSoundtrackDeletionDate(BackendCtx(), id)
|
||||
@@ -430,24 +430,24 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
|
||||
zap.Int("percent", int((foldersSynced/numberOfFoldersToSync)*100)))
|
||||
}
|
||||
|
||||
func insertGameNew(name string, path string, hash string) int32 {
|
||||
func insertSoundtrackNew(name string, path string, hash string) int32 {
|
||||
var duplicateError = errors.New("ERROR: duplicate key value violates unique")
|
||||
id, err := repo.InsertSoundtrack(BackendCtx(), repository.InsertSoundtrackParams{SoundtrackName: name, Path: path, Hash: hash})
|
||||
handleError("InsertSoundtrack", err, "")
|
||||
if err != nil {
|
||||
logging.GetLogger().Warn("ID collision detected, resetting sequence")
|
||||
if strings.HasPrefix(err.Error(), duplicateError.Error()) {
|
||||
logging.GetLogger().Debug("Resetting game ID sequence")
|
||||
logging.GetLogger().Debug("Resetting soundtrack ID sequence")
|
||||
_, err = repo.ResetSoundtrackIdSeq(BackendCtx())
|
||||
handleError("ResetSoundtrackIdSeq", err, "")
|
||||
id = insertGameNew(name, path, hash)
|
||||
id = insertSoundtrackNew(name, path, hash)
|
||||
}
|
||||
}
|
||||
return id
|
||||
|
||||
}
|
||||
|
||||
func newCheckSongs(entries []os.DirEntry, gameDir string, id int32) int32 {
|
||||
func newCheckSongs(entries []os.DirEntry, soundtrackDir string, id int32) int32 {
|
||||
//hasher := md5.New()
|
||||
var numberOfSongs int32
|
||||
numberOfFiles := len(entries)
|
||||
@@ -457,7 +457,7 @@ func newCheckSongs(entries []os.DirEntry, gameDir string, id int32) int32 {
|
||||
for _, entry := range entries {
|
||||
poolSong.Submit(func() {
|
||||
defer songWg.Done()
|
||||
if newCheckSong(entry, gameDir, id) {
|
||||
if newCheckSong(entry, soundtrackDir, id) {
|
||||
numberOfSongs++
|
||||
}
|
||||
})
|
||||
@@ -466,7 +466,7 @@ func newCheckSongs(entries []os.DirEntry, gameDir string, id int32) int32 {
|
||||
return numberOfSongs
|
||||
}
|
||||
|
||||
func newCheckSong(entry os.DirEntry, gameDir string, id int32) bool {
|
||||
func newCheckSong(entry os.DirEntry, soundtrackDir string, id int32) bool {
|
||||
fileInfo, err := entry.Info()
|
||||
if err != nil {
|
||||
logging.GetLogger().Error("Failed to get file info", zap.String("filename", entry.Name()), zap.String("error", err.Error()))
|
||||
@@ -474,7 +474,7 @@ func newCheckSong(entry os.DirEntry, gameDir string, id int32) bool {
|
||||
}
|
||||
|
||||
if isSong(fileInfo) {
|
||||
path := gameDir + entry.Name()
|
||||
path := soundtrackDir + entry.Name()
|
||||
|
||||
songHash := getHashForFile(path)
|
||||
//numberOfSongs++
|
||||
@@ -490,7 +490,7 @@ func newCheckSong(entry os.DirEntry, gameDir string, id int32) bool {
|
||||
}
|
||||
}
|
||||
logging.GetLogger().Debug("Song changed",
|
||||
zap.Int32("game_id", id),
|
||||
zap.Int32("soundtrack_id", id),
|
||||
zap.String("path", path),
|
||||
zap.String("song_name", songName),
|
||||
zap.String("song_hash", songHash))
|
||||
@@ -548,8 +548,8 @@ func handleError(funcName string, err error, msg string) {
|
||||
}
|
||||
}
|
||||
|
||||
func getHashForDir(gameDir string) string {
|
||||
directory, _ := directory_checksum.ScanDirectory(gameDir, afero.NewOsFs())
|
||||
func getHashForDir(soundtrackDir string) string {
|
||||
directory, _ := directory_checksum.ScanDirectory(soundtrackDir, afero.NewOsFs())
|
||||
hash, _ := directory.ComputeDirectoryChecksums()
|
||||
|
||||
return hash
|
||||
|
||||
Reference in New Issue
Block a user