Skip to content

Commit

Permalink
Reset Button
Browse files Browse the repository at this point in the history
  • Loading branch information
zsmn committed Nov 11, 2020
1 parent 2d5d342 commit 73095a2
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 8 deletions.
1 change: 1 addition & 0 deletions VSS-Referee.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ RCC_DIR = tmp/rc
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += PROJECT_PATH=\\\"$${PWD}\\\"
LIBS *= -lprotobuf -lGLU -pthread -lQt5Core -lpthread

# Compiling protobuf
Expand Down
7 changes: 6 additions & 1 deletion constants/constants.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include "constants.h"

Constants::Constants(QString fileName){
_fileName = fileName;
loadConstants();
}

void Constants::loadConstants(){
// Opening file and setting initial config
QFile file(fileName);
QFile file(_fileName);
QString args;
file.open(QIODevice::ReadOnly | QIODevice::Text);

Expand Down
2 changes: 2 additions & 0 deletions constants/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Constants
{
public:
Constants(QString fileName);
QString _fileName;
void loadConstants();

/// Getters

Expand Down
5 changes: 3 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv);

/// Creating constants pointer
Constants* constants = new Constants(":/constants/constants.json");
Constants* constants = new Constants(QString(PROJECT_PATH) + "/constants/constants.json");
Utils::setConstants(constants);

/// Parsing from constants
Expand Down Expand Up @@ -42,7 +42,7 @@ int main(int argc, char *argv[])
VSSVisionClient *vssVisionClient = new VSSVisionClient(visionAddress, visionPort);
VSSReferee *vssReferee = new VSSReferee(vssVisionClient, refereeAddress, refereePort, constants);
VSSReplacer *vssReplacer = new VSSReplacer(replacerAddress, replacerPort, firaSimAddress, firaSimPort, constants);
RefereeView *refView = new RefereeView();
RefereeView *refView = new RefereeView(constants);

/// Make connections with signals and slots
// Foul connetion
Expand All @@ -62,6 +62,7 @@ int main(int argc, char *argv[])

// Manual referee connection
QObject::connect(refView->getUI(), SIGNAL(sendManualCommand(VSSRef::Foul, VSSRef::Color, VSSRef::Quadrant)), vssReferee, SLOT(takeManualCommand(VSSRef::Foul, VSSRef::Color, VSSRef::Quadrant)));
QObject::connect(refView->getUI(), SIGNAL(resetAll()), vssReferee, SLOT(resetAll()));

/// Set team
// Command line parser, get arguments
Expand Down
44 changes: 43 additions & 1 deletion src/entity/refereeview/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#include <QStyle>
#include <QStyleFactory>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
MainWindow::MainWindow(QWidget *parent, Constants *constants) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);

_constants = constants;

// Dark theme
this->setStyle(QStyleFactory::create("Fusion"));
QPalette darkPalette;
Expand Down Expand Up @@ -70,6 +72,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
connect(ui->startGame, SIGNAL(released()), this, SLOT(sendStart()));
connect(ui->stopGame, SIGNAL(released()), this, SLOT(sendStop()));

// Reset game
connect(ui->buttonReset, SIGNAL(released()), this, SLOT(reloadAll()));

// Setting initial scores values and colors
leftTeamGoalsScored = 0;
rightTeamGoalsScored = 0;
Expand Down Expand Up @@ -237,3 +242,40 @@ void MainWindow::sendStart(){
void MainWindow::sendStop(){
emit sendManualCommand(VSSRef::Foul::STOP);
}

void MainWindow::reloadAll(){
// Reload all
getConstants()->loadConstants();

// Reset names
leftTeamName = getConstants()->getLeftTeamName();
rightTeamName = getConstants()->getRightTeamName();
ui->teamLeftName->setText(leftTeamName);
ui->teamRightName->setText(rightTeamName);

blueIsLeft = (getConstants()->getLeftTeamColor() == VSSRef::Color::BLUE);

if(blueIsLeft) ui->teamLeftScore->setStyleSheet("QLabel { color : #417EFF; }");
else ui->teamLeftScore->setStyleSheet("QLabel { color : #FFF33E; }");

if(!blueIsLeft) ui->teamRightScore->setStyleSheet("QLabel { color : #417EFF; }");
else ui->teamRightScore->setStyleSheet("QLabel { color : #FFF33E; }");

// Reset scores
leftTeamGoalsScored = 0;
ui->teamLeftScore->setText(QString("%1").arg(leftTeamGoalsScored));

rightTeamGoalsScored = 0;
ui->teamRightScore->setText(QString("%1").arg(rightTeamGoalsScored));

emit resetAll();
}

Constants* MainWindow::getConstants(){
if(_constants == NULL){
std::cout << "[ERROR] Referee is requesting constants, but it's NULL\n";
return NULL;
}

return _constants;
}
9 changes: 8 additions & 1 deletion src/entity/refereeview/mainwindow/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QMainWindow>
#include <src/entity/refereeview/soccerview/soccerview.h>
#include <include/vssref_common.pb.h>
#include <constants/constants.h>

#include <QPushButton>

Expand All @@ -15,7 +16,7 @@ class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent);
MainWindow(QWidget *parent, Constants *constants);
~MainWindow();

