-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInfo.cpp
More file actions
88 lines (79 loc) · 1.5 KB
/
UserInfo.cpp
File metadata and controls
88 lines (79 loc) · 1.5 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
#include "UserInfo.h"
UserInfo::UserInfo( int _score /*= 0*/ )
{
this->iScore = 0;
}
void UserInfo::SetCurrentScore( int _score )
{
if (this->iHighScore < _score)
{
this->iHighScore = _score;
}
iScore = _score;
}
void UserInfo::SetCurrentScore( int _score, int _hScore )
{
iHighScore = iHighScore<_hScore ? _hScore : iHighScore;
iScore = _score;
}
int UserInfo::LoadHistoryScore()
{
std::string timeStr;
int highscore;
int lastscore;
//whether the file exists
std::fstream fp;
fp.open(USER_FILE_NAME,std::ios::in);
if (!fp) // not exist
{
// create
std::ofstream inFile;
inFile.open(USER_FILE_NAME);
if (inFile.is_open())
{
std::cout<<"Create Successful!\n";
}
inFile.close();
// assign
this->iHighScore = this->iScore;
return iHighScore;
}
else // exist
{
std::ifstream fin(USER_FILE_NAME);
if (! fin.is_open())
{
std::cout<<"Failed to open file!\n";
return 0;
}
else
{
while(!fin.eof())
{
fin>>timeStr>>highscore>>lastscore;
fin.get(); //
if(fin.peek() == '\n') break;
}
}
fin.close();
this->iHighScore = highscore;
return highscore;
}
}
bool UserInfo::SaveScoreInfo2File()
{
std::ofstream fout(USER_FILE_NAME, std::ios::app);
// current time
time_t t = time(0);
char tmp[64];
strftime( tmp, sizeof(tmp), "%Y-%m-%d_%H:%M:%S",localtime(&t) );
if (! fout.is_open())
{
return false;
}
//record
fout<<std::setw(20)<<tmp<<"\t" <<this->iHighScore<<"\t"<<this->iScore<<std::endl;
// close
fout.close();
return true;
}