-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayGame.lua
More file actions
269 lines (203 loc) · 6.17 KB
/
PlayGame.lua
File metadata and controls
269 lines (203 loc) · 6.17 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local func = require("functions")
local http = require("httpwork")
local widget = require( "widget" )
local url = require("socket.url")
local socket = require("socket")
local group
local MainGroup
local scView
local ExtraGrp
local UList, SList
local UserList = {}
local SelectList = {}
local LoadData2
local PlayGameStarted
local ConBut
local ScaleLine
local xPos
local QUnit
local Suffix = ""
local Prefix = ""
local Answer
local AnsPos
local AnswerSet
local TimerCount
local tmID
local tmGrp
local LoadQuestion
local UserAnswer
local LoadTime
local FinishTime
local ConfirmAnswer
local NumVal
local QGroup
function round(n)
return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end
function comma_value(amount)
local formatted = amount
local k
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k==0) then
break
end
end
return formatted
end
local DrawScaleLine = function()
local vl
if ScaleLine ~= nil then
ScaleLine:removeSelf()
ScaleLine = nil
end
ScaleLine = func.DrawLine(xPos,800,xPos,1000,6,1,0,0)
MainGroup:insert(ScaleLine)
if NumVal ~= nil then
NumVal:removeSelf()
NumVal = nil
end
vl = AnsPos - xPos
if _G.GameStructure[1].Questions[_G.CurrentQuestion].UseCommas == "1" then
UserAnswer = comma_value(round(Answer - (vl * QUnit)) )
else
UserAnswer = round(Answer - (vl * QUnit))
end
NumVal = func.StandardLabelBold(Prefix .. UserAnswer .. Suffix,display.contentWidth/2,700,60,255/255,255/255,0/255,nil,nil,1)
MainGroup:insert(NumVal)
end
local ScaleMove = function(event)
if event.phase == "moved" and AnswerSet == 0 then
xPos = event.x
DrawScaleLine()
end
end
local DisplayTimer = function()
if tmGrp ~= nil then
tmGrp:removeSelf()
tmGrp = nil
end
if TimerCount == 0 then
AnswerSet = 1
if ConBut ~= nil then
ConBut:removeSelf()
ConBut = nil
end
timer.cancel( tmID )
if FinishTime == 0 then
FinishTime = socket.gettime()*1000
end
_G.GameStructure[1].Questions[_G.CurrentQuestion].UserAnswer = UserAnswer
_G.GameStructure[1].Questions[_G.CurrentQuestion].AnswerTime = FinishTime - LoadTime
_G.CurrentQuestion = _G.CurrentQuestion + 1
if _G.CurrentQuestion > tonumber(_G.GameStructure[1].NoQuestions) then
func.LoadScreen("FinishGame","crossFade",1000)
else
LoadQuestion()
end
return
end
tmGrp = display.newGroup()
tmGrp.alpha = 0
tmGrp.xScale = 1
tmGrp.yScale = 1
tmGrp:insert(func.StandardImage("Circle.png",550,60))
tmGrp:insert(func.StandardLabelBold(TimerCount,550,60,40,255/255,255/255,0/255,nil,nil,1))
transition.to( tmGrp, { time=500, alpha=1,x=-260, y=-40,xScale=1.5,yScale=1.5,transition=easing.outQuart } )
TimerCount = TimerCount - 1
if ConBut == nil and AnswerSet == 0 then
ConBut = func.StandardImage("Confirm.png",display.contentWidth/2,1100,ConfirmAnswer,nil,nil,1.4)
QGroup:insert(ConBut)
end
end
local StartTimer = function()
tmID = timer.performWithDelay( 1000, DisplayTimer, 0 )
end
ConfirmAnswer = function(event)
if event.phase == "ended" then
AnswerSet = 1
if ConBut ~= nil then
ConBut:removeSelf()
ConBut = nil
end
FinishTime = socket.gettime()*1000
QGroup:insert(func.StandardLabelBold("Answer Set",display.contentWidth/2,1100,40,91/255,155/255,213/255,nil,nil,1))
end
end
LoadQuestion = function()
if QGroup ~= nil then
QGroup:removeSelf()
QGroup = nil
end
local qText = ""
local qImage
AnswerSet = 0
Answer = _G.GameStructure[1].Questions[_G.CurrentQuestion].ActualAnswer
QUnit = _G.GameStructure[1].Questions[_G.CurrentQuestion].QUnit
TimerCount = _G.GameStructure[1].TimeLimit
Prefix = _G.GameStructure[1].Questions[_G.CurrentQuestion].Prefix
Suffix = _G.GameStructure[1].Questions[_G.CurrentQuestion].Suffix
qText = _G.CurrentQuestion .. ". " .. _G.GameStructure[1].Questions[_G.CurrentQuestion].QuestionText
QGroup = display.newGroup()
QGroup:insert(func.StandardLabelBoldW(qText .. "?",20,250,600,40,91/255,155/255,213/255))
if _G.GameStructure[1].Questions[_G.CurrentQuestion].HasImage == "Yes" then
qImage = func.StandardImage("img" .. _G.GameStructure[1].Questions[_G.CurrentQuestion].QuestionID .. ".jpg",-100,500,nil,nil,nil,nil,nil,nil,1,nil,"Temporary",550,250)
else
qImage = func.StandardImage("QMark.png",-100,500)
end
QGroup:insert(qImage)
transition.to( qImage, { time=500, x=400, transition=easing.outQuart } )
xPos = display.contentWidth/2
AnsPos = math.random(15,600)
DrawScaleLine()
MainGroup:insert(QGroup)
StartTimer()
LoadTime = socket.gettime()*1000
FinishTime = 0
_G.HaltNav = nil
end
function scene:enterScene(event)
_G.HaltNav = 1
_G.CurrentQuestion = 1
transition.to( MainGroup, { time=700, alpha=1, transition=easing.outQuart,onComplete=LoadQuestion } )
end
function scene:createScene( event )
group = self.view
local prior_scene = storyboard.getPrevious()
storyboard.removeScenePurgeAll( prior_scene )
MainGroup = display.newGroup()
MainGroup.alpha = 0
MainGroup:insert( func.StandardImage("GameOn.png",display.contentWidth/2,60,nil,nil,nil,0.7))
MainGroup:insert( func.StandardImage("Scale.png",display.contentWidth/2,900,ScaleMove,nil,nil,1.8))
group:insert(MainGroup)
end
function scene:destroyScene( event )
if ConBut ~= nil then
ConBut:removeSelf()
ConBut = nil
end
if NumVal ~= nil then
NumVal:removeSelf()
NumVal = nil
end
if QGroup ~= nil then
QGroup:removeSelf()
QGroup = nil
end
timer.cancel( tmID )
network.cancel( _G.NetworkID )
end
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )
-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )
-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.removeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )
---------------------------------------------------------------------------------
return scene