feat: server, client 디렉토리 조정 및 client 헤더 파일 추가. 시나리오

혹은 대화형 클라이언트로 수정
This commit is contained in:
bumpsoo 2026-02-04 13:16:36 +00:00
parent b887f15662
commit 1d103d7f16
12 changed files with 281 additions and 112 deletions

170
client/Client.cpp Normal file
View file

@ -0,0 +1,170 @@
#include "Client.h"
#include <iostream>
#include <thread>
BaseClient::BaseClient(boost::asio::io_context &io_context,
const std::string &host, const std::string &port)
: socket_(io_context) {
tcp::resolver resolver(io_context);
boost::asio::connect(socket_, resolver.resolve(host, port));
}
void BaseClient::SendPacket(PacketID id, const void *payload,
size_t payloadSize) {
PacketHeader header;
header.id = static_cast<uint16_t>(id);
header.size = static_cast<uint16_t>(payloadSize);
std::vector<uint8_t> buffer(sizeof(PacketHeader) + payloadSize);
std::memcpy(buffer.data(), &header, sizeof(PacketHeader));
if (payload && payloadSize > 0) {
std::memcpy(buffer.data() + sizeof(PacketHeader), payload, payloadSize);
}
boost::asio::write(socket_, boost::asio::buffer(buffer));
}
bool BaseClient::ReceiveHeader(PacketHeader &header) {
try {
boost::asio::read(socket_, boost::asio::buffer(&header, sizeof(header)));
return true;
} catch (...) {
return false;
}
}
void BaseClient::Login(const std::string &nickname) {
PKT_CS_Login loginPkt;
std::memset(loginPkt.nickname, 0, sizeof(loginPkt.nickname));
std::strncpy(loginPkt.nickname, nickname.c_str(),
sizeof(loginPkt.nickname) - 1);
SendPacket(PacketID::Login, &loginPkt, sizeof(loginPkt));
std::cout << "로그인 요청 보냄: " << nickname << std::endl;
}
void BaseClient::HandleUpgradeResult() {
PKT_SC_UpgradeResult res;
if (ReceivePayload(res)) {
currentLevel_ = res.currentLevel;
currentGold_ = res.currentGold;
std::string resultStr =
(res.result == 1) ? "성공" : (res.result == 0 ? "파괴!!!" : "실패");
std::cout << ">> 강화 결과: " << resultStr
<< " (현재레벨: " << currentLevel_
<< ", 남은골드: " << currentGold_ << ")" << std::endl;
}
}
void BaseClient::HandleSellResult() {
PKT_SC_SellResult res;
if (ReceivePayload(res)) {
currentLevel_ = 0;
currentGold_ = res.totalGold;
std::cout << ">> 판매 결과: " << res.earnedGold
<< " 골드 획득! (총 골드: " << currentGold_ << ")" << std::endl;
}
}
void InteractiveClient::Run() {
std::cout << "닉네임을 입력하세요: ";
std::cin >> nickname_;
Login(nickname_);
while (true) {
if (currentLevel_ == 0) {
std::cout << "\n[현재 검 없음] 1. 검 구매 시도, 2. 종료\n입력: ";
} else {
std::cout << "\n[현재 검 레벨: " << currentLevel_
<< ", 골드: " << currentGold_
<< "] 1. 강화 시도, 2. 판매, 3. 종료\n입력: ";
}
int choice;
if (!(std::cin >> choice))
break;
if (currentLevel_ == 0) {
if (choice == 1) {
SendPacket(PacketID::CS_UpgradeSword);
PacketHeader header;
if (ReceiveHeader(header) &&
header.id == static_cast<uint16_t>(PacketID::SC_UpgradeResult)) {
HandleUpgradeResult();
}
} else if (choice == 2) {
break;
}
} else {
if (choice == 1) {
SendPacket(PacketID::CS_UpgradeSword);
PacketHeader header;
if (ReceiveHeader(header) &&
header.id == static_cast<uint16_t>(PacketID::SC_UpgradeResult)) {
HandleUpgradeResult();
}
} else if (choice == 2) {
SendPacket(PacketID::CS_SellSword);
PacketHeader header;
if (ReceiveHeader(header) &&
header.id == static_cast<uint16_t>(PacketID::SC_SellResult)) {
HandleSellResult();
}
} else if (choice == 3) {
break;
}
}
}
}
#include <fstream>
void ScenarioClient::Run() {
std::ifstream is(scenarioFile_);
if (!is.is_open()) {
std::cerr << "시나리오 파일을 열 수 없습니다: " << scenarioFile_
<< std::endl;
return;
}
if (!(is >> nickname_))
return;
Login(nickname_);
std::string line;
while (is >> line) {
int choice = std::stoi(line);
std::cout << "\n시나리오 입력 실행: " << choice << std::endl;
if (currentLevel_ == 0) {
if (choice == 1) {
SendPacket(PacketID::CS_UpgradeSword);
PacketHeader header;
if (ReceiveHeader(header) &&
header.id == static_cast<uint16_t>(PacketID::SC_UpgradeResult)) {
HandleUpgradeResult();
}
} else if (choice == 2) {
break;
}
} else {
if (choice == 1) {
SendPacket(PacketID::CS_UpgradeSword);
PacketHeader header;
if (ReceiveHeader(header) &&
header.id == static_cast<uint16_t>(PacketID::SC_UpgradeResult)) {
HandleUpgradeResult();
}
} else if (choice == 2) {
SendPacket(PacketID::CS_SellSword);
PacketHeader header;
if (ReceiveHeader(header) &&
header.id == static_cast<uint16_t>(PacketID::SC_SellResult)) {
HandleSellResult();
}
} else if (choice == 3) {
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}

39
client/main.cpp Normal file
View file

@ -0,0 +1,39 @@
#include "Client.h"
#include <iostream>
int main(int argc, char *argv[]) {
bool isScenario = false;
std::string nickname;
std::string scenarioFile;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--scenario" && i + 1 < argc) {
isScenario = true;
scenarioFile = argv[++i];
} else {
nickname = arg;
}
}
try {
boost::asio::io_context io_context;
if (isScenario) {
if (scenarioFile.empty()) {
std::cerr
<< "시나리오 파일명이 필요합니다. 사용법: --scenario <파일경로>"
<< std::endl;
return 1;
}
ScenarioClient client(io_context, "127.0.0.1", "30000", scenarioFile);
client.Run();
} else {
InteractiveClient client(io_context, "127.0.0.1", "30000");
client.Run();
}
} catch (std::exception &e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}