2021-12-06 21:15:31 -05:00
|
|
|
#include "gameparser.h"
|
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
#include <QBuffer>
|
2021-12-06 21:15:31 -05:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QRegularExpression>
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
GameParser::GameParser()
|
|
|
|
{
|
2021-12-06 21:15:31 -05:00
|
|
|
api = new tesseract::TessBaseAPI();
|
|
|
|
|
|
|
|
if (api->Init(nullptr, "eng")) {
|
|
|
|
qDebug() << "Could not initialize tesseract!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
api->SetPageSegMode(tesseract::PageSegMode::PSM_SINGLE_BLOCK);
|
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
GameParser::~GameParser()
|
|
|
|
{
|
2021-12-06 21:15:31 -05:00
|
|
|
api->End();
|
|
|
|
delete api;
|
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
GameParseResult GameParser::parseImage(QImage img)
|
|
|
|
{
|
2021-12-06 21:15:31 -05:00
|
|
|
QBuffer buf;
|
|
|
|
img = img.convertToFormat(QImage::Format_Grayscale8);
|
|
|
|
img.save(&buf, "PNG", 100);
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
Pix *image = pixReadMem((const l_uint8 *)buf.data().data(), buf.size());
|
2021-12-06 21:15:31 -05:00
|
|
|
api->SetImage(image);
|
|
|
|
api->SetSourceResolution(300);
|
|
|
|
|
|
|
|
const QString text = api->GetUTF8Text();
|
|
|
|
|
|
|
|
// TODO: clean up these names
|
|
|
|
const bool hasWorldFullText = text.contains("This World is currently full.") || text.contains("Players in queue");
|
|
|
|
const bool hasLobbyErrorText = text.contains("The lobby server connection has encountered an error.");
|
|
|
|
const bool hasCONFIGURATIONText = text.contains("CONFIGURATION") || text.contains("ONLINE");
|
|
|
|
const bool hasConnectingToData = text.contains("Connecting to data center");
|
|
|
|
const bool worldTotallyFull = text.contains("3001");
|
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
if (hasLobbyErrorText) {
|
2021-12-06 21:15:31 -05:00
|
|
|
qDebug() << "LOBBY ERROR";
|
|
|
|
|
|
|
|
return {ScreenState::LobbyError, -1};
|
|
|
|
} else {
|
2022-08-15 11:14:37 -04:00
|
|
|
if (worldTotallyFull) {
|
2021-12-06 21:15:31 -05:00
|
|
|
qDebug() << "TOTALLY FULL WORLD (CLOSED BY SQENIX)";
|
|
|
|
|
|
|
|
return {ScreenState::WorldFull, -1};
|
|
|
|
} else {
|
2022-08-15 11:14:37 -04:00
|
|
|
if (hasConnectingToData) {
|
2021-12-06 21:15:31 -05:00
|
|
|
qDebug() << "CONNECTING TO DATA CENTER";
|
|
|
|
|
|
|
|
return {ScreenState::ConnectingToDataCenter, -1};
|
|
|
|
} else {
|
2022-08-15 11:14:37 -04:00
|
|
|
if (hasWorldFullText) {
|
2021-12-06 21:15:31 -05:00
|
|
|
qDebug() << "FULL WORLD";
|
|
|
|
|
|
|
|
// attempt to extract number of players in queue
|
|
|
|
QRegularExpression exp("(?:Players in queue: )([\\d|,]*)");
|
|
|
|
|
|
|
|
auto match = exp.match(text);
|
2022-08-15 11:14:37 -04:00
|
|
|
if (match.isValid()) {
|
2021-12-06 21:15:31 -05:00
|
|
|
return {ScreenState::InLoginQueue, match.captured(1).remove(',').toInt()};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {ScreenState::InLoginQueue, -1};
|
|
|
|
} else {
|
2022-08-15 11:14:37 -04:00
|
|
|
if (hasCONFIGURATIONText) {
|
2021-12-06 21:15:31 -05:00
|
|
|
qDebug() << "TITLE SCREEN";
|
|
|
|
return {ScreenState::EnteredTitleScreen, -1};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: figure out how to properly clear tesseract data
|
|
|
|
api->Clear();
|
|
|
|
api->ClearAdaptiveClassifier();
|
|
|
|
|
|
|
|
return {ScreenState::Splash, -1};
|
|
|
|
}
|