-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayers.cpp
167 lines (123 loc) · 3.05 KB
/
players.cpp
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
#include "players.h"
#include <QDebug>
QString loggedUsername;
char loggedUserNameInChar[NAMEMAX];
FILE * userFile =NULL;
UNode * headofUList=(UNode *)malloc(sizeof(UNode));
extern int scores;
bool readUserFile()
{
UNode * cur = headofUList;
if(!(userFile=fopen("userfile.dat","r+b"))) return false;
UNode * temp = (UNode *)malloc(sizeof(UNode));
while( fread(temp,sizeof(UNode),1,userFile))
{
temp->name[NAMEMAX-1]=0;
//test
qDebug()<<sizeof(UNode);
qDebug()<<temp->name;
qDebug()<<sizeof(temp->name);
qDebug()<<temp->highestScore;
qDebug()<<sizeof(temp->highestScore);
qDebug()<<temp->next;
qDebug()<<sizeof(temp->next);
temp->next= (UNode *)malloc(sizeof(UNode));
cur->next=temp;
cur=temp;
temp=temp->next;
}
temp->next=NULL;
cur->next=NULL;
fclose(userFile);
if(cur==headofUList) return false;
else return true;
}
bool writeUserFile()
{
UNode * cur = headofUList->next;
if(!(userFile=fopen("userfile.dat","wb"))) return false;
while(cur!=NULL)
{
fwrite(cur,sizeof(UNode),1,userFile);
cur=cur->next;
}
fclose(userFile);
if(cur==headofUList->next) return false;
else return true;
}
bool refreshHigestScore()
{
//check New Or Old
UNode * temp=locateUser();
qDebug()<<"temp = "<<temp<<"is located";
if(temp!=NULL)
{
//test
qDebug()<<temp->name;
qDebug()<<temp->highestScore;
qDebug()<<temp->next;
if(scores>=temp->highestScore)
{
temp->highestScore=scores;
sortUser();
return true;
}
else return false;
}
else if(temp==NULL)
{
temp=(UNode *)malloc(sizeof(UNode));
strcpy(temp->name,loggedUserNameInChar);
temp->highestScore=scores;
findTail()->next=temp;
temp->next = NULL;
sortUser();
return true;
}
}
UNode * locateUser()
{
for(UNode * temp=headofUList->next;temp!=NULL;temp=temp->next)
if(!(strcmp(loggedUserNameInChar,temp->name))) return temp;
return NULL;
}
void sortUser()
{
//Bubble Sort
int timescyc=getLength();
for(;timescyc!=0;timescyc--)
{
for( UNode * temp = headofUList->next;temp->next!=NULL;temp=temp->next)
{
if(temp->highestScore<temp->next->highestScore)
swapUNode(temp);
}
}
}
UNode *findTail()
{
UNode * temp = headofUList;
for(;temp->next!=NULL;temp=temp->next)
{}
return temp;
}
int getLength()
{
int res=0;
UNode * temp = headofUList;
for(;temp->next!=NULL;temp=temp->next)
{
res++;
}
return res;
}
void swapUNode(UNode * pre)
{
UNode * piv = (UNode *)malloc(sizeof(UNode));
strcpy(piv->name,pre->next->name);
strcpy(pre->next->name,pre->name);
strcpy(pre->name,piv->name);
piv->highestScore=pre->next->highestScore;
pre->next->highestScore=pre->highestScore;
pre->highestScore=piv->highestScore;
}