From 9bfedf3cf0e7d0e8f37f525a106dcf475cd7276a Mon Sep 17 00:00:00 2001 From: Flamefire Date: Wed, 25 Sep 2013 20:19:22 +0200 Subject: [PATCH 1/7] Rename SScores --- Vocaluxe/Base/CDataBase.cs | 8 ++++---- Vocaluxe/Screens/CScreenHighscore.cs | 8 ++++---- VocaluxeLib/Structs.cs | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Vocaluxe/Base/CDataBase.cs b/Vocaluxe/Base/CDataBase.cs index b755a95b..cf37bf3b 100644 --- a/Vocaluxe/Base/CDataBase.cs +++ b/Vocaluxe/Base/CDataBase.cs @@ -243,13 +243,13 @@ namespace Vocaluxe.Base return lastInsertID; } - public static void LoadScore(out List scores, SPlayer player) + public static void LoadScore(out List scores, SPlayer player) { using (var connection = new SQLiteConnection()) { connection.ConnectionString = "Data Source=" + _HighscoreFilePath; - scores = new List(); + scores = new List(); try { @@ -290,13 +290,13 @@ namespace Vocaluxe.Base { while (reader.Read()) { - var score = new SScores + var score = new SDBScoreEntry { Name = reader.GetString(0), Score = reader.GetInt32(1), Date = new DateTime(reader.GetInt64(2)).ToString("dd/MM/yyyy"), Difficulty = (EGameDifficulty)reader.GetInt32(3), - LineNr = reader.GetInt32(4), + VoiceNr = reader.GetInt32(4), ID = reader.GetInt32(5) }; diff --git a/Vocaluxe/Screens/CScreenHighscore.cs b/Vocaluxe/Screens/CScreenHighscore.cs index 933c2706..a8be7e46 100644 --- a/Vocaluxe/Screens/CScreenHighscore.cs +++ b/Vocaluxe/Screens/CScreenHighscore.cs @@ -44,7 +44,7 @@ namespace Vocaluxe.Screens private string[] _TextDate; private string[] _ParticleEffectNew; - private List[] _Scores; + private List[] _Scores; private List _NewEntryIDs; private int _Round; private int _Pos; @@ -169,7 +169,7 @@ namespace Vocaluxe.Screens string name = _Scores[_Round][_Pos + p].Name; name += " [" + CLanguage.Translate(Enum.GetName(typeof(EGameDifficulty), _Scores[_Round][_Pos + p].Difficulty)) + "]"; if (_IsDuet) - name += " (P" + (_Scores[_Round][_Pos + p].LineNr + 1) + ")"; + name += " (P" + (_Scores[_Round][_Pos + p].VoiceNr + 1) + ")"; _Texts[_TextName[p]].Text = name; _Texts[_TextScore[p]].Text = _Scores[_Round][_Pos + p].Score.ToString("00000"); @@ -195,9 +195,9 @@ namespace Vocaluxe.Screens _Round = 0; _Pos = 0; CPoints points = CGame.GetPoints(); - _Scores = new List[points.NumRounds]; + _Scores = new List[points.NumRounds]; for (int i = 0; i < _Scores.Length; i++) - _Scores[i] = new List(); + _Scores[i] = new List(); _NewEntryIDs.Clear(); _AddScoresToDB(); _LoadScores(); diff --git a/VocaluxeLib/Structs.cs b/VocaluxeLib/Structs.cs index 4bef014d..895d593b 100644 --- a/VocaluxeLib/Structs.cs +++ b/VocaluxeLib/Structs.cs @@ -197,13 +197,13 @@ namespace VocaluxeLib public bool SongFinished; } - public struct SScores + public struct SDBScoreEntry { public string Name; public int Score; public string Date; public EGameDifficulty Difficulty; - public int LineNr; + public int VoiceNr; public int ID; } From e3f744677e25a29cf7e6a89d6bf1eb989f23e398 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Thu, 26 Sep 2013 13:47:19 +0200 Subject: [PATCH 2/7] Better handling of Points (Preparation for future changes) Fix some warnings of unused results (show errors on failure!) --- Vocaluxe/Base/CDataBase.cs | 206 +++++++++++++-------------- Vocaluxe/Base/CGame.cs | 6 +- Vocaluxe/Screens/CScreenCredits.cs | 30 ++-- Vocaluxe/Screens/CScreenHighscore.cs | 2 +- Vocaluxe/SongQueue/CSongQueue.cs | 4 +- VocaluxeLib/Game/CPoints.cs | 14 +- VocaluxeLib/Structs.cs | 4 +- 7 files changed, 129 insertions(+), 137 deletions(-) diff --git a/Vocaluxe/Base/CDataBase.cs b/Vocaluxe/Base/CDataBase.cs index cf37bf3b..ada2a0a6 100644 --- a/Vocaluxe/Base/CDataBase.cs +++ b/Vocaluxe/Base/CDataBase.cs @@ -63,8 +63,16 @@ namespace Vocaluxe.Base _CreditsRessourcesFilePath = Path.Combine(Environment.CurrentDirectory, CSettings.FileCreditsRessourcesDB); _InitHighscoreDB(); - _InitCoverDB(); - _InitCreditsRessourcesDB(); + if (!_InitCoverDB()) + { + CLog.LogError("Error initializing Cover-DB", true, true); + return; + } + if (!_InitCreditsRessourcesDB()) + { + CLog.LogError("Error initializing Cover-DB", true, true); + return; + } GC.Collect(); } @@ -93,26 +101,6 @@ namespace Vocaluxe.Base return _GetDataBaseSongInfos(songID, out sArtist, out sTitle, out numPlayed, out dateAdded, _HighscoreFilePath); } - public static void IncreaseSongCounter(SPlayer player) - { - using (var connection = new SQLiteConnection()) - { - connection.ConnectionString = "Data Source=" + _HighscoreFilePath; - - try - { - connection.Open(); - } - catch (Exception) {} - - using (var command = new SQLiteCommand(connection)) - { - int dataBaseSongID = _GetDataBaseSongID(player, command); - _IncreaseSongCounter(dataBaseSongID, command); - } - } - } - public static void IncreaseSongCounter(int dataBaseSongID) { using (var connection = new SQLiteConnection()) @@ -170,11 +158,27 @@ namespace Vocaluxe.Base return -1; } + int medley = 0; + int duet = 0; + int shortSong = 0; + switch (player.GameMode) + { + case EGameMode.TR_GAMEMODE_MEDLEY: + medley = 1; + break; + case EGameMode.TR_GAMEMODE_DUET: + duet = 1; + break; + case EGameMode.TR_GAMEMODE_SHORTSONG: + shortSong = 1; + break; + } + using (var command = new SQLiteCommand(connection)) { int dataBaseSongID = CSongs.GetSong(player.SongID).DataBaseSongID; - return _AddScore(CProfiles.GetPlayerName(player.ProfileID), (int)Math.Round(player.Points), player.VoiceNr, player.DateTicks, player.Medley ? 1 : 0, - player.Duet ? 1 : 0, player.ShortSong ? 1 : 0, (int)CProfiles.GetDifficulty(player.ProfileID), dataBaseSongID, command); + return _AddScore(CProfiles.GetPlayerName(player.ProfileID), (int)Math.Round(player.Points), player.VoiceNr, player.DateTicks, medley, + duet, shortSong, (int)CProfiles.GetDifficulty(player.ProfileID), dataBaseSongID, command); } } } @@ -243,7 +247,7 @@ namespace Vocaluxe.Base return lastInsertID; } - public static void LoadScore(out List scores, SPlayer player) + public static void LoadScore(out List scores, int songID, EGameMode gameMode) { using (var connection = new SQLiteConnection()) { @@ -263,47 +267,51 @@ namespace Vocaluxe.Base using (var command = new SQLiteCommand(connection)) { int medley = 0; - if (player.Medley) - medley = 1; - int duet = 0; - if (player.Duet) - duet = 1; - int shortSong = 0; - if (player.ShortSong) - shortSong = 1; - - int dataBaseSongID = _GetDataBaseSongID(player, command); - if (dataBaseSongID >= 0) + switch (gameMode) { - command.CommandText = "SELECT PlayerName, Score, Date, Difficulty, LineNr, id FROM Scores " + - "WHERE [SongID] = @SongID AND [Medley] = @Medley AND [Duet] = @Duet AND [ShortSong] = @ShortSong " + - "ORDER BY [Score] DESC"; - command.Parameters.Add("@SongID", DbType.Int32, 0).Value = dataBaseSongID; - command.Parameters.Add("@Medley", DbType.Int32, 0).Value = medley; - command.Parameters.Add("@Duet", DbType.Int32, 0).Value = duet; - command.Parameters.Add("@ShortSong", DbType.Int32, 0).Value = shortSong; + case EGameMode.TR_GAMEMODE_MEDLEY: + medley = 1; + break; + case EGameMode.TR_GAMEMODE_DUET: + duet = 1; + break; + case EGameMode.TR_GAMEMODE_SHORTSONG: + shortSong = 1; + break; + } - SQLiteDataReader reader = command.ExecuteReader(); - if (reader != null && reader.HasRows) + int dataBaseSongID = _GetDataBaseSongID(songID, command); + if (dataBaseSongID < 0) + return; + + command.CommandText = "SELECT PlayerName, Score, Date, Difficulty, LineNr, id FROM Scores " + + "WHERE [SongID] = @SongID AND [Medley] = @Medley AND [Duet] = @Duet AND [ShortSong] = @ShortSong " + + "ORDER BY [Score] DESC"; + command.Parameters.Add("@SongID", DbType.Int32, 0).Value = dataBaseSongID; + command.Parameters.Add("@Medley", DbType.Int32, 0).Value = medley; + command.Parameters.Add("@Duet", DbType.Int32, 0).Value = duet; + command.Parameters.Add("@ShortSong", DbType.Int32, 0).Value = shortSong; + + SQLiteDataReader reader = command.ExecuteReader(); + if (reader != null && reader.HasRows) + { + while (reader.Read()) { - while (reader.Read()) - { - var score = new SDBScoreEntry - { - Name = reader.GetString(0), - Score = reader.GetInt32(1), - Date = new DateTime(reader.GetInt64(2)).ToString("dd/MM/yyyy"), - Difficulty = (EGameDifficulty)reader.GetInt32(3), - VoiceNr = reader.GetInt32(4), - ID = reader.GetInt32(5) - }; + var score = new SDBScoreEntry + { + Name = reader.GetString(0), + Score = reader.GetInt32(1), + Date = new DateTime(reader.GetInt64(2)).ToString("dd/MM/yyyy"), + Difficulty = (EGameDifficulty)reader.GetInt32(3), + VoiceNr = reader.GetInt32(4), + ID = reader.GetInt32(5) + }; - scores.Add(score); - } - reader.Dispose(); + scores.Add(score); } + reader.Dispose(); } } } @@ -316,9 +324,9 @@ namespace Vocaluxe.Base command.ExecuteNonQuery(); } - private static int _GetDataBaseSongID(SPlayer player, SQLiteCommand command) + private static int _GetDataBaseSongID(int songID, SQLiteCommand command) { - CSong song = CSongs.GetSong(player.SongID); + CSong song = CSongs.GetSong(songID); if (song == null) return -1; @@ -430,21 +438,35 @@ namespace Vocaluxe.Base { if (File.Exists(_HighscoreFilePath)) { - _CreateOrConvert(oldDBFilePath); - _CreateOrConvert(_HighscoreFilePath); - _ImportData(oldDBFilePath); - - File.Delete(oldDBFilePath); + if (!_CreateOrConvert(oldDBFilePath)) + { + CLog.LogError("Cannot init Highscore DB: Error opening database: " + oldDBFilePath, true, true); + return; + } + if (!_CreateOrConvert(_HighscoreFilePath)) + { + CLog.LogError("Cannot init Highscore DB: Error opening database: " + _HighscoreFilePath, true, true); + return; + } + if (!_ImportData(oldDBFilePath)) + { + CLog.LogError("Cannot init Highscore DB: Error importing data", true, true); + return; + } } else { File.Copy(oldDBFilePath, _HighscoreFilePath); - _CreateOrConvert(_HighscoreFilePath); - File.Delete(oldDBFilePath); + if (!_CreateOrConvert(_HighscoreFilePath)) + { + CLog.LogError("Cannot init Highscore DB: Error opening database: " + _HighscoreFilePath, true, true); + return; + } } + File.Delete(oldDBFilePath); } - else - _CreateOrConvert(_HighscoreFilePath); + else if (!_CreateOrConvert(_HighscoreFilePath)) + CLog.LogError("Cannot init Highscore DB: Error opening database: " + _HighscoreFilePath, true, true); } private static void _CreateHighscoreDB(string filePath) @@ -525,6 +547,7 @@ namespace Vocaluxe.Base /// private static bool _CreateOrConvert(string filePath) { + bool result = true; using (var connection = new SQLiteConnection()) { connection.ConnectionString = "Data Source=" + filePath; @@ -565,46 +588,24 @@ namespace Vocaluxe.Base } catch (Exception) {} - if (reader == null) + if (reader == null || reader.FieldCount == 0) { // create new database/tables if (version == 1) { //Check for USDX 1.1 DB _CreateHighscoreDBV1(filePath); - _ConvertFrom110(filePath); - _UpdateDatabase(1, connection); - _UpdateDatabase(2, connection); + result &= !_ConvertFrom110(filePath); + result &= _UpdateDatabase(1, connection); + result &= _UpdateDatabase(2, connection); } else if (version == 0 && scoresTableExists) { //Check for USDX 1.01 or CMD Mod DB _CreateHighscoreDBV1(filePath); - _ConvertFrom101(filePath); - _UpdateDatabase(1, connection); - _UpdateDatabase(2, connection); - } - else - _CreateHighscoreDB(filePath); - } - else if (reader.FieldCount == 0) - { - // create new database/tables - if (version == 1) - { - //Check for USDX 1.1 DB - _CreateHighscoreDBV1(filePath); - _ConvertFrom110(filePath); - _UpdateDatabase(1, connection); - _UpdateDatabase(2, connection); - } - else if (version == 0 && scoresTableExists) - { - //Check for USDX 1.01 or CMD Mod DB - _CreateHighscoreDBV1(filePath); - _ConvertFrom101(filePath); - _UpdateDatabase(1, connection); - _UpdateDatabase(2, connection); + result &= _ConvertFrom101(filePath); + result &= _UpdateDatabase(1, connection); + result &= _UpdateDatabase(2, connection); } else _CreateHighscoreDB(filePath); @@ -616,7 +617,7 @@ namespace Vocaluxe.Base if (currentVersion < CSettings.DatabaseHighscoreVersion) { // update database - _UpdateDatabase(currentVersion, connection); + result &= _UpdateDatabase(currentVersion, connection); } } @@ -625,7 +626,7 @@ namespace Vocaluxe.Base } } - return true; + return result; } /// @@ -972,8 +973,7 @@ namespace Vocaluxe.Base lastDateAdded = dateAdded; } } - if (reader != null) - reader.Dispose(); + reader.Dispose(); foreach (int id in ids) _IncreaseSongCounter(id, command); diff --git a/Vocaluxe/Base/CGame.cs b/Vocaluxe/Base/CGame.cs index dced2fa5..c64eaabc 100644 --- a/Vocaluxe/Base/CGame.cs +++ b/Vocaluxe/Base/CGame.cs @@ -197,9 +197,7 @@ namespace Vocaluxe.Base Players[i].CurrentLine = -1; Players[i].CurrentNote = -1; Players[i].SongID = -1; - Players[i].Medley = false; - Players[i].Duet = false; - Players[i].ShortSong = false; + Players[i].GameMode = EGameMode.TR_GAMEMODE_NORMAL; Players[i].DateTicks = DateTime.Now.Ticks; Players[i].SongFinished = false; } @@ -384,7 +382,7 @@ namespace Vocaluxe.Base { if (note.StartBeat < nextStart) { - if (note.Hit && note.HitNote.NoteType==ENoteType.Golden) + if (note.Hit && note.HitNote.NoteType == ENoteType.Golden) Players[p].PointsGoldenNotes += note.Points; Players[p].Points += note.Points; } diff --git a/Vocaluxe/Screens/CScreenCredits.cs b/Vocaluxe/Screens/CScreenCredits.cs index 06d36d1f..7c340a7a 100644 --- a/Vocaluxe/Screens/CScreenCredits.cs +++ b/Vocaluxe/Screens/CScreenCredits.cs @@ -183,24 +183,28 @@ namespace Vocaluxe.Screens public override void LoadTheme(string xmlPath) { + bool ressourceOK = true; //Vocaluxe-Logo - CDataBase.GetCreditsRessource("Logo_voc.png", ref _TexLogo); + ressourceOK &= CDataBase.GetCreditsRessource("Logo_voc.png", ref _TexLogo); //Little stars for logo - CDataBase.GetCreditsRessource("PerfectNoteStar.png", ref _TexPerfectNoteStar); + ressourceOK &= CDataBase.GetCreditsRessource("PerfectNoteStar.png", ref _TexPerfectNoteStar); - CDataBase.GetCreditsRessource("redDot.png", ref _TexRedDot); - CDataBase.GetCreditsRessource("blueDot.png", ref _TexBlueDot); + ressourceOK &= CDataBase.GetCreditsRessource("redDot.png", ref _TexRedDot); + ressourceOK &= CDataBase.GetCreditsRessource("blueDot.png", ref _TexBlueDot); - CDataBase.GetCreditsRessource("brunzel.png", ref _TexNameBrunzel); - CDataBase.GetCreditsRessource("Darkice.png", ref _TexNameDarkice); - CDataBase.GetCreditsRessource("flokuep.png", ref _TexNameFlokuep); - CDataBase.GetCreditsRessource("flamefire.png", ref _TexNameFlamefire); - CDataBase.GetCreditsRessource("bohning.png", ref _TexNameBohning); - CDataBase.GetCreditsRessource("mesand.png", ref _TexNameMesand); - CDataBase.GetCreditsRessource("babene03.png", ref _TexNameBabene03); - CDataBase.GetCreditsRessource("pantero.png", ref _TexNamePantero); - CDataBase.GetCreditsRessource("Pinky007.png", ref _TexNamePinky007); + ressourceOK &= CDataBase.GetCreditsRessource("brunzel.png", ref _TexNameBrunzel); + ressourceOK &= CDataBase.GetCreditsRessource("Darkice.png", ref _TexNameDarkice); + ressourceOK &= CDataBase.GetCreditsRessource("flokuep.png", ref _TexNameFlokuep); + ressourceOK &= CDataBase.GetCreditsRessource("flamefire.png", ref _TexNameFlamefire); + ressourceOK &= CDataBase.GetCreditsRessource("bohning.png", ref _TexNameBohning); + ressourceOK &= CDataBase.GetCreditsRessource("mesand.png", ref _TexNameMesand); + ressourceOK &= CDataBase.GetCreditsRessource("babene03.png", ref _TexNameBabene03); + ressourceOK &= CDataBase.GetCreditsRessource("pantero.png", ref _TexNamePantero); + ressourceOK &= CDataBase.GetCreditsRessource("Pinky007.png", ref _TexNamePinky007); + + if (!ressourceOK) + CLog.LogError("Could not load all ressources!", true, true); //Prepare Text int lastY = 280; diff --git a/Vocaluxe/Screens/CScreenHighscore.cs b/Vocaluxe/Screens/CScreenHighscore.cs index a8be7e46..9a75c943 100644 --- a/Vocaluxe/Screens/CScreenHighscore.cs +++ b/Vocaluxe/Screens/CScreenHighscore.cs @@ -239,7 +239,7 @@ namespace Vocaluxe.Screens for (int round = 0; round < points.NumRounds; round++) { SPlayer player = points.GetPlayer(round, CGame.NumPlayer)[0]; - CDataBase.LoadScore(out _Scores[round], player); + CDataBase.LoadScore(out _Scores[round], player.SongID, player.GameMode); } } diff --git a/Vocaluxe/SongQueue/CSongQueue.cs b/Vocaluxe/SongQueue/CSongQueue.cs index b23f1288..d11aeb4c 100644 --- a/Vocaluxe/SongQueue/CSongQueue.cs +++ b/Vocaluxe/SongQueue/CSongQueue.cs @@ -123,9 +123,7 @@ namespace Vocaluxe.SongQueue _CurrentSong, _SongQueue[_CurrentSong].SongID, players, - _SongQueue[_CurrentSong].GameMode == EGameMode.TR_GAMEMODE_MEDLEY, - _SongQueue[_CurrentSong].GameMode == EGameMode.TR_GAMEMODE_DUET, - _SongQueue[_CurrentSong].GameMode == EGameMode.TR_GAMEMODE_SHORTSONG); + _SongQueue[_CurrentSong].GameMode); } _CurrentSong++; } diff --git a/VocaluxeLib/Game/CPoints.cs b/VocaluxeLib/Game/CPoints.cs index 00749b0f..7326ccf6 100644 --- a/VocaluxeLib/Game/CPoints.cs +++ b/VocaluxeLib/Game/CPoints.cs @@ -35,15 +35,13 @@ namespace VocaluxeLib.Game _Rounds[round, player].Points = 0f; _Rounds[round, player].PointsGoldenNotes = 0f; _Rounds[round, player].PointsLineBonus = 0f; - _Rounds[round, player].Medley = false; - _Rounds[round, player].Duet = false; - _Rounds[round, player].ShortSong = false; + _Rounds[round, player].GameMode = EGameMode.TR_GAMEMODE_NORMAL; _Rounds[round, player].SongFinished = false; } } } - public void SetPoints(int round, int songID, SPlayer[] players, bool medley, bool duet, bool shortSong) + public void SetPoints(int round, int songID, SPlayer[] players, EGameMode gameMode) { long dateTicks = DateTime.Now.Ticks; for (int player = 0; player < players.Length; player++) @@ -53,9 +51,7 @@ namespace VocaluxeLib.Game _Rounds[round, player].Points = players[player].Points; _Rounds[round, player].PointsGoldenNotes = players[player].PointsGoldenNotes; _Rounds[round, player].PointsLineBonus = players[player].PointsLineBonus; - _Rounds[round, player].Medley = medley; - _Rounds[round, player].Duet = duet; - _Rounds[round, player].ShortSong = shortSong; + _Rounds[round, player].GameMode = gameMode; _Rounds[round, player].DateTicks = dateTicks; _Rounds[round, player].SongFinished = players[player].SongFinished; } @@ -87,9 +83,7 @@ namespace VocaluxeLib.Game players[p].PointsLineBonus = _Rounds[round, p].PointsLineBonus; players[p].SongID = _Rounds[round, p].SongID; players[p].VoiceNr = _Rounds[round, p].VoiceNr; - players[p].Medley = _Rounds[round, p].Medley; - players[p].Duet = _Rounds[round, p].Duet; - players[p].ShortSong = _Rounds[round, p].ShortSong; + players[p].GameMode = _Rounds[round, p].GameMode; players[p].DateTicks = _Rounds[round, p].DateTicks; players[p].SongFinished = _Rounds[round, p].SongFinished; players[p].ProfileID = _Rounds[round, p].ProfileID; diff --git a/VocaluxeLib/Structs.cs b/VocaluxeLib/Structs.cs index 895d593b..13ee20ca 100644 --- a/VocaluxeLib/Structs.cs +++ b/VocaluxeLib/Structs.cs @@ -190,9 +190,7 @@ namespace VocaluxeLib public int CurrentNote; public int SongID; - public bool Medley; - public bool Duet; - public bool ShortSong; + public EGameMode GameMode; public long DateTicks; public bool SongFinished; } From c27ae20b771019be802c2554896c3b43f23c93d7 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Fri, 27 Sep 2013 17:14:28 +0200 Subject: [PATCH 3/7] Change min/max in LineBase to use simpler LINQ query --- VocaluxeLib/Songs/CLineBase.cs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/VocaluxeLib/Songs/CLineBase.cs b/VocaluxeLib/Songs/CLineBase.cs index 0a819509..41d88909 100644 --- a/VocaluxeLib/Songs/CLineBase.cs +++ b/VocaluxeLib/Songs/CLineBase.cs @@ -63,15 +63,10 @@ namespace VocaluxeLib.Songs { get { - int min = int.MaxValue; - int max = int.MinValue; - foreach (T note in _Notes) - { - if (note.Tone < min) - min = note.Tone; - if (note.Tone > max) - max = note.Tone; - } + if (_Notes.Count == 0) + return 0; + int min = _Notes.Max(note => note.Tone); + int max = _Notes.Min(note => note.Tone); return min - (max - min) / 4; } From 074a1ba82dbfd61f8aa56491c67f441214ad5f20 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Fri, 4 Oct 2013 23:02:54 +0200 Subject: [PATCH 4/7] Change signature of getDBScores --- Vocaluxe/Base/CDataBase.cs | 10 +++++----- Vocaluxe/Base/CGame.cs | 8 ++++---- Vocaluxe/Screens/CScreenHighscore.cs | 17 ++++++----------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Vocaluxe/Base/CDataBase.cs b/Vocaluxe/Base/CDataBase.cs index ada2a0a6..888ea52d 100644 --- a/Vocaluxe/Base/CDataBase.cs +++ b/Vocaluxe/Base/CDataBase.cs @@ -247,21 +247,20 @@ namespace Vocaluxe.Base return lastInsertID; } - public static void LoadScore(out List scores, int songID, EGameMode gameMode) + public static List LoadScore(int songID, EGameMode gameMode) { + var scores = new List(); using (var connection = new SQLiteConnection()) { connection.ConnectionString = "Data Source=" + _HighscoreFilePath; - scores = new List(); - try { connection.Open(); } catch (Exception) { - return; + return scores; } using (var command = new SQLiteCommand(connection)) @@ -284,7 +283,7 @@ namespace Vocaluxe.Base int dataBaseSongID = _GetDataBaseSongID(songID, command); if (dataBaseSongID < 0) - return; + return scores; command.CommandText = "SELECT PlayerName, Score, Date, Difficulty, LineNr, id FROM Scores " + "WHERE [SongID] = @SongID AND [Medley] = @Medley AND [Duet] = @Duet AND [ShortSong] = @ShortSong " + @@ -315,6 +314,7 @@ namespace Vocaluxe.Base } } } + return scores; } private static void _IncreaseSongCounter(int dataBaseSongID, SQLiteCommand command) diff --git a/Vocaluxe/Base/CGame.cs b/Vocaluxe/Base/CGame.cs index c64eaabc..de97c5b0 100644 --- a/Vocaluxe/Base/CGame.cs +++ b/Vocaluxe/Base/CGame.cs @@ -149,14 +149,14 @@ namespace Vocaluxe.Base return _SongQueue.GetSong(); } - public static CSong GetSong(int num) + public static CSong GetSong(int round) { - return _SongQueue.GetSong(num); + return _SongQueue.GetSong(round); } - public static EGameMode GetGameMode(int num) + public static EGameMode GetGameMode(int round) { - return _SongQueue.GetGameMode(num); + return _SongQueue.GetGameMode(round); } public static int GetNumSongs() diff --git a/Vocaluxe/Screens/CScreenHighscore.cs b/Vocaluxe/Screens/CScreenHighscore.cs index 9a75c943..ab44c452 100644 --- a/Vocaluxe/Screens/CScreenHighscore.cs +++ b/Vocaluxe/Screens/CScreenHighscore.cs @@ -194,10 +194,6 @@ namespace Vocaluxe.Screens base.OnShow(); _Round = 0; _Pos = 0; - CPoints points = CGame.GetPoints(); - _Scores = new List[points.NumRounds]; - for (int i = 0; i < _Scores.Length; i++) - _Scores[i] = new List(); _NewEntryIDs.Clear(); _AddScoresToDB(); _LoadScores(); @@ -231,15 +227,14 @@ namespace Vocaluxe.Screens private void _LoadScores() { - CPoints points = CGame.GetPoints(); - if (points == null) - return; - _Pos = 0; - for (int round = 0; round < points.NumRounds; round++) + int rounds = CGame.NumRounds; + _Scores = new List[rounds]; + for (int round = 0; round < rounds; round++) { - SPlayer player = points.GetPlayer(round, CGame.NumPlayer)[0]; - CDataBase.LoadScore(out _Scores[round], player.SongID, player.GameMode); + int songID = CGame.GetSong(round).ID; + EGameMode gameMode = CGame.GetGameMode(round); + _Scores[round] = CDataBase.LoadScore(songID, gameMode); } } From e6d8cf3ad942fb81075fb739be5ee8ab8d7cbd0d Mon Sep 17 00:00:00 2001 From: Flamefire Date: Tue, 8 Oct 2013 17:26:38 +0200 Subject: [PATCH 5/7] Fix crash --- Vocaluxe/Base/CGame.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Vocaluxe/Base/CGame.cs b/Vocaluxe/Base/CGame.cs index de97c5b0..d8ea972c 100644 --- a/Vocaluxe/Base/CGame.cs +++ b/Vocaluxe/Base/CGame.cs @@ -404,9 +404,6 @@ namespace Vocaluxe.Base } Players[p].SungLines.RemoveRange(deleteLine, Players[p].SungLines.Count - deleteLine); } - CSong song = GetSong(); - for (int i = 0; i < GetSong().Notes.Voices[0].Lines.Length; i++) - CLog.LogError((i + 1) + ". " + song.Notes.Voices[0].Lines[i].NoteCount + " - " + Players[0].SungLines[i].NoteCount); CSound.SetPosition(soundStream, time); CVideo.Skip(vidStream, time, GetSong().VideoGap); From 271772301df8a148c0c41107f204a320a3e28d74 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Tue, 8 Oct 2013 18:01:08 +0200 Subject: [PATCH 6/7] Fix some bugs where round was actually roundNr Now round (0 based) is always used --- Vocaluxe/Base/CGame.cs | 2 +- Vocaluxe/Screens/CScreenHighscore.cs | 25 +++------ Vocaluxe/Screens/CScreenNames.cs | 12 ++--- Vocaluxe/Screens/CScreenScore.cs | 31 ++++++------ Vocaluxe/SongQueue/CSongQueue.cs | 76 ++++++++++++++++++---------- Vocaluxe/SongQueue/ISongQueue.cs | 12 ++--- VocaluxeLib/CHelper.cs | 9 ++++ 7 files changed, 92 insertions(+), 75 deletions(-) diff --git a/Vocaluxe/Base/CGame.cs b/Vocaluxe/Base/CGame.cs index d8ea972c..ada61872 100644 --- a/Vocaluxe/Base/CGame.cs +++ b/Vocaluxe/Base/CGame.cs @@ -131,7 +131,7 @@ namespace Vocaluxe.Base public static void NextRound() { - _SongQueue.NextRound(Players); + _SongQueue.StartNextRound(Players); } public static bool IsFinished() diff --git a/Vocaluxe/Screens/CScreenHighscore.cs b/Vocaluxe/Screens/CScreenHighscore.cs index ab44c452..411cd2cd 100644 --- a/Vocaluxe/Screens/CScreenHighscore.cs +++ b/Vocaluxe/Screens/CScreenHighscore.cs @@ -242,7 +242,7 @@ namespace Vocaluxe.Screens { _IsDuet = false; CPoints points = CGame.GetPoints(); - CSong song = CGame.GetSong(_Round + 1); + CSong song = CGame.GetSong(_Round); if (song == null) return; @@ -250,7 +250,7 @@ namespace Vocaluxe.Screens if (points.NumRounds > 1) _Texts[_TextSongName].Text += " (" + (_Round + 1) + "/" + points.NumRounds + ")"; - switch (CGame.GetGameMode(_Round + 1)) + switch (CGame.GetGameMode(_Round)) { case EGameMode.TR_GAMEMODE_NORMAL: _Texts[_TextSongMode].Text = "TR_GAMEMODE_NORMAL"; @@ -279,28 +279,15 @@ namespace Vocaluxe.Screens private void _ChangePos(int num) { - if (num > 0) - { - if (_Pos + num + _NumEntrys < _Scores[_Round].Count) - _Pos += num; - } - if (num < 0) - { - _Pos += num; - if (_Pos < 0) - _Pos = 0; - } + _Pos += num; + _Pos = _Pos.Clamp(0, _Scores[_Round].Count - 1); } private void _ChangeRound(int num) { CPoints points = CGame.GetPoints(); - if (_Round + num < points.NumRounds && _Round + num > -1) - _Round += num; - else if (_Round + num >= points.NumRounds) - _Round = points.NumRounds - 1; - else if (_Round + num < 0) - _Round = 0; + _Round += num; + _Round = _Round.Clamp(0, points.NumRounds - 1); _UpdateRound(); } diff --git a/Vocaluxe/Screens/CScreenNames.cs b/Vocaluxe/Screens/CScreenNames.cs index f1f548ab..6ad757c9 100644 --- a/Vocaluxe/Screens/CScreenNames.cs +++ b/Vocaluxe/Screens/CScreenNames.cs @@ -239,7 +239,7 @@ namespace Vocaluxe.Screens break; case Keys.F10: - if (CGame.GetNumSongs() == 1 && CGame.GetSong(1).IsDuet) + if (CGame.GetNumSongs() == 1 && CGame.GetSong(0).IsDuet) { CSelectSlide selectSlideDuetPart = _SelectSlides[_SelectSlideDuetPlayer[_SelectingFastPlayerNr - 1]]; selectSlideDuetPart.Selection = (selectSlideDuetPart.Selection + 1) % 2; @@ -603,12 +603,12 @@ namespace Vocaluxe.Screens CProfiles.GetAvatarTextureFromProfile(CGame.Players[i].ProfileID) : _OriginalPlayerAvatarTextures[i]; _Texts[_TextPlayer[i]].Text = CProfiles.GetPlayerName(CGame.Players[i].ProfileID, i + 1); - if (CGame.GetNumSongs() == 1 && CGame.GetSong(1).IsDuet) + if (CGame.GetNumSongs() == 1 && CGame.GetSong(0).IsDuet) { _SelectSlides[_SelectSlideDuetPlayer[i]].Clear(); _SelectSlides[_SelectSlideDuetPlayer[i]].Visible = i + 1 <= CGame.NumPlayer; - _SelectSlides[_SelectSlideDuetPlayer[i]].AddValue(CGame.GetSong(1).DuetPart1); - _SelectSlides[_SelectSlideDuetPlayer[i]].AddValue(CGame.GetSong(1).DuetPart2); + _SelectSlides[_SelectSlideDuetPlayer[i]].AddValue(CGame.GetSong(0).DuetPart1); + _SelectSlides[_SelectSlideDuetPlayer[i]].AddValue(CGame.GetSong(0).DuetPart2); _SelectSlides[_SelectSlideDuetPlayer[i]].Selection = i % 2; } else @@ -623,7 +623,7 @@ namespace Vocaluxe.Screens { for (int i = 0; i < CGame.NumPlayer; i++) { - if (CGame.GetNumSongs() == 1 && CGame.GetSong(1).IsDuet) + if (CGame.GetNumSongs() == 1 && CGame.GetSong(0).IsDuet) CGame.Players[i].VoiceNr = _SelectSlides[_SelectSlideDuetPlayer[i]].Selection; } @@ -652,7 +652,7 @@ namespace Vocaluxe.Screens if (_Texts["TextPlayer" + i].Text == "") _Texts["TextPlayer" + i].Text = CProfiles.GetPlayerName(-1, i); _Equalizers["EqualizerPlayer" + i].Visible = true; - if (CGame.GetNumSongs() == 1 && CGame.GetSong(1).IsDuet) + if (CGame.GetNumSongs() == 1 && CGame.GetSong(0).IsDuet) _SelectSlides["SelectSlideDuetPlayer" + i].Visible = true; } else diff --git a/Vocaluxe/Screens/CScreenScore.cs b/Vocaluxe/Screens/CScreenScore.cs index fac4c499..51cd0273 100644 --- a/Vocaluxe/Screens/CScreenScore.cs +++ b/Vocaluxe/Screens/CScreenScore.cs @@ -118,7 +118,8 @@ namespace Vocaluxe.Screens public override void OnShow() { base.OnShow(); - _Round = CGame.NumRounds > 1 ? 0 : 1; + //-1 --> Show average + _Round = CGame.NumRounds > 1 ? -1 : 0; _Points = CGame.GetPoints(); _SavePlayedSongs(); @@ -130,8 +131,8 @@ namespace Vocaluxe.Screens public override bool UpdateGame() { var players = new SPlayer[CGame.NumPlayer]; - if (_Round != 0) - players = _Points.GetPlayer(_Round - 1, CGame.NumPlayer); + if (_Round >= 0) + players = _Points.GetPlayer(_Round, CGame.NumPlayer); else { for (int i = 0; i < CGame.NumRounds; i++) @@ -263,7 +264,7 @@ namespace Vocaluxe.Screens { CSong song = null; var players = new SPlayer[CGame.NumPlayer]; - if (_Round != 0) + if (_Round >= 0) { song = CGame.GetSong(_Round); if (song == null) @@ -271,8 +272,8 @@ namespace Vocaluxe.Screens _Texts[_TextSong].Text = song.Artist + " - " + song.Title; if (_Points.NumRounds > 1) - _Texts[_TextSong].Text += " (" + _Round + "/" + _Points.NumRounds + ")"; - players = _Points.GetPlayer(_Round - 1, CGame.NumPlayer); + _Texts[_TextSong].Text += " (" + (_Round + 1) + "/" + _Points.NumRounds + ")"; + players = _Points.GetPlayer(_Round, CGame.NumPlayer); } else { @@ -386,16 +387,14 @@ namespace Vocaluxe.Screens private void _ChangeRound(int num) { - if ((_Round + num) <= _Points.NumRounds && (_Round + num > 0)) - { - _Round += num; - _UpdateRatings(); - } - else if (num < 0 && _Round == 1 && CGame.NumRounds > 1) - { - _Round = 0; - _UpdateRatings(); - } + if (_Points.NumRounds < 1) + return; + _Round += num; + _Round = _Round.Clamp(-1, _Points.NumRounds - 1); + if (_Round >= _Points.NumRounds) + _Round = _Points.NumRounds - 1; + + _UpdateRatings(); } private void _SavePlayedSongs() diff --git a/Vocaluxe/SongQueue/CSongQueue.cs b/Vocaluxe/SongQueue/CSongQueue.cs index d11aeb4c..3fb3169f 100644 --- a/Vocaluxe/SongQueue/CSongQueue.cs +++ b/Vocaluxe/SongQueue/CSongQueue.cs @@ -39,7 +39,7 @@ namespace Vocaluxe.SongQueue class CSongQueue : ISongQueue { private List _SongQueue; - private int _CurrentSong; + private int _CurrentRound; private CPoints _Points; #region Implementation @@ -52,9 +52,7 @@ namespace Vocaluxe.SongQueue public EGameMode GetCurrentGameMode() { - if (_CurrentSong >= 0 && _CurrentSong < _SongQueue.Count) - return _SongQueue[_CurrentSong].GameMode; - return EGameMode.TR_GAMEMODE_NORMAL; + return GetGameMode(_CurrentRound); } public bool AddVisibleSong(int visibleIndex, EGameMode gameMode) @@ -105,7 +103,7 @@ namespace Vocaluxe.SongQueue public void Reset() { - _CurrentSong = -1; + _CurrentRound = -1; } public void Start(SPlayer[] players) @@ -113,37 +111,51 @@ namespace Vocaluxe.SongQueue _Points = new CPoints(_SongQueue.Count, players); } - public void NextRound(SPlayer[] players) + public void StartNextRound(SPlayer[] players) { - if (_CurrentSong < _SongQueue.Count && _SongQueue.Count > 0) + if (IsFinished()) + return; + if (_CurrentRound > -1) { - if (_CurrentSong > -1) - { - _Points.SetPoints( - _CurrentSong, - _SongQueue[_CurrentSong].SongID, - players, - _SongQueue[_CurrentSong].GameMode); - } - _CurrentSong++; + _Points.SetPoints( + _CurrentRound, + _SongQueue[_CurrentRound].SongID, + players, + _SongQueue[_CurrentRound].GameMode); } + _CurrentRound++; } public bool IsFinished() { - return _CurrentSong == _SongQueue.Count || _SongQueue.Count == 0; + return _CurrentRound >= _SongQueue.Count || _SongQueue.Count == 0; } + /// + /// Get current round nr (1 ~ n) + /// + /// current round nr (1 ~ n) public int GetCurrentRoundNr() { - return _CurrentSong + 1; + return _CurrentRound + 1; } + /// + /// Returns current round + /// + /// Current round (0 based) + public int GetCurrentRound() + { + return _CurrentRound; + } + + /// + /// Get current song + /// + /// Song of current round or null if there is none/game finished public CSong GetSong() { - if (_CurrentSong >= 0 && _CurrentSong < _SongQueue.Count) - return CGameModes.Get(_SongQueue[_CurrentSong].GameMode).GetSong(_SongQueue[_CurrentSong].SongID); - return null; + return GetSong(_CurrentRound); } public int GetNumSongs() @@ -151,18 +163,28 @@ namespace Vocaluxe.SongQueue return _SongQueue.Count; } - public CSong GetSong(int num) + /// + /// Get song of specified round + /// + /// Round (0 based) + /// Current song or null if out of bounds + public CSong GetSong(int round) { - if (num - 1 < _SongQueue.Count && num > 0) - return CSongs.GetSong(_SongQueue[num - 1].SongID); + if (round < _SongQueue.Count && round >= 0) + return CSongs.GetSong(_SongQueue[round].SongID); return null; } - public EGameMode GetGameMode(int num) + /// + /// Get gameMode of specified round + /// + /// Round (0 based) + /// Current song or null if out of bounds + public EGameMode GetGameMode(int round) { - if (num - 1 < _SongQueue.Count && num > 0) - return _SongQueue[num - 1].GameMode; + if (round < _SongQueue.Count && round >= 0) + return _SongQueue[round].GameMode; return EGameMode.TR_GAMEMODE_NORMAL; } diff --git a/Vocaluxe/SongQueue/ISongQueue.cs b/Vocaluxe/SongQueue/ISongQueue.cs index c3e32f45..1831fd74 100644 --- a/Vocaluxe/SongQueue/ISongQueue.cs +++ b/Vocaluxe/SongQueue/ISongQueue.cs @@ -25,8 +25,6 @@ namespace Vocaluxe.SongQueue { void Init(); - EGameMode GetCurrentGameMode(); - bool AddVisibleSong(int visibleIndex, EGameMode gameMode); bool AddSong(int absoluteIndex, EGameMode gameMode); bool RemoveVisibleSong(int visibleIndex); @@ -35,15 +33,17 @@ namespace Vocaluxe.SongQueue void Reset(); void Start(SPlayer[] players); - void NextRound(SPlayer[] players); + void StartNextRound(SPlayer[] players); bool IsFinished(); - int GetCurrentRoundNr(); CPoints GetPoints(); int GetNumSongs(); + int GetCurrentRoundNr(); + CSong GetSong(); - CSong GetSong(int num); - EGameMode GetGameMode(int num); + CSong GetSong(int round); + EGameMode GetCurrentGameMode(); + EGameMode GetGameMode(int round); } } \ No newline at end of file diff --git a/VocaluxeLib/CHelper.cs b/VocaluxeLib/CHelper.cs index ad04b6db..04ea1a90 100644 --- a/VocaluxeLib/CHelper.cs +++ b/VocaluxeLib/CHelper.cs @@ -47,6 +47,15 @@ namespace VocaluxeLib return (int)result; } + public static T Clamp(this T val, T min, T max) where T : IComparable + { + if (val.CompareTo(min) < 0) + return min; + if (val.CompareTo(max) > 0) + return max; + return val; + } + /// /// Concat strings into one string with ", " as separator. /// From f97547693340521b812ff6bc9bd153a28224b16c Mon Sep 17 00:00:00 2001 From: Flamefire Date: Mon, 21 Oct 2013 16:15:27 +0200 Subject: [PATCH 7/7] Fix typo --- VocaluxeLib/Songs/CLineBase.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/VocaluxeLib/Songs/CLineBase.cs b/VocaluxeLib/Songs/CLineBase.cs index 08297651..658b71de 100644 --- a/VocaluxeLib/Songs/CLineBase.cs +++ b/VocaluxeLib/Songs/CLineBase.cs @@ -65,8 +65,8 @@ namespace VocaluxeLib.Songs { if (_Notes.Count == 0) return 0; - int min = _Notes.Max(note => note.Tone); - int max = _Notes.Min(note => note.Tone); + int min = _Notes.Min(note => note.Tone); + int max = _Notes.Max(note => note.Tone); return min - (max - min) / 4; }