1 /*****************************************************************************
2 * LibreMines *
3 * Copyright (C) 2020-2021 Bruno Bollos Correa *
4 * *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 *****************************************************************************
18 */
19
20
21 #include <QDataStream>
22 #include <QDebug>
23 #include "libreminesscore.h"
24
LibreMinesScore()25 LibreMinesScore::LibreMinesScore():
26 iTimeInNs(0),
27 gameDifficulty(NONE),
28 width(0),
29 heigth(0),
30 mines(0),
31 bGameCompleted(false),
32 dPercentageGameCompleted(0)
33 {
34
35 }
36
operator QString() const37 LibreMinesScore::operator QString() const
38 {
39 QString strGameDiffuclty;
40 if(this->gameDifficulty == EASY)
41 strGameDiffuclty = "Easy";
42 else if(this->gameDifficulty == MEDIUM)
43 strGameDiffuclty = "Medium";
44 else if(this->gameDifficulty == HARD)
45 strGameDiffuclty = "Hard";
46 else if(this->gameDifficulty == CUSTOMIZED)
47 {
48 strGameDiffuclty = "Customized " + QString::number(this->width) +
49 " x " + QString::number(this->heigth) + " : " +
50 QString::number(this->mines);
51 }
52
53
54 return QString(this->username + '\n'
55 + "Time: " + QString::number(this->iTimeInNs*1e-9) + " secs\n"
56 + "Difficulty: " + strGameDiffuclty + '\n'
57 + "Game Completed: " + QString::number(this->dPercentageGameCompleted)) + '%';
58 }
59
bFirstIsBetter(const LibreMinesScore & first,const LibreMinesScore & second)60 bool LibreMinesScore::bFirstIsBetter(const LibreMinesScore &first, const LibreMinesScore &second)
61 {
62 if(first.gameDifficulty != second.gameDifficulty ||
63 first.width != second.width ||
64 first.heigth != second.heigth)
65 {
66 qFatal("The two Scores must have the same game diffuculty");
67 }
68
69 if(first.bGameCompleted && second.bGameCompleted)
70 {
71 return first.iTimeInNs < second.iTimeInNs;
72 }
73 else if(first.bGameCompleted && !second.bGameCompleted)
74 {
75 return true;
76 }
77 else if(!first.bGameCompleted && second.bGameCompleted)
78 {
79 return false;
80 }
81 else
82 {
83 return first.dPercentageGameCompleted > second.dPercentageGameCompleted;
84 }
85 }
86
sort(QList<LibreMinesScore> & l)87 void LibreMinesScore::sort(QList<LibreMinesScore>& l)
88 {
89 // todo: Use iterators instead of indexes
90 for(int i=0; i<l.size()-1; i++)
91 {
92 for(int j=i+1; j<l.size(); j++)
93 {
94 if(LibreMinesScore::bFirstIsBetter(l[j], l[i]))
95 {
96 l.move(j, i);
97 }
98 }
99 }
100
101 }
102
103
operator <<(QDataStream & stream,const LibreMinesScore & score)104 QDataStream& operator<< (QDataStream& stream, const LibreMinesScore& score)
105 {
106 stream.setVersion(QDataStream::Qt_5_12);
107 //write
108 stream << score.iTimeInNs;
109 stream << (uchar)score.gameDifficulty;
110 stream << score.width;
111 stream << score.heigth;
112 stream << score.mines;
113 stream << score.bGameCompleted;
114 stream << score.dPercentageGameCompleted;
115 stream << score.username;
116
117 return stream;
118 };
119
operator >>(QDataStream & stream,LibreMinesScore & score)120 QDataStream& operator>> (QDataStream& stream, LibreMinesScore& score)
121 {
122 stream.setVersion(QDataStream::Qt_5_12);
123 //read
124 stream >> score.iTimeInNs;
125 uchar charGameDifficulty;
126 stream >> charGameDifficulty;
127 score.gameDifficulty = (GAME_DIFFICULTY)charGameDifficulty;
128 stream >> score.width;
129 stream >> score.heigth;
130 stream >> score.mines;
131 stream >> score.bGameCompleted;
132 stream >> score.dPercentageGameCompleted;
133 stream >> score.username;
134
135 return stream;
136 };
137
operator <<(QDebug debug,const LibreMinesScore & score)138 QDebug operator<<(QDebug debug, const LibreMinesScore& score)
139 {
140 QDebugStateSaver saver(debug);
141
142 debug.nospace() << qPrintable((QString)score);
143 return debug;
144 }
145