-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.lua
More file actions
168 lines (143 loc) · 5.12 KB
/
database.lua
File metadata and controls
168 lines (143 loc) · 5.12 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
--NOTE: on android, clearing app data doesnt' delete the database, just contents of it, apparently.
require("helpers")
local sqlite3 = require("sqlite3")
db = ""
local dbVersionID = 1
function startDatabase()
-- Open "data.db". If the file doesn't exist, it will be created
local path = system.pathForFile("data.db", system.DocumentsDirectory)
db = sqlite3.open(path)
-- Handle the "applicationExit" event to close the database
local function onSystemEvent(event)
if (event.type == "applicationExit" and db:isopen()) then db:close() end
end
Runtime:addEventListener("system", onSystemEvent)
end
function upgradeDatabaseVersion(oldDBversion)
--if oldDbVersion is nil, that should mean we're making the DB for the first time and can skip this step
if (oldDBversion == nil or oldDBversion == dbVersionID) then return end
if (oldDBversion < 1) then
--do any scripting to match upgrade to version 1
--which should be none, since that's the baseline for this feature.
end
Exec("UPDATE systemData SET dbVersionID = " .. dbVersionID)
end
function ResetDatabase()
db:close()
path = system.pathForFile("data.db", system.DocumentsDirectory)
db = sqlite3.open(path)
db:exec("drop table test")
db:exec("drop table plusCodesVisited")
db:exec("drop table acheivements")
db:exec("drop table playerData")
db:exec("drop table systemData")
db:exec("drop table weeklyVisited")
db:exec("drop table dailyVisited")
db:exec("drop table trophysBought")
db:exec("drop table areasOwned")
db:close()
startDatabase()
end
function Query(sql)
results = {}
local tempResults = db:rows(sql)
for row in db:rows(sql) do
table.insert(results, row)
end
if (debugDB) then print(dump(results)) end
return results --results is a table of tables EX {[1] : {[1] : 1}} for count(*) when there are results.
end
function Exec(sql)
results = {}
local resultCode = db:exec(sql);
if (resultCode == 0) then
return 0
end
--now its all error tracking.
local errormsg = db:errmsg()
print(errormsg)
native.showAlert("dbExec error", errormsg .. "|" .. sql)
return resultCode
end
function Score()
local query = "SELECT totalPoints as p from playerData"
local qResults = Query(query)
if (#qResults > 0) then
for i,row in ipairs(qResults) do
return row[1]
end
else
return "?"
end
end
--THis loads a single terrain data entry from the DB.
function LoadTerrainData(pluscode) --plus code does not contain a + here
if (debugDB) then print("loading terrain data for " .. pluscode) end
local query = "SELECT * from terrainData WHERE plusCode = '" .. pluscode .. "'"
local results = Query(query)
for i,row in ipairs(results) do
if (debugDB) then print(dump(row)) end
return row
end
return {} --empty table means no data found.
end
--This loads all terrain for a cell8
function LoadTerrainDataCell8(pluscode) --plus code does not contain a + here
if (debugDB) then print("loading terrain data for " .. pluscode) end
local query = "SELECT * from terrainData WHERE plusCode LIKE '" .. pluscode .. "%'"
local results = Query(query)
return results
end
function DownloadedCell8(pluscode)
local query = "SELECT COUNT(*) as c FROM dataDownloaded WHERE pluscode8 = '" .. pluscode .. "'"
for i,row in ipairs(Query(query)) do
if (row[1] >= 1) then --any number of entries over 1 means this block was visited.
return true
else
return false
end
end
return false
end
function SpendPoints(points)
local cmd = "UPDATE playerData SET totalPoints = totalPoints - " .. points
db:exec(cmd)
end
function GetServerAddress()
local query = "SELECT serverAddress FROM systemData"
for i,row in ipairs(Query(query)) do
if (#row == 1) then
return row[1]
else
return "noServerFound"
end
end
return ""
end
function SetServerAddress(url)
local cmd = "UPDATE systemData SET serverAddress = '" .. url .. "'"
db:exec(cmd)
end
function getHintInfo(plusCode)
local cmd = 'SELECT * FROM geocacheHints WHERE plusCode8 = "' .. plusCode .. '"'
local results = Query(cmd)
for i,row in ipairs(Query(cmd)) do
return row
end
--insert the data now.
local exec = "INSERT INTO geocacheHints(plusCode8, hintsLeft, secretsLeft) VALUES('" .. plusCode .. "', 3, 1)"
db:exec(exec)
local t = {}
t[2] = plusCode
t[3] = 3
t[4] = 1
return t
end
function spendHint(plusCode)
local cmd = 'UPDATE geocacheHints SET hintsLeft = (hintsLeft - 1) WHERE plusCode8 = "' .. plusCode .. '"'
db:exec(cmd)
end
function spendSecret(plusCode)
local cmd = 'UPDATE geocacheHints SET secretsLeft = (secretsLeft - 1) WHERE plusCode8 = "' .. plusCode .. '"'
db:exec(cmd)
end