forked from mohankarki54/SE-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_client2.cpp
More file actions
328 lines (298 loc) · 9.25 KB
/
chat_client2.cpp
File metadata and controls
328 lines (298 loc) · 9.25 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// chat_client.cpp
// ~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <cstdlib>
#include <deque>
#include <iostream>
#include <thread>
#include <string>
#include "asio.hpp"
#include "chat_message2.hpp"
using asio::ip::tcp;
int num1 = 0;
int r_value = 0;
typedef std::deque<chat_message> chat_message_queue;
class chat_client
{
public:
chat_client(asio::io_context& io_context,
const tcp::resolver::results_type& endpoints)
: io_context_(io_context),
socket_(io_context)
{
do_connect(endpoints);
}
void write(const chat_message& msg)
{
asio::post(io_context_,
[this, msg]()
{
bool write_in_progress = !write_msgs_.empty();
write_msgs_.push_back(msg);
if (!write_in_progress)
{
do_write();
}
});
}
void close()
{
asio::post(io_context_, [this]() { socket_.close(); });
}
private:
void do_connect(const tcp::resolver::results_type& endpoints)
{
asio::async_connect(socket_, endpoints,
[this](std::error_code ec, tcp::endpoint)
{
if (!ec)
{
do_read_header();
}
});
}
void do_read_header()
{
asio::async_read(socket_,
asio::buffer(read_msg_.data(), chat_message::header_length),
[this](std::error_code ec, std::size_t /*length*/)
{
if (!ec && read_msg_.decode_header())
{
if(read_msg_.ca.stand != true){
if(read_msg_.ca.track_num ==0){
std::cout << "-----------------------------------" << '\n';
std::cout << "Your first card " << read_msg_.ca.c1_face << " of "<< read_msg_.ca.c1_suit<< std::endl;
std::cout << "Your second card " << read_msg_.ca.c2_face << " of "<< read_msg_.ca.c2_suit<< std::endl;
std::cout << "Dealer first card " << read_msg_.ca.d1_face << " of "<< read_msg_.ca.d1_suit<< std::endl;
std::cout << "----------------------------------------" << '\n';
}
else if(read_msg_.ca.r_value == 1){
r_value++;
std::cout << "-----------------------------------" << '\n';
std::cout << "Your first card " << read_msg_.ca.c1_face << " of "<< read_msg_.ca.c1_suit<< std::endl;
std::cout << "Your second card " << read_msg_.ca.c2_face << " of "<< read_msg_.ca.c2_suit<< std::endl;
std::cout << "Dealer first card " << read_msg_.ca.d1_face << " of "<< read_msg_.ca.d1_suit<< std::endl;
std::cout << "----------------------------------------" << '\n';
}
else{
std::cout << "-------------------------------------------" << '\n';
std::cout << "Your card " << read_msg_.ca.c1_face << " of "<< read_msg_.ca.c1_suit<< std::endl;
std::cout << "-------------------------------------------" << '\n';
}
}
else{
std::cout << "Hello" << '\n';
}
do_read_body();
}
else
{
socket_.close();
}
});
}
void do_read_body()
{
asio::async_read(socket_,
asio::buffer(read_msg_.body(), read_msg_.body_length()),
[this](std::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
if (read_msg_.gs.valid && read_msg_.ca.stand != true){
std::cout << "/* -------------------------------- */" << '\n';
std::cout << "/* What you like to do? */" << '\n';
std::cout << "Enter Hit for Hit" << '\n';
std::cout << "Enter Stand for Stand" << '\n';
std::cout << "Enter Split for Split" << '\n';
std::cout << "Enter Insurance for Insurance" << '\n';
std::cout << "Enter New to start the new game. *Game should be over*" << '\n';
std::cout << "/* -------------------------------- */" << '\n';
}
else{
std::cout << "You have now $" << read_msg_.gs.players_credit << '\n';
int cre = read_msg_.gs.players_credit - 100;
if(cre < 0){
int dd = -1 * cre;
std::cout << "Sorry, You are Losing by $"<< dd << '\n';
}
else if(cre > 0){
std::cout << "Congrtz, You winning by $" << cre << '\n';
}
else{
std::cout << "Currently, you are even out." << '\n';
}
}
//std::cout.write(read_msg_.body(), read_msg_.body_length());
//std::cout << "\n";
do_read_header();
}
else
{
socket_.close();
}
});
}
void do_write()
{
asio::async_write(socket_,
asio::buffer(write_msgs_.front().data(),
write_msgs_.front().length()),
[this](std::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
write_msgs_.pop_front();
if (!write_msgs_.empty())
{
do_write();
}
}
else
{
socket_.close();
}
});
}
private:
asio::io_context& io_context_;
tcp::socket socket_;
chat_message read_msg_;
chat_message_queue write_msgs_;
};
float send_betamount(){
char dollar[chat_message::max_body_length + 1];
std::cout << "/-----------------------------/" << '\n';
std::cout << "Enter the bet amount: $";
std::cin.getline(dollar, chat_message::max_body_length + 1);
std::string fs(dollar);
float dol =std::stof(fs);
return dol;
}
int main(int argc, char* argv[])
{
try
{
if (argc != 3)
{
std::cerr << "Usage: chat_client <host> <port>\n";
return 1;
}
asio::io_context io_context;
tcp::resolver resolver(io_context);
auto endpoints = resolver.resolve(argv[1], argv[2]);
chat_client c(io_context, endpoints);
std::thread t([&io_context](){ io_context.run(); });
char ans[chat_message::max_body_length + 1];
char nam[chat_message::max_body_length + 1];
chat_message msg;
std::cout << "Welcome to the BlackJack Game" << '\n';
std::cout << "Do you like to start the game? ";
std::cin.getline(ans, chat_message::max_body_length + 1);
int result = strcmp(ans, "yes");
if(result==0){
msg.ca.start_game = true;
}
else{
msg.ca.start_game = false;
}
std::cout << "Enter your name: ";
//char line[chat_message::max_body_length + 1];
while (std::cin.getline(nam, chat_message::max_body_length + 1))
{
msg.ca.bet = true;
msg.ca.track_num = num1;
msg.ca.r_value = r_value;
if(num1 == 0 ){
msg.ca.name_valid = true;
strcpy(msg.ca.name, nam);
num1++;
}else{
if(strcmp(nam, "Hit")==0){
msg.ca.hit = true;
msg.ca.stand = false;
msg.ca.insurance = false;
msg.ca.split = false;
msg.ca.bet = false;
msg.ca.new_round = false;
}
else if(strcmp(nam, "Stand")==0){
char an[chat_message::max_body_length + 1];
std::cout << " Are you sure you want to stand? ";
std::cin.getline(an, chat_message::max_body_length + 1);
if(strcmp(an, "yes") == 0){
msg.ca.hit = false;
msg.ca.stand = true;
msg.ca.insurance =false;
msg.ca.split =false;
msg.ca.bet = false;
msg.ca.new_round = false;
}
}
else if(strcmp(nam, "Split")==0){
// std::cout << " I am to Split" << '\n';
msg.ca.hit = false;
msg.ca.stand =false;
msg.ca.insurance =false;
msg.ca.split = true;
msg.ca.bet = false;
msg.ca.new_round = false;
}
else if(strcmp(nam, "Insurance")==0){
// std::cout << " I am to Insurance" << '\n';
msg.ca.hit = false;
msg.ca.stand =false;
msg.ca.insurance = true;
msg.ca.split =false;
msg.ca.bet = false;
msg.ca.new_round = false;
}
else if(strcmp(nam, "New")==0){
// std::cout << " I am to Insurance" << '\n';
msg.ca.hit = false;
msg.ca.stand =false;
msg.ca.insurance = false;
msg.ca.split =false;
msg.ca.bet = false;
msg.ca.new_round = true;
}
else{
msg.ca.hit = false;
msg.ca.stand =false;
msg.ca.insurance = false;
msg.ca.split =false;
msg.ca.bet = false;
msg.ca.new_round = false;
}
}
if(msg.ca.bet){
float d = send_betamount();
msg.ca.bet_amo_ = d;
if(msg.ca.stand){
msg.ca.stand = false;
std::cout << "/*-------Starting the new round-------*/" << '\n';
std::cout << '\n';
}
}
//get the line from command and encode it.
msg.body_length(std::strlen(nam));
std::memcpy(msg.body(), nam, msg.body_length());
msg.encode_header();
c.write(msg);
}
c.close();
t.join();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}