wavelength 1.0.0
Loading...
Searching...
No Matches
stats.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <vector>
5
7#include "os/string.h"
8
17class Stats {
18public:
24 class Event {
25 public:
31 explicit Event(const char *pszName) : mName(pszName) {
32 }
33
40 virtual ~Event() {
41 }
42
48 virtual String ToString() const = 0;
49
51 const char *GetName() const {
52 return mName;
53 }
54
55 private:
56 const char *mName;
57 };
58
64 class GemHitEvent : public Event {
65 public:
75 GemHitEvent(int nPlayer, int nTrack, int nGemTick, int nButtonTick, float fSlopMs)
76 : Event("gem_hit"), mPlayer(nPlayer), mTrack(nTrack), mGemTick(nGemTick),
77 mButtonTick(nButtonTick), mSlopMs(fSlopMs) {
78 }
79
86 ~GemHitEvent() override {
87 }
88
96 String ToString() const override;
97
98 private:
99 unsigned char mPlayer;
100 unsigned char mTrack;
101 int mGemTick;
102 int mButtonTick;
103 float mSlopMs;
104 };
105
111 class GemMissEvent : public Event {
112 public:
122 GemMissEvent(int nPlayer, int nTrack, int nGemTick, int nButtonTick, float fSlopMs)
123 : Event("gem_miss"), mPlayer(nPlayer), mTrack(nTrack), mGemTick(nGemTick),
124 mButtonTick(nButtonTick), mSlopMs(fSlopMs) {
125 }
126
133 ~GemMissEvent() override {
134 }
135
143 String ToString() const override;
144
145 private:
146 unsigned char mPlayer;
147 unsigned char mTrack;
148 int mGemTick;
149 int mButtonTick;
150 float mSlopMs;
151 };
152
158 class GemPassEvent : public Event {
159 public:
167 GemPassEvent(int nPlayer, int nTrack, int nTick)
168 : Event("gem_pass"), mPlayer(nPlayer), mTrack(nTrack), mTick(nTick) {
169 }
170
177 ~GemPassEvent() override {
178 }
179
187 String ToString() const override;
188
189 private:
190 unsigned char mPlayer;
191 signed char mTrack;
192 int mTick;
193 };
194
200 class ChangeTrackEvent : public Event {
201 public:
210 ChangeTrackEvent(int nPlayer, int nOldTrack, int nTick, int nNewTrack)
211 : Event("change_track"), mPlayer(nPlayer), mOldTrack(nOldTrack), mNewTrack(nNewTrack),
212 mTick(nTick) {
213 }
214
221 ~ChangeTrackEvent() override {
222 }
223
231 String ToString() const override;
232
233 private:
234 unsigned char mPlayer;
235 signed char mOldTrack;
236 signed char mNewTrack;
237 int mTick;
238 };
239
245 class CaptureTrackEvent : public Event {
246 public:
255 CaptureTrackEvent(int nPlayer, int nTrack, int nTick, int nNumBars)
256 : Event("capture_track"), mPlayer(nPlayer), mTrack(nTrack), mTick(nTick),
257 mNumBars(nNumBars) {
258 }
259
267 }
268
276 String ToString() const override;
277
278 private:
279 unsigned char mPlayer;
280 unsigned char mTrack;
281 int mTick;
282 unsigned char mNumBars;
283 };
284
290 class CompleteStageEvent : public Event {
291 public:
297 explicit CompleteStageEvent(int nTick) : Event("complete_stage"), mTick(nTick) {
298 }
299
307 }
308
316 String ToString() const override;
317
318 private:
319 int mTick;
320 };
321
327 class AxeEvent : public Event {
328 public:
337 AxeEvent(int nPlayer, int nTrack, int nTick, int nStart)
338 : Event("axe"), mPlayer(nPlayer), mTrack(nTrack), mTick(nTick), mStart(nStart) {
339 }
340
347 ~AxeEvent() override {
348 }
349
357 String ToString() const override;
358
359 private:
360 unsigned char mPlayer;
361 unsigned char mTrack;
362 int mTick;
363 int mStart;
364 };
365
371 class ScratchEvent : public Event {
372 public:
381 ScratchEvent(int nPlayer, int nTrack, int nTick, int nStart)
382 : Event("scratch"), mPlayer(nPlayer), mTrack(nTrack), mTick(nTick), mStart(nStart) {
383 }
384
391 ~ScratchEvent() override {
392 }
393
401 String ToString() const override;
402
403 private:
404 unsigned char mPlayer;
405 unsigned char mTrack;
406 int mTick;
407 int mStart;
408 };
409
415 class CatchPowerupEvent : public Event {
416 public:
424 CatchPowerupEvent(int nPlayer, int nTick, int nType)
425 : Event("catch_powerup"), mPlayer(nPlayer), mTick(nTick), mType(nType) {
426 }
427
435 }
436
444 String ToString() const override;
445
446 private:
447 unsigned char mPlayer;
448 int mTick;
449 unsigned char mType;
450 };
451
457 class DeployPowerupEvent : public Event {
458 public:
466 DeployPowerupEvent(int nPlayer, int nTick, int nType)
467 : Event("deploy_powerup"), mPlayer(nPlayer), mTick(nTick), mType(nType) {
468 }
469
477 }
478
486 String ToString() const override;
487
488 private:
489 unsigned char mPlayer;
490 int mTick;
491 unsigned char mType;
492 };
493
499 class LoseSoloGameEvent : public Event {
500 public:
508 LoseSoloGameEvent(int nTick, int nScore, float fProgress)
509 : Event("lose_solo_game"), mTick(nTick), mScore(nScore), mProgress(fProgress) {
510 }
511
519 }
520
528 String ToString() const override;
529
530 private:
531 int mTick;
532 int mScore;
533 float mProgress;
534 };
535
541 class WinSoloGameEvent : public Event {
542 public:
551 WinSoloGameEvent(int nScore, float fEnergized, int nFullMixBars, int nBestStreak)
552 : Event("win_solo_game"), mScore(nScore), mEnergized(fEnergized),
553 mFullMixBars(nFullMixBars), mBestStreak(nBestStreak) {
554 }
555
562 ~WinSoloGameEvent() override {
563 }
564
572 String ToString() const override;
573
574 private:
575 int mScore;
576 float mEnergized;
577 int mFullMixBars;
578 int mBestStreak;
579 };
580
586 class EndMultiGameEvent : public Event {
587 public:
589 static constexpr int kNumScores = 4;
590
599 EndMultiGameEvent(int nScore0, int nScore1, int nScore2, int nScore3)
600 : Event("end_multi_game"), mScores{nScore0, nScore1, nScore2, nScore3} {
601 }
602
610 }
611
619 String ToString() const override;
620
621 private:
622 int mScores[kNumScores];
623 };
624
630 class PlayerAbortedEvent : public Event {
631 public:
638 PlayerAbortedEvent(int nPlayer, int nTick)
639 : Event("player_aborted"), mPlayer(nPlayer), mTick(nTick) {
640 }
641
649 }
650
658 String ToString() const override;
659
660 private:
661 unsigned char mPlayer;
662 int mTick;
663 };
664
670 class JuiceEvent : public Event {
671 public:
678 JuiceEvent(float fValue, int nTick) : Event("juice"), mValue(fValue), mTick(nTick) {
679 }
680
687 ~JuiceEvent() override {
688 }
689
697 String ToString() const override;
698
699 private:
700 float mValue;
701 int mTick;
702 };
703
709 class StatsMemcardUser final : public MemcardUser {
710 public:
718 void OnFileSaved(int nStatus) override;
719 };
720
728
736
744 static Stats *shared();
745
756 void Begin([[maybe_unused]] int nNumTracks, [[maybe_unused]] int nNumBars) {
757 }
758
765 void End();
766
777 void GemHit(int nTrack, int nGemTick, int nButtonTick, float fSlopMs);
778
789 void GemMiss(int nTrack, int nGemTick, int nButtonTick, float fSlopMs);
790
799 void GemMiss(int nTrack, int nButtonTick);
800
809 void GemPass(int nTrack, int nTick);
810
819 void SetTrackPlayer(int nTrack, int nPlayer);
820
830 void ChangeTrack(int nPlayer, int nTrack, int nTick);
831
841 void CaptureTrack(int nTrack, int nTick, int nNumBars);
842
850 void CompleteStage(int nTick);
851
860 void AxeBegin(int nTrack, int nTick);
861
870 void AxeEnd(int nTrack, int nTick);
871
880 void ScratchBegin(int nTrack, int nTick);
881
890 void ScratchEnd(int nTrack, int nTick);
891
901 void DeployPowerup(int nPlayer, int nTick, int nType);
902
912 void CatchPowerup(int nPlayer, int nTick, int nType);
913
923 void LoseSoloGame(int nTick, int nScore, float fProgress);
924
935 void WinSoloGame(int nScore, int nFullMixBars, int nBestStreak, float fEnergized);
936
947 void EndMultiGame(int nScore0, int nScore1, int nScore2, int nScore3);
948
957 void PlayerAborted(int nPlayer, int nTick);
958
967 void Juice(float fValue, int nTick);
968
969private:
980 static String FormatField(const String &name, const char *pszValue, bool bQuoted);
981
991 static String FormatField(const String &name, int nValue);
992
1002 static String FormatField(const String &name, bool bValue);
1003
1011 static void AppendTimestamp(String &text);
1012
1019 void WriteSessionLog();
1020
1021 std::vector<int> mPlayerTracks;
1022 std::vector<int> mTrackPlayers;
1023 int mSongBars;
1024 String mStartTime;
1025 std::vector<std::unique_ptr<Event>> mEvents;
1026 const char *mLogName;
1027 int mMemcardSlot;
1028 String mLog;
1029 std::unique_ptr<StatsMemcardUser> mMemcardUser;
1030 int mActive;
1031};
1032
1038extern Stats *TheStats;
Receiver of the outcome of the memory card work MCManager runs.
Definition memcarduser.h:19
AxeEvent(int nPlayer, int nTrack, int nTick, int nStart)
Construct the event.
Definition stats.h:337
String ToString() const override
Format the player, the track, the tick, and whether the freestyle starts.
~AxeEvent() override
Release the event.
Definition stats.h:347
~CaptureTrackEvent() override
Release the event.
Definition stats.h:266
String ToString() const override
Format the player, the track, the tick, and the bars.
CaptureTrackEvent(int nPlayer, int nTrack, int nTick, int nNumBars)
Construct the event.
Definition stats.h:255
~CatchPowerupEvent() override
Release the event.
Definition stats.h:434
CatchPowerupEvent(int nPlayer, int nTick, int nType)
Construct the event.
Definition stats.h:424
String ToString() const override
Format the player, the tick, and the kind of power-up.
String ToString() const override
Format the player, the two tracks, and the tick.
~ChangeTrackEvent() override
Release the event.
Definition stats.h:221
ChangeTrackEvent(int nPlayer, int nOldTrack, int nTick, int nNewTrack)
Construct the event.
Definition stats.h:210
String ToString() const override
Format the tick.
CompleteStageEvent(int nTick)
Construct the event.
Definition stats.h:297
~CompleteStageEvent() override
Release the event.
Definition stats.h:306
String ToString() const override
Format the player, the tick, and the kind of power-up.
DeployPowerupEvent(int nPlayer, int nTick, int nType)
Construct the event.
Definition stats.h:466
~DeployPowerupEvent() override
Release the event.
Definition stats.h:476
~EndMultiGameEvent() override
Release the event.
Definition stats.h:609
String ToString() const override
Format the four scores.
EndMultiGameEvent(int nScore0, int nScore1, int nScore2, int nScore3)
Construct the event.
Definition stats.h:599
static constexpr int kNumScores
Number of scores the event records.
Definition stats.h:589
const char * GetName() const
Definition stats.h:51
virtual ~Event()
Release the event.
Definition stats.h:40
Event(const char *pszName)
Construct an event.
Definition stats.h:31
virtual String ToString() const =0
Format the fields of the event.
String ToString() const override
Format the player, the track, the two ticks, and the slop.
~GemHitEvent() override
Release the event.
Definition stats.h:86
GemHitEvent(int nPlayer, int nTrack, int nGemTick, int nButtonTick, float fSlopMs)
Construct the event.
Definition stats.h:75
~GemMissEvent() override
Release the event.
Definition stats.h:133
String ToString() const override
Format the player, the track, and the press, and the nearest gem when the slop is not 0.
GemMissEvent(int nPlayer, int nTrack, int nGemTick, int nButtonTick, float fSlopMs)
Construct the event.
Definition stats.h:122
String ToString() const override
Format the player, the track, and the tick.
~GemPassEvent() override
Release the event.
Definition stats.h:177
GemPassEvent(int nPlayer, int nTrack, int nTick)
Construct the event.
Definition stats.h:167
String ToString() const override
Format the juice and the tick.
~JuiceEvent() override
Release the event.
Definition stats.h:687
JuiceEvent(float fValue, int nTick)
Construct the event.
Definition stats.h:678
~LoseSoloGameEvent() override
Release the event.
Definition stats.h:518
String ToString() const override
Format the tick, the score, and the fraction played.
LoseSoloGameEvent(int nTick, int nScore, float fProgress)
Construct the event.
Definition stats.h:508
String ToString() const override
Format the player and the tick.
~PlayerAbortedEvent() override
Release the event.
Definition stats.h:648
PlayerAbortedEvent(int nPlayer, int nTick)
Construct the event.
Definition stats.h:638
ScratchEvent(int nPlayer, int nTrack, int nTick, int nStart)
Construct the event.
Definition stats.h:381
~ScratchEvent() override
Release the event.
Definition stats.h:391
String ToString() const override
Format the player, the track, the tick, and whether the scratch starts.
Receiver of the result of saving the log to a memory card.
Definition stats.h:709
void OnFileSaved(int nStatus) override
Empty the log once the memory card task ends.
WinSoloGameEvent(int nScore, float fEnergized, int nFullMixBars, int nBestStreak)
Construct the event.
Definition stats.h:551
String ToString() const override
Format the score, the fraction energized, the full mix bars, and the longest streak.
~WinSoloGameEvent() override
Release the event.
Definition stats.h:562
Log of the gameplay events of one session, written as text to a file or a memory card when the sessio...
Definition stats.h:17
static Stats * shared()
Report the single instance, constructing it on first use.
void LoseSoloGame(int nTick, int nScore, float fProgress)
Record a lost solo song.
void GemHit(int nTrack, int nGemTick, int nButtonTick, float fSlopMs)
Record a caught gem.
void CompleteStage(int nTick)
Record the end of a section.
void ChangeTrack(int nPlayer, int nTrack, int nTick)
Record a player moving to another track.
void AxeEnd(int nTrack, int nTick)
Record the end of a guitar freestyle.
void EndMultiGame(int nScore0, int nScore1, int nScore2, int nScore3)
Record the end of a multiplayer song.
void AxeBegin(int nTrack, int nTick)
Record the start of a guitar freestyle.
void PlayerAborted(int nPlayer, int nTick)
Record a player who left the song.
void GemMiss(int nTrack, int nGemTick, int nButtonTick, float fSlopMs)
Record a press that missed the nearest gem.
void CaptureTrack(int nTrack, int nTick, int nNumBars)
Record a captured phrase.
void ScratchEnd(int nTrack, int nTick)
Record the end of a scratch.
void SetTrackPlayer(int nTrack, int nPlayer)
Record which player plays a track.
void Begin(int nNumTracks, int nNumBars)
Start the log of a song.
Definition stats.h:756
void Juice(float fValue, int nTick)
Record a new value of the juice meter.
void WinSoloGame(int nScore, int nFullMixBars, int nBestStreak, float fEnergized)
Record a won solo song.
void DeployPowerup(int nPlayer, int nTick, int nType)
Record a deployed power-up.
void GemMiss(int nTrack, int nButtonTick)
Record a press with no gem near it.
void ScratchBegin(int nTrack, int nTick)
Record the start of a scratch.
~Stats()
Release the log.
void End()
End the session, write the log, and release the events.
void CatchPowerup(int nPlayer, int nTick, int nType)
Record a caught power-up.
Stats()
Construct an inactive log.
void GemPass(int nTrack, int nTick)
Record a gem that passed without a press on a track that has a player.
Growable text buffer that is also a PrnStream.
Definition string.h:19
Stats * TheStats
The session log, Stats::shared() as the unit's static initialiser stored it.