-
Notifications
You must be signed in to change notification settings - Fork 3
/
opponentjs.cpp
203 lines (177 loc) · 7.39 KB
/
opponentjs.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* \file opponentjs.cpp
*
* \section LICENSE
*
* Copyright (C) 2015-present Thorsten Roth
*
* This file is part of StackAndConquer.
*
* StackAndConquer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* StackAndConquer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with StackAndConquer. If not, see <https://www.gnu.org/licenses/>.
*
* \section DESCRIPTION
* Interface to CPU script JS engine.
*/
#include "./opponentjs.h"
#include <QDebug>
#include <QFile>
#include <QJSEngine>
#include <QMessageBox>
OpponentJS::OpponentJS(QWidget *pParent, const quint8 nID,
const QPoint BoardDimensions,
const quint8 nHeightTowerWin, const quint8 nNumOfPlayers,
const QString &sOut, const QString &sPad,
QObject *pParentObj)
: m_pParent(pParent),
m_nID(nID),
m_BoardDimensions(BoardDimensions),
m_nHeightTowerWin(nHeightTowerWin),
m_nNumOfPlayers(nNumOfPlayers),
m_sOut(sOut),
m_sPad(sPad),
m_jsEngine(new QJSEngine(pParentObj)) {
m_obj = m_jsEngine->globalObject();
m_obj.setProperty(QStringLiteral("game"), m_jsEngine->newQObject(this));
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto OpponentJS::loadAndEvalCpuScript(const QString &sFilepath,
const QJsonArray &emptyBoard) -> bool {
QFile f(sFilepath);
if (!f.open(QFile::ReadOnly)) {
qWarning() << "Couldn't open JS file:" << sFilepath;
return false;
}
QString source = QString::fromUtf8(f.readAll());
f.close();
this->log("Script: " + sFilepath);
QJSValue result(m_jsEngine->evaluate(source, sFilepath));
if (result.isError()) {
qCritical().noquote()
<< "Error in CPU P" + QString::number(m_nID) + "script at line " +
result.property(QStringLiteral("lineNumber")).toString() +
"\n " + result.toString();
emit scriptError();
return false;
}
// Check if callCPU() is available for calling the script
if (!m_obj.hasProperty(QStringLiteral("callCPU")) ||
!m_obj.property(QStringLiteral("callCPU")).isCallable()) {
qCritical() << "Error in CPU P" + QString::number(m_nID) +
"script - function callCPU() not found or not callable!";
emit scriptError();
return false;
}
// Call (optional) init() function if available
if (m_obj.hasProperty(QStringLiteral("initCPU")) &&
m_obj.property(QStringLiteral("initCPU")).isCallable()) {
QJsonDocument jsdoc(emptyBoard);
QString sJsonEmptyBoard(
QString::fromLatin1(jsdoc.toJson(QJsonDocument::Compact)));
QJSValueList arguments;
arguments << sJsonEmptyBoard;
result = m_obj.property(QStringLiteral("initCPU")).call(arguments);
if (result.isError()) {
qCritical().noquote()
<< "CPU P" + QString::number(m_nID) +
"- Error calling \"initCPU\" function at line: " +
result.property(QStringLiteral("lineNumber")).toString() +
"\n " + result.toString();
QMessageBox::warning(m_pParent, tr("Warning"),
tr("CPU script execution error! "
"Please check the debug log."));
emit scriptError();
return false;
}
}
return true;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void OpponentJS::callJsCpu(const QJsonArray &board,
const QJsonDocument &legalMoves,
const QJsonArray &towersNeededToWin,
const QJsonArray &stonesLeft,
const QJsonArray &lastMove) {
QJsonDocument jsdoc(board);
QString sJsonBoard(QString::fromLatin1(jsdoc.toJson(QJsonDocument::Compact)));
QJSValueList arguments;
arguments << sJsonBoard;
m_legalMoves = legalMoves;
m_TowersNeededToWin = towersNeededToWin;
m_StonesLeft = stonesLeft;
m_LastMove = lastMove;
QJSValue result = m_obj.property(QStringLiteral("callCPU")).call(arguments);
// qDebug() << "Result of callCPU(): " + result.toString();
if (result.isError()) {
qCritical().noquote()
<< "CPU P" + QString::number(m_nID) +
"- Error calling \"callCPU\" function at line: " +
result.property(QStringLiteral("lineNumber")).toString() +
"\n " + result.toString();
QMessageBox::warning(m_pParent, tr("Warning"),
tr("CPU script execution error! "
"Please check the debug log."));
emit scriptError();
return;
}
// CPU has to return an int array with length 3
QJsonArray move;
if (result.isArray()) {
if (3 == result.property(QStringLiteral("length")).toInt()) {
if (result.property(0).isNumber() && // From (-1 --> set stone at "To")
result.property(1).isNumber() && // Number of stones
result.property(2).isNumber()) { // To
move << result.property(0).toInt() << result.property(1).toInt()
<< result.property(2).toInt();
if (move.at(0).toInt() >= -1 && move.at(0).toInt() < board.size() &&
move.at(1).toInt() > 0 && move.at(2).toInt() >= 0 &&
move.at(2).toInt() < board.size()) {
emit actionCPU(move);
return;
}
}
}
}
qCritical() << "CPU P" + QString::number(m_nID) +
"script invalid return from callCPU(): " +
result.toString();
QMessageBox::warning(m_pParent, tr("Warning"),
tr("CPU script execution error! "
"Please check the debug log."));
emit scriptError();
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Functions accessible by external JavaScript
auto OpponentJS::getID() -> quint8 { return m_nID; }
auto OpponentJS::getNumOfPlayers() -> quint8 { return m_nNumOfPlayers; }
auto OpponentJS::getHeightToWin() -> quint8 { return m_nHeightTowerWin; }
auto OpponentJS::getTowersNeededToWin() -> QJsonArray {
return m_TowersNeededToWin;
}
auto OpponentJS::getNumberOfStones() -> QJsonArray { return m_StonesLeft; }
auto OpponentJS::getLastMove() -> QJsonArray { return m_LastMove; }
auto OpponentJS::getLegalMoves() -> QString {
QString sJsonMoves(
QString::fromLatin1(m_legalMoves.toJson(QJsonDocument::Compact)));
return sJsonMoves;
}
auto OpponentJS::getBoardDimensionX() -> int { return m_BoardDimensions.x(); }
auto OpponentJS::getBoardDimensionY() -> int { return m_BoardDimensions.y(); }
auto OpponentJS::getOutside() -> QString { return m_sOut; }
auto OpponentJS::getPadding() -> QString { return m_sPad; }
void OpponentJS::log(const QString &sMsg) {
qDebug() << "CPU P" + QString::number(m_nID) + " - " + sMsg;
}