From 441abf1e6a15a39f1ef7bf5e4ee2a4a43bff327b Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Thu, 20 Jan 2022 05:18:00 +0000
Subject: [PATCH] Added names and ready notice

---
 click.cpp |  112 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 100 insertions(+), 12 deletions(-)

diff --git a/click.cpp b/click.cpp
index f373fce..ba85093 100644
--- a/click.cpp
+++ b/click.cpp
@@ -2,20 +2,23 @@
 #include "json.hpp"
 #include "protocol.hpp"
 
+#include <cstdint>
 #include <cstdlib>
 #include <getopt.h>
 #include <iostream>
+#include <string>
 #include <unordered_map>
 
-enum clickType { Buy, Sell, Flash };
+enum clickType { Buy, Sell, Flash, Delete };
 
 static std::unordered_map<std::string, clickType> mapClick;
 
 void initialise()
 {
-	mapClick = {{"BUY", Buy}, {"SELL", Sell}, {"FLASH", Flash},
-	            {"b", Buy},   {"s", Sell},    {"f", Flash},
-	            {"B", Buy},   {"S", Sell},    {"F", Flash}};
+	mapClick = {{"BUY", Buy},       {"SELL", Sell}, {"FLASH", Flash},
+	            {"b", Buy},         {"s", Sell},    {"f", Flash},
+	            {"B", Buy},         {"S", Sell},    {"F", Flash},
+	            {"DELETE", Delete}, {"D", Delete},  {"d", Delete}};
 }
 
 void usage()
@@ -25,13 +28,75 @@
 			  << std::endl
 			  << "USAGE" << std::endl
 			  << "\t-a product" << std::endl
-			  << "\t-t click type (buy, sell, flash)" << std::endl
+			  << "\t-t click type (buy, sell, flash, delete)" << std::endl
 			  << "\t-p price" << std::endl
 			  << "\t-v volume" << std::endl
 			  << "\t-i id" << std::endl
+			  << "\t-l loop count (default to 1)" << std::endl
 			  << "Always need product, need side, price and volume for "
 				 "adding/flash, need id for deleting"
 			  << std::endl;
+}
+
+void buy(std::string& product, double price, uint64_t volume)
+{
+	json::AddMessage a(json::ADD, product, price, book::Buy, volume);
+	json::AddedMessage* b =
+		static_cast<json::AddedMessage*>(protocol::addOrder(a));
+	std::cout << b->as_string() << std::endl;
+}
+
+void sell(std::string& product, double price, uint64_t volume)
+{
+	json::AddMessage a(json::ADD, product, price, book::Sell, volume);
+	json::AddedMessage* b =
+		static_cast<json::AddedMessage*>(protocol::addOrder(a));
+	std::cout << b->as_string() << std::endl;
+	delete b;
+}
+
+void flash(std::string& product, double price, uint64_t volume,
+           book::OrderSideEnum side)
+{
+	json::AddMessage a(json::ADD, product, price, side, volume);
+	json::Message* b = static_cast<json::Message*>(protocol::addOrder(a));
+	if (b->type == json::ERROR) {
+		std::cout << static_cast<json::ErrorMessage*>(b)->as_string()
+				  << std::endl;
+		return;
+	}
+	json::DeleteMessage c(json::DELETE, product,
+	                      static_cast<json::AddedMessage*>(b)->id);
+	json::Message* d = protocol::deleteOrder(c);
+	std::cout << static_cast<json::AddedMessage*>(b)->as_string() << std::endl;
+	if (d->type == json::DELETED) {
+		std::cout << static_cast<json::DeletedMessage*>(d)->as_string()
+				  << std::endl;
+	} else if (d->type == json::REJECT) {
+		std::cout << static_cast<json::RejectMessage*>(d)->as_string()
+				  << std::endl;
+	} else if (d->type == json::ERROR) {
+		std::cout << static_cast<json::ErrorMessage*>(d)->as_string()
+				  << std::endl;
+	}
+	delete b;
+	delete d;
+}
+
+void deleteOrder(std::string& product, std::string& id)
+{
+	json::DeleteMessage a(json::DELETE, product, id);
+	json::Message* b = protocol::deleteOrder(a);
+	if (b->type == json::DELETED) {
+		std::cout << static_cast<json::DeletedMessage*>(b)->as_string()
+				  << std::endl;
+	} else if (b->type == json::REJECT) {
+		std::cout << static_cast<json::RejectMessage*>(b)->as_string()
+				  << std::endl;
+	} else if (b->type == json::ERROR) {
+		std::cout << static_cast<json::ErrorMessage*>(b)->as_string()
+				  << std::endl;
+	}
 }
 
 int main(int argc, char** argv)
@@ -39,19 +104,23 @@
 	int c;
 	std::string product, id;
 	double price;
-	clickType click;
+	clickType click, side;
 	uint64_t volume;
+	uint64_t times = 1;
 	initialise();
 	if (argc == 1) {
 		usage(), exit(0);
 	}
-	while ((c = getopt(argc, argv, "a::t::p::v::i::")) != -1) {
+	while ((c = getopt(argc, argv, "a::t::s::p::v::i::l::")) != -1) {
 		switch (c) {
 		case 'a':
 			product = std::string(optarg);
 			break;
-		case 's':
+		case 't':
 			click = mapClick[optarg];
+			break;
+		case 's':
+			side = mapClick[optarg];
 			break;
 		case 'p':
 			price = std::stod(optarg);
@@ -62,15 +131,34 @@
 		case 'i':
 			id = std::string(optarg);
 			break;
+		case 'l':
+			times = std::stoll(optarg);
+			break;
 		case '?':
+			std::cout << "*1" << std::endl;
 		default:
+			std::cout << "*2 " << (char)c << std::endl;
 			usage();
 			exit(0);
 		}
 	}
-	switch (click) {
-	case Buy:
-	case Sell:
-	case Flash:;
+	for (std::uint64_t i = 0; i < times; i++) {
+		switch (click) {
+		case Buy:
+			buy(product, price, volume);
+			break;
+		case Sell:
+			sell(product, price, volume);
+			break;
+		case Flash:
+			if (side == clickType::Buy)
+				flash(product, price, volume, book::Buy);
+			else
+				flash(product, price, volume, book::Sell);
+			break;
+		case Delete:
+			deleteOrder(product, id);
+			break;
+		}
 	}
 }

--
Gitblit v1.9.3