Sub process_globals
Dim SQL As SQL
Dim NumberOfScores As Int
'size of board
NumberOfScores = 10
Dim LowestScore As Int
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
CreateDatabase
End If
'test
ResetScores
AddScore("a", 123)
AddScore("sda", 123)
AddScore("aa", 334)
AddScore("gra", 123)
AddScore("wa", 123)
AddScore("gb", 1)
AddScore("gea", 123)
LogScores
AddScore("ea", 123)
AddScore("ga", 122)
AddScore("da", 123)
AddScore("ag", 23)
AddScore("wwa", 5)
AddScore("fa", 423)
AddScore("age", 2)
LogScores
End Sub
Sub CreateDatabase
SQL.Initialize(File.DirInternal, "highscore.db", True)
'create the tables if they do not exist
Dim fields As Map
fields.Initialize
fields.Put("name", DBUtils.DB_TEXT)
fields.Put("score", DBUtils.DB_INTEGER)
DBUtils.CreateTable(SQL, "main", fields, "")
LowestScore = GetLowestScore
End Sub
Sub ResetScores
'delete all scores
SQL.ExecNonQuery("DELETE FROM main")
LowestScore = GetLowestScore
End Sub
Sub AddScore(Name As String, Score As Int)
If Score <= LowestScore Then
Log("Name=" & Name & ", Score=" & Score & ", Score is too low!")
Else
SQL.ExecNonQuery2("INSERT INTO main (name, score) VALUES(?, ?)", Array As Object(Name, Score))
SQL.ExecNonQuery2("DELETE FROM main WHERE score <= ?", Array As Object(LowestScore))
LowestScore = GetLowestScore
End If
End Sub
Sub GetLowestScore As Int
If SQL.ExecQuerySingleResult("SELECT count(*) FROM main") < NumberOfScores Then Return 0
Dim table As List
table = DBUtils.ExecuteMemoryTable(SQL, "SELECT score FROM main ORDER BY score DESC", Null, 0)
Dim s() As String
s = table.Get(NumberOfScores - 1)
Return s(0)
End Sub
Sub LogScores
Dim table As List
table = DBUtils.ExecuteMemoryTable(SQL, "SELECT name, score FROM main ORDER BY score DESC", Null, 0)
Dim s() As String
For i = 0 To table.Size - 1
s = table.Get(i)
Log("Name=" & s(0) & ", Score=" & s(1))
Next
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub