-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaderboard.cpp
108 lines (93 loc) · 2.89 KB
/
leaderboard.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
//
// Created by funny on 4/17/2023.
//
#include "leaderboard.h"
#include <SFML/graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <fstream>
using namespace std;
void leaderboard::leaderboardwindow() {
sf::Font font;
if (!font.loadFromFile("files/font.ttf")) cout << "font load error" << endl;
sf::Text leaderboardtitle("LEADERBOARD", font, 20);
leaderboardtitle.setStyle(sf::Text::Bold | sf::Text::Underlined);
leaderboardtitle.setPosition(WIDTH / 4 - 70, HEIGHT / 4 - 120);
string temp = "";
for (int i = 0; i < 5; i++) {
string place = to_string(i + 1);
temp += place + ". ";
string entry = scorevect[i].time + " " + scorevect[i].name;
temp += entry;
if (scorevect[i].recentscore)
temp += "*";
temp += "\n\n";
}
sf::Text leaderboardscores(temp, font, 18);
leaderboardscores.setStyle(sf::Text::Bold);
leaderboardscores.setPosition(WIDTH / 4 - 119, HEIGHT / 4 + 120 - 195);
sf::RenderWindow window(sf::VideoMode(WIDTH / 2, HEIGHT / 2), "Leaderboard yaayy");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
}
}
window.clear(sf::Color::Blue);
window.draw(leaderboardtitle);
window.draw(leaderboardscores);
window.display();
}
}
leaderboard::leaderboard(int w, int h) {
WIDTH = w;
HEIGHT = h;
fstream file("files/leaderboard.txt", ios_base::in);
if (not file.is_open()) {
cout << "file failed to open" << endl;
}
for (int i = 0; i < 5; i++) {
string time;
string name;
string temp;
getline(file, temp);
time = temp.substr(0, 5);
name = temp.substr(6);
scorevect.emplace_back(score(time, name, false));
}
}
void leaderboard::addscore(std::string t, std::string n) {
int minues = stoi(t.substr(0, 2));
int seconds = stoi(t.substr(3, 2));
int total = (minues * 60) + seconds;
int count = 0;
for (int i = 0; i < 5; i++) {
if (total > scorevect[i].total)
count++;
}
for (int i = 0; i < scorevect.size(); i++) {
scorevect[i].recentscore = false;
}
scorevect.insert(scorevect.begin() + count, score(t, n, true));
ofstream file("files/leaderboard.txt", ios_base::out);
for (int i = 0; i < 5; i++) {
string temp = scorevect[i].time + "," + scorevect[i].name;
file << temp << "\n";
}
}
void leaderboard::clear() {
delete this;
}
score::score(std::string Time, std::string Name, bool recent) {
time = Time;
name = Name;
recentscore = recent;
int minues = stoi(time.substr(0, 2));
int seconds = stoi(time.substr(3, 2));
total = (minues * 60) + seconds;
}