2007-02-22 00:48:18 +00:00
|
|
|
/***************************************************************************
|
2007-09-05 19:14:34 +00:00
|
|
|
* Copyright (C) 2006 by FThauer FHammer LMay *
|
2007-02-22 00:48:18 +00:00
|
|
|
* f.thauer@web.de *
|
|
|
|
|
* *
|
|
|
|
|
* This program 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 2 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program 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 this program; if not, write to the *
|
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
#include "startnetworkgamedialogimpl.h"
|
2007-06-07 19:37:35 +00:00
|
|
|
#include "session.h"
|
2007-06-05 21:56:33 +00:00
|
|
|
#include "configfile.h"
|
2007-08-02 20:52:13 +00:00
|
|
|
#include <net/socket_msg.h>
|
2007-02-22 00:48:18 +00:00
|
|
|
|
2007-06-07 21:19:06 +00:00
|
|
|
startNetworkGameDialogImpl::startNetworkGameDialogImpl(QWidget *parent, ConfigFile *config)
|
2007-10-03 13:36:47 +00:00
|
|
|
: QDialog(parent), myW(NULL), myPlayerId(0), isAdmin(false), myConfig(config), mySession(NULL)
|
2007-02-22 00:48:18 +00:00
|
|
|
{
|
2007-06-07 21:19:06 +00:00
|
|
|
setupUi(this);
|
2007-02-22 00:48:18 +00:00
|
|
|
|
|
|
|
|
connect( pushButton_cancel, SIGNAL( clicked() ), this, SLOT( cancel() ) );
|
|
|
|
|
connect( pushButton_startGame, SIGNAL( clicked() ), this, SLOT( startGame() ) );
|
2007-06-05 20:54:16 +00:00
|
|
|
connect( pushButton_Kick, SIGNAL( clicked() ), this, SLOT( kickPlayer() ) );
|
2007-09-02 16:36:52 +00:00
|
|
|
connect( treeWidget, SIGNAL( currentItemChanged ( QTreeWidgetItem*, QTreeWidgetItem*) ), this, SLOT( playerSelected(QTreeWidgetItem*, QTreeWidgetItem*) ) );
|
2007-09-07 08:58:36 +00:00
|
|
|
connect( lineEdit_ChatInput, SIGNAL( returnPressed () ), this, SLOT( sendChatMessage() ) );
|
|
|
|
|
|
2007-09-04 17:38:13 +00:00
|
|
|
|
|
|
|
|
clearDialog();
|
2007-02-22 00:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-29 22:30:18 +00:00
|
|
|
void startNetworkGameDialogImpl::exec() {
|
2007-09-02 16:36:52 +00:00
|
|
|
|
|
|
|
|
GameInfo info = mySession->getClientGameInfo(0);
|
2007-08-29 22:30:18 +00:00
|
|
|
label_maxPlayerNumber->setText(QString::number(info.data.maxNumberOfPlayers));
|
|
|
|
|
|
|
|
|
|
QDialog::exec();
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-22 00:48:18 +00:00
|
|
|
void startNetworkGameDialogImpl::startGame() {
|
2007-08-02 20:52:13 +00:00
|
|
|
assert(mySession);
|
2007-09-05 19:14:34 +00:00
|
|
|
mySession->sendStartEvent(checkBox_fillUpWithComputerOpponents->isChecked());
|
2007-02-22 00:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void startNetworkGameDialogImpl::cancel() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-02 20:52:13 +00:00
|
|
|
void startNetworkGameDialogImpl::refresh(int actionID) {
|
2007-02-22 00:48:18 +00:00
|
|
|
|
2007-08-02 20:52:13 +00:00
|
|
|
if (actionID == MSG_NET_GAME_CLIENT_START)
|
|
|
|
|
{
|
|
|
|
|
QTimer::singleShot(500, this, SLOT(accept()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
void startNetworkGameDialogImpl::joinedNetworkGame(unsigned playerId, QString playerName, int rights) {
|
2007-08-02 20:52:13 +00:00
|
|
|
|
2007-10-03 13:36:47 +00:00
|
|
|
myPlayerId = playerId;
|
2007-08-02 20:52:13 +00:00
|
|
|
isAdmin = rights == PLAYER_RIGHTS_ADMIN;
|
2007-09-02 16:36:52 +00:00
|
|
|
addConnectedPlayer(playerId, playerName, rights);
|
2007-08-02 20:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
void startNetworkGameDialogImpl::addConnectedPlayer(unsigned playerId, QString playerName, int rights) {
|
|
|
|
|
|
|
|
|
|
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget, 0);
|
|
|
|
|
item->setData(0, Qt::UserRole, playerId);
|
|
|
|
|
item->setData(0, Qt::DisplayRole, playerName);
|
2007-03-22 01:24:01 +00:00
|
|
|
|
2007-10-03 12:30:04 +00:00
|
|
|
GameInfo info = mySession->getClientGameInfo(0);
|
|
|
|
|
|
|
|
|
|
if(this->isVisible() && myConfig->readConfigInt("PlayNetworkGameNotification")) {
|
|
|
|
|
if(treeWidget->topLevelItemCount() < info.data.maxNumberOfPlayers) {
|
|
|
|
|
myW->getMySDLPlayer()->playSound("playerconnected", 0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
myW->getMySDLPlayer()->playSound("onlinegameready", 0);
|
|
|
|
|
}
|
2007-08-10 19:26:14 +00:00
|
|
|
}
|
2007-08-08 12:54:20 +00:00
|
|
|
|
2007-05-23 07:16:28 +00:00
|
|
|
checkPlayerQuantity();
|
2007-03-22 01:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
void startNetworkGameDialogImpl::updatePlayer(unsigned playerId, QString newPlayerName)
|
2007-08-26 22:41:37 +00:00
|
|
|
{
|
2007-09-02 16:36:52 +00:00
|
|
|
QTreeWidgetItemIterator it(treeWidget);
|
|
|
|
|
while (*it) {
|
|
|
|
|
if ((*it)->data(0, Qt::UserRole) == playerId)
|
|
|
|
|
{
|
|
|
|
|
(*it)->setData(0, Qt::DisplayRole, newPlayerName);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++it;
|
2007-08-26 22:41:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
void startNetworkGameDialogImpl::removePlayer(unsigned playerId, QString) {
|
2007-05-02 22:46:29 +00:00
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
QTreeWidgetItemIterator it(treeWidget);
|
|
|
|
|
while (*it) {
|
|
|
|
|
if ((*it)->data(0, Qt::UserRole) == playerId)
|
|
|
|
|
{
|
|
|
|
|
treeWidget->takeTopLevelItem(treeWidget->indexOfTopLevelItem(*it));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++it;
|
2007-05-02 22:46:29 +00:00
|
|
|
}
|
2007-05-23 07:16:28 +00:00
|
|
|
|
|
|
|
|
checkPlayerQuantity();
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-03 13:36:47 +00:00
|
|
|
void startNetworkGameDialogImpl::newGameAdmin(unsigned playerId, QString)
|
|
|
|
|
{
|
|
|
|
|
if (myPlayerId == playerId)
|
|
|
|
|
{
|
|
|
|
|
isAdmin = true;
|
|
|
|
|
checkPlayerQuantity();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
void startNetworkGameDialogImpl::playerSelected(QTreeWidgetItem* item, QTreeWidgetItem*) {
|
2007-06-05 20:54:16 +00:00
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
if (item)
|
|
|
|
|
pushButton_Kick->setEnabled(isAdmin);
|
2007-06-05 20:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void startNetworkGameDialogImpl::kickPlayer() {
|
|
|
|
|
|
2007-06-05 21:56:33 +00:00
|
|
|
|
2007-06-05 20:54:16 +00:00
|
|
|
QTreeWidgetItem *item = treeWidget->currentItem();
|
2007-06-07 19:37:35 +00:00
|
|
|
if (item)
|
|
|
|
|
{
|
|
|
|
|
QString playerName = item->text(0);
|
|
|
|
|
if(playerName == QString::fromUtf8(myConfig->readConfigString("MyName").c_str())) {
|
|
|
|
|
{ QMessageBox::warning(this, tr("Server Error"),
|
|
|
|
|
tr("You should not kick yourself from this game!"),
|
|
|
|
|
QMessageBox::Close); }
|
|
|
|
|
}
|
|
|
|
|
else {
|
2007-06-07 21:19:06 +00:00
|
|
|
assert(mySession);
|
2007-09-05 19:14:34 +00:00
|
|
|
mySession->kickPlayer(item->data(0, Qt::UserRole).toUInt());
|
2007-06-07 19:37:35 +00:00
|
|
|
}
|
2007-06-05 21:56:33 +00:00
|
|
|
}
|
2007-08-27 09:57:51 +00:00
|
|
|
pushButton_Kick->setEnabled(false);
|
2007-06-05 20:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-23 07:16:28 +00:00
|
|
|
void startNetworkGameDialogImpl::checkPlayerQuantity() {
|
|
|
|
|
|
2007-09-03 22:51:00 +00:00
|
|
|
if(isAdmin){
|
|
|
|
|
pushButton_Kick->show();
|
|
|
|
|
pushButton_startGame->show();
|
|
|
|
|
checkBox_fillUpWithComputerOpponents->show();
|
|
|
|
|
|
|
|
|
|
if (treeWidget->topLevelItemCount() >= 2) {
|
|
|
|
|
pushButton_startGame->setEnabled(true);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
pushButton_startGame->setEnabled(false);
|
|
|
|
|
}
|
2007-08-08 12:54:20 +00:00
|
|
|
}
|
2007-05-02 22:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-02 16:36:52 +00:00
|
|
|
void startNetworkGameDialogImpl::clearDialog()
|
2007-08-27 09:57:51 +00:00
|
|
|
{
|
2007-09-03 22:51:00 +00:00
|
|
|
pushButton_Kick->hide();
|
|
|
|
|
pushButton_startGame->hide();
|
2007-08-27 09:57:51 +00:00
|
|
|
pushButton_Kick->setEnabled(false);
|
2007-09-02 16:36:52 +00:00
|
|
|
pushButton_startGame->setEnabled(false);
|
|
|
|
|
treeWidget->clear();
|
2007-09-03 22:51:00 +00:00
|
|
|
checkBox_fillUpWithComputerOpponents->hide();
|
2007-10-03 13:36:47 +00:00
|
|
|
|
|
|
|
|
myPlayerId = 0;
|
2007-08-27 09:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
2007-06-07 21:19:06 +00:00
|
|
|
void startNetworkGameDialogImpl::setSession(Session *session)
|
|
|
|
|
{
|
|
|
|
|
mySession = session;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-22 00:48:18 +00:00
|
|
|
void startNetworkGameDialogImpl::keyPressEvent ( QKeyEvent * event ) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (event->key() == 16777220) { pushButton_startGame->click(); } //ENTER
|
|
|
|
|
|
|
|
|
|
}
|
2007-09-07 08:58:36 +00:00
|
|
|
|
|
|
|
|
void startNetworkGameDialogImpl::sendChatMessage() { /*myChat->sendMessage();*/ }
|