aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/Classes/UScores.pas
blob: f12438688c340b91fcc73de7366d51e6ff0ea93e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
unit UScores;

interface

uses USongs, SQLiteTable3;

procedure InitScore(const Filename: string);
procedure ReadScore(var Song: TSong);
procedure WriteScore(var Song: TSong);
procedure AddScore(var Song: TSong; Level: integer; Name: string; Score: integer);

var
ScoreDB: TSqliteDatabase;
sFilename: string;

implementation

uses IniFiles, SysUtils;

procedure InitScore(const Filename: string);
//var
  //TableData: TSqliteTable;
begin
  //Open Database
  ScoreDB := TSqliteDatabase.Create(Filename);
  sFilename := Filename;

  try
  //Look for Tables => When not exist Create them
  if not ScoreDB.TableExists('US_Scores') then
    ScoreDB.execsql('CREATE TABLE `US_Scores` (`SongID` INT( 11 ) NOT NULL , `Difficulty` INT( 1 ) NOT NULL , `Player` VARCHAR( 150 ) NOT NULL , `Score` INT( 5 ) NOT NULL );');

  if not ScoreDB.TableExists('US_Songs') then
    ScoreDB.execsql('CREATE TABLE `US_Songs` (`ID` INTEGER PRIMARY KEY, `Artist` VARCHAR( 255 ) NOT NULL , `Title` VARCHAR( 255 ) NOT NULL );');

  finally
  //ScoreDB.Free;
  end;

end;

procedure ReadScore(var Song: TSong);
var
  TableData: TSqliteTable;
  Dif: Byte;
begin
  //ScoreDB := TSqliteDatabase.Create(sFilename);
  try
  try
  //Search Song in DB
  TableData := ScoreDB.GetTable('SELECT `Difficulty`, `Player`, `Score` FROM `us_scores` WHERE `SongID` = (SELECT `ID` FROM `us_songs` WHERE `Artist` = "' + Song.Artist + '" AND `Title` = "' + Song.Title + '" LIMIT 1) ORDER BY `Score` DESC  LIMIT 15');
  //Empty Old Scores
  SetLength (Song.Score[0], 0);
  SetLength (Song.Score[1], 0);
  SetLength (Song.Score[2], 0);

  while not TableData.Eof do//Go through all Entrys
  begin//Add one Entry to Array
    Dif := StrtoInt(TableData.FieldAsString(TableData.FieldIndex['Difficulty']));
    if (Dif>=0) AND (Dif<=2) then
    begin
      SetLength(Song.Score[Dif], Length(Song.Score[Dif]) + 1);

      Song.Score[Dif, high(Song.Score[Dif])].Name := TableData.FieldAsString(TableData.FieldIndex['Player']);
      Song.Score[Dif, high(Song.Score[Dif])].Score:= StrtoInt(TableData.FieldAsString(TableData.FieldIndex['Score']));
    end;
    TableData.Next;
  end;

  except //Im Fehlerfall
  for Dif := 0 to 2 do
  begin
  SetLength(Song.Score[Dif], 1);
  Song.Score[Dif, 1].Name := 'Error Reading ScoreDB';
  end;
  end;
  finally
  //ScoreDb.Free;
  end;
end;

procedure AddScore(var Song: TSong; Level: integer; Name: string; Score: integer);
var
ID: Integer;
TableData: TSqliteTable;
begin
  //ScoreDB := TSqliteDatabase.Create(sFilename);
  try

  ID := ScoreDB.GetTableValue('SELECT `ID` FROM `US_Songs` WHERE `Artist` = "' + Song.Artist + '" AND `Title` = "' + Song.Title + '"');
  if ID = 0 then //Song doesn't exist -> Create
  begin
    ScoreDB.ExecSQL ('INSERT INTO `US_Songs` ( `ID` , `Artist` , `Title` ) VALUES (NULL , "' + Song.Artist + '", "' + Song.Title + '");');
    ID := ScoreDB.GetTableValue('SELECT `ID` FROM `US_Songs` WHERE `Artist` = "' + Song.Artist + '" AND `Title` = "' + Song.Title + '"');
    if ID = 0 then //Could not Create Table
      exit;
  end;
  //Create new Entry
  ScoreDB.ExecSQL('INSERT INTO `US_Scores` ( `SongID` , `Difficulty` , `Player` , `Score` ) VALUES ("' + InttoStr(ID) + '", "' + InttoStr(Level) + '", "' + Name + '", "' + InttoStr(Score) + '");');

  //Delete Last Position when there are more than 5 Entrys
  if ScoreDB.GetTableValue('SELECT COUNT(`SongID`) FROM `US_Scores` WHERE `SongID` = "' + InttoStr(ID) + '" AND `Difficulty` = "' + InttoStr(Level) +'"') > 5 then
  begin
    TableData := ScoreDB.GetTable('SELECT `Player`, `Score` FROM `US_Scores` WHERE SongID = "' + InttoStr(ID) + '" AND `Difficulty` = "' + InttoStr(Level) +'" ORDER BY `Score` ASC LIMIT 1');
    ScoreDB.ExecSQL('DELETE FROM `US_Scores` WHERE SongID = "' + InttoStr(ID) + '" AND `Difficulty` = "' + InttoStr(Level) +'" AND `Player` = "' + TableData.FieldAsString(TableData.FieldIndex['Player']) + '" AND `Score` = "' + TableData.FieldAsString(TableData.FieldIndex['Score']) + '"');
  end;

  finally
  //ScoreDB.Free;
  end;
end;

//Not used with new SQLLite DB System
procedure WriteScore(var Song: TSong);
{var
  F:    TIniFile;
  S:    integer;
  Lev:      integer;
  LevS:     string;
  FileName: string;}
begin
  {FileName := Song.Path + ChangeFileExt(Song.FileName, '.sco');
  if (not FileExists(FileName)) or (FileExists(FileName) and  DeleteFile(FileName)) then begin
    // file has been deleted -> creating new file
    F := TIniFile.Create(FileName);

    for Lev := 0 to 2 do begin
      case Lev of
        0:  LevS := 'Easy';
        1:  LevS := 'Normal';
        2:  LevS := 'Hard';
      end;

      for S := 0 to high(Song.Score[Lev]) do begin
        F.WriteString(LevS + IntToStr(S+1), 'Name', Song.Score[Lev, S].Name);
        F.WriteInteger(LevS + IntToStr(S+1), 'Score', Song.Score[Lev, S].Score);

      end; // for S
    end; // for Lev
    F.Free;
  end; // if}
end;

end.