From e3f744677e25a29cf7e6a89d6bf1eb989f23e398 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Thu, 26 Sep 2013 13:47:19 +0200 Subject: [PATCH] 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; }