diff options
| author | totycro <totycro@unknown-horizons.org> | 2011-04-10 00:55:13 +0200 |
|---|---|---|
| committer | totycro <totycro@unknown-horizons.org> | 2011-04-10 00:55:13 +0200 |
| commit | da481762bded855ffd709114ee3a16ec4dbaaeb6 (patch) | |
| tree | 10f323140f11280f5185c19666a8412199613e0e | |
| parent | 5b4ddc78484448886135e031bee3d423e2fd3b83 (diff) | |
| download | foop-da481762bded855ffd709114ee3a16ec4dbaaeb6.tar.gz foop-da481762bded855ffd709114ee3a16ec4dbaaeb6.tar.bz2 foop-da481762bded855ffd709114ee3a16ec4dbaaeb6.zip | |
Tell player about their color
minor cleanup
| -rw-r--r-- | pacman-c++/client.cpp | 3 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 50 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.h | 2 | ||||
| -rw-r--r-- | pacman-c++/pacman.proto | 4 | ||||
| -rw-r--r-- | pacman-c++/server.cpp | 30 | ||||
| -rw-r--r-- | pacman-c++/util.cpp | 31 | ||||
| -rw-r--r-- | pacman-c++/util.h | 9 |
7 files changed, 83 insertions, 46 deletions
diff --git a/pacman-c++/client.cpp b/pacman-c++/client.cpp index fbcde6d..c35510a 100644 --- a/pacman-c++/client.cpp +++ b/pacman-c++/client.cpp | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #include "client.h" | 1 | #include "client.h" |
| 2 | #include "clicklabel.h" | 2 | #include "clicklabel.h" |
| 3 | #include "audioplayer.h" | 3 | #include "audioplayer.h" |
| 4 | #include "pacman.pb.h" | ||
| 4 | 5 | ||
| 5 | Client::Client() | 6 | Client::Client() |
| 6 | { | 7 | { |
| @@ -64,6 +65,8 @@ void Client::mutedChanged(bool muted) const | |||
| 64 | 65 | ||
| 65 | int main(int argc, char ** argv) | 66 | int main(int argc, char ** argv) |
| 66 | { | 67 | { |
| 68 | GOOGLE_PROTOBUF_VERIFY_VERSION; | ||
| 69 | |||
| 67 | QApplication app(argc, argv); | 70 | QApplication app(argc, argv); |
| 68 | app.setOrganizationName("TU Wien FOOP"); | 71 | app.setOrganizationName("TU Wien FOOP"); |
| 69 | app.setApplicationName("Pacman Client"); | 72 | app.setApplicationName("Pacman Client"); |
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 420ea8f..25f554a 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -15,16 +15,20 @@ MainWidget::MainWidget(QWidget *parent) | |||
| 15 | //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); | 15 | //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); |
| 16 | //AudioPlayer::self()->play(AudioPlayer::Intro); | 16 | //AudioPlayer::self()->play(AudioPlayer::Intro); |
| 17 | 17 | ||
| 18 | bool connected = connectToServer(); | 18 | Color::Color myColor = connectToServer(); |
| 19 | if (!connected) { | 19 | if (myColor == Color::none) { |
| 20 | QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); | 20 | QMessageBox::critical(this, "Error", "Failed to connect to server, falling back to local test mode"); |
| 21 | // TODO: quit application here or sth | 21 | // TODO: quit application here or sth |
| 22 | m_socket = NULL; | 22 | m_socket = NULL; |
| 23 | QTimer *timer = new QTimer(this); | 23 | QTimer *timer = new QTimer(this); |
| 24 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); | 24 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); |
| 25 | timer->start(Constants::tick); | 25 | timer->start(Constants::tick); |
| 26 | } else { | ||
| 27 | connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); | ||
| 26 | } | 28 | } |
| 27 | connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); | 29 | |
| 30 | // TODO: use mycolor | ||
| 31 | qDebug() << "myc" << myColor; | ||
| 28 | 32 | ||
| 29 | startGame(); | 33 | startGame(); |
| 30 | } | 34 | } |
| @@ -146,17 +150,8 @@ void MainWidget::tick() | |||
| 146 | } | 150 | } |
| 147 | } | 151 | } |
| 148 | } else { | 152 | } else { |
| 149 | QByteArray data = m_socket->readAll(); | ||
| 150 | //qDebug() << "read qbytes " << data.length(); | ||
| 151 | // TODO: normal conversion to std::string won't work, | ||
| 152 | // probably due to \0-bytes. both those methods will yield | ||
| 153 | // a 3 char long string | ||
| 154 | //std::string dataStr = std::string(data.constData()); | ||
| 155 | //std::string dataStr = QString(data).toStdString(); | ||
| 156 | std::string dataStr; | 153 | std::string dataStr; |
| 157 | for (int i = 0; i < data.size(); ++i) { | 154 | Util::QByteArrayToStdString(m_socket->readAll(), dataStr); |
| 158 | dataStr += data[i]; | ||
| 159 | } | ||
| 160 | 155 | ||
| 161 | //qDebug() << "read str " << dataStr.length(); | 156 | //qDebug() << "read str " << dataStr.length(); |
| 162 | ProtoBuf::MapUpdate packet; | 157 | ProtoBuf::MapUpdate packet; |
| @@ -181,7 +176,7 @@ void MainWidget::keyPressEvent(QKeyEvent* event) | |||
| 181 | return; | 176 | return; |
| 182 | 177 | ||
| 183 | QWidget::keyPressEvent(event); | 178 | QWidget::keyPressEvent(event); |
| 184 | Transmission::field_t newKey = translateKey(event->key()); | 179 | Transmission::field_t newKey = translateKey(event->key(), -1); |
| 185 | if (m_currentKey == newKey || newKey == -1) { | 180 | if (m_currentKey == newKey || newKey == -1) { |
| 186 | return; | 181 | return; |
| 187 | } | 182 | } |
| @@ -191,15 +186,7 @@ void MainWidget::keyPressEvent(QKeyEvent* event) | |||
| 191 | // send to server | 186 | // send to server |
| 192 | ProtoBuf::KeyPressUpdate packet; | 187 | ProtoBuf::KeyPressUpdate packet; |
| 193 | packet.set_newkey(m_currentKey); | 188 | packet.set_newkey(m_currentKey); |
| 194 | std::string dataStr = packet.SerializeAsString(); | 189 | Util::sendPacket(packet, m_socket); |
| 195 | unsigned int bytesWritten = m_socket->write(dataStr.c_str(), dataStr.length()); | ||
| 196 | m_socket->flush(); | ||
| 197 | if (bytesWritten != dataStr.length()) { | ||
| 198 | qDebug() << "written: " << bytesWritten; | ||
| 199 | qDebug() << "strl: " << dataStr.length(); | ||
| 200 | } | ||
| 201 | Q_ASSERT(bytesWritten == dataStr.length()); | ||
| 202 | |||
| 203 | qDebug() << "send key: " << m_currentKey; | 190 | qDebug() << "send key: " << m_currentKey; |
| 204 | } else { | 191 | } else { |
| 205 | 192 | ||
| @@ -244,10 +231,23 @@ void MainWidget::playerScoreClicked() | |||
| 244 | return; | 231 | return; |
| 245 | } | 232 | } |
| 246 | 233 | ||
| 247 | bool MainWidget::connectToServer() | 234 | Color::Color MainWidget::connectToServer() |
| 248 | { | 235 | { |
| 249 | m_socket = new QTcpSocket(this); | 236 | m_socket = new QTcpSocket(this); |
| 250 | m_socket->connectToHost("127.0.0.1", Constants::port); | 237 | m_socket->connectToHost("127.0.0.1", Constants::port); |
| 251 | return m_socket->waitForConnected(Constants::connection_timeout); | 238 | bool worked = m_socket->waitForConnected(Constants::connection_timeout); |
| 239 | if (worked) { | ||
| 240 | // additional init | ||
| 241 | // first packet is our color | ||
| 242 | worked = m_socket->waitForReadyRead(); | ||
| 243 | if (worked) { | ||
| 244 | std::string data; | ||
| 245 | Util::QByteArrayToStdString(m_socket->readAll(), data); | ||
| 246 | ProtoBuf::WhoAmI packet; | ||
| 247 | packet.ParseFromString(data); | ||
| 248 | return static_cast<Color::Color>(packet.color() & Transmission::color_mask); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | return Color::none; | ||
| 252 | } | 252 | } |
| 253 | 253 | ||
diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index 04beec2..266f329 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h | |||
| @@ -30,7 +30,7 @@ private: | |||
| 30 | void createMenu(); | 30 | void createMenu(); |
| 31 | void updateScore(); | 31 | void updateScore(); |
| 32 | bool isRunning(); | 32 | bool isRunning(); |
| 33 | bool connectToServer(); | 33 | Color::Color connectToServer(); |
| 34 | 34 | ||
| 35 | private slots: | 35 | private slots: |
| 36 | void startGame(); | 36 | void startGame(); |
diff --git a/pacman-c++/pacman.proto b/pacman-c++/pacman.proto index 7a582df..ae8f966 100644 --- a/pacman-c++/pacman.proto +++ b/pacman-c++/pacman.proto | |||
| @@ -7,3 +7,7 @@ message KeyPressUpdate { | |||
| 7 | message MapUpdate { | 7 | message MapUpdate { |
| 8 | repeated uint32 field = 1 [packed=true]; | 8 | repeated uint32 field = 1 [packed=true]; |
| 9 | } | 9 | } |
| 10 | |||
| 11 | message WhoAmI { | ||
| 12 | required uint32 color = 1; | ||
| 13 | } | ||
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index bbe4ad6..422f7e0 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp | |||
| @@ -77,14 +77,21 @@ void Server::waitForClientConnections() | |||
| 77 | // server must stay alive as long as sockets (qt parent mem mechanism) | 77 | // server must stay alive as long as sockets (qt parent mem mechanism) |
| 78 | tcpSrv->listen(QHostAddress::Any, Constants::port); | 78 | tcpSrv->listen(QHostAddress::Any, Constants::port); |
| 79 | 79 | ||
| 80 | Color::Color playerColors[3] = { Color::red, Color::blue, Color::green }; | ||
| 80 | //for (unsigned int i=0; i<Color::max; ++i) { | 81 | //for (unsigned int i=0; i<Color::max; ++i) { |
| 81 | for (unsigned int i=0; i<1; ++i) { | 82 | for (unsigned int i=0; i<1; ++i) { |
| 82 | bool connectionAvailable = tcpSrv->waitForNewConnection(-1); | 83 | bool connectionAvailable = tcpSrv->waitForNewConnection(-1); |
| 83 | Q_ASSERT(connectionAvailable); | 84 | Q_ASSERT(connectionAvailable); |
| 84 | QTcpSocket *socket = tcpSrv->nextPendingConnection(); | 85 | QTcpSocket *socket = tcpSrv->nextPendingConnection(); |
| 85 | // TODO: color assignment | ||
| 86 | m_clientConnections[Color::red] = socket; | ||
| 87 | connect(socket, SIGNAL(readyRead()), this, SLOT(keyPressUpdate())); | 86 | connect(socket, SIGNAL(readyRead()), this, SLOT(keyPressUpdate())); |
| 87 | |||
| 88 | // assign color | ||
| 89 | Color::Color color = playerColors[i]; | ||
| 90 | m_clientConnections[color] = socket; | ||
| 91 | // notify player of color | ||
| 92 | ProtoBuf::WhoAmI packet; | ||
| 93 | packet.set_color(color); | ||
| 94 | Util::sendPacket(packet, socket); | ||
| 88 | } | 95 | } |
| 89 | } | 96 | } |
| 90 | 97 | ||
| @@ -93,16 +100,7 @@ void Server::sendUpdate(QSharedPointer< ProtoBuf::MapUpdate > packet) | |||
| 93 | std::string dataStr = packet->SerializeAsString(); | 100 | std::string dataStr = packet->SerializeAsString(); |
| 94 | const char *data = dataStr.c_str(); | 101 | const char *data = dataStr.c_str(); |
| 95 | foreach(QTcpSocket *socket, m_clientConnections) { | 102 | foreach(QTcpSocket *socket, m_clientConnections) { |
| 96 | //qDebug() << "sending str len: " << dataStr.length(); | 103 | Util::sendPacket(data, dataStr.length(), socket); |
| 97 | int bytesWritten = socket->write(data, dataStr.length()); | ||
| 98 | if (bytesWritten != dataStr.length()) { | ||
| 99 | qDebug() << "written: " << bytesWritten; | ||
| 100 | qDebug() << "strl: " << dataStr.length(); | ||
| 101 | } | ||
| 102 | Q_ASSERT(bytesWritten == dataStr.length()); | ||
| 103 | } | ||
| 104 | foreach(QTcpSocket *socket, m_clientConnections) { | ||
| 105 | socket->flush(); | ||
| 106 | } | 104 | } |
| 107 | } | 105 | } |
| 108 | 106 | ||
| @@ -117,12 +115,8 @@ void Server::keyPressUpdate() | |||
| 117 | qDebug() << "data?"; | 115 | qDebug() << "data?"; |
| 118 | if (socket->bytesAvailable() > 0) { | 116 | if (socket->bytesAvailable() > 0) { |
| 119 | qDebug() << "data!"; | 117 | qDebug() << "data!"; |
| 120 | QByteArray data = socket->readAll(); | ||
| 121 | // see mainwidget.cpp:153 | ||
| 122 | std::string dataStr; | 118 | std::string dataStr; |
| 123 | for (int i = 0; i < data.size(); ++i) { | 119 | Util::QByteArrayToStdString(socket->readAll(), dataStr); |
| 124 | dataStr += data[i]; | ||
| 125 | } | ||
| 126 | ProtoBuf::KeyPressUpdate packet; | 120 | ProtoBuf::KeyPressUpdate packet; |
| 127 | packet.ParseFromString(dataStr); | 121 | packet.ParseFromString(dataStr); |
| 128 | Transmission::field_t direction = packet.newkey(); | 122 | Transmission::field_t direction = packet.newkey(); |
| @@ -130,10 +124,8 @@ void Server::keyPressUpdate() | |||
| 130 | m_actorMovements[ color ] = Util::transmissionMovementToActor(direction); | 124 | m_actorMovements[ color ] = Util::transmissionMovementToActor(direction); |
| 131 | } | 125 | } |
| 132 | } | 126 | } |
| 133 | |||
| 134 | } | 127 | } |
| 135 | 128 | ||
| 136 | |||
| 137 | int main(int argc, char ** argv) | 129 | int main(int argc, char ** argv) |
| 138 | { | 130 | { |
| 139 | GOOGLE_PROTOBUF_VERIFY_VERSION; | 131 | GOOGLE_PROTOBUF_VERIFY_VERSION; |
diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp index 6b9780b..ceba2b8 100644 --- a/pacman-c++/util.cpp +++ b/pacman-c++/util.cpp | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | #include "util.h" | 1 | #include "util.h" |
| 2 | 2 | ||
| 3 | #include <QtNetwork/QTcpSocket> | ||
| 4 | |||
| 3 | namespace Util { | 5 | namespace Util { |
| 4 | 6 | ||
| 5 | Transmission::map_t createUninitialisedMap() { | 7 | Transmission::map_t createUninitialisedMap() { |
| @@ -20,7 +22,7 @@ namespace Util { | |||
| 20 | cur = Transmission::none; | 22 | cur = Transmission::none; |
| 21 | } | 23 | } |
| 22 | } | 24 | } |
| 23 | return map; | 25 | return map; |
| 24 | } | 26 | } |
| 25 | // temporary | 27 | // temporary |
| 26 | Transmission::map_t createDummyMap() | 28 | Transmission::map_t createDummyMap() |
| @@ -138,4 +140,31 @@ namespace Util { | |||
| 138 | } | 140 | } |
| 139 | return Actor::None; // for pleasing the compiler | 141 | return Actor::None; // for pleasing the compiler |
| 140 | } | 142 | } |
| 143 | |||
| 144 | void QByteArrayToStdString(const QByteArray& arr, std::string& str) { | ||
| 145 | // TODO: normal conversion to std::string won't work, | ||
| 146 | // probably due to \0-bytes. | ||
| 147 | //std::string dataStr = std::string(data.constData()); | ||
| 148 | //std::string dataStr = QString(data).toStdString(); | ||
| 149 | for (int i=0; i<arr.size(); ++i) { | ||
| 150 | str += arr[i]; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | void sendPacket(const ::google::protobuf::Message& packet, QTcpSocket *socket) { | ||
| 155 | std::string dataStr = packet.SerializeAsString(); | ||
| 156 | const char *data = dataStr.c_str(); | ||
| 157 | sendPacket(data, dataStr.length(), socket); | ||
| 158 | } | ||
| 159 | |||
| 160 | void sendPacket(const char *data, unsigned int length, QTcpSocket *socket) { | ||
| 161 | unsigned int bytesWritten = socket->write(data, length); | ||
| 162 | if (bytesWritten != length) { | ||
| 163 | qDebug() << "written: " << bytesWritten; | ||
| 164 | qDebug() << "strl: " << length; | ||
| 165 | } | ||
| 166 | Q_ASSERT(bytesWritten == length); | ||
| 167 | socket->flush(); | ||
| 168 | } | ||
| 169 | |||
| 141 | } | 170 | } |
diff --git a/pacman-c++/util.h b/pacman-c++/util.h index 8bd03bc..4c0ccb0 100644 --- a/pacman-c++/util.h +++ b/pacman-c++/util.h | |||
| @@ -3,6 +3,9 @@ | |||
| 3 | 3 | ||
| 4 | #include "constants.h" | 4 | #include "constants.h" |
| 5 | #include "actor.h" | 5 | #include "actor.h" |
| 6 | #include "pacman.pb.h" | ||
| 7 | |||
| 8 | class QTcpSocket; | ||
| 6 | 9 | ||
| 7 | namespace Util { | 10 | namespace Util { |
| 8 | Transmission::map_t createUninitialisedMap(); | 11 | Transmission::map_t createUninitialisedMap(); |
| @@ -16,5 +19,11 @@ namespace Util { | |||
| 16 | Transmission::field_t def = -1); | 19 | Transmission::field_t def = -1); |
| 17 | Actor::Movement transmissionMovementToActor(Transmission::field_t field, | 20 | Actor::Movement transmissionMovementToActor(Transmission::field_t field, |
| 18 | Actor::Movement def = Actor::Movement(-1)); | 21 | Actor::Movement def = Actor::Movement(-1)); |
| 22 | |||
| 23 | void QByteArrayToStdString(const QByteArray& arr, std::string& str); | ||
| 24 | |||
| 25 | // send packet with error check and flush | ||
| 26 | void sendPacket(const ::google::protobuf::Message& packet, QTcpSocket *socket); | ||
| 27 | void sendPacket(const char *data, unsigned int length, QTcpSocket *socket); | ||
| 19 | } | 28 | } |
| 20 | #endif // UTIL_H | 29 | #endif // UTIL_H |
