summaryrefslogtreecommitdiff
path: root/quacker/macondo.cpp
blob: eda49e3048b55d6712803412b3d5e62d14d018b3 (plain)
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
#include "macondo.h"
#include "quacker.h"

#include <QGridLayout>
#include <QPushButton>
#include <QProcess>
#include <QTimer>
#include <random>

Macondo::Macondo(TopLevel *topLevel) : QWidget() {
	m_topLevel = topLevel;
	m_updateTimer = new QTimer(this);
	QGridLayout *layout = new QGridLayout(this);
	m_runButton = new QPushButton(tr("Run"));
	layout->addWidget(m_runButton, 0, 0);
	layout->setAlignment(Qt::AlignTop);
	const char *home = getenv("HOME");
	// TODO: configurable path
	m_execPath = home ? home : "/";
	m_execPath += "/apps/macondo/macondo";
	connect(m_runButton, SIGNAL(clicked()), this, SLOT(run()));
	connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateResults()));
	m_updateTimer->setInterval(100);
}

void Macondo::run() {
	if (m_process) return;
	printf("running macondo %s\n", m_execPath.c_str());
	m_process = new QProcess;
	QStringList args;
	m_process->start(m_execPath.c_str(), args);
	connect(m_process, SIGNAL(started()), this, SLOT(processStarted()));
}

void Macondo::processStarted() {
	m_updateTimer->start();
	std::default_random_engine rand;
	std::uniform_int_distribution<int> distribution(0, 26);
	// save game file with random name
	char filename[] = "tmpGameXXXXXXXXXXXX.gcg";
	for (int i = 0; filename[i]; i++) {
		if (filename[i] == 'X') {
			filename[i] = distribution(rand) + 'A';
		}
	}
	m_topLevel->writeFile(filename);
	std::string command = "load ";
	command += filename;
	command += "\n";
	m_process->write(command.c_str());
}

void Macondo::updateResults() {
	QProcess::ProcessState state = m_process->state();
	QByteArray data = m_process->readAllStandardError();
	printf("%s",data.constData());
	data = m_process->readAllStandardOutput();
	printf("%s", data.constData());
	fflush(stdout);
}