-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_log.cpp
100 lines (92 loc) · 2.28 KB
/
game_log.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
#include "game_log.h"
#include "message.h"
#include <algorithm>
#include <cassert>
#include <sstream>
game_log::game_log(const double display_time_secs)
: m_display_time_secs{display_time_secs}
{
assert(m_display_time_secs >= 0.0);
}
void game_log::add_message(
const message& m
) noexcept
{
m_timed_messages.push_back(
std::make_pair(
0.001 * m_clock.getElapsedTime().asMilliseconds(),
m
)
);
}
std::string get_last_log_messages(
const game_log& l,
const chess_color color
) noexcept
{
return l.get_last_messages(color);
}
std::string game_log::get_last_messages(const chess_color color) const
{
std::stringstream s;
for (const auto& m: m_timed_messages)
{
if (m.second.get_color() != color) continue;
s << m.second << '\n';
}
std::string t{s.str()};
if (t.empty()) return t;
t.pop_back(); // Remove newline
return t;
}
void test_log()
{
#ifndef NDEBUG
// log::log
{
const game_log l(0.00001);
assert(l.get_last_messages(chess_color::black) == "");
assert(l.get_last_messages(chess_color::white) == "");
}
// log::add_message
{
game_log l(0.001);
l.add_message(message(message_type::select, chess_color::white, piece_type::pawn));
assert(l.get_last_messages(chess_color::black) == "");
assert(l.get_last_messages(chess_color::white) != "");
}
// log::get_last_messages: messages expire
{
game_log l(0.001);
l.add_message(message(message_type::select, chess_color::white, piece_type::pawn));
assert(l.get_last_messages(chess_color::black) == "");
assert(l.get_last_messages(chess_color::white) != "");
sf::sleep(sf::milliseconds(3));
l.tick();
assert(l.get_last_messages(chess_color::white) == "");
}
// get_last_log_messages
{
const game_log l(0.001);
assert(get_last_log_messages(l, chess_color::black) == "");
assert(get_last_log_messages(l, chess_color::white) == "");
}
#endif // NDEBUG
}
void game_log::tick()
{
const double now_secs{
0.001 * m_clock.getElapsedTime().asMilliseconds()
};
m_timed_messages.erase(
std::remove_if(
std::begin(m_timed_messages),
std::end(m_timed_messages),
[this, now_secs](const auto& p)
{
return now_secs - p.first > m_display_time_secs;
}
),
std::end(m_timed_messages)
);
}