// Detection updates
Expand Down Expand Up @@ -47,6 +48,10 @@ class MainWindow : public QMainWindow
int leftTeamGoalsScored;
int rightTeamGoalsScored;

// Constants
Constants *_constants;
Constants* getConstants();

public slots:
void switchSides();
void addGoal(VSSRef::Color team);
Expand All @@ -63,9 +68,11 @@ public slots:
void sendBlueGoalKick();
void sendStart();
void sendStop();
void reloadAll();

signals:
void sendManualCommand(VSSRef::Foul foul, VSSRef::Color teamColor = VSSRef::Color::NONE, VSSRef::Quadrant quadrant = VSSRef::Quadrant::NO_QUADRANT);
void resetAll();
};

#endif // MAINWINDOW_H
13 changes: 13 additions & 0 deletions src/entity/refereeview/mainwindow/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,19 @@
</property>
</widget>
</widget>
<widget class="QPushButton" name="buttonReset">
<property name="geometry">
<rect>
<x>870</x>
<y>220</y>
<width>101</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>Reset Game</string>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
Expand Down
14 changes: 12 additions & 2 deletions src/entity/refereeview/refereeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ QString RefereeView::name(){
return "Referee View";
}

RefereeView::RefereeView()
RefereeView::RefereeView(Constants *constants)
{
_refereeUI = new MainWindow(nullptr);
_constants = constants;
_refereeUI = new MainWindow(nullptr, getConstants());
_refereeUI->show();
}

Expand Down Expand Up @@ -62,3 +63,12 @@ void RefereeView::setTeams(QString teamLeft, VSSRef::Color leftColor, QString te
MainWindow* RefereeView::getUI(){
return _refereeUI;
}

Constants* RefereeView::getConstants(){
if(_constants == NULL){
std::cout << "[ERROR] Referee is requesting constants, but it's NULL\n";
return NULL;
}

return _constants;
}
6 changes: 5 additions & 1 deletion src/entity/refereeview/refereeview.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class RefereeView : public Entity
{
public:
RefereeView();
RefereeView(Constants *constants);
~RefereeView();

QString name();
Expand All @@ -33,6 +33,10 @@ class RefereeView : public Entity

// UI
static MainWindow *_refereeUI;

// Constants
Constants *_constants;
Constants* getConstants();
};

#endif // REFEREEVIEW_H
54 changes: 54 additions & 0 deletions src/entity/vssreferee/vssreferee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,57 @@ void VSSReferee::setGameStartStop(){
setTeamFoul(VSSRef::Foul::STOP, VSSRef::Color::NONE, VSSRef::Quadrant::NO_QUADRANT, true);
_stopTimer.start();
}

void VSSReferee::resetAll(){
// Reset pointers
for(int x = 0; x < 2; x++){
free(time[x]);
free(timers[x]);
}

free(time);
free(timers);

// Reset vars initially
_placementIsSet = false;
_blueSent = false;
_yellowSent = false;
_stopEnabled = false;
_manualStop = false;
_manualGameOn = false;
timePassed = 0;
startedGKTimer = false;
startedStuckTimer = false;
_startedPenaltyTimer = false;
startedDisputateTimer = false;
_gameHalf = VSSRef::Half::FIRST_HALF;

// Start timers
_placementTimer.start();
_gameTimer.start();
_gkTimer.start();
_ballStuckTimer.start();
_ballVelTimer.start();
_stopTimer.start();

// Allocating memory to time and timers (for gk checking)
time = static_cast<float**>(malloc(2 * sizeof(float *)));
for(int x = 0; x < 2; x++){
time[x] = static_cast<float*>(malloc(static_cast<unsigned int>(getConstants()->getQtPlayers()) * sizeof(float)));
}

timers = static_cast<Timer**>(malloc(2 * sizeof(Timer *)));
for(int x = 0; x < 2; x++){
timers[x] = static_cast<Timer*>(malloc(static_cast<unsigned int>(getConstants()->getQtPlayers()) * sizeof(Timer)));
}

// Starting gk checking values and timers
for(int x = 0; x < 2; x++){
for(int y = 0; y < getConstants()->getQtPlayers(); y++){
timers[x][y].start();
time[x][y] = 0.0;
}
}

setGameStartStop();
}
1 change: 1 addition & 0 deletions src/entity/vssreferee/vssreferee.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public slots:
void teamSent(VSSRef::Color color); // if a team sent it packet
void requestGoalie(VSSRef::Color team); // request team goalie
void takeManualCommand(VSSRef::Foul foul, VSSRef::Color color, VSSRef::Quadrant quadrant); // manual referee commands from UI
void resetAll();
};

#endif // VSSREFEREE_H

0 comments on commit 73095a2

Please sign in to comment.