From e43c5707ae91bdea9b443debd2a25fc8894e4167 Mon Sep 17 00:00:00 2001 From: 404wrong <2497440459@qq.com> Date: Mon, 22 Jan 2024 11:11:20 +0800 Subject: [PATCH] Squashed commit of the following: commit 06b687d9963504308c2d0924541dc589cc83b1ca Author: 404wrong <2497440459@qq.com> Date: Mon Jan 22 11:10:41 2024 +0800 Update GameScene.cpp commit b22548e315b081bab2cc8005c4fad88ce4d1c463 Author: 404wrong <2497440459@qq.com> Date: Mon Jan 22 09:10:30 2024 +0800 1 commit c42a2f896f4529ae87cc593dda6b8598289f4680 Author: 404wrong <2497440459@qq.com> Date: Mon Jan 22 09:06:32 2024 +0800 1 commit 4c4bb6cff84207926517c30221ccf61971957994 Author: 404wrong <2497440459@qq.com> Date: Mon Jan 22 08:57:17 2024 +0800 update new move commit f6506e9e26ed8227e2efc5300541e0d7e2d82f81 Author: 404wrong <2497440459@qq.com> Date: Mon Jan 22 08:51:42 2024 +0800 update timer commit 3f0ee20e80b54dcfb19d609444486d2c68853e14 Author: 404wrong <2497440459@qq.com> Date: Mon Jan 22 08:37:32 2024 +0800 update network --- Classes/Boxes/controler.cpp | 25 +++++++++ Classes/Boxes/controler.h | 1 + Classes/GameFrame/GameScene.cpp | 51 +++++++++++++++++- Classes/GameFrame/GameScene.h | 96 +++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+), 2 deletions(-) diff --git a/Classes/Boxes/controler.cpp b/Classes/Boxes/controler.cpp index 35b22b5..72b4a1c 100644 --- a/Classes/Boxes/controler.cpp +++ b/Classes/Boxes/controler.cpp @@ -226,6 +226,7 @@ bool controler::success() { } void controler::move(pii dir) { + gs->trymove(std::to_string(dir.first+1) + std::to_string(dir.second+1)); if (!suc) { player->father->processObjects(player, player->father, player->father, dir, player->pos, 0, player, player->pos, { 1,1 }, nullptr, 0); //if (big != player->father) { @@ -250,7 +251,31 @@ void controler::move(pii dir) { success(); } } +void controler::move1(pii dir) { + if (!suc) { + player->father->processObjects(player, player->father, player->father, dir, player->pos, 0, player, player->pos, { 1,1 }, nullptr, 0); + //if (big != player->father) { + big = player->father; + if (player->father->father) { + big = player->father->father; + } + //} + draw(gs, size); + mv.push_back(dir); + std::vector stepRec(mv); + if (SaveManager::getInstance()->info != nullptr) { + delete SaveManager::getInstance()->info; + SaveManager::getInstance()->info = nullptr; + + } + SaveManager::getInstance()->info = new SaveInfo(this->lid, {}, ""); + for (int i = 0; i < mv.size() - 1; ++i) { + SaveManager::getInstance()->info->actions.push_back(mv[i]); + }; + success(); + } +} void controler::reload(std::vector step) { if (this->needLoad) { for (int i = 0; i < step.size(); i++) { diff --git a/Classes/Boxes/controler.h b/Classes/Boxes/controler.h index 358526a..07cbae7 100644 --- a/Classes/Boxes/controler.h +++ b/Classes/Boxes/controler.h @@ -47,6 +47,7 @@ extern class controler { bool success(); void move(pii dir); + void move1(pii dir); void reload(std::vector step); diff --git a/Classes/GameFrame/GameScene.cpp b/Classes/GameFrame/GameScene.cpp index 35a25f2..41edf23 100644 --- a/Classes/GameFrame/GameScene.cpp +++ b/Classes/GameFrame/GameScene.cpp @@ -30,7 +30,7 @@ bool GameScene::init() { return false; } - + id = "1"; Size winSize = Director::getInstance()->getWinSize(); auto backGround = Sprite::create("MainMenu/MainBG.png"); @@ -113,7 +113,7 @@ bool GameScene::init() optMenu->alignItemsHorizontallyWithPadding(100); optMenu->setPosition(Vec2(winSize.width / 2, 48)); this->addChild(optMenu, 1); - + this->schedule(CC_SCHEDULE_SELECTOR(GameScene::getmove), 0.1f); SaveInfo* info = SaveManager::getInstance()->info; if (info) { @@ -241,5 +241,52 @@ void GameScene::onMouseUp(Event* event) log("Mouse button released at (%.2f, %.2f). Distance moved: %.2f", endPos.x, endPos.y, distance); } } +void GameScene::gm(network::HttpClient* sender, network::HttpResponse* response) { + if (!response) { + CCLOG("HttpRequest failed!"); + return; + } + + long statusCode = response->getResponseCode(); + + if (statusCode == 200) { + // 如果响应状态码为200,表示请求成功 + CCLOG("HttpRequest succeeded!"); + + // 获取响应数据 + std::vector* buffer = response->getResponseData(); + std::string responseData(buffer->begin(), buffer->end()); + + // 在这里解析服务器返回的数据 + for (int i = 0; i < responseData.size(); i++) { + controler::get()->move1({ responseData[i++] - '1',responseData[i] - '1' }); + } + } + else { + // 请求失败,输出错误信息 + CCLOG("HttpRequest failed. Status code: %ld", statusCode); + } +} +void GameScene::getmove(float dt) { + // 创建一个HttpRequest对象 + network::HttpRequest* request = new network::HttpRequest(); + // 设置请求的URL + request->setUrl("http://10.24.50.187:9092/cpp/get?c=1"); + // 设置请求方式为POST + request->setRequestType(network::HttpRequest::Type::POST); + + // 构造要发送的数据 + std::string postData = "121321323"; + // 设置请求体 + request->setRequestData(postData.c_str(), postData.length()); + + // 设置回调函数,处理服务器的响应 + request->setResponseCallback(CC_CALLBACK_2(GameScene::gm, this)); + + // 发送请求 + network::HttpClient::getInstance()->send(request); + // 释放HttpRequest对象,避免内存泄漏 + request->release(); +} diff --git a/Classes/GameFrame/GameScene.h b/Classes/GameFrame/GameScene.h index fd13eb8..60fc7b2 100644 --- a/Classes/GameFrame/GameScene.h +++ b/Classes/GameFrame/GameScene.h @@ -2,12 +2,14 @@ #define __GAME_SCENE_H__ #include "cocos2d.h" +#include "network/HttpClient.h" USING_NS_CC; using pii = std::pair; class GameScene : public Layer { protected: + std::string id; EventListenerKeyboard* _listener; EventListenerMouse* mouseListener; Vec2 startPos; @@ -32,6 +34,100 @@ class GameScene : public Layer { void GameScene::onMouseUp(Event* event); + void GameScene::setId(network::HttpClient* sender, network::HttpResponse* response) { + if (!response) { + CCLOG("HttpRequest failed!"); + return; + } + + long statusCode = response->getResponseCode(); + + if (statusCode == 200) { + // 如果响应状态码为200,表示请求成功 + CCLOG("HttpRequest succeeded!"); + + // 获取响应数据 + std::vector* buffer = response->getResponseData(); + std::string responseData(buffer->begin(), buffer->end()); + + // 在这里解析服务器返回的数据 + id=responseData.c_str(); + } + else { + // 请求失败,输出错误信息 + CCLOG("HttpRequest failed. Status code: %ld", statusCode); + } + + } + void getId() { + // 创建一个HttpRequest对象 + network::HttpRequest* request = new network::HttpRequest(); + // 设置请求的URL + request->setUrl("http://10.24.50.187:9092/cpp/new"); + // 设置请求方式为POST + request->setRequestType(network::HttpRequest::Type::POST); + + // 构造要发送的数据 + std::string postData = "121321323"; + // 设置请求体 + request->setRequestData(postData.c_str(), postData.length()); + + // 设置回调函数,处理服务器的响应 + request->setResponseCallback(CC_CALLBACK_2(GameScene::setId, this)); + + // 发送请求 + network::HttpClient::getInstance()->send(request); + // 释放HttpRequest对象,避免内存泄漏 + request->release(); + + } + void GameScene::setmove(network::HttpClient* sender, network::HttpResponse* response) { + if (!response) { + CCLOG("HttpRequest failed!"); + return; + } + + long statusCode = response->getResponseCode(); + + if (statusCode == 200) { + // 如果响应状态码为200,表示请求成功 + CCLOG("HttpRequest succeeded!"); + + // 获取响应数据 + std::vector* buffer = response->getResponseData(); + std::string responseData(buffer->begin(), buffer->end()); + + // 在这里解析服务器返回的数据 + id = responseData.c_str(); + } + else { + // 请求失败,输出错误信息 + CCLOG("HttpRequest failed. Status code: %ld", statusCode); + } + } + void trymove(std::string str) { + // 创建一个HttpRequest对象 + network::HttpRequest* request = new network::HttpRequest(); + // 设置请求的URL + request->setUrl("http://10.24.50.187:9092/cpp/add?c=1&m="+str); + // 设置请求方式为POST + request->setRequestType(network::HttpRequest::Type::POST); + + // 构造要发送的数据 + std::string postData = "121321323"; + // 设置请求体 + request->setRequestData(postData.c_str(), postData.length()); + + // 设置回调函数,处理服务器的响应 + request->setResponseCallback(CC_CALLBACK_2(GameScene::setmove, this)); + + // 发送请求 + network::HttpClient::getInstance()->send(request); + // 释放HttpRequest对象,避免内存泄漏 + request->release(); + } + void gm(network::HttpClient* sender, network::HttpResponse* response); + void getmove(float dt); ~GameScene() {} };