-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
85 lines (67 loc) · 2.3 KB
/
mainwindow.cpp
File metadata and controls
85 lines (67 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <QtWebKitWidgets>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushSwitchBot, &QAbstractButton::released, this, &MainWindow::onSwitchBot);
connect(ui->pushGo, &QAbstractButton::released, this, &MainWindow::onGo);
connect(&m_Thread, &WorkerThread::ready, this, &MainWindow::onMoveReady);
ui->webView->settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
ui->webView->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onMoveReady(int x, int y, QString State)
{
QWebFrame* pFrame = ui->webView->page()->mainFrame();
// Get current world state
QString CurState = pFrame->evaluateJavaScript("gridToString()").toString();
// Check if state changed while we were computing this move
if (CurState != State)
return;
// Place new cell
pFrame->evaluateJavaScript(QString("place(%1, %2)").arg(x).arg(y));
}
void MainWindow::timerEvent(QTimerEvent *)
{
if (m_Thread.isRunning())
return;
QWebFrame* pFrame = ui->webView->page()->mainFrame();
int NumCells = pFrame->evaluateJavaScript("app.game.playerManager.localPlayer.cells").toInt();
if (NumCells < 1)
return;
// Define some javascript functions
// FIXME: we need to do it only once
QFile Script(":/script.js");
Script.open(QFile::ReadOnly);
QString ScriptContent = QTextStream(&Script).readAll();
pFrame->evaluateJavaScript(ScriptContent);
// Get current world state
QString State = pFrame->evaluateJavaScript("gridToString()").toString();
// Search for best move in separate thread
m_Thread.setState(State);
m_Thread.start(QThread::IdlePriority);
}
void MainWindow::onSwitchBot()
{
m_BotIsRunning = !m_BotIsRunning;
if (m_BotIsRunning)
{
ui->pushSwitchBot->setText("Stop bot");
m_iTimer = startTimer(300);
}
else
{
ui->pushSwitchBot->setText("Start bot");
killTimer(m_iTimer);
}
}
void MainWindow::onGo()
{
ui->webView->load(QUrl(ui->lineAddress->text()));
}