-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfileStorage.lua
executable file
·144 lines (115 loc) · 4.35 KB
/
fileStorage.lua
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
----------------------------------
-- THE BIG 3 VIDEO GAME ---
-- [email protected] ---
----------------------------------
-- Script writes a JSON file to store the player's high scores and other data.
-- load the JSON library.
local json = require("json")
local filename = "gameData.json"
local storyboard = require( "storyboard" )
local GA = require ( "GameAnalytics" )
local json = require("json")
function saveTable(t)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local file = io.open(path, "w")
if file then
local contents = json.encode(t)
file:write( contents )
io.close( file )
return true
else
return false
end
end
function loadTable()
local path = system.pathForFile( filename, system.DocumentsDirectory)
-- print( path )
local contents = ""
local myTable = {}
local file = io.open( path, "r" )
if file then
-- read all contents of file into a string
local contents = file:read( "*a" )
myTable = json.decode(contents);
io.close( file )
storyboard.state.bPlayedPieTime = myTable.bPlayedPieTime
storyboard.state.bPlayedJokeyTime = myTable.bPlayedJokeyTime
storyboard.state.bPlayedWhoDatLady = myTable.bPlayedWhoDatLady
storyboard.state.pieTimeRounds = myTable.pieTimeRounds
return myTable
end
return nil
end
function save(bSaveScore,data)
if( not data ) then
data = loadTable()
end
if( not data ) then
data = {}
end
data.introMonth = tonumber(os.date("%m"))
if( not data.bPlayedPerry ) then
data.bPlayedPerry = false
data.perryHighScore = 0
end
if( not data.bPlayedMole ) then
data.bPlayedMole = false
data.moleHighScore = 0
end
if( not data.bPlayedDon ) then
data.bPlayedDon = false
data.donHighScore = 0
end
if( not data.highScore ) then
data.highScore = 0
end
if( not data.pieTimeRounds ) then
data.pieTimeRounds = 0
end
if( storyboard.state.character ) then
if( storyboard.state.character == "perry" ) then
data.bPlayedPerry = true
GA.newEvent( "design", { event_id="score:Perry", area="main", value=storyboard.state.score})
if( bSaveScore and storyboard.state.score > data.perryHighScore ) then
data.perryHighScore = storyboard.state.score
end
elseif( storyboard.state.character == "mole" ) then
data.bPlayedMole = true
GA.newEvent( "design", { event_id="score:Mole", area="main", value=storyboard.state.score})
if( bSaveScore and storyboard.state.score > data.moleHighScore ) then
data.moleHighScore = storyboard.state.score
end
else
data.bPlayedDon = true
GA.newEvent( "design", { event_id="score:Don", area="main", value=storyboard.state.score})
if( bSaveScore and storyboard.state.score > data.donHighScore ) then
data.donHighScore = storyboard.state.score
end
end
end
if( bSaveScore and storyboard.state.score > data.highScore ) then
data.highScore = storyboard.state.score
end
local myCat = "perryHighScore"
if ( system.getInfo("platformName") == "Android" ) then
myCat = "CgkIhf7UyIMOEAIQAA"
end
gameNetwork.request( "setHighScore", { localPlayerScore = { category= myCat, value=data.perryHighScore },})
myCat = "donHighScore"
if ( system.getInfo("platformName") == "Android" ) then
myCat = "CgkIhf7UyIMOEAIQAg"
end
gameNetwork.request( "setHighScore", { localPlayerScore = { category= myCat, value=data.donHighScore },})
myCat = "moleHighScore"
if ( system.getInfo("platformName") == "Android" ) then
myCat = "CgkIhf7UyIMOEAIQAQ"
end
gameNetwork.request( "setHighScore", { localPlayerScore = { category= myCat, value=data.moleHighScore },})
saveTable(data)
myCat = "overallHighScore"
if ( system.getInfo("platformName") == "Android" ) then
myCat = "CgkIhf7UyIMOEAIQAw"
end
gameNetwork.request( "setHighScore", { localPlayerScore = { category= myCat, value=data.highScore },})
saveTable(data)
end