-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogger.cpp
More file actions
174 lines (165 loc) · 5.44 KB
/
Logger.cpp
File metadata and controls
174 lines (165 loc) · 5.44 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
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
167
168
169
170
171
172
173
174
#include "Logger.h"
void Logger::write_Log()
{
int serial_number = rand() % 9999;
char buffer [33];
std::sprintf(buffer,"%d",serial_number);
char seedstring[4];
std::sprintf(seedstring,"%d",seed);
std::string path = "Logs/log_";
path += seedstring;
path += "_";
path += buffer;
path += "_";
path += get_date();
std::cout << path << std::endl;
log_data.open(path,std::fstream::out );
log_data << "--------------------- LOG "<< serial_number <<" ---------------------- \n" ;
log_data << "Number of players : " << num_Players << "\n";
log_data << "Number of agents : " << connected << "\n";
log_data << "Manual player id : " << manual_Player_id << "\n";
log_data << "Screen height : " << SCREEN_HEIGHT << " \t Screen width : " << SCREEN_WIDTH << "\n";
log_data << "Numbr of collumns : " << NUM_COLS << " \t Numbr of rows : " << NUM_ROWS << std::endl;
log_data << "Map : " << level << "\n";
log_data << "-----------------------------------------------------\n";
}
void Logger::write_state(std::vector<Player*> &all_Players)
{
state++;
std::string current_log = "";
for(int i = 0; i < NUM_COLS*NUM_ROWS; i++){
current_log += world_Map[i];
}
current_log += "\n";
current_log += "P ";
for ( int j = 0; j < num_SPlayers; j++)
{
current_log += std::to_string(all_Players[j]->get_mapX());
current_log += " ";
current_log += std::to_string(all_Players[j]->get_mapY());
current_log += " ";
current_log += std::to_string(all_Players[j]->get_Range());
current_log += " ";
current_log += std::to_string(all_Players[j]->is_Alive());
current_log += " ";
current_log += std::to_string(all_Players[j]->get_Speed());
current_log += " ";
current_log += std::to_string(all_Players[j]->get_Team_Id());
current_log += " ";
}
current_log += "\n";
if ( current_log.compare(last_log) ){
last_log = current_log;
log_data << last_log;
}
}
void Logger::write_winner(int winner, char * agent)
{
log_data << "-----------------------------------------------------\n";
if ( winner != -1)
log_data << " Player : " << winner+1 << " won!! Agent:" << agent;
else
log_data << " Draw !!";
}
std::string Logger::get_date(void)
{
time_t now;
char the_date[12];
the_date[0] = '\0';
now = time(NULL);
if (now != -1)
{
std::strftime(the_date, 12, "%d_%m_%Y", gmtime(&now));
}
return std::string(the_date);
}
void Logger::read_state( int state , std::vector<Player*> &all_Players)
{
if ( state < lvls.size() ){
int count = 0;
for (int y = 0; y < NUM_ROWS; y++)
{
for (int x = 0; x < NUM_COLS; x++)
{
world_Map[ mIndex(x,y)] = lvls[state][count];
count++;
}
}
std::istringstream iss(positions[state]);
std::string ignore, px, py, r, al, sp, te;
iss >> ignore;
for (int i = 0; i < num_Players; i++)
{
iss >> px; // x position
iss >> py; // y position
iss >> r; // ranges
iss >> al; // is player i alive?
iss >> sp; // player speed
iss >> te; // player's team
all_Players[i]->set_position(atoi(px.c_str()), atoi(py.c_str()));
if ( al.compare("0") == 0 )
all_Players[i]->die();
else if ( !all_Players[i]->is_Alive() )
all_Players[i]->resurect();
}
}
else{
state = lvls.size()-1;
}
}
void Logger::read_log_header()
{
std::string output;
for (int i = 0; i < 8 ; i++){
std::getline(log_data,output);
std::cout << output << std::endl;
std::istringstream iss(output);
if ( i == 1){
std::string word;
while(iss >> word) {
if ( word.compare(":") == 0 ){
iss >> word;
num_Players = atoi(word.c_str());
}
}
}
else if ( i == 6){
std::string word;
while(iss >> word) {
if ( word.compare(":") == 0 ){
iss >> word;
level = (char*) malloc(sizeof(char) * word.length());
sprintf(level, "%s", word.c_str());
}
}
}
}
while(std::getline(log_data,output)) // To get you all the lines.
{
if ( output.compare("-----------------------------------------------------") == 0 )
{
std::getline(log_data,output);
std::istringstream iss(output);
std::string word;
while(iss >> word) {
if ( word.compare(":") == 0 ){
iss >> word;
std::cout << "Player " << atoi(word.c_str());
iss >> word;
std::cout <<" agent: "<< word << " won!" << std::endl;
}
else if ( word.compare("Draw") == 0 ){
std::cout << "Draw!"<< std::endl;
}
}
}
else
{
/* MAP */
lvls.push_back(output);
/* PLAYERS */
std::getline(log_data,output);
positions.push_back(output);
}
}
}