-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerContainer.cpp
72 lines (58 loc) · 1.79 KB
/
PlayerContainer.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
#include "PlayerContainer.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_woarchive.hpp>
#include <boost/archive/text_wiarchive.hpp>
#include <locale>
PlayerContainer::PlayerContainer() {}
PlayerContainer::~PlayerContainer() {
clearPlayers();
}
void PlayerContainer::addPlayer() {
std::shared_ptr<Player> newPlayer = std::make_shared<Player>();
newPlayer->readFromConsole();
players.push_back(newPlayer);
}
void PlayerContainer::addOnlinePlayer() {
std::shared_ptr<OnlinePlayer> newOnlinePlayer = std::make_shared<OnlinePlayer>();
newOnlinePlayer->readFromConsole();
players.push_back(newOnlinePlayer);
}
void PlayerContainer::displayPlayers() const {
for (const auto& player : players) {
player->displayToConsole();
}
}
void PlayerContainer::readFromFile(const std::string& filename) {
std::ifstream inputFile(filename);
//file.imbue(std::locale("en_US.UTF-8"));
if (inputFile.is_open()) {
boost::archive::text_iarchive ia(inputFile);
ia >> *this;
}
/*while (!inputFile.eof()) {
Player* newPlayer = new Player();
newPlayer->readFromFile(inputFile);
if (inputFile.is_open()) {
players.push_back(newPlayer);
}
else {
delete newPlayer;
}
}*/
inputFile.close();
}
void PlayerContainer::writeToFile(const std::string& filename) const {
std::ofstream outputFile(filename);
if (outputFile.is_open()) {
boost::archive::text_oarchive oa(outputFile);
oa << *this;
/*for (const auto& player : players) {
player->writeToFile(outputFile);
}*/
}
outputFile.close();
}
void PlayerContainer::clearPlayers() {
players.clear();
}