This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
SDi-STOMP/app.cpp

92 lines
2.2 KiB
C++
Raw Normal View History

2023-11-29 11:53:20 +01:00
#include "app.h"
2023-12-26 18:10:52 +01:00
App::App(Stomp* st) {
st_ = st;
st_->connectRequest("sdi.hevs.ch", 61614, "/", "sdi10", "809c02f36becb0868da98761fe3209f6");
st_->sendRequest("/topic/sdi10.gem.command", "right");
st_->subscribeRequest("/topic/sdi10.gem.field", 1);
2023-11-29 11:53:20 +01:00
}
2023-11-30 09:53:53 +01:00
2023-12-26 18:10:52 +01:00
void App::connectConfirmation(bool success, QString version) {
2023-11-30 09:53:53 +01:00
}
void App::sendConfirmation(bool success) {
}
void App::subscribeConfirmation(bool success) {
2023-12-26 18:10:52 +01:00
st_->sendRequest("/topic/sdi10.gem.command", "up");
2023-11-30 09:53:53 +01:00
}
void App::receiveIndication(int id, QString destination, QString body) {
2023-12-26 18:10:52 +01:00
//qDebug() << "Indication " << id << " : " << destination << Qt::endl << body << Qt::endl;
if(destination.contains("field")){
fillField(body);
}
2023-12-26 22:09:42 +01:00
computeMove();
2023-11-30 09:53:53 +01:00
}
void App::disconnectConfirmation() {
}
void App::disconnectIndication() {
}
2023-12-26 18:10:52 +01:00
void App::addGem(int x, int y, int pts) {
Gem g;
g.x = x;
g.y = y;
g.pts = pts;
2023-12-26 22:09:42 +01:00
g.relativePts = computeRelativePts(g);
2023-12-26 18:10:52 +01:00
gems_.append(g);
}
2023-12-26 22:09:42 +01:00
void App::printGem() {
for (Gem g: gems_) {
qDebug() << g.x << ":" << g.y << " - " << g.pts << Qt::endl;
}
}
2023-12-26 18:10:52 +01:00
void App::fillField(QString body) {
2023-12-26 22:09:42 +01:00
int x = 0;
int y = 0;
gems_.clear();
2023-12-26 18:10:52 +01:00
for(int i = 0; i<body.length(); i++) {
const QChar c = body.at(i);
if(c == 'Y') {
myVehicle_.x = x;
myVehicle_.y = y;
myVehicle_.me = true;
}
2023-12-26 22:09:42 +01:00
if(c == 'g') addGem(x, y, 100);
if(c == 'G') addGem(x, y, 250);
if(c == 'D') addGem(x, y, 500);
2023-12-26 18:10:52 +01:00
if(c == '\n') {
y++;
x = 0;
2023-12-26 22:09:42 +01:00
} else {
x++;
2023-12-26 18:10:52 +01:00
}
}
}
2023-12-26 22:09:42 +01:00
QVector2D App::computeRelativePts(Gem g) {
QVector2D v = QVector2D(myVehicle_.x, myVehicle_.y) - QVector2D(g.x, g.y);
}
void App::computeMove() {
Gem myGem;
for(Gem g : gems_) {
if(g.pts > myGem.pts) myGem = g;
}
if(myGem.x>myVehicle_.x) st_->sendRequest("/topic/sdi10.gem.command", "right");
else if(myGem.x<myVehicle_.x) st_->sendRequest("/topic/sdi10.gem.command", "left");
else if(myGem.y>myVehicle_.y) st_->sendRequest("/topic/sdi10.gem.command", "down");
else if(myGem.y<myVehicle_.y) st_->sendRequest("/topic/sdi10.gem.command", "up");
}