#include "server.h" #include #include #include "util.h" #include "pacman.pb.h" #include "block.h" Server::Server(QWidget *parent) : SceneHolder(parent) { qDebug() << "[Server] Waiting for clients"; waitForClientConnections(); qDebug() << "[Server] All Clients connected"; Transmission::map_t map = Util::createDemoMap(); updateMap(map); Util::deleteMap(map); map = NULL; QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(tick())); timer->start(Constants::tick); } void Server::tick() { qDebug() << "[Tick] Doing server update"; Transmission::map_t map = calculateUpdates(); updateMap(map); sendUpdate(map); Util::deleteMap(map); } Transmission::map_t Server::calculateUpdates() { Transmission::map_t map = Util::createEmptyMap(); //TODO: ai //m_actorMovements[Color::blue] = Actor::Movement((qrand() % 4) + 1); //m_actorMovements[Color::green] = Actor::Movement((qrand() % 4) + 1); QMutableMapIterator i(m_actorMovements); while (i.hasNext()) { i.next(); int turn = 0; invalid_direction: ++turn; Actor *actor = m_actors.value(i.key()); QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint()); qDebug() << "[Calc] Actor wants to move: color=" << i.key() << "pos=" << mapPosition << "direction=" << i.value(); QPoint newMapPosition = mapPosition; switch (i.value()) { case Actor::Up: newMapPosition += QPoint(0, -1); break; case Actor::Down: newMapPosition += QPoint(0, 1); break; case Actor::Left: newMapPosition += QPoint(-1, 0); break; case Actor::Right: newMapPosition += QPoint(1, 0); break; case Actor::None: break; default: Q_ASSERT(false); } if (newMapPosition.x() < 0) newMapPosition.setX(0); if (newMapPosition.x() >= visualMap.size()) newMapPosition.setX(visualMap.size() - 1); if (newMapPosition.y() < 0) newMapPosition.setY(0); if (newMapPosition.y() >= visualMap[newMapPosition.x()].size()) newMapPosition.setY(visualMap[newMapPosition.x()].size() - 1); // // TODO: support actors eating each other // TODO: old item - REMOVE THAT? GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()]; /* check if there's an item at new location of actor */ GameEntity *item = visualMap[newMapPosition.x()][newMapPosition.y()]; if (item != NULL && oldItem != item) { qDebug() << "[Calc] Found item at new actor location"; if (!item->checkEnter(actor)) { /* movement invalid. e.g. move against wall */ newMapPosition = mapPosition; qDebug() << "[Calc] Item blocks actor"; } else { /* apply actions of entering this field */ bool survive = item->enter(actor); if (!survive) { map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty | actor->color(); } } } // /* movement didn't work - e.g. was blocked */ if (mapPosition == newMapPosition) { /* */ if (turn == 1 && i.value() != actor->direction()) { /* set direction back to last known direction and try again */ qDebug() << "[Calc] Movement was blocked. Trying last known actor direction"; m_actorMovements[i.key()] = actor->direction(); goto invalid_direction; } else { /* second turn didn't work too -> stop movement */ m_actorMovements[i.key()] = Actor::None; qDebug() << "[Calc] No good direction known. Movement stopped"; } } map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | i.key() | Util::actorMovementToTransmission(i.value()); /* DEBUG: uncomments to disable auto-movement */ //m_actorMovements[i.key()] = Actor::None; if (i.value() == Actor::None) { /* set actor to non-moving */ m_actorMovements[i.key()] = Actor::None; i.remove(); } } return map; } void Server::waitForClientConnections() { QTcpServer *tcpSrv = new QTcpServer(this); // server must stay alive as long as sockets (qt parent mem mechanism) tcpSrv->listen(QHostAddress::Any, Constants::Networking::port); #define SINGLE #ifdef SINGLE for (unsigned int i = 0; i < 1; ++i) { #else for (unsigned int i = 0; Color::order[i] != Color::none; ++i) { #endif bool connectionAvailable = tcpSrv->waitForNewConnection(-1); Q_ASSERT(connectionAvailable); Q_UNUSED(connectionAvailable); QTcpSocket *socket = tcpSrv->nextPendingConnection(); connect(socket, SIGNAL(readyRead()), this, SLOT(keyPressUpdate())); /* assign color */ Color::Color color = Color::order[i]; m_clientConnections[color] = socket; /* notify player of color */ //TODO: allocate + reuse packet ProtoBuf::WhoAmI packet; packet.set_color(color); Util::sendPacket(packet, socket); qDebug() << "[Connect] New Player: color=" << color; } } void Server::sendUpdate(Transmission::map_t map) { ProtoBuf::MapUpdate packet; for (unsigned int x = 0; x < Constants::map_size.width; ++x) { for (unsigned int y = 0; y < Constants::map_size.height; ++y) packet.add_field(map[x][y]); } for(unsigned i = 0; Color::order[i] != Color::none; ++i) { packet.add_round_points(m_actors.value(Color::order[i])->getRoundPoints()); packet.add_game_points(m_actors.value(Color::order[i])->getGamePoints()); } QSharedPointer data = Util::createPacket(packet); foreach(QTcpSocket *socket, m_clientConnections) { if (!Util::sendPacket(data.data(), socket)) { qDebug() << "[sendUpdate] Error while sending data to client, exiting..."; qApp->quit(); } } } void Server::keyPressUpdate() { QMapIterator i(m_clientConnections); while (i.hasNext()) { i.next(); Color::Color color = i.key(); QTcpSocket *socket = i.value(); QDataStream in(i.value()); while (socket->bytesAvailable() > (qint64)sizeof(qint64)) { QSharedPointer data = Util::receivePacket(socket); //TODO: allocate + reuse packet ProtoBuf::KeyPressUpdate packet; bool worked = packet.ParseFromArray(data->data(), data->size()); Q_ASSERT(worked); Q_UNUSED(worked); Transmission::field_t direction = packet.newkey(); qDebug() << "[KeyPress] actor=" << color << "direction=" << direction; m_actorMovements[color] = Util::transmissionMovementToActor(direction); } } } bool Constants::server = true; int main(int argc, char ** argv) { /* Verify that the version of the library that we linked against is * compatible with the version of the headers we compiled against. */ GOOGLE_PROTOBUF_VERIFY_VERSION; QApplication app(argc, argv, false); app.setApplicationName("Pacman Server"); app.setWindowIcon(QIcon(":/appicon")); qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); Server Server; int ret = app.exec(); /* Delete all global objects allocated by libprotobuf */ google::protobuf::ShutdownProtobufLibrary(); return ret; }