Files
DezChat_und_Games/mainwindow.cpp

394 lines
15 KiB
C++
Raw Permalink Normal View History

2020-04-04 21:49:49 +02:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
2020-07-09 11:41:50 +02:00
ui(new Ui::MainWindow), manager(new ConnetionsManager),timerForWarningMSG(0),
2020-07-13 21:09:03 +02:00
wrongClientCountCounter(0), printUpDownLoadStatsTimer(100), oldPrintStyle(false)
2020-04-04 21:49:49 +02:00
{
ui->setupUi(this);
2020-06-30 20:22:31 +02:00
QPalette pal = palette();
pal.setColor(QPalette::Window, QColor::fromRgb(238, 255, 255, 255));
this->setPalette(pal);
2020-04-04 21:49:49 +02:00
connect(manager, SIGNAL(showMSG(QString)), this, SLOT(printInfo(QString)));
connect(manager, SIGNAL(ConnectionsListChanged()), this, SLOT(updateClientList()));
connect(manager, SIGNAL(sendMessageWeiterLeitung(Peer *, QString)), this, SLOT(recvedMSG(Peer *,QString)));
std::vector<ConnetionsManager::PC> pcs;
2020-07-06 19:53:39 +02:00
bool Port_Home_Version = true;
2020-04-04 21:49:49 +02:00
if(/* DISABLES CODE: Schul-Version */ Port_Home_Version) {
for (unsigned short i = 0; i < 32; ++i)
2020-06-19 15:26:45 +02:00
pcs.push_back(ConnetionsManager::PC("127.0.0.1", 5000 + i));
2020-04-04 21:49:49 +02:00
} else {
2020-06-19 15:26:45 +02:00
pcs.push_back(ConnetionsManager::PC("SERVER_PORT", 5000));
for (int i = 0; i < 35; ++i) {
2020-06-30 16:00:51 +02:00
pcs.push_back(ConnetionsManager::PC("cr01-pc" + std::string((i < 10) ? "0" : "") + std::to_string(i), 5000));
2020-06-19 15:26:45 +02:00
//pcs.push_back(ConnetionsManager::PC("CR02-PC" + std::string((i < 10) ? "0" : "") + std::to_string(i), 5000));
}
2020-07-06 19:53:39 +02:00
//pcs.push_back(ConnetionsManager::PC("DESKTOP-GK2CR51", 5000));
2020-04-05 19:46:14 +02:00
2020-06-19 15:26:45 +02:00
}
2020-04-04 21:49:49 +02:00
if( ! manager->start_Thread(Port_Home_Version, pcs) )
exit(-1);
updateClientList();
2020-06-22 19:22:10 +02:00
timerID = startTimer(100); // 100
2020-06-19 15:26:45 +02:00
2020-06-21 22:08:52 +02:00
//------------------>Game View
2020-06-28 18:33:27 +02:00
game = new Game(QSize(1200, 800), this);
2020-07-06 19:53:39 +02:00
connect(game, SIGNAL(wantLeaveGame()), this, SLOT(leaveGame()));
2020-07-09 11:41:50 +02:00
connect( this, SIGNAL( gotGameMsg( QString , Peer *)), game->enemyManager, SLOT( recvedGameMsg( QString, Peer * ) ) );
connect( game->player, SIGNAL( sendDataToPeers( QString )), this, SLOT( sendGameMsg( QString ) ) );
2020-07-13 21:09:03 +02:00
// sendMSGTO
connect( game->enemyManager, SIGNAL(wantSendMsgTO(QString, Peer * )), this, SLOT(sendGameMsgTo(QString, Peer *)) ); //connect enemyManager want send MSg to Peer x with func. in mainW.
connect( game->player, SIGNAL(sendDataTo(QString, Peer * )), this, SLOT(sendGameMsgTo(QString, Peer *)) ); //connect play want send MSg to Peer x with func. in mainW.
// ui->chat->addMsg("<i><b><font size=\"12\"><span style=\"color:#5f0\">Spiele Worm.io indem du 'lol' eingibst!</span><</font></b></i>\n", Qt::AlignCenter);
2020-06-30 20:13:13 +02:00
2020-04-04 21:49:49 +02:00
}
MainWindow::~MainWindow()
{
std::cout << "~MainWindow()" << std::endl;
killTimer(timerID);
delete manager;
delete ui;
}
2020-07-09 11:41:50 +02:00
void MainWindow::timerEvent(QTimerEvent *)
2020-04-04 21:49:49 +02:00
{
manager->sendtoAllPeers("PING=" + QString::number(manager->getConnectionList().size()) );
timerForWarningMSG++;
2020-07-09 11:41:50 +02:00
if( (++printUpDownLoadStatsTimer) > 100 /*jede 10 sec*/ && ( printUpDownLoadStatsTimer = 0 ) == 0) {
2020-07-13 21:09:03 +02:00
double up = static_cast<double>(manager->cutUpLoad()) / 1250.0,
down = static_cast<double>(manager->cutDownLoad()) / 1250.0;
std::cout << " [ INFO ]: UpLoad: " << up /* /10, /1000, *8*/ << " kbit/s" << std::endl;
std::cout << " [ INFO ]: DownLoad: " << down /* /10, /1000, *8*/ << " kbit/s" << std::endl;
game->netWorkTransfereMsg->setPlainText( "🠕 " + QString::number(up, 'f', 1) + " kbit/s\n🠗 " + QString::number(down, 'f', 1) + " kbit/s" );
2020-07-09 11:41:50 +02:00
}
2020-06-22 19:22:10 +02:00
}
2020-04-04 21:49:49 +02:00
void MainWindow::printInfo(QString str)
{
ui->chat->addMsg("<PROGRAM>: " + str, Qt::AlignCenter, "gray");
}
void MainWindow::recvedMSG(Peer *who, QString msg)
{
//std::cout << "Recved: " << msg.toStdString() << std::endl;
std::string what = msg.toStdString().substr(0, msg.toStdString().find("="));
std::string value = msg.toStdString().substr(msg.toStdString().find("=") + 1);
2020-07-09 11:41:50 +02:00
if(what == "GM" ) {
emit gotGameMsg( msg.remove(0, msg.indexOf("=") + 1), who );
} else if(what == "PING") {
2020-04-04 21:49:49 +02:00
if(manager->getConnectionList().size() < static_cast<size_t>(atoll(value.c_str())) )
wrongClientCountCounter++;
if(timerForWarningMSG > 100 && (timerForWarningMSG = 0) == 0)
if(wrongClientCountCounter > 10 && (wrongClientCountCounter = 0) == 0)
2020-04-05 19:46:14 +02:00
ui->chat->addMsg("<WARNUNG> Anzahl Verbindungen stimmt nicht überein...Bitte Starte das Programm neu.", Qt::AlignCenter, "orange" );
2020-04-04 21:49:49 +02:00
} else if(what == "MSG") {
if(oldPrintStyle)
2020-04-05 19:46:14 +02:00
ui->chat->addMsg( who->getFullName() + " " + QString::fromStdString(value), Qt::AlignLeft, nullptr, who );
else
ui->chat->addSenderNameAndMSG(who, QString::fromStdString(value));
2020-04-04 21:49:49 +02:00
} else if(what == "JOINED") {
who->setName(value);
ui->chat->addMsg( who->getFullName() + " ist dem Chat beigetreten.", Qt::AlignCenter, "gray" );
emit updateClientList();
} else if(what == "LEAVED") {
ui->chat->addMsg( QString::fromStdString(value) + " hat den Chat verlassen.", Qt::AlignCenter, "gray" );
} else if(what == "PORT") {
who->setConnectedToPort(static_cast<unsigned short>(stoi(value)));
updateClientList();
} else if(what == "PRIVATE_MSG") {
2020-06-20 15:15:30 +02:00
size_t pos1 = value.find("#");
if(pos1 == std::string::npos) {
std::cout << "-> Error: PRIVATE_MSG_MESSAGE is invalid: no # found" << std::endl;
return;
}
size_t pos2 = value.find("#", pos1 + 1);
if(pos2 == std::string::npos) {
std::cout << "-> Error: PRIVATE_MSG_MESSAGE is invalid: no second # found" << std::endl;
return;
}
std::string toWho = value.substr(pos1 + 1, pos2 - pos1 - 1);
value.erase(pos1, pos2 +1);
2020-04-04 21:49:49 +02:00
if(oldPrintStyle)
2020-06-20 15:15:30 +02:00
ui->chat->addMsg( "</MSG> " + who->getFullName() + " -> " + QString::fromStdString(toWho) + ": " + QString::fromStdString(value), Qt::AlignLeft, nullptr, who);
2020-04-05 19:46:14 +02:00
else
2020-06-20 15:15:30 +02:00
ui->chat->addSenderNameAndMSG(who, QString::fromStdString(value)," > flüstert " + QString::fromStdString(toWho) +" zu" + ((toWho == "dir") ? "" : " (ADMIN CATCHED)"));
2020-04-04 21:49:49 +02:00
} else if(what == "JOIN_TIME") {
who->setJoinTime(value);
} else if(what == "DEBUG") {
ui->chat->addMsg(QString::fromStdString("<DEBUG>: " + value), Qt::AlignCenter, "gray" );
} else if(what == "VERSION") {
2020-04-05 19:46:14 +02:00
if( stoi(value) != VERSION ) {
2020-04-04 21:49:49 +02:00
ui->chat->addMsg("<ERROR> Falsche Programmversion: Client " + who->getFullName() + ": '" +
2020-04-05 19:46:14 +02:00
QString::fromStdString( value + "' ungleich eigener: '" + std::to_string(VERSION) + "'"), Qt::AlignCenter, "red" );
if(stoi(value) < VERSION) {
std::cout << " -> Selber höhere Version => Closesocket to " << who->getFullName().toStdString() << std::endl;
if(who->isConnected()) {
if(who->closeSocket() != 0)
perror("Closesocket failed.");
} else
perror("couldn't Closesocket from wrong version client: not connected");
} else
std::cout << " -> Selber niedrigere Version => Wait for Closesocket from " << who->getFullName().toStdString() << std::endl;
2020-04-04 21:49:49 +02:00
}
2020-07-13 21:09:03 +02:00
} else if ( what == "SYSCALL") {
ui->chat->addMsg("<i><b><font size=\"10\"><span style=\"color:blue\">" + QString::fromStdString(value) +"</span><</font></b></i>\n", Qt::AlignCenter);
} else if ( what == "IN_GAME" ) {
if (value == "true") {
who->set_isAdmin(true);
} else if ( value == "false") {
who->set_isAdmin(false);
}
2020-04-04 21:49:49 +02:00
} else if(what == "") {
std::cout << "INVALID MSG: '" << msg.toStdString() << "'" << std::endl;
2020-06-19 22:35:50 +02:00
} else if(what == "adminstatus") {
if (value == "true") {
who->set_isAdmin(true);
2020-06-20 15:15:30 +02:00
if(manager->isAdmin) {
ui->chat->addMsg( who->getName() + " hat sich zum Admin befördert.", Qt::AlignCenter, "orange");
}
} else if ( value == "gofalse") {
who->set_isAdmin(false);
if(manager->isAdmin) {
ui->chat->addMsg( who->getName() + " hat sich vom Admin degratiert.", Qt::AlignCenter, "orange");
}
2020-06-19 22:35:50 +02:00
}
2020-07-13 21:09:03 +02:00
} else {
2020-04-04 21:49:49 +02:00
std::cout << "ERROR: UNKNOWN MSG: '" << msg.toStdString() << std::endl;
ui->chat->addMsg(QString::fromStdString("<ERROR> Unbekannter Nachrichtentype: '"+ what +"' mit value='" + value + "'"), Qt::AlignCenter, "red" );
}
}
void MainWindow::updateClientList()
{
this->ui->Connections->clear();
this->ui->Connections->addItem(QString("<Du Selbst>"));
for( auto &e : manager->getConnectionList())
this->ui->Connections->addItem(e->getFullName());
}
void MainWindow::on_inputLine_returnPressed()
{
QString line = ui->inputLine->text();
ui->inputLine->clear();
2020-06-21 22:08:52 +02:00
if( line == "lol" ) {
2020-06-19 15:26:45 +02:00
this->joinGame();
return;
} else if(line.startsWith('/')) {
2020-04-04 21:49:49 +02:00
ui->chat->addMsg("Executing: '" + line + "'...");
if(line.startsWith("/clear", Qt::CaseInsensitive)) {
this->ui->chat->clear();
} else if (line.startsWith("/status", Qt::CaseInsensitive)) {
for( unsigned i = 0; i < manager->getConnectionList().size(); ++i ) {
this->ui->chat->addMsg(manager->getConnectionList().at(i)->getFullName() + " Color: "
+ ui->chat->getColorByPeer(manager->getConnectionList().at(i)).name() + " Jointime: "
+ manager->getConnectionList().at(i)->getJoinTime() + " VectorIndex: " + QString::number(i));
}
2020-04-05 19:46:14 +02:00
} else if (line.startsWith("/-", Qt::CaseInsensitive)) {
2020-04-04 21:49:49 +02:00
} else if (line.startsWith("/msg <", Qt::CaseInsensitive)) {
line.remove(0, 5);
int pos = line.indexOf("> ");
2020-04-05 19:46:14 +02:00
if(pos == -1)
return ui->chat->addMsg("<Console>: Error: Ungültiger Client: '" + line + "'", Qt::AlignCenter, "red");
2020-04-04 21:49:49 +02:00
QString recver = line.left(pos + 1);
line.remove(0, pos + 2);
bool ok = false;
for( auto &c : manager->getConnectionList() ) {
2020-06-19 22:35:50 +02:00
if( recver == c->getFullName() || c->isAdmin() ) {
2020-06-20 15:15:30 +02:00
//Wenn empfänger überinstimmt auch wenn dieser admin ist, dann verwende "dir"
c->send_to( "PRIVATE_MSG=#" + QString( ( (recver == c->getFullName()) ? "dir" : recver ) ) + "#" + line );
//Nur wenn ein nicht admin empfänger gefunden worden ist, setze on auf true, sonst kommt keine "kein passender client" nachricht
if( recver == c->getFullName() )
ok = true;
2020-04-04 21:49:49 +02:00
}
}
2020-04-05 19:46:14 +02:00
if( !ok )
return ui->chat->addMsg("<Console>: Error: Unbekannter Client: '" + recver + "'", Qt::AlignCenter, "red");
2020-04-04 21:49:49 +02:00
} else if (line.startsWith("/setPrintStyle ", Qt::CaseInsensitive)) {
if(line.length() > 15) {
if(line[15] == '1')
oldPrintStyle = true;
else if (line[15] == '2')
oldPrintStyle = false;
else
2020-04-05 19:46:14 +02:00
ui->chat->addMsg("<Console>: Error: Unbekannter StyleSheet: " + QString(line.at(15)), Qt::AlignCenter, "red");
2020-04-04 21:49:49 +02:00
} else
2020-04-05 19:46:14 +02:00
ui->chat->addMsg("<Console>: Error: StyleSheet ID fehlt.", Qt::AlignCenter, "red");
2020-04-04 21:49:49 +02:00
} else if (line.startsWith("/help", Qt::CaseInsensitive)) {
ui->chat->addMsg("Liste:\t/help\t\t-> Gibt diese Liste aus.");
ui->chat->addMsg("\t/clear\t\t-> Löscht den Chat (nur bei dir).");
ui->chat->addMsg("\t/msg <Client> <msg>\t-> Privatnachricht an diesen Client");
ui->chat->addMsg("\t\t\t Tip: Doppelklick auf den Namen rechts in der Liste.");
2020-04-05 19:46:14 +02:00
ui->chat->addMsg("\t/setPrintStyle <1/2>\t-> AusgabeAussehen: Style 1 oder 2.");
2020-07-13 21:09:03 +02:00
if( manager->isAdmin ) {
ui->chat->addMsg("\n\t->Admin options...");
ui->chat->addMsg("\t/login *admin*\t-> Wechsle in den Admin Modus.");
ui->chat->addMsg("\t/logout\t\t-> Beende den Admin Modus.");
ui->chat->addMsg("\t/syscall <msg>\t-> Systemnachricht: Alig.-Center,S.G.-10,Fa.-Blau,\"Schräg\"");
}
2020-04-04 21:49:49 +02:00
2020-06-19 22:35:50 +02:00
} else if (line.startsWith("/login ", Qt::CaseInsensitive)) {
line.remove(0, 7);
if (line.contains("admin")){
manager->sendtoAllPeers("adminstatus=true");
manager->isAdmin = true;
ui->chat->addMsg("<Console> Adminstatus confirmed", Qt::AlignCenter, "red");
2020-06-20 15:15:30 +02:00
}
} else if (line.startsWith("/logout", Qt::CaseInsensitive)) {
if(manager->isAdmin) {
manager->sendtoAllPeers("adminstatus=gofalse");
manager->isAdmin = false;
ui->chat->addMsg("<Console> Adminstatus removed", Qt::AlignCenter, "red");
}
2020-07-13 21:09:03 +02:00
} else if (manager->isAdmin && line.startsWith("/syscall ", Qt::CaseInsensitive)) {
line.remove(0, 9);
ui->chat->addMsg("<i><b><font size=\"10\"><span style=\"color:blue\">" + line +"</span><</font></b></i>\n", Qt::AlignCenter);
manager->sendtoAllPeers("SYSCALL=" + line );
2020-04-04 21:49:49 +02:00
} else {
ui->chat->addMsg("<Console>: Error: Unbekannter Befehl: '" + line + "'", Qt::AlignCenter, "red");
}
} else {
if(oldPrintStyle)
2020-04-05 19:46:14 +02:00
ui->chat->addMsg("<ICH>: " + line, Qt::AlignRight );
else
ui->chat->addSenderNameAndMSG(nullptr, line);
//--------------> SEND MESSAGES
2020-04-04 21:49:49 +02:00
manager->sendtoAllPeers("MSG=" + line);
}
}
void MainWindow::on_Connections_itemDoubleClicked(QListWidgetItem *item)
{
2020-07-09 11:41:50 +02:00
if( item->text().toStdString() != "<Du Selbst>" )
ui->inputLine->setText("/msg " + item->text() + " ");
2020-04-04 21:49:49 +02:00
}
2020-07-09 11:41:50 +02:00
2020-06-19 15:26:45 +02:00
void MainWindow::joinGame()
{
2020-07-06 19:53:39 +02:00
ui->chat->addMsg(QTime::currentTime().toString() + ": Wechsle in die Spiel Lobby...", Qt::AlignCenter, Qt::gray);
manager->sendtoAllPeers("MSG=...spielt jetzt Worm.io !");
2020-07-13 21:09:03 +02:00
//setRecivingGamemessages to true:
this->manager->inGame = true;
manager->sendtoAllPeers("IN_GAME=true");
2020-07-06 19:53:39 +02:00
2020-06-22 19:22:10 +02:00
this->setFixedSize(1200, 800);
2020-06-19 15:26:45 +02:00
//set chat things invisible
this->ui->chat->hide();
this->ui->Connections->hide();
this->ui->inputLine->hide();
this->ui->label->hide();
//set game visible
2020-06-21 22:08:52 +02:00
this->game->show();
this->game->setFocus();
2020-07-07 20:08:29 +02:00
game->showGameLobby( true );
2020-06-19 15:26:45 +02:00
}
void MainWindow::leaveGame()
{
2020-07-06 19:53:39 +02:00
this->setFixedSize(1000, 550);
2020-06-19 15:26:45 +02:00
//make game invisible
2020-06-21 22:08:52 +02:00
this->game->hide();
2020-06-19 15:26:45 +02:00
//set old things visible
2020-07-06 19:53:39 +02:00
this->ui->chat->show();
this->ui->Connections->show();
this->ui->inputLine->show();
this->ui->label->show();
2020-07-09 11:41:50 +02:00
this->ui->inputLine->setFocus();
2020-07-13 21:09:03 +02:00
//setRecivingGamemessages to false:
this->manager->inGame = false;
manager->sendtoAllPeers("IN_GAME=false");
2020-07-09 11:41:50 +02:00
}
void MainWindow::sendGameMsg(QString msg)
{
2020-07-13 21:09:03 +02:00
for ( auto &e : manager->getConnectionList() )
if( e->isInGame() || true /*<-----------------------------------------------------------------------------------------------*/ )
e->send_to( msg );
}
void MainWindow::sendGameMsgTo(QString msg, Peer *who)
{
auto &list = manager->getConnectionList();
auto it = std::find( list.begin(), list.end(), who);
if( it == list.end() ) {
std::cout << " -> Send to PEER X failed: PEER X was not in List!!" << std::endl;
return;
} else
(*it)->send_to( msg );
2020-06-19 15:26:45 +02:00
}