|
| 1 | +/*** |
| 2 | +* ==++== |
| 3 | +* |
| 4 | +* Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +* you may not use this file except in compliance with the License. |
| 7 | +* You may obtain a copy of the License at |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +* |
| 16 | +* ==--== |
| 17 | +* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 18 | +* |
| 19 | +* BlackJackClient.cpp : Defines the entry point for the console application |
| 20 | +* |
| 21 | +* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 22 | +****/ |
| 23 | + |
| 24 | +#include "stdafx.h" |
| 25 | +#include <iostream> |
| 26 | +#include <streambuf> |
| 27 | +#include <sstream> |
| 28 | +#include <fstream> |
| 29 | +#include "../BlackJack_Server/messagetypes.h" |
| 30 | + |
| 31 | +#ifdef _MS_WINDOWS |
| 32 | +# define iequals(x, y) (_stricmp((x), (y))==0) |
| 33 | +#else |
| 34 | +# define iequals(x, y) boost::iequals((x), (y)) |
| 35 | +#endif |
| 36 | + |
| 37 | +using namespace std; |
| 38 | +using namespace web; |
| 39 | +using namespace utility; |
| 40 | +using namespace http; |
| 41 | +using namespace http::client; |
| 42 | + |
| 43 | +http_response CheckResponse(const std::string &url, const http_response &response) |
| 44 | +{ |
| 45 | + ucout << response.to_string() << endl; |
| 46 | + return response; |
| 47 | +} |
| 48 | + |
| 49 | +http_response CheckResponse(const std::string &url, const http_response &response, bool &refresh) |
| 50 | +{ |
| 51 | + ucout << response.to_string() << endl; |
| 52 | + BJPutResponse answer = BJPutResponse::FromJSON(response.extract_json().get()); |
| 53 | + refresh = answer.Status == ST_Refresh; |
| 54 | + return response; |
| 55 | +} |
| 56 | + |
| 57 | +void PrintResult(BJHandResult result) |
| 58 | +{ |
| 59 | + switch (result) |
| 60 | + { |
| 61 | + case HR_PlayerBlackJack: ucout << "Black Jack"; break; |
| 62 | + case HR_PlayerWin: ucout << "Player wins"; break; |
| 63 | + case HR_ComputerWin: ucout << "Computer Wins"; break; |
| 64 | + case HR_Push:ucout << "Push"; break; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +void PrintCard(const Card &card) |
| 69 | +{ |
| 70 | + switch (card.value) |
| 71 | + { |
| 72 | + case CV_King: ucout << "K"; break; |
| 73 | + case CV_Queen: ucout << "Q"; break; |
| 74 | + case CV_Jack: ucout << "J"; break; |
| 75 | + case CV_Ace: ucout << "A"; break; |
| 76 | + default: ucout << (int)card.value; break; |
| 77 | + } |
| 78 | + switch (card.suit) |
| 79 | + { |
| 80 | + case CS_Club: ucout << "C"; break; |
| 81 | + case CS_Spade: ucout << "S"; break; |
| 82 | + case CS_Heart: ucout << "H"; break; |
| 83 | + case CS_Diamond: ucout << "D"; break; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void PrintHand(bool suppress_bet, const BJHand &hand) |
| 88 | +{ |
| 89 | + if (!suppress_bet) |
| 90 | + { |
| 91 | + if ( hand.insurance > 0 ) |
| 92 | + ucout << "Bet: " << hand.bet << "Insurance: " << hand.insurance << " Hand: "; |
| 93 | + else |
| 94 | + ucout << "Bet: " << hand.bet << " Hand: "; |
| 95 | + } |
| 96 | + for (auto iter = hand.cards.begin(); iter != hand.cards.end(); iter++) |
| 97 | + { |
| 98 | + PrintCard(*iter); ucout << " "; |
| 99 | + } |
| 100 | + PrintResult(hand.result); |
| 101 | +} |
| 102 | + |
| 103 | +void PrintTable(const http_response &response, bool &refresh) |
| 104 | +{ |
| 105 | + BJHand hand; |
| 106 | + |
| 107 | + refresh = false; |
| 108 | + |
| 109 | + if ( response.status_code() == status_codes::OK ) |
| 110 | + { |
| 111 | + if ( response.headers().content_type() == U("application/json") ) |
| 112 | + { |
| 113 | + BJPutResponse answer = BJPutResponse::FromJSON(response.extract_json().get()); |
| 114 | + json::value players = answer.Data[PLAYERS]; |
| 115 | + |
| 116 | + refresh = answer.Status == ST_Refresh; |
| 117 | + |
| 118 | + for (auto iter = players.begin(); iter != players.end(); ++iter) |
| 119 | + { |
| 120 | + auto& player = iter->second; |
| 121 | + |
| 122 | + json::value name = player[NAME]; |
| 123 | + json::value bet = player[BALANCE]; |
| 124 | + |
| 125 | + bool suppressMoney = iter == players.begin(); |
| 126 | + |
| 127 | + if ( suppressMoney ) |
| 128 | + ucout << "'" << name.as_string() << "'" ; |
| 129 | + else |
| 130 | + fprintf(stdout, "'%s' Balance = $%02g ", name.as_string().c_str(), bet.as_double()); |
| 131 | + PrintHand(suppressMoney, BJHand::FromJSON(player[HAND])); |
| 132 | + ucout << std::endl; |
| 133 | + } |
| 134 | + |
| 135 | + switch ( answer.Status ) |
| 136 | + { |
| 137 | + case ST_PlaceBet: |
| 138 | + ucout << "Place your bet!\n"; |
| 139 | + break; |
| 140 | + case ST_YourTurn: |
| 141 | + ucout << "Your turn!\n"; |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// |
| 149 | +// Entry point for the blackjack client. |
| 150 | +// Arguments: BlackJack_Client.exe <port> |
| 151 | +// If port is not specified, client will assume that the server is listening on port 34568 |
| 152 | +// |
| 153 | +#ifdef _MS_WINDOWS |
| 154 | +int wmain(int argc, wchar_t *argv[]) |
| 155 | +#else |
| 156 | +int main(int argc, char *argv[]) |
| 157 | +#endif |
| 158 | +{ |
| 159 | + utility::string_t port = U("34568"); |
| 160 | + if(argc == 2) |
| 161 | + { |
| 162 | + port = argv[1]; |
| 163 | + } |
| 164 | + |
| 165 | + utility::string_t address = U("http://localhost:"); |
| 166 | + address.append(port); |
| 167 | + |
| 168 | + http::uri uri = http::uri(address); |
| 169 | + |
| 170 | + http_client bjDealer(http::uri_builder(uri).append_path(U("/blackjack/dealer")).to_uri()); |
| 171 | + |
| 172 | + utility::string_t userName; |
| 173 | + utility::string_t table; |
| 174 | + |
| 175 | + json::value availableTables = json::value::array(); |
| 176 | + |
| 177 | + bool was_refresh = false; |
| 178 | + |
| 179 | + while (true) |
| 180 | + { |
| 181 | + while ( was_refresh ) |
| 182 | + { |
| 183 | + was_refresh = false; |
| 184 | + utility::ostringstream_t buf; |
| 185 | + buf << table << U("?request=refresh&name=") << userName; |
| 186 | + PrintTable(CheckResponse("blackjack/dealer", bjDealer.request(methods::PUT, buf.str()).get()), was_refresh); |
| 187 | + } |
| 188 | + |
| 189 | + std::string method; |
| 190 | + ucout << "Enter method:"; |
| 191 | + cin >> method; |
| 192 | + |
| 193 | + if ( iequals(method.c_str(), "quit") ) |
| 194 | + { |
| 195 | + if ( !userName.empty() && !table.empty() ) |
| 196 | + { |
| 197 | + utility::ostringstream_t buf; |
| 198 | + buf << table << U("?name=") << userName; |
| 199 | + CheckResponse("blackjack/dealer", bjDealer.request(methods::DEL, buf.str()).get()); |
| 200 | + } |
| 201 | + break; |
| 202 | + } |
| 203 | + |
| 204 | + if ( iequals(method.c_str(), "name") ) |
| 205 | + { |
| 206 | + ucout << "Enter user name:"; |
| 207 | + ucin >> userName; |
| 208 | + } |
| 209 | + else if ( iequals(method.c_str(), "join") ) |
| 210 | + { |
| 211 | + ucout << "Enter table name:"; |
| 212 | + ucin >> table; |
| 213 | + |
| 214 | + if ( userName.empty() ) { ucout << "Must have a name first!\n"; continue; } |
| 215 | + |
| 216 | + utility::ostringstream_t buf; |
| 217 | + buf << table << U("?name=") << userName; |
| 218 | + CheckResponse("blackjack/dealer", bjDealer.request(methods::POST, buf.str()).get(), was_refresh); |
| 219 | + } |
| 220 | + else if ( iequals(method.c_str(), "hit") |
| 221 | + || iequals(method.c_str(), "stay") |
| 222 | + || iequals(method.c_str(), "double") ) |
| 223 | + { |
| 224 | + utility::ostringstream_t buf; |
| 225 | + buf << table << U("?request=") << utility::conversions::to_string_t(method) << U("&name=") << userName; |
| 226 | + PrintTable(CheckResponse("blackjack/dealer", bjDealer.request(methods::PUT, buf.str()).get()), was_refresh); |
| 227 | + } |
| 228 | + else if ( iequals(method.c_str(), "bet") |
| 229 | + || iequals(method.c_str(), "insure") ) |
| 230 | + { |
| 231 | + utility::string_t bet; |
| 232 | + ucout << "Enter bet:"; |
| 233 | + ucin >> bet; |
| 234 | + |
| 235 | + if ( userName.empty() ) { ucout << "Must have a name first!\n"; continue; } |
| 236 | + |
| 237 | + utility::ostringstream_t buf; |
| 238 | + buf << table << U("?request=") << utility::conversions::to_string_t(method) << U("&name=") << userName << U("&amount=") << bet; |
| 239 | + PrintTable(CheckResponse("blackjack/dealer", bjDealer.request(methods::PUT, buf.str()).get()), was_refresh); |
| 240 | + } |
| 241 | + else if ( iequals(method.c_str(), "newtbl") ) |
| 242 | + { |
| 243 | + CheckResponse("blackjack/dealer", bjDealer.request(methods::POST).get(), was_refresh); |
| 244 | + } |
| 245 | + else if ( iequals(method.c_str(), "leave") ) |
| 246 | + { |
| 247 | + ucout << "Enter table:"; |
| 248 | + ucin >> table; |
| 249 | + |
| 250 | + if ( userName.empty() ) { ucout << "Must have a name first!\n"; continue; } |
| 251 | + |
| 252 | + utility::ostringstream_t buf; |
| 253 | + buf << table << U("?name=") << userName; |
| 254 | + CheckResponse("blackjack/dealer", bjDealer.request(methods::DEL, buf.str()).get(), was_refresh); |
| 255 | + } |
| 256 | + else if ( iequals(method.c_str(), "list") ) |
| 257 | + { |
| 258 | + was_refresh = false; |
| 259 | + http_response response = CheckResponse("blackjack/dealer", bjDealer.request(methods::GET).get()); |
| 260 | + |
| 261 | + if ( response.status_code() == status_codes::OK ) |
| 262 | + { |
| 263 | + availableTables = response.extract_json().get(); |
| 264 | + for (auto iter = availableTables.begin(); iter != availableTables.end(); ++iter) |
| 265 | + { |
| 266 | + BJTable bj_table = BJTable::FromJSON(iter->second); |
| 267 | + json::value id = json::value::number(bj_table.Id); |
| 268 | + |
| 269 | + ucout << "table " << bj_table.Id << ": {capacity: " << (long unsigned int)bj_table.Capacity << " no. players: " << (long unsigned int)bj_table.Players.size() << " }\n"; |
| 270 | + } |
| 271 | + ucout << std::endl; |
| 272 | + } |
| 273 | + } |
| 274 | + else |
| 275 | + { |
| 276 | + ucout << utility::conversions::to_string_t(method) << ": not understood\n"; |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + return 0; |
| 281 | +} |
| 282 | + |
0 commit comments