-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwheel.cpp
More file actions
180 lines (161 loc) · 5.17 KB
/
wheel.cpp
File metadata and controls
180 lines (161 loc) · 5.17 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
175
176
177
178
179
180
#include "wheel.h"
#include "betting_strategy.h"
#include "logger.h"
#include "pub_sub.h"
#include <time.h>
namespace casino {
namespace wheel {
void Wheel::_rolling()
{
time_t timeNow = time(NULL);
pub_sub::Event ev { timeNow + ROLL_TIME_MS / 1000, pub_sub::ROLLING };
_switch_to(ev);
myLogger.info("starting rolling");
int t = ROLL_TIME_MS;
std::this_thread::sleep_for(std::chrono::milliseconds(t));
myWinningNumber = (rand() % (MAX_NUM - MIN_NUM + 1)) + MIN_NUM;
myLogger.info("stopped rolling");
}
void Wheel::_dispensing()
{
int t = DISPENSE_TIME_MS;
time_t timeNow = time(NULL);
pub_sub::Event ev { timeNow + DISPENSE_TIME_MS / 1000, pub_sub::DISPENSING, "winning-number in integer-data", myWinningNumber, 0 };
_switch_to(ev);
myLogger.info("dispensing winnings");
std::this_thread::sleep_for(std::chrono::milliseconds(t / 2));
for (auto mp : myBets) {
if (mp.first == myWinningNumber) {
for (auto sub_bet : mp.second) {
// Inform winners
double winningAmount = sub_bet.second * 1ll * (MAX_NUM - MIN_NUM);
pub_sub::Event winEv { 0, pub_sub::MONEY_WON, "winning-data", myWinningNumber, winningAmount };
sub_bet.first->accept(winEv);
// Bets on winning place is not removed
}
} else {
for (auto sub_bet : mp.second) {
// Inform losers
pub_sub::Event loseEv { 0, pub_sub::MONEY_LOST, "losing-data", mp.first, sub_bet.second };
sub_bet.first->accept(loseEv);
// Clear lost money
sub_bet.second = 0;
}
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(t / 2));
myLogger.info("winnings dispensed");
}
void Wheel::_betting()
{
time_t timeNow = time(NULL);
pub_sub::Event ev { timeNow + BET_TIME_MS / 1000, pub_sub::BETTING };
int t = BET_TIME_MS;
_switch_to(ev);
myLogger.info("betting is open now");
std::this_thread::sleep_for(std::chrono::milliseconds(t));
myLogger.info("betting closed");
}
void Wheel::_start()
{
myLogger.info("Starting thread with ID: ", std::this_thread::get_id());
while (true) {
if (!run_loop) {
myLogger.debug("run_loop false");
return;
}
_betting();
_rolling();
_dispensing();
}
}
void Wheel::_switch_to(pub_sub::Event event)
{
// TODO: We should add mutex usage here
myCurrentState = event;
myEventLog.push_back(event);
publish(event);
}
Wheel::Wheel()
{
myLogger = logger::Logger("Wheel");
run_loop = false;
srand(time(NULL));
myMoney = INITIAL_WHEEL_MONEY;
}
Wheel::~Wheel()
{
if (worker_thread.joinable())
worker_thread.~thread();
}
bool Wheel::place_bet(pub_sub::Subscriber* gambler, betting_strategy::t_bet bet)
{
int num = bet.first;
double amt = bet.second;
if (players_at_table.find(gambler) == players_at_table.end()) {
if (players_at_table.size() == MAX_PLAYER) {
myLogger.error("can't take bets of more than ", MAX_PLAYER, " players at once");
return false;
}
}
if (myCurrentState.event_type != pub_sub::BETTING) {
myLogger.warning("can't take bets right now");
return false;
}
if (!(MIN_NUM <= num && num <= MAX_NUM)) {
myLogger.error("num out of range, " + num);
return false;
}
if (gambler == nullptr) {
myLogger.error("gambler unknown");
return false;
}
players_at_table.insert(gambler);
myBets[num][gambler] = amt;
return true;
}
bool Wheel::reset_bet(pub_sub::Subscriber* gambler)
{
if (gambler == nullptr) {
myLogger.error("gambler unknown");
return false;
}
for (int i = MIN_NUM; i <= MAX_NUM; ++i) {
myBets[i][gambler] = 0;
}
players_at_table.erase(gambler);
return true;
}
bool Wheel::taking_bets()
{
return myCurrentState.event_type == pub_sub::BETTING;
}
void Wheel::subscribe(pub_sub::Subscriber* s)
{
mySubscribers.insert(s);
}
void Wheel::unsubscribe(pub_sub::Subscriber* s)
{
if (mySubscribers.count(s))
mySubscribers.erase(s);
}
void Wheel::publish(pub_sub::Event e)
{
for (pub_sub::Subscriber* s : mySubscribers) {
s->accept(e);
}
}
void Wheel::start()
{
myLogger.debug("starting thread");
run_loop = true;
worker_thread = std::thread([this] { _start(); });
}
void Wheel::stop()
{
myLogger.debug("stopping thread");
run_loop = false;
worker_thread.join();
}
} // namespace wheel
} // namespace casino