-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHiScore.h
68 lines (48 loc) · 1.83 KB
/
HiScore.h
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
// Copyright (c) 2021 Mathema GmbH
// SPDX-License-Identifier: BSD-3-Clause
// Author: Günter Woigk (Kio!)
// Copyright (c) 2021 [email protected]
// BSD 2-clause license
#pragma once
#include "settings.h"
#include "cdefs.h"
#include "FlashDrive.h"
// ********** Hiscore Table ****************
struct HiScore
{
mutable char name[10]{"Megatokio"}; // not 0-terminated
uint16 score = 99;
uint16 playtime = 99;
int16 year = 2020;
int8 month = 1, day = 1, hour = 0, minute = 0; // when
HiScore()=default;
HiScore(cstr nam, uint16 score, uint16 secs, int16 y, int8 m, int8 d, int8 hr, int8 min);
HiScore(cstr nam, uint16 score, uint16 secs, const datetime_t&);
void print() const { printf("%5u: %10s in%5u secs %4u-%02u-%02u %02u:%02u\n",
score,name,playtime,year,month,day,hour,minute); }
};
struct HiScores : public Flash::FlashData
{
static constexpr uint16 TYPE = 0x12d4;
static constexpr uint nelem = (Flash::page_size-sizeof(Flash::FlashData))/sizeof(HiScore);
HiScore hiscores[nelem];
HiScores() : Flash::FlashData(TYPE,sizeof(HiScores)) {}
bool isHiscore (uint score) { return score > hiscores[nelem-1].score; }
void addHiscore (const HiScore&);
void addHiscore (cstr name, uint16 score, uint16 seconds, int16 y, int8 m, int8 d, int8 hour, int8 minute);
void addHiscore (cstr name, uint16 score, uint16 seconds, const datetime_t&);
Flash::ErrNo readFromFlash();
Flash::ErrNo writeToFlash(); // caller must stop core1!
void print() const;
private:
bool is_valid();
void check_table();
};
inline void HiScores::addHiscore (cstr name, uint16 score, uint16 seconds, int16 y, int8 m, int8 d, int8 hour, int8 minute)
{
addHiscore(HiScore(name,score,seconds,y,m,d,hour,minute));
}
inline void HiScores::addHiscore (cstr name, uint16 score, uint16 seconds, const datetime_t& t)
{
addHiscore(HiScore(name,score,seconds,t));
}