Handle ptsave urls with GameController

This commit is contained in:
Tamás Bálint Misius 2023-08-22 23:31:38 +02:00
parent 3ab697d05f
commit 6ceb51b408
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
12 changed files with 64 additions and 48 deletions

View File

@ -480,34 +480,15 @@ int Main(int argc, char *argv[])
{ {
std::cout << "Got Ptsave: id: " << saveIdPart << std::endl; std::cout << "Got Ptsave: id: " << saveIdPart << std::endl;
} }
ByteString saveHistoryPart = "0";
if (auto split = saveIdPart.SplitBy('@'))
{
saveHistoryPart = split.After();
saveIdPart = split.Before();
}
int saveId = saveIdPart.ToNumber<int>(); int saveId = saveIdPart.ToNumber<int>();
int saveHistory = saveHistoryPart.ToNumber<int>();
auto getSave = std::make_unique<http::GetSaveRequest>(saveId, 0); gameController->OpenSavePreview(saveId, saveHistory, savePreviewUrl);
getSave->Start();
getSave->Wait();
std::unique_ptr<SaveInfo> newSave;
try
{
newSave = getSave->Finish();
}
catch (const http::RequestError &ex)
{
throw std::runtime_error("Could not load save info\n" + ByteString(ex.what()));
}
auto getSaveData = std::make_unique<http::GetSaveDataRequest>(saveId, 0);
getSaveData->Start();
getSaveData->Wait();
std::unique_ptr<GameSave> saveData;
try
{
saveData = std::make_unique<GameSave>(getSaveData->Finish());
}
catch (const http::RequestError &ex)
{
throw std::runtime_error("Could not load save\n" + ByteString(ex.what()));
}
newSave->SetGameSave(std::move(saveData));
gameController->LoadSave(std::move(newSave));
} }
catch (std::exception & e) catch (std::exception & e)
{ {

View File

@ -0,0 +1,8 @@
#pragma once
enum SavePreviewType
{
savePreviewNormal,
savePreviewInstant,
savePreviewUrl,
};

View File

@ -543,7 +543,7 @@ bool GameController::MouseUp(int x, int y, unsigned button, MouseupReason reason
{ {
int saveID = str.Substr(3, si.first - 3).ToNumber<int>(true); int saveID = str.Substr(3, si.first - 3).ToNumber<int>(true);
if (saveID) if (saveID)
OpenSavePreview(saveID, 0, false); OpenSavePreview(saveID, 0, savePreviewNormal);
} }
break; break;
case sign::Type::Thread: case sign::Type::Thread:
@ -1223,9 +1223,9 @@ void GameController::OpenSaveDone()
} }
} }
void GameController::OpenSavePreview(int saveID, int saveDate, bool instant) void GameController::OpenSavePreview(int saveID, int saveDate, SavePreviewType savePreviewType)
{ {
activePreview = new PreviewController(saveID, saveDate, instant, [this] { OpenSaveDone(); }, nullptr); activePreview = new PreviewController(saveID, saveDate, savePreviewType, [this] { OpenSaveDone(); }, nullptr);
ui::Engine::Ref().ShowWindow(activePreview->GetView()); ui::Engine::Ref().ShowWindow(activePreview->GetView());
} }
@ -1233,7 +1233,7 @@ void GameController::OpenSavePreview()
{ {
if(gameModel->GetSave()) if(gameModel->GetSave())
{ {
activePreview = new PreviewController(gameModel->GetSave()->GetID(), 0, false, [this] { OpenSaveDone(); }, nullptr); activePreview = new PreviewController(gameModel->GetSave()->GetID(), 0, savePreviewNormal, [this] { OpenSaveDone(); }, nullptr);
ui::Engine::Ref().ShowWindow(activePreview->GetView()); ui::Engine::Ref().ShowWindow(activePreview->GetView());
} }
} }

View File

@ -3,6 +3,7 @@
#include "client/StartupInfo.h" #include "client/StartupInfo.h"
#include "gui/interface/Point.h" #include "gui/interface/Point.h"
#include "gui/interface/Colour.h" #include "gui/interface/Colour.h"
#include "gui/SavePreviewType.h"
#include "simulation/Sign.h" #include "simulation/Sign.h"
#include "simulation/Particle.h" #include "simulation/Particle.h"
#include "Misc.h" #include "Misc.h"
@ -135,7 +136,7 @@ public:
void OpenLogin(); void OpenLogin();
void OpenProfile(); void OpenProfile();
void OpenTags(); void OpenTags();
void OpenSavePreview(int saveID, int saveDate, bool instant); void OpenSavePreview(int saveID, int saveDate, SavePreviewType savePreiviewType);
void OpenSavePreview(); void OpenSavePreview();
void OpenLocalSaveWindow(bool asCurrent); void OpenLocalSaveWindow(bool asCurrent);
void OpenLocalBrowse(); void OpenLocalBrowse();

View File

@ -18,7 +18,7 @@
#include "gui/login/LoginView.h" #include "gui/login/LoginView.h"
#include "Config.h" #include "Config.h"
PreviewController::PreviewController(int saveID, int saveDate, bool instant, std::function<void ()> onDone_, std::unique_ptr<VideoBuffer> thumbnail): PreviewController::PreviewController(int saveID, int saveDate, SavePreviewType savePreviewType, std::function<void ()> onDone_, std::unique_ptr<VideoBuffer> thumbnail):
saveId(saveID), saveId(saveID),
loginWindow(NULL), loginWindow(NULL),
HasExited(false) HasExited(false)
@ -27,7 +27,8 @@ PreviewController::PreviewController(int saveID, int saveDate, bool instant, std
previewView = new PreviewView(std::move(thumbnail)); previewView = new PreviewView(std::move(thumbnail));
previewModel->AddObserver(previewView); previewModel->AddObserver(previewView);
previewView->AttachController(this); previewView->AttachController(this);
previewModel->SetDoOpen(instant); previewModel->SetDoOpen(savePreviewType != savePreviewNormal);
previewModel->SetFromUrl(savePreviewType == savePreviewUrl);
previewModel->UpdateSave(saveID, saveDate); previewModel->UpdateSave(saveID, saveDate);
@ -81,6 +82,11 @@ bool PreviewController::GetDoOpen()
return previewModel->GetDoOpen(); return previewModel->GetDoOpen();
} }
bool PreviewController::GetFromUrl()
{
return previewModel->GetFromUrl();
}
void PreviewController::DoOpen() void PreviewController::DoOpen()
{ {
previewModel->SetDoOpen(true); previewModel->SetDoOpen(true);

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "client/ClientListener.h" #include "client/ClientListener.h"
#include "gui/SavePreviewType.h"
#include <functional> #include <functional>
#include <memory> #include <memory>
@ -19,12 +20,13 @@ public:
inline int SaveID() { return saveId; } inline int SaveID() { return saveId; }
bool HasExited; bool HasExited;
PreviewController(int saveID, int saveDate, bool instant, std::function<void ()> onDone, std::unique_ptr<VideoBuffer> thumbnail); PreviewController(int saveID, int saveDate, SavePreviewType savePreviewType, std::function<void ()> onDone, std::unique_ptr<VideoBuffer> thumbnail);
void Exit(); void Exit();
void DoOpen(); void DoOpen();
void OpenInBrowser(); void OpenInBrowser();
void ShowLogin(); void ShowLogin();
bool GetDoOpen(); bool GetDoOpen();
bool GetFromUrl();
const SaveInfo *GetSaveInfo() const; const SaveInfo *GetSaveInfo() const;
std::unique_ptr<SaveInfo> TakeSaveInfo(); std::unique_ptr<SaveInfo> TakeSaveInfo();
PreviewView * GetView() { return previewView; } PreviewView * GetView() { return previewView; }

View File

@ -75,6 +75,16 @@ bool PreviewModel::GetDoOpen()
return doOpen; return doOpen;
} }
void PreviewModel::SetFromUrl(bool fromUrl)
{
this->fromUrl = fromUrl;
}
bool PreviewModel::GetFromUrl()
{
return fromUrl;
}
bool PreviewModel::GetCanOpen() bool PreviewModel::GetCanOpen()
{ {
return canOpen; return canOpen;

View File

@ -18,6 +18,7 @@ class SaveInfo;
class PreviewModel class PreviewModel
{ {
bool doOpen = false; bool doOpen = false;
bool fromUrl = false;
bool canOpen = true; bool canOpen = true;
std::vector<PreviewView*> observers; std::vector<PreviewView*> observers;
std::unique_ptr<SaveInfo> saveInfo; std::unique_ptr<SaveInfo> saveInfo;
@ -63,8 +64,10 @@ public:
void UpdateSave(int saveID, int saveDate); void UpdateSave(int saveID, int saveDate);
void SetFavourite(bool favourite); void SetFavourite(bool favourite);
bool GetDoOpen(); bool GetDoOpen();
bool GetFromUrl();
bool GetCanOpen(); bool GetCanOpen();
void SetDoOpen(bool doOpen); void SetDoOpen(bool doOpen);
void SetFromUrl(bool fromUrl);
void Update(); void Update();
void OnSaveReady(); void OnSaveReady();
bool ParseSaveInfo(ByteString &saveInfoResponse); bool ParseSaveInfo(ByteString &saveInfoResponse);

View File

@ -271,16 +271,19 @@ void PreviewView::CheckComment()
void PreviewView::DoDraw() void PreviewView::DoDraw()
{ {
Window::DoDraw();
Graphics * g = GetGraphics(); Graphics * g = GetGraphics();
for (size_t i = 0; i < commentTextComponents.size(); i++) if (!c->GetFromUrl())
{ {
int linePos = commentTextComponents[i]->Position.Y+commentsPanel->ViewportPosition.Y+commentTextComponents[i]->Size.Y+4; Window::DoDraw();
if (linePos > 0 && linePos < Size.Y-commentBoxHeight) for (size_t i = 0; i < commentTextComponents.size(); i++)
g->BlendLine( {
Position + Vec2{ 1+XRES/2, linePos }, int linePos = commentTextComponents[i]->Position.Y+commentsPanel->ViewportPosition.Y+commentTextComponents[i]->Size.Y+4;
Position + Vec2{ Size.X-2, linePos }, if (linePos > 0 && linePos < Size.Y-commentBoxHeight)
0xFFFFFF_rgb .WithAlpha(100)); g->BlendLine(
Position + Vec2{ 1+XRES/2, linePos },
Position + Vec2{ Size.X-2, linePos },
0xFFFFFF_rgb .WithAlpha(100));
}
} }
if (c->GetDoOpen()) if (c->GetDoOpen())
{ {
@ -288,8 +291,10 @@ void PreviewView::DoDraw()
g->BlendRect(RectSized(Position + Size / 2 - Vec2{ 100, 25 }, Vec2{ 200, 50 }), 0xFFFFFF_rgb .WithAlpha(180)); g->BlendRect(RectSized(Position + Size / 2 - Vec2{ 100, 25 }, Vec2{ 200, 50 }), 0xFFFFFF_rgb .WithAlpha(180));
g->BlendText(Position + Vec2{(Size.X/2)-((Graphics::TextSize("Loading save...").X - 1)/2), (Size.Y/2)-5}, "Loading save...", style::Colour::InformationTitle.NoAlpha().WithAlpha(255)); g->BlendText(Position + Vec2{(Size.X/2)-((Graphics::TextSize("Loading save...").X - 1)/2), (Size.Y/2)-5}, "Loading save...", style::Colour::InformationTitle.NoAlpha().WithAlpha(255));
} }
g->DrawRect(RectSized(Position, Size), 0xFFFFFF_rgb); if (!c->GetFromUrl())
{
g->DrawRect(RectSized(Position, Size), 0xFFFFFF_rgb);
}
} }
void PreviewView::OnDraw() void PreviewView::OnDraw()

View File

@ -217,7 +217,7 @@ void SearchController::OpenSave(int saveID, int saveDate, std::unique_ptr<VideoB
delete activePreview; delete activePreview;
Graphics * g = searchView->GetGraphics(); Graphics * g = searchView->GetGraphics();
g->BlendFilledRect(RectSized(Vec2{ XRES/3, WINDOWH-20 }, Vec2{ XRES/3, 20 }), 0x000000_rgb .WithAlpha(150)); //dim the "Page X of Y" a little to make the CopyTextButton more noticeable g->BlendFilledRect(RectSized(Vec2{ XRES/3, WINDOWH-20 }, Vec2{ XRES/3, 20 }), 0x000000_rgb .WithAlpha(150)); //dim the "Page X of Y" a little to make the CopyTextButton more noticeable
activePreview = new PreviewController(saveID, saveDate, instantOpen, [this] { OpenSaveDone(); }, std::move(thumbnail)); activePreview = new PreviewController(saveID, saveDate, instantOpen ? savePreviewInstant : savePreviewNormal, [this] { OpenSaveDone(); }, std::move(thumbnail));
activePreview->GetView()->MakeActiveWindow(); activePreview->GetView()->MakeActiveWindow();
} }

View File

@ -2192,7 +2192,7 @@ int LuaScriptInterface::simulation_loadSave(lua_State * l)
int saveID = luaL_optint(l,1,0); int saveID = luaL_optint(l,1,0);
int instant = luaL_optint(l,2,0); int instant = luaL_optint(l,2,0);
int history = luaL_optint(l,3,0); //Exact second a previous save was saved int history = luaL_optint(l,3,0); //Exact second a previous save was saved
luacon_controller->OpenSavePreview(saveID, history, instant?true:false); luacon_controller->OpenSavePreview(saveID, history, instant ? savePreviewInstant : savePreviewNormal);
return 0; return 0;
} }

View File

@ -505,7 +505,7 @@ AnyType TPTScriptInterface::tptS_load(std::deque<String> * words)
if (saveID.Value() > 0) if (saveID.Value() > 0)
{ {
c->OpenSavePreview(saveID.Value(), 0, false); c->OpenSavePreview(saveID.Value(), 0, savePreviewNormal);
return NumberType(0); return NumberType(0);
} }
else else