-
Notifications
You must be signed in to change notification settings - Fork 1
/
PaneDisplay.lua
565 lines (493 loc) · 18.3 KB
/
PaneDisplay.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
-- get the machine_profile now at file init; no need to keep fetching with each SetCommand
local machine_profile = PROFILEMAN:GetMachineProfile()
-- the height of the footer is defined in ./Graphics/_footer.lua, but we'll
-- use it here when calculating where to position the PaneDisplay
local footer_height = 32
-- height of the PaneDisplay in pixels
local pane_height = 60
local text_zoom = WideScale(0.8, 0.9)
-- -----------------------------------------------------------------------
-- Convenience function to return the SongOrCourse and StepsOrTrail for a
-- for a player.
local GetSongAndSteps = function(player)
local SongOrCourse = (GAMESTATE:IsCourseMode() and GAMESTATE:GetCurrentCourse()) or GAMESTATE:GetCurrentSong()
local StepsOrTrail = (GAMESTATE:IsCourseMode() and GAMESTATE:GetCurrentTrail(player)) or GAMESTATE:GetCurrentSteps(player)
return SongOrCourse, StepsOrTrail
end
-- -----------------------------------------------------------------------
local GetScoreFromProfile = function(profile, SongOrCourse, StepsOrTrail)
-- if we don't have everything we need, return nil
if not (profile and SongOrCourse and StepsOrTrail) then return nil end
return profile:GetHighScoreList(SongOrCourse, StepsOrTrail):GetHighScores()[1]
end
local GetScoreForPlayer = function(player)
local highScore
if PROFILEMAN:IsPersistentProfile(player) then
local SongOrCourse, StepsOrTrail = GetSongAndSteps(player)
highScore = GetScoreFromProfile(PROFILEMAN:GetProfile(player), SongOrCourse, StepsOrTrail)
end
return highScore
end
-- -----------------------------------------------------------------------
local SetNameAndScore = function(name, score, nameActor, scoreActor)
if not scoreActor or not nameActor then return end
scoreActor:settext(score)
nameActor:settext(name)
end
local GetMachineTag = function(gsEntry)
if not gsEntry then return end
if gsEntry["machineTag"] then
-- Make sure we only use up to 4 characters for space concerns.
return gsEntry["machineTag"]:sub(1, 4):upper()
end
-- User doesn't have a machineTag set. We'll "make" one based off of
-- their name.
if gsEntry["name"] then
-- 4 Characters is the "intended" length.
return gsEntry["name"]:sub(1,4):upper()
end
return ""
end
local GetScoresRequestProcessor = function(res, params)
local master = params.master
if master == nil then return end
-- If we're not hovering over a song when we get the request, then we don't
-- have to update anything. We don't have to worry about courses here since
-- we don't run the RequestResponseActor in CourseMode.
if GAMESTATE:GetCurrentSong() == nil then return end
local data = res.statusCode == 200 and JsonDecode(res.body) or nil
local requestCacheKey = params.requestCacheKey
-- If we have data, and the requestCacheKey is not in the cache, cache it.
if data ~= nil and SL.GrooveStats.RequestCache[requestCacheKey] == nil then
SL.GrooveStats.RequestCache[requestCacheKey] = {
Response=res,
Timestamp=GetTimeSinceStart()
}
end
for i=1,2 do
local paneDisplay = master:GetChild("PaneDisplayP"..i)
local machineScore = paneDisplay:GetChild("MachineHighScore")
local machineName = paneDisplay:GetChild("MachineHighScoreName")
local playerScore = paneDisplay:GetChild("PlayerHighScore")
local playerName = paneDisplay:GetChild("PlayerHighScoreName")
local loadingText = paneDisplay:GetChild("Loading")
local playerStr = "player"..i
local rivalNum = 1
local worldRecordSet = false
local personalRecordSet = false
-- First check to see if the leaderboard even exists.
if data and data[playerStr] and data[playerStr]["gsLeaderboard"] then
-- And then also ensure that the chart hash matches the currently parsed one.
-- It's better to just not display anything than display the wrong scores.
if SL["P"..i].Streams.Hash == data[playerStr]["chartHash"] then
for gsEntry in ivalues(data[playerStr]["gsLeaderboard"]) do
if gsEntry["rank"] == 1 then
SetNameAndScore(
GetMachineTag(gsEntry),
string.format("%.2f%%", gsEntry["score"]/100),
machineName,
machineScore
)
worldRecordSet = true
end
if gsEntry["isSelf"] then
-- Let's check if the GS high score is higher than the local high score
local player = PlayerNumber[i]
local localScore = GetScoreForPlayer(player)
-- GS's score entry is a value like 9823, so we need to divide it by 100 to get 98.23
local gsScore = gsEntry["score"] / 100
-- GetPercentDP() returns a value like 0.9823, so we need to multiply it by 100 to get 98.23
if not localScore or gsScore >= localScore:GetPercentDP() * 100 then
-- It is! Let's use it instead of the local one.
SetNameAndScore(
GetMachineTag(gsEntry),
string.format("%.2f%%", gsScore),
playerName,
playerScore
)
personalRecordSet = true
end
end
if gsEntry["isRival"] then
local rivalScore = paneDisplay:GetChild("Rival"..rivalNum.."Score")
local rivalName = paneDisplay:GetChild("Rival"..rivalNum.."Name")
SetNameAndScore(
GetMachineTag(gsEntry),
string.format("%.2f%%", gsEntry["score"]/100),
rivalName,
rivalScore
)
rivalNum = rivalNum + 1
end
end
end
end
-- Fall back to to using the machine profile's record if we never set the world record.
-- This chart may not have been ranked, or there is no WR, or the request failed.
if not worldRecordSet then
machineName:queuecommand("SetDefault")
machineScore:queuecommand("SetDefault")
end
-- Fall back to to using the personal profile's record if we never set the record.
-- This chart may not have been ranked, or we don't have a score for it, or the request failed.
if not personalRecordSet then
playerName:queuecommand("SetDefault")
playerScore:queuecommand("SetDefault")
end
-- Iterate over any remaining rivals and hide them.
-- This also handles the failure case as rivalNum will never have been incremented.
for j=rivalNum,3 do
local rivalScore = paneDisplay:GetChild("Rival"..j.."Score")
local rivalName = paneDisplay:GetChild("Rival"..j.."Name")
rivalScore:settext("??.??%")
rivalName:settext("----")
end
if res.error or res.statusCode ~= 200 then
local error = res.error and ToEnumShortString(res.error) or nil
if error == "Timeout" then
loadingText:settext("Timed Out")
elseif error or (res.statusCode ~= nil and res.statusCode ~= 200) then
loadingText:settext("Failed")
end
else
if data and data[playerStr] then
if data[playerStr]["isRanked"] then
loadingText:settext("Loaded")
else
loadingText:settext("Not Ranked")
end
else
-- Just hide the text
loadingText:queuecommand("Set")
end
end
end
end
-- -----------------------------------------------------------------------
-- define the x positions of four columns, and the y positions of three rows of PaneItems
local pos = {
col = { WideScale(-20,-143), WideScale(-1,-23), WideScale(54,76), WideScale(-280, 190) },
row = { -30, -17, 49, 4 }
}
local num_rows = 4
local num_cols = 3
-- HighScores handled as special cases for now until further refactoring
local PaneItems = {
-- first row
{ name=THEME:GetString("RadarCategory","Taps"), rc='RadarCategory_TapsAndHolds'},
{ name=THEME:GetString("RadarCategory","Jumps"), rc='RadarCategory_Jumps'},
-- { name=THEME:GetString("ScreenSelectMusic","NPS") },
-- second row
{ name=THEME:GetString("RadarCategory","Holds"), rc='RadarCategory_Holds'},
{ name=THEME:GetString("RadarCategory","Mines"), rc='RadarCategory_Mines'},
-- { name=THEME:GetString("RadarCategory","Lifts"), rc='RadarCategory_Lifts'},
-- third row
{ name=THEME:GetString("RadarCategory","Hands"), rc='RadarCategory_Hands'},
-- { name=THEME:GetString("RadarCategory","Fakes"), rc='RadarCategory_Fakes'},
-- fourth row
{ name=THEME:GetString("RadarCategory","Rolls"), rc='RadarCategory_Rolls'}
-- { name=THEME:GetString("RadarCategory","Fakes"), rc='RadarCategory_Fakes'},
}
-- -----------------------------------------------------------------------
local af = Def.ActorFrame{ Name="PaneDisplayMaster" }
af[#af+1] = RequestResponseActor(17, 50)..{
Name="GetScoresRequester",
OnCommand=function(self)
-- Create variables for both players, even if they're not currently active.
self.IsParsing = {false, false}
end,
-- Broadcasted from ./PerPlayer/DensityGraph.lua
P1ChartParsingMessageCommand=function(self) self.IsParsing[1] = true end,
P2ChartParsingMessageCommand=function(self) self.IsParsing[2] = true end,
P1ChartParsedMessageCommand=function(self)
self.IsParsing[1] = false
self:queuecommand("ChartParsed")
end,
P2ChartParsedMessageCommand=function(self)
self.IsParsing[2] = false
self:queuecommand("ChartParsed")
end,
ChartParsedCommand=function(self)
local master = self:GetParent()
if not IsServiceAllowed(SL.GrooveStats.GetScores) then
if SL.GrooveStats.IsConnected then
-- loadingText is made visible when requests complete.
-- If we disable the service from a previous request, surface it to the user here.
for i=1,2 do
local loadingText = master:GetChild("PaneDisplayP"..i):GetChild("Loading")
loadingText:settext("Disabled")
loadingText:visible(true)
end
end
return
end
-- Make sure we're still not parsing either chart.
if self.IsParsing[1] or self.IsParsing[2] then return end
-- This makes sure that the Hash in the ChartInfo cache exists.
local sendRequest = false
local headers = {}
local query = {}
local requestCacheKey = ""
for i=1,2 do
local pn = "P"..i
if SL[pn].ApiKey ~= "" and SL[pn].Streams.Hash ~= "" then
query["chartHashP"..i] = SL[pn].Streams.Hash
headers["x-api-key-player-"..i] = SL[pn].ApiKey
requestCacheKey = requestCacheKey .. SL[pn].Streams.Hash .. SL[pn].ApiKey .. pn
local loadingText = master:GetChild("PaneDisplayP"..i):GetChild("Loading")
loadingText:visible(true)
loadingText:settext("Loading ...")
sendRequest = true
end
end
-- Only send the request if it's applicable.
if sendRequest then
requestCacheKey = CRYPTMAN:SHA256String(requestCacheKey.."-player-scores")
local params = {requestCacheKey=requestCacheKey, master=master}
RemoveStaleCachedRequests()
-- If the data is still in the cache, run the request processor directly
-- without making a request with the cached response.
if SL.GrooveStats.RequestCache[requestCacheKey] ~= nil then
local res = SL.GrooveStats.RequestCache[requestCacheKey].Response
GetScoresRequestProcessor(res, params)
else
self:playcommand("MakeGrooveStatsRequest", {
endpoint="player-scores.php?"..NETWORK:EncodeQueryParameters(query),
method="GET",
headers=headers,
timeout=10,
callback=GetScoresRequestProcessor,
args=params,
})
end
end
end
}
for player in ivalues(PlayerNumber) do
local pn = ToEnumShortString(player)
af[#af+1] = Def.ActorFrame{ Name="PaneDisplay"..ToEnumShortString(player) }
local af2 = af[#af]
af2.InitCommand=function(self)
self:visible(GAMESTATE:IsHumanPlayer(player))
if player == PLAYER_1 then
self:x(_screen.w * 0.25 - 5)
elseif player == PLAYER_2 then
self:x(_screen.w * 0.75 + 5)
end
self:y(_screen.h - footer_height - pane_height)
end
af2.PlayerJoinedMessageCommand=function(self, params)
if player==params.Player then
-- ensure BackgroundQuad is colored before it is made visible
self:GetChild("BackgroundQuad"):playcommand("Set")
self:visible(true)
:zoom(0):croptop(0):bounceend(0.3):zoom(1)
:playcommand("Update")
end
end
af2.PlayerUnjoinedMessageCommand=function(self, params)
if player==params.Player then
self:accelerate(0.3):croptop(1):sleep(0.01):zoom(0):queuecommand("Hide")
end
end
af2.PlayerProfileSetMessageCommand=function(self, params)
if player == params.Player then
self:playcommand("Set")
end
end
af2.HideCommand=function(self) self:visible(false) end
af2.OnCommand=function(self) self:playcommand("Set") end
af2.SLGameModeChangedMessageCommand=function(self) self:playcommand("Set") end
af2.CurrentCourseChangedMessageCommand=function(self) self:playcommand("Set") end
af2.CurrentSongChangedMessageCommand=function(self) self:playcommand("Set") end
af2["CurrentSteps"..pn.."ChangedMessageCommand"]=function(self) self:playcommand("Set") end
af2["CurrentTrail"..pn.."ChangedMessageCommand"]=function(self) self:playcommand("Set") end
-- -----------------------------------------------------------------------
-- colored background Quad
-- -----------------------------------------------------------------------
-- loop through the six sub-tables in the PaneItems table
-- add one BitmapText as the label and one BitmapText as the value for each PaneItem
for i, item in ipairs(PaneItems) do
local col = ((i-1)%num_cols) + 1
local row = math.floor((i-1)/num_cols) + 1
af2[#af2+1] = Def.ActorFrame{
Name=item.name,
-- numerical value
LoadFont("_eurostile normal")..{
InitCommand=function(self)
self:zoom(.5):diffuse(Color.White):horizalign(right)
self:x(pos.col[col])
self:y(pos.row[row])
end,
SetCommand=function(self)
local SongOrCourse, StepsOrTrail = GetSongAndSteps(player)
if not SongOrCourse then self:settext("?"); return end
if not StepsOrTrail then self:settext(""); return end
if item.rc then
local val = StepsOrTrail:GetRadarValues(player):GetValue( item.rc )
-- the engine will return -1 as the value for autogenerated content; show a question mark instead if so
self:settext( val >= 0 and val or "?" )
end
end
},
-- label
LoadFont("_eurostile normal")..{
Text=item.name,
InitCommand=function(self)
self:zoom(text_zoom):diffuse(Color.Black):horizalign(left)
self:x(pos.col[col]+9999)
self:y(pos.row[row])
end
},
}
end
-- Machine/World Record Machine Tag
af2[#af2+1] = LoadFont("_eurostile normal")..{
Name="MachineHighScoreName",
InitCommand=function(self)
self:zoom(text_zoom):diffuse(Color.Black):maxwidth(30)
self:x(pos.col[3]-130*text_zoom)
self:y(pos.row[1])
end,
SetCommand=function(self)
-- We overload this actor to work both for GrooveStats and also offline.
-- If we're connected, we let the ResponseProcessor set the text
if IsServiceAllowed(SL.GrooveStats.GetScores) then
self:settext("----")
else
self:queuecommand("SetDefault")
end
end,
SetDefaultCommand=function(self)
local SongOrCourse, StepsOrTrail = GetSongAndSteps(player)
local machineScore = GetScoreFromProfile(machine_profile, SongOrCourse, StepsOrTrail)
self:settext(machineScore and machineScore:GetName() or "----")
DiffuseEmojis(self:ClearAttributes())
end
}
-- Machine/World Record HighScore
af2[#af2+1] = LoadFont("_eurostile normal")..{
Name="MachineHighScore",
InitCommand=function(self)
self:zoom(text_zoom):diffuse(Color.Black):horizalign(right)
self:x(pos.col[3]+25*text_zoom)
self:y(pos.row[1])
end,
SetCommand=function(self)
-- We overload this actor to work both for GrooveStats and also offline.
-- If we're connected, we let the ResponseProcessor set the text
if IsServiceAllowed(SL.GrooveStats.GetScores) then
self:settext("??.??%")
else
self:queuecommand("SetDefault")
end
end,
SetDefaultCommand=function(self)
local SongOrCourse, StepsOrTrail = GetSongAndSteps(player)
local machineScore = GetScoreFromProfile(machine_profile, SongOrCourse, StepsOrTrail)
if machineScore ~= nil then
self:settext(FormatPercentScore(machineScore:GetPercentDP()))
else
self:settext("??.??%")
end
end
}
-- Player Profile/GrooveStats Machine Tag
af2[#af2+1] = LoadFont("_eurostile normal")..{
Name="PlayerHighScoreName",
InitCommand=function(self)
self:zoom(text_zoom):diffuse(Color.Black):maxwidth(30)
self:x(pos.col[3]-50*text_zoom)
self:y(pos.row[2])
end,
SetCommand=function(self)
-- We overload this actor to work both for GrooveStats and also offline.
-- If we're connected, we let the ResponseProcessor set the text
if IsServiceAllowed(SL.GrooveStats.GetScores) then
self:settext("----")
else
self:queuecommand("SetDefault")
end
end,
SetDefaultCommand=function(self)
local playerScore = GetScoreForPlayer(player)
self:settext(playerScore and playerScore:GetName() or "----")
DiffuseEmojis(self:ClearAttributes())
end
}
-- Player Profile/GrooveStats HighScore
af2[#af2+1] = LoadFont("_eurostile normal")..{
Name="PlayerHighScore",
InitCommand=function(self)
self:zoom(text_zoom):diffuse(Color.Black):horizalign(right)
self:x(pos.col[3]+90*text_zoom)
self:y(pos.row[2])
end,
SetCommand=function(self)
-- We overload this actor to work both for GrooveStats and also offline.
-- If we're connected, we let the ResponseProcessor set the text
if IsServiceAllowed(SL.GrooveStats.GetScores) then
self:settext("??.??%")
else
self:queuecommand("SetDefault")
end
end,
SetDefaultCommand=function(self)
local playerScore = GetScoreForPlayer(player)
if playerScore ~= nil then
self:settext(FormatPercentScore(playerScore:GetPercentDP()))
else
self:settext("??.??%")
end
end
}
af2[#af2+1] = LoadFont("_eurostile normal")..{
Name="Loading",
Text="Loading ... ",
InitCommand=function(self)
self:zoom(text_zoom):diffuse(Color.Black)
self:x(0)
self:y(0)
self:visible(false)
end,
SetCommand=function(self)
self:settext("Loading ...")
self:visible(false)
end
}
-- Add actors for Rival score data. Hidden by default
-- We position relative to column 3 for spacing reasons.
for i=1,3 do
-- Rival Machine Tag
af2[#af2+1] = LoadFont("_eurostile normal")..{
Name="Rival"..i.."Name",
InitCommand=function(self)
self:zoom(text_zoom):diffuse(Color.Black):maxwidth(30)
self:x(pos.col[3]+50*text_zoom)
self:y(pos.row[i])
end,
OnCommand=function(self)
self:visible(IsServiceAllowed(SL.GrooveStats.GetScores))
end,
SetCommand=function(self)
self:settext("----")
end
}
-- Rival HighScore
af2[#af2+1] = LoadFont("_eurostile normal")..{
Name="Rival"..i.."Score",
InitCommand=function(self)
self:zoom(text_zoom):diffuse(Color.Black):horizalign(right)
self:x(pos.col[3]+125*text_zoom)
self:y(pos.row[i])
end,
OnCommand=function(self)
self:visible(IsServiceAllowed(SL.GrooveStats.GetScores))
end,
SetCommand=function(self)
self:settext("??.??%")
end
}
end
end
return af