Merge remote-tracking branch 'voc/release-0.1' into release-0.1

This commit is contained in:
Florian Kuepper committed 2012-03-03 22:23:11 +01:00
commit e43ee5eebf
1 file changed
+315 -79
+315 -79
View File
@@ -42,10 +42,45 @@ namespace Vocaluxe.Base
}
#region Highscores
public static int AddScore(string PlayerName, int Score, int LineNr, long Date, int Medley, int Duet, int Diff,
string Artist, string Title, int NumPlayed, string FilePath)
{
SPlayer player = new SPlayer();
player.Name = PlayerName;
player.Points = Score;
player.LineNr = LineNr;
player.DateTicks = Date;
player.Medley = (Medley == 1);
player.Duet = (Duet == 1);
player.Difficulty = (EGameDifficulty)Diff;
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
{
connection.Open();
}
catch (Exception)
{
return -1;
}
command = new SQLiteCommand(connection);
int DataBaseSongID = GetDataBaseSongID(Artist, Title, NumPlayed, command);
int result = AddScore(player, command, DataBaseSongID);
command.Dispose();
connection.Close();
connection.Dispose();
return result;
}
public static int AddScore(SPlayer player)
{
int lastInsertID = -1;
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + _HighscoreFilePath;
SQLiteCommand command;
@@ -62,6 +97,18 @@ namespace Vocaluxe.Base
command = new SQLiteCommand(connection);
int DataBaseSongID = GetDataBaseSongID(player, command);
int result = AddScore(player, command, DataBaseSongID);
connection.Close();
connection.Dispose();
return result;
}
private static int AddScore(SPlayer player, SQLiteCommand command, int DataBaseSongID)
{
int lastInsertID = -1;
if (DataBaseSongID >= 0)
{
@@ -72,7 +119,38 @@ namespace Vocaluxe.Base
int Duet = 0;
if (player.Duet)
Duet = 1;
command.CommandText = "SELECT id FROM Scores WHERE SongID = @SongID AND PlayerName = @PlayerName AND Score = @Score AND " +
"LineNr = @LineNr AND Date = @Date AND Medley = @Medley AND Duet = @Duet AND Difficulty = @Difficulty";
command.Parameters.Add("@SongID", System.Data.DbType.Int32, 0).Value = DataBaseSongID;
command.Parameters.Add("@PlayerName", System.Data.DbType.String, 0).Value = player.Name;
command.Parameters.Add("@Score", System.Data.DbType.Int32, 0).Value = (int)Math.Round(player.Points);
command.Parameters.Add("@LineNr", System.Data.DbType.Int32, 0).Value = (int)player.LineNr;
command.Parameters.Add("@Date", System.Data.DbType.Int64, 0).Value = player.DateTicks;
command.Parameters.Add("@Medley", System.Data.DbType.Int32, 0).Value = Medley;
command.Parameters.Add("@Duet", System.Data.DbType.Int32, 0).Value = Duet;
command.Parameters.Add("@Difficulty", System.Data.DbType.Int32, 0).Value = (int)player.Difficulty;
SQLiteDataReader reader = null;
try
{
reader = command.ExecuteReader();
}
catch (Exception)
{
;
}
if (reader != null && reader.HasRows)
{
if (reader.Read())
return reader.GetInt32(0);
}
if (reader != null)
reader.Close();
command.CommandText = "INSERT INTO Scores (SongID, PlayerName, Score, LineNr, Date, Medley, Duet, Difficulty) " +
"VALUES (@SongID, @PlayerName, @Score, @LineNr, @Date, @Medley, @Duet, @Difficulty)";
command.Parameters.Add("@SongID", System.Data.DbType.Int32, 0).Value = DataBaseSongID;
@@ -88,7 +166,7 @@ namespace Vocaluxe.Base
//Read last insert line
command.CommandText = "SELECT id FROM Scores ORDER BY Date DESC LIMIT 0, 1";
SQLiteDataReader reader = null;
reader = null;
try
{
reader = command.ExecuteReader();
@@ -110,10 +188,6 @@ namespace Vocaluxe.Base
}
}
command.Dispose();
connection.Close();
connection.Dispose();
return lastInsertID;
}
@@ -198,9 +272,38 @@ namespace Vocaluxe.Base
if (song == null)
return -1;
return GetDataBaseSongID(song.Artist, song.Title, 0, command);
}
private static int GetDataBaseSongID(string Artist, string Title, string FilePath, int DefNumPlayed)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
{
connection.Open();
}
catch (Exception)
{
return -1;
}
command = new SQLiteCommand(connection);
int id = GetDataBaseSongID(Artist, Title, DefNumPlayed, command);
command.Dispose();
connection.Close();
connection.Dispose();
return id;
}
private static int GetDataBaseSongID(string Artist, string Title, int DefNumPlayed, SQLiteCommand command)
{
command.CommandText = "SELECT id FROM Songs WHERE [Title] = @title AND [Artist] = @artist";
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = song.Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = song.Artist;
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = Artist;
SQLiteDataReader reader = null;
try
@@ -226,14 +329,15 @@ namespace Vocaluxe.Base
reader.Close();
command.CommandText = "INSERT INTO Songs (Title, Artist, NumPlayed) " +
"VALUES (@title, @artist, 0)";
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = song.Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = song.Artist;
"VALUES (@title, @artist, @numplayed)";
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = Artist;
command.Parameters.Add("@numplayed", System.Data.DbType.Int32, 0).Value = DefNumPlayed;
command.ExecuteNonQuery();
command.CommandText = "SELECT id FROM Songs WHERE [Title] = @title AND [Artist] = @artist";
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = song.Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = song.Artist;
command.Parameters.Add("@title", System.Data.DbType.String, 0).Value = Title;
command.Parameters.Add("@artist", System.Data.DbType.String, 0).Value = Artist;
reader = null;
try
@@ -264,24 +368,136 @@ namespace Vocaluxe.Base
return -1;
}
private static bool InitHighscoreDB()
private static bool GetDataBaseSongInfos(int SongID, out string Artist, out string Title, out int NumPlayed, string FilePath)
{
Artist = String.Empty;
Title = String.Empty;
NumPlayed = 0;
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
{
connection.Open();
}
catch (Exception)
{
return false;
}
command = new SQLiteCommand(connection);
command.CommandText = "SELECT Artist, Title, NumPlayed FROM Songs WHERE [id] = @id";
command.Parameters.Add("@id", System.Data.DbType.String, 0).Value = SongID;
SQLiteDataReader reader = null;
try
{
reader = command.ExecuteReader();
}
catch (Exception)
{
return false;
}
if (reader != null && reader.HasRows)
{
reader.Read();
Artist = reader.GetString(0);
Title = reader.GetString(1);
NumPlayed = reader.GetInt32(2);
reader.Close();
reader.Dispose();
command.Dispose();
connection.Close();
connection.Dispose();
return true;
}
else
{
if (reader != null)
reader.Close();
}
command.Dispose();
connection.Close();
connection.Dispose();
return false;
}
private static void InitHighscoreDB()
{
string OldDBFilePath = Path.Combine(Environment.CurrentDirectory, CSettings.sFileOldHighscoreDB);
if (File.Exists(OldDBFilePath))
{
if (File.Exists(_HighscoreFilePath))
{
CLog.LogError("Found old Highscore DB (" + OldDBFilePath + "). If you want to import it just delete the new one (" + _HighscoreFilePath + ")");
CreateOrConvert(OldDBFilePath);
CreateOrConvert(_HighscoreFilePath);
ImportData(OldDBFilePath, _HighscoreFilePath);
File.Delete(OldDBFilePath);
}
else
{
File.Copy(OldDBFilePath, _HighscoreFilePath);
File.Move(OldDBFilePath, OldDBFilePath + ".old");
CreateOrConvert(_HighscoreFilePath);
File.Delete(OldDBFilePath);
}
}
else
CreateOrConvert(_HighscoreFilePath);
}
private static void CreateHighscoreDB(string FilePath)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + _HighscoreFilePath;
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
{
connection.Open();
}
catch (Exception)
{
return;
}
command = new SQLiteCommand(connection);
command.CommandText = "CREATE TABLE IF NOT EXISTS Version ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Value INTEGER NOT NULL);";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Version (id, Value) VALUES(NULL, " + CSettings.iDatabaseHighscoreVersion.ToString() + ")";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE IF NOT EXISTS Songs ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"Artist TEXT NOT NULL, Title TEXT NOT NULL, NumPlayed INTEGER);";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE IF NOT EXISTS Scores ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"SongID INTEGER NOT NULL, PlayerName TEXT NOT NULL, Score INTEGER NOT NULL, LineNr INTEGER NOT NULL, Date BIGINT NOT NULL, " +
"Medley INTEGER NOT NULL, Duet INTEGER NOT NULL, Difficulty INTEGER NOT NULL);";
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
connection.Dispose();
}
/// <summary>
/// Creates a new Vocaluxe Database if no file exists. Converts an existing old Ultrastar Deluxe highscore database into vocaluxe format.
/// </summary>
/// <param name="FilePath">Database file path</param>
/// <returns></returns>
private static bool CreateOrConvert(string FilePath)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
@@ -310,12 +526,12 @@ namespace Vocaluxe.Base
if (reader == null)
{
// create new database/tables
CreateHighscoreDB();
CreateHighscoreDB(FilePath);
}
else if (reader.FieldCount == 0)
{
// create new database/tables
CreateHighscoreDB();
CreateHighscoreDB(FilePath);
}
else
{
@@ -353,10 +569,10 @@ namespace Vocaluxe.Base
//Check for USDX 1.1 DB
if (version == 1)
ConvertFrom110();
ConvertFrom110(FilePath);
//Check for USDX 1.01 or CMD Mod DB
else if (version == 0 && scoresTableExists)
ConvertFrom101();
ConvertFrom101(FilePath);
command.Dispose();
@@ -366,61 +582,15 @@ namespace Vocaluxe.Base
return true;
}
private static void CreateHighscoreDB()
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + _HighscoreFilePath;
SQLiteCommand command;
try
{
connection.Open();
}
catch (Exception)
{
return;
}
command = new SQLiteCommand(connection);
command.CommandText = "CREATE TABLE IF NOT EXISTS Version ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Value INTEGER NOT NULL);";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Version (id, Value) VALUES(NULL, " + CSettings.iDatabaseHighscoreVersion.ToString() + ")";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE IF NOT EXISTS Songs ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"Artist TEXT NOT NULL, Title TEXT NOT NULL, NumPlayed INTEGER);";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE IF NOT EXISTS Scores ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
"SongID INTEGER NOT NULL, PlayerName TEXT NOT NULL, Score INTEGER NOT NULL, LineNr INTEGER NOT NULL, Date BIGINT NOT NULL, " +
"Medley INTEGER NOT NULL, Duet INTEGER NOT NULL, Difficulty INTEGER NOT NULL);";
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
connection.Dispose();
/*
// Auslesen des zuletzt eingefügten Datensatzes.
command.CommandText = "SELECT id, name FROM beispiel ORDER BY id DESC LIMIT 0, 1";
while (reader.Read())
{
Console.WriteLine("Dies ist der {0}. eingefügte Datensatz mit dem Wert: \"{1}\"", reader[0].ToString(), reader[1].ToString());
}
*/
}
/// <summary>
/// Converts a USDX 1.1 database into the Vocaluxe format
/// </summary>
/// <param name="FilePath">Database file path</param>
/// <returns>True if succeeded</returns>
private static bool ConvertFrom110()
private static bool ConvertFrom110(string FilePath)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + _HighscoreFilePath;
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
try
@@ -538,11 +708,12 @@ namespace Vocaluxe.Base
/// <summary>
/// Converts a USDX 1.01 or CMD 1.01 database to Vocaluxe format
/// </summary>
/// <param name="FilePath">Database file path</param>
/// <returns>True if succeeded</returns>
private static bool ConvertFrom101()
private static bool ConvertFrom101(string FilePath)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + _HighscoreFilePath;
connection.ConnectionString = "Data Source=" + FilePath;
SQLiteCommand command;
SQLiteDataReader reader = null;
@@ -595,11 +766,11 @@ namespace Vocaluxe.Base
List<SData> songs = new List<SData>();
Sqlite3.sqlite3 OldDB;
int res = Sqlite3.sqlite3_open(_HighscoreFilePath, out OldDB);
int res = Sqlite3.sqlite3_open(FilePath, out OldDB);
if (res != Sqlite3.SQLITE_OK)
{
CLog.LogError("Error opening Database: " + _HighscoreFilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
CLog.LogError("Error opening Database: " + FilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
}
else
{
@@ -608,11 +779,11 @@ namespace Vocaluxe.Base
if (res != Sqlite3.SQLITE_OK)
{
CLog.LogError("Error query Database: " + _HighscoreFilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
CLog.LogError("Error query Database: " + FilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
}
else
{
Sqlite3.sqlite3_step(Stmt);
//Sqlite3.sqlite3_step(Stmt);
Encoding UTF8 = Encoding.UTF8;
Encoding CP1252 = Encoding.GetEncoding(1252);
@@ -643,11 +814,11 @@ namespace Vocaluxe.Base
if (res != Sqlite3.SQLITE_OK)
{
CLog.LogError("Error query Database: " + _HighscoreFilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
CLog.LogError("Error query Database: " + FilePath + " (" + Sqlite3.sqlite3_errmsg(OldDB) + ")");
}
else
{
Sqlite3.sqlite3_step(Stmt);
//Sqlite3.sqlite3_step(Stmt);
Encoding UTF8 = Encoding.UTF8;
Encoding CP1252 = Encoding.GetEncoding(1252);
@@ -711,6 +882,71 @@ namespace Vocaluxe.Base
return true;
}
private static bool ImportData(string SourceDBPath, string DestinationDBPath)
{
#region open db
SQLiteConnection connSource = new SQLiteConnection();
connSource.ConnectionString = "Data Source=" + SourceDBPath;
try
{
connSource.Open();
}
catch (Exception e)
{
CLog.LogError("Error on import high score data. Can't open source database \"" + SourceDBPath + "\" (" + e.Message + ")");
return false;
}
#endregion open db
SQLiteCommand cmdSource = new SQLiteCommand(connSource);
SQLiteDataReader rSource;
#region import table scores
cmdSource.CommandText = "SELECT SongID, PlayerName, Score, LineNr, Date, Medley, Duet, Difficulty FROM Scores";
rSource = cmdSource.ExecuteReader();
if (rSource == null)
{
cmdSource.Dispose();
connSource.Close();
return false;
}
if (rSource.FieldCount == 0)
{
rSource.Close();
cmdSource.Dispose();
connSource.Close();
return true;
}
while (rSource.Read())
{
int songid = rSource.GetInt32(0);
string player = rSource.GetString(1);
int score = rSource.GetInt32(2);
int linenr = rSource.GetInt32(3);
long date = rSource.GetInt64(4);
int medley = rSource.GetInt32(5);
int duet = rSource.GetInt32(6);
int diff = rSource.GetInt32(7);
string artist, title;
int numplayed;
if (GetDataBaseSongInfos(songid, out artist, out title, out numplayed, SourceDBPath))
{
AddScore(player, score, linenr, date, medley, duet, diff, artist, title, numplayed, _HighscoreFilePath);
}
}
#endregion import table scores
rSource.Close();
cmdSource.Dispose();
connSource.Close();
return true;
}
#endregion Highscores
#region Cover