From b108a0f12226952333baa7d07e19949a01bb7e73 Mon Sep 17 00:00:00 2001 From: happyZYM Date: Wed, 22 May 2024 12:31:09 +0000 Subject: [PATCH] write basic argparse for train system and transaction system --- src/include/data.h | 23 +++++ src/train_system.cpp | 140 ++++++++++++++++++++++++++++- src/transaction_system.cpp | 174 +++++++++++++++++++++++++++++++++++-- test/ctest_config | 12 +-- 4 files changed, 334 insertions(+), 15 deletions(-) diff --git a/src/include/data.h b/src/include/data.h index f38438f..7336cd3 100644 --- a/src/include/data.h +++ b/src/include/data.h @@ -1,6 +1,7 @@ #ifndef DATA_H #define DATA_H #include +#include "storage/driver.h" #include "utils.h" struct FullUserData { char username[21]; @@ -9,4 +10,26 @@ struct FullUserData { char mailAddr[31]; uint8_t privilege; }; + +// waring: this struct is extremely large, later DiskManager should be optimized to handle this +struct FullTrainData { + char trainID[21]; + uint8_t stationNum; + // char stations[100][41]; + hash_t stations_hash[100]; + uint32_t seatNum : 18; + uint16_t startTime : 12; + uint16_t saleDate_beg : 10, saleDate_end : 10; + uint8_t type : 6; + struct StationData { + uint32_t price : 18; + uint16_t travelTime : 15; + uint16_t stopoverTime : 15; + }; + StationData stations[100]; +}; + +class TrainDataDrive: public DataDriverBase { + +}; #endif \ No newline at end of file diff --git a/src/train_system.cpp b/src/train_system.cpp index 6ee1089..bf0ec01 100644 --- a/src/train_system.cpp +++ b/src/train_system.cpp @@ -8,10 +8,142 @@ #include "engine.h" #include "utils.h" -std::string TicketSystemEngine::AddTrain(const std::string &command) { return "AddTrain"; } +std::string TicketSystemEngine::AddTrain(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token; + std::string trainID; + int stationNum, seatNum; + std::string stations[100]; + int prices[100]; + int startTime; + int travelTimes[100], stopoverTimes[100]; + int saleDate_begin, saleDate_end; + char type[2]; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'i': { + command_stream >> trainID; + break; + } + case 'n': { + command_stream >> stationNum; + break; + } + case 'm': { + command_stream >> seatNum; + break; + } + case 's': { + std::string station_raw; + command_stream >> station_raw; + // TODO + break; + } + case 'p': { + std::string price_raw; + command_stream >> price_raw; + // TODO + break; + } + case 'x': { + command_stream >> startTime; + break; + } + case 't': { + std::string travelTime_raw; + command_stream >> travelTime_raw; + // TODO + break; + } + case 'o': { + std::string stopoverTime_raw; + command_stream >> stopoverTime_raw; + // TODO + break; + } + case 'd': { + std::string saleDate_raw; + command_stream >> saleDate_raw; + // TODO + break; + } + case 'y': { + command_stream >> type; + break; + } + } + } + response_stream << '[' << command_id << "] AddTrain"; + return response_stream.str(); +} -std::string TicketSystemEngine::DeleteTrain(const std::string &command) { return "DeleteTrain"; } +std::string TicketSystemEngine::DeleteTrain(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token; + std::string trainID; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'i': { + command_stream >> trainID; + break; + } + } + } + response_stream << '[' << command_id << "] DeleteTrain"; + return response_stream.str(); +} -std::string TicketSystemEngine::ReleaseTrain(const std::string &command) { return "ReleaseTrain"; } +std::string TicketSystemEngine::ReleaseTrain(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token; + std::string trainID; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'i': { + command_stream >> trainID; + break; + } + } + } + response_stream << '[' << command_id << "] ReleaseTrain"; + return response_stream.str(); +} -std::string TicketSystemEngine::QueryTrain(const std::string &command) { return "QueryTrain"; } \ No newline at end of file +std::string TicketSystemEngine::QueryTrain(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token; + std::string trainID; + int date; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'i': { + command_stream >> trainID; + break; + } + case 'd': { + std::string date_raw; + command_stream >> date_raw; + // TODO + break; + } + } + } + response_stream << '[' << command_id << "] QueryTrain"; + return response_stream.str(); +} \ No newline at end of file diff --git a/src/transaction_system.cpp b/src/transaction_system.cpp index 26d7755..932eb45 100644 --- a/src/transaction_system.cpp +++ b/src/transaction_system.cpp @@ -8,12 +8,176 @@ #include "engine.h" #include "utils.h" -std::string TicketSystemEngine::BuyTicket(const std::string &command) { return "BuyTicket"; } +std::string TicketSystemEngine::QueryTicket(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token; + int date; + std::string from, to; + std::string order_by = "time"; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'd': { + std::string date_raw; + command_stream >> date_raw; + // TODO: parse date + break; + } + case 's': { + command_stream >> from; + break; + } + case 't': { + command_stream >> to; + break; + } + case 'p': { + command_stream >> order_by; + break; + } + default: { + throw std::invalid_argument("Invalid argument"); + } + } + } + response_stream << "[" << command_id << "] QueryTicket"; + return response_stream.str(); +} -std::string TicketSystemEngine::QueryOrder(const std::string &command) { return "QueryOrder"; } +std::string TicketSystemEngine::QueryTransfer(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token; + int date; + std::string from, to; + std::string order_by = "time"; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'd': { + std::string date_raw; + command_stream >> date_raw; + // TODO: parse date + break; + } + case 's': { + command_stream >> from; + break; + } + case 't': { + command_stream >> to; + break; + } + case 'p': { + command_stream >> order_by; + break; + } + default: { + throw std::invalid_argument("Invalid argument"); + } + } + } + response_stream << "[" << command_id << "] QueryTransfer"; + return response_stream.str(); +} -std::string TicketSystemEngine::RefundTicket(const std::string &command) { return "RefundTicket"; } +std::string TicketSystemEngine::BuyTicket(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token; + std::string user_name; + std::string train_id; + int date; + std::string from, to; + int ticket_num; + std::string accept_queue = "false"; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'u': { + command_stream >> user_name; + break; + } + case 'i': { + command_stream >> train_id; + break; + } + case 'd': { + std::string date_raw; + command_stream >> date_raw; + // TODO: parse date + break; + } + case 'f': { + command_stream >> from; + break; + } + case 't': { + command_stream >> to; + break; + } + case 'n': { + command_stream >> ticket_num; + break; + } + case 'q': { + command_stream >> accept_queue; + break; + } + } + } + response_stream << "[" << command_id << "] BuyTicket"; + return response_stream.str(); +} -std::string TicketSystemEngine::QueryTransfer(const std::string &command) { return "QueryTransfer"; } +std::string TicketSystemEngine::QueryOrder(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token; + std::string user_name; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'u': { + command_stream >> user_name; + break; + } + } + } + response_stream << "[" << command_id << "] QueryOrder"; + return response_stream.str(); +} -std::string TicketSystemEngine::QueryTicket(const std::string &command) { return "QueryTicket"; } \ No newline at end of file +std::string TicketSystemEngine::RefundTicket(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token; + std::string user_name; + int order = 1; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'u': { + command_stream >> user_name; + break; + } + case 'n': { + command_stream >> order; + break; + } + } + } + response_stream << "[" << command_id << "] RefundTicket"; + return response_stream.str(); +} \ No newline at end of file diff --git a/test/ctest_config b/test/ctest_config index a347c7e..dee992d 100644 --- a/test/ctest_config +++ b/test/ctest_config @@ -5,9 +5,9 @@ add_test(NAME basic_4 COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py basic_4 --ski add_test(NAME basic_5 COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py basic_5 --skip-check --ignore-first-dependency --enable-tested-program-logging) add_test(NAME basic_6 COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py basic_6 --skip-check --ignore-first-dependency --enable-tested-program-logging) add_test(NAME basic_extra COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py basic_extra --skip-check --ignore-first-dependency --enable-tested-program-logging) -add_test(NAME pressure_1_easy COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_1_easy --skip-check --ignore-first-dependency --enable-tested-program-logging) -add_test(NAME pressure_2_easy COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_2_easy --skip-check --ignore-first-dependency --enable-tested-program-logging) -add_test(NAME pressure_3_easy COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_3_easy --skip-check --ignore-first-dependency --enable-tested-program-logging) -add_test(NAME pressure_1_hard COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_1_hard --skip-check --ignore-first-dependency --enable-tested-program-logging) -add_test(NAME pressure_2_hard COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_2_hard --skip-check --ignore-first-dependency --enable-tested-program-logging) -add_test(NAME pressure_3_hard COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_3_hard --skip-check --ignore-first-dependency --enable-tested-program-logging) \ No newline at end of file +add_test(NAME pressure_1_easy COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_1_easy --skip-check --ignore-first-dependency) +add_test(NAME pressure_2_easy COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_2_easy --skip-check --ignore-first-dependency) +add_test(NAME pressure_3_easy COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_3_easy --skip-check --ignore-first-dependency) +add_test(NAME pressure_1_hard COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_1_hard --skip-check --ignore-first-dependency) +add_test(NAME pressure_2_hard COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_2_hard --skip-check --ignore-first-dependency) +add_test(NAME pressure_3_hard COMMAND ${PROJECT_SOURCE_DIR}/test/ojtest.py pressure_3_hard --skip-check --ignore-first-dependency) \ No newline at end of file