PB: server compiles again, standalone RemoteClient is able to log in

This commit is contained in:
Max-Wilhelm Bruker
2011-12-30 21:50:25 +01:00
parent 10018280e5
commit d3b96b1a88
39 changed files with 988 additions and 807 deletions

View File

@@ -25,11 +25,26 @@
#include "server_card.h"
#include "server_cardzone.h"
#include "server_counter.h"
#include "decklist.h"
#include "pb/event_game_closed.pb.h"
#include "pb/event_game_host_changed.pb.h"
#include "pb/event_game_state_changed.pb.h"
#include "pb/event_connection_state_changed.pb.h"
#include "pb/event_kicked.pb.h"
#include "pb/event_ping.pb.h"
#include "pb/event_join.pb.h"
#include "pb/event_leave.pb.h"
#include "pb/event_delete_arrow.pb.h"
#include "pb/event_set_active_player.pb.h"
#include "pb/event_set_active_phase.pb.h"
#include "pb/serverinfo_playerping.pb.h"
#include <google/protobuf/descriptor.h>
#include "protocol.h"
#include <QTimer>
#include <QDebug>
Server_Game::Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, const QList<int> &_gameTypes, bool _onlyBuddies, bool _onlyRegistered, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server_Room *_room)
: QObject(), room(_room), hostId(0), creatorInfo(new ServerInfo_User(_creator->getUserInfo())), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), gameTypes(_gameTypes), activePlayer(-1), activePhase(-1), onlyBuddies(_onlyBuddies), onlyRegistered(_onlyRegistered), spectatorsAllowed(_spectatorsAllowed), spectatorsNeedPassword(_spectatorsNeedPassword), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), inactivityCounter(0), secondsElapsed(0), gameMutex(QMutex::Recursive)
: QObject(), room(_room), hostId(0), creatorInfo(new ServerInfo_User(_creator->copyUserInfo(false))), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), gameTypes(_gameTypes), activePlayer(-1), activePhase(-1), onlyBuddies(_onlyBuddies), onlyRegistered(_onlyRegistered), spectatorsAllowed(_spectatorsAllowed), spectatorsNeedPassword(_spectatorsNeedPassword), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), inactivityCounter(0), secondsElapsed(0), gameMutex(QMutex::Recursive)
{
connect(this, SIGNAL(sigStartGameIfReady()), this, SLOT(doStartGameIfReady()), Qt::QueuedConnection);
@@ -47,7 +62,7 @@ Server_Game::~Server_Game()
QMutexLocker roomLocker(&room->roomMutex);
QMutexLocker locker(&gameMutex);
sendGameEvent(new Event_GameClosed);
sendGameEventContainer(prepareGameEvent(Event_GameClosed(), -1));
QMapIterator<int, Server_Player *> playerIterator(players);
while (playerIterator.hasNext())
@@ -56,13 +71,16 @@ Server_Game::~Server_Game()
room->removeGame(this);
delete creatorInfo;
creatorInfo = 0;
qDebug() << "Server_Game destructor: gameId=" << gameId;
}
void Server_Game::pingClockTimeout()
{
QMutexLocker locker(&gameMutex);
++secondsElapsed;
Event_Ping event;
event.set_seconds_elapsed(++secondsElapsed);
QList<ServerInfo_PlayerPing *> pingList;
QMapIterator<int, Server_Player *> playerIterator(players);
@@ -79,9 +97,12 @@ void Server_Game::pingClockTimeout()
allPlayersInactive = false;
} else
pingTime = -1;
pingList.append(new ServerInfo_PlayerPing(player->getPlayerId(), pingTime));
ServerInfo_PlayerPing *pingInfo = event.add_ping_list();
pingInfo->set_player_id(player->getPlayerId());
pingInfo->set_ping_time(pingTime);
}
sendGameEvent(new Event_Ping(secondsElapsed, pingList));
sendGameEventContainer(prepareGameEvent(event, -1));
const int maxTime = room->getServer()->getMaxGameInactivityTime();
if (allPlayersInactive) {
@@ -144,7 +165,15 @@ void Server_Game::doStartGameIfReady()
playerIterator.toFront();
while (playerIterator.hasNext()) {
Server_Player *player = playerIterator.next().value();
sendGameEventToPlayer(player, new Event_GameStateChanged(gameStarted, 0, 0, getGameState(player)));
Event_GameStateChanged event;
event.set_game_started(true);
event.set_active_player_id(0);
event.set_active_phase(0);
QListIterator<ServerInfo_Player> gameStateIterator(getGameState(player));
while (gameStateIterator.hasNext())
event.add_player_list()->CopyFrom(gameStateIterator.next());
player->sendGameEvent(prepareGameEvent(event, -1));
}
/* QSqlQuery query;
@@ -200,31 +229,37 @@ void Server_Game::stopGameIfFinished()
playerIterator.toFront();
while (playerIterator.hasNext()) {
Server_Player *player = playerIterator.next().value();
sendGameEventToPlayer(player, new Event_GameStateChanged(gameStarted, -1, -1, getGameState(player)));
Event_GameStateChanged event;
event.set_game_started(false);
QListIterator<ServerInfo_Player> gameStateIterator(getGameState(player));
while (gameStateIterator.hasNext())
event.add_player_list()->CopyFrom(gameStateIterator.next());
player->sendGameEvent(prepareGameEvent(event, -1));
}
}
ResponseCode Server_Game::checkJoin(ServerInfo_User *user, const QString &_password, bool spectator, bool overrideRestrictions)
Response::ResponseCode Server_Game::checkJoin(ServerInfo_User *user, const QString &_password, bool spectator, bool overrideRestrictions)
{
if (!(overrideRestrictions && (user->getUserLevel() & ServerInfo_User::IsModerator))) {
if (!(overrideRestrictions && (user->user_level() & ServerInfo_User::IsModerator))) {
if ((_password != password) && !(spectator && !spectatorsNeedPassword))
return RespWrongPassword;
if (!(user->getUserLevel() & ServerInfo_User::IsRegistered) && onlyRegistered)
return RespUserLevelTooLow;
return Response::RespWrongPassword;
if (!(user->user_level() & ServerInfo_User::IsRegistered) && onlyRegistered)
return Response::RespUserLevelTooLow;
if (onlyBuddies)
if (!room->getServer()->getBuddyList(creatorInfo->getName()).contains(user->getName()))
return RespOnlyBuddies;
if (room->getServer()->getIgnoreList(creatorInfo->getName()).contains(user->getName()))
return RespInIgnoreList;
if (!room->getServer()->isInBuddyList(QString::fromStdString(creatorInfo->name()), QString::fromStdString(user->name())))
return Response::RespOnlyBuddies;
if (room->getServer()->isInIgnoreList(QString::fromStdString(creatorInfo->name()), QString::fromStdString(user->name())))
return Response::RespInIgnoreList;
if (spectator) {
if (!spectatorsAllowed)
return RespSpectatorsNotAllowed;
return Response::RespSpectatorsNotAllowed;
}
}
if (!spectator && (gameStarted || (getPlayerCount() >= getMaxPlayers())))
return RespGameFull;
return Response::RespGameFull;
return RespOk;
return Response::RespOk;
}
bool Server_Game::containsUser(const QString &userName) const
@@ -233,7 +268,7 @@ bool Server_Game::containsUser(const QString &userName) const
QMapIterator<int, Server_Player *> playerIterator(players);
while (playerIterator.hasNext())
if (playerIterator.next().value()->getUserInfo()->getName() == userName)
if (playerIterator.next().value()->getUserInfo()->name() == userName.toStdString())
return true;
return false;
}
@@ -245,13 +280,17 @@ Server_Player *Server_Game::addPlayer(Server_ProtocolHandler *handler, bool spec
const QList<int> &keyList = players.keys();
int playerId = keyList.isEmpty() ? 0 : (keyList.last() + 1);
Server_Player *newPlayer = new Server_Player(this, playerId, handler->getUserInfo(), spectator, handler);
Server_Player *newPlayer = new Server_Player(this, playerId, handler->copyUserInfo(true), spectator, handler);
newPlayer->moveToThread(thread());
sendGameEvent(new Event_Join(newPlayer->getProperties()));
Event_Join joinEvent;
joinEvent.mutable_player_properties()->CopyFrom(newPlayer->getProperties());
sendGameEventContainer(prepareGameEvent(joinEvent, -1));
players.insert(playerId, newPlayer);
if (newPlayer->getUserInfo()->getName() == creatorInfo->getName()) {
if (newPlayer->getUserInfo()->name() == creatorInfo->name()) {
hostId = playerId;
sendGameEvent(new Event_GameHostChanged(playerId));
sendGameEventContainer(prepareGameEvent(Event_GameHostChanged(), playerId));
}
if (broadcastUpdate)
@@ -268,7 +307,7 @@ void Server_Game::removePlayer(Server_Player *player)
players.remove(player->getPlayerId());
removeArrowsToPlayer(player);
sendGameEvent(new Event_Leave(player->getPlayerId()));
sendGameEventContainer(prepareGameEvent(Event_Leave(), player->getPlayerId()));
bool playerActive = activePlayer == player->getPlayerId();
bool playerHost = hostId == player->getPlayerId();
bool spectator = player->getSpectator();
@@ -289,7 +328,7 @@ void Server_Game::removePlayer(Server_Player *player)
}
if (newHostId != -1) {
hostId = newHostId;
sendGameEvent(new Event_GameHostChanged(hostId));
sendGameEventContainer(prepareGameEvent(Event_GameHostChanged(), hostId));
}
}
stopGameIfFinished();
@@ -319,7 +358,10 @@ void Server_Game::removeArrowsToPlayer(Server_Player *player)
toDelete.append(a);
}
for (int i = 0; i < toDelete.size(); ++i) {
sendGameEvent(new Event_DeleteArrow(p->getPlayerId(), toDelete[i]->getId()));
Event_DeleteArrow event;
event.set_arrow_id(toDelete[i]->getId());
sendGameEventContainer(prepareGameEvent(event, p->getPlayerId()));
p->deleteArrow(toDelete[i]->getId());
}
}
@@ -334,7 +376,8 @@ bool Server_Game::kickPlayer(int playerId)
if (!playerToKick)
return false;
sendGameEventToPlayer(playerToKick, new Event_Kicked);
playerToKick->sendGameEvent(prepareGameEvent(Event_Kicked(), -1));
removePlayer(playerToKick);
return true;
@@ -345,7 +388,11 @@ void Server_Game::setActivePlayer(int _activePlayer)
QMutexLocker locker(&gameMutex);
activePlayer = _activePlayer;
sendGameEvent(new Event_SetActivePlayer(activePlayer, activePlayer));
Event_SetActivePlayer event;
event.set_active_player_id(activePlayer);
sendGameEventContainer(prepareGameEvent(event, -1));
setActivePhase(0);
}
@@ -359,13 +406,20 @@ void Server_Game::setActivePhase(int _activePhase)
QList<Server_Arrow *> toDelete = player->getArrows().values();
for (int i = 0; i < toDelete.size(); ++i) {
Server_Arrow *a = toDelete[i];
sendGameEvent(new Event_DeleteArrow(player->getPlayerId(), a->getId()));
Event_DeleteArrow event;
event.set_arrow_id(a->getId());
sendGameEventContainer(prepareGameEvent(event, player->getPlayerId()));
player->deleteArrow(a->getId());
}
}
activePhase = _activePhase;
sendGameEvent(new Event_SetActivePhase(-1, activePhase));
Event_SetActivePhase event;
event.set_phase(activePhase);
sendGameEventContainer(prepareGameEvent(event, -1));
}
void Server_Game::nextTurn()
@@ -389,163 +443,161 @@ void Server_Game::postConnectionStatusUpdate(Server_Player *player, bool connect
{
QMutexLocker locker(&gameMutex);
sendGameEvent(new Event_ConnectionStateChanged(player->getPlayerId(), connectionStatus));
Event_ConnectionStateChanged event;
event.set_connected(connectionStatus);
sendGameEventContainer(prepareGameEvent(event, player->getPlayerId()));
}
QList<ServerInfo_Player *> Server_Game::getGameState(Server_Player *playerWhosAsking) const
QList<ServerInfo_Player> Server_Game::getGameState(Server_Player *playerWhosAsking) const
{
QMutexLocker locker(&gameMutex);
QList<ServerInfo_Player *> result;
QList<ServerInfo_Player> result;
QMapIterator<int, Server_Player *> playerIterator(players);
while (playerIterator.hasNext()) {
Server_Player *player = playerIterator.next().value();
ServerInfo_Player playerInfo;
playerInfo.mutable_properties()->CopyFrom(player->getProperties());
if (player == playerWhosAsking)
playerInfo.set_deck_list(player->getDeck()->writeToString_Native().toStdString());
QList<ServerInfo_Arrow *> arrowList;
QMapIterator<int, Server_Arrow *> arrowIterator(player->getArrows());
while (arrowIterator.hasNext()) {
Server_Arrow *arrow = arrowIterator.next().value();
Server_Card *targetCard = qobject_cast<Server_Card *>(arrow->getTargetItem());
if (targetCard)
arrowList.append(new ServerInfo_Arrow(
arrow->getId(),
arrow->getStartCard()->getZone()->getPlayer()->getPlayerId(),
arrow->getStartCard()->getZone()->getName(),
arrow->getStartCard()->getId(),
targetCard->getZone()->getPlayer()->getPlayerId(),
targetCard->getZone()->getName(),
targetCard->getId(),
arrow->getColor()
));
else
arrowList.append(new ServerInfo_Arrow(
arrow->getId(),
arrow->getStartCard()->getZone()->getPlayer()->getPlayerId(),
arrow->getStartCard()->getZone()->getName(),
arrow->getStartCard()->getId(),
qobject_cast<Server_Player *>(arrow->getTargetItem())->getPlayerId(),
QString(),
-1,
arrow->getColor()
));
ServerInfo_Arrow *arrowInfo = playerInfo.add_arrow_list();
arrowInfo->set_id(arrow->getId());
arrowInfo->set_start_player_id(arrow->getStartCard()->getZone()->getPlayer()->getPlayerId());
arrowInfo->set_start_zone(arrow->getStartCard()->getZone()->getName().toStdString());
arrowInfo->set_start_card_id(arrow->getStartCard()->getId());
arrowInfo->mutable_arrow_color()->CopyFrom(arrow->getColor().get_color());
if (targetCard) {
arrowInfo->set_target_player_id(targetCard->getZone()->getPlayer()->getPlayerId());
arrowInfo->set_target_zone(targetCard->getZone()->getName().toStdString());
arrowInfo->set_target_card_id(targetCard->getId());
} else
arrowInfo->set_target_player_id(qobject_cast<Server_Player *>(arrow->getTargetItem())->getPlayerId());
}
QList<ServerInfo_Counter *> counterList;
QMapIterator<int, Server_Counter *> counterIterator(player->getCounters());
while (counterIterator.hasNext()) {
Server_Counter *counter = counterIterator.next().value();
counterList.append(new ServerInfo_Counter(counter->getId(), counter->getName(), counter->getColor(), counter->getRadius(), counter->getCount()));
ServerInfo_Counter *counterInfo = playerInfo.add_counter_list();
counterInfo->set_id(counter->getId());
counterInfo->set_name(counter->getName().toStdString());
counterInfo->mutable_counter_color()->CopyFrom(counter->getColor().get_color());
counterInfo->set_radius(counter->getRadius());
counterInfo->set_count(counter->getCount());
}
QList<ServerInfo_Zone *> zoneList;
QMapIterator<QString, Server_CardZone *> zoneIterator(player->getZones());
while (zoneIterator.hasNext()) {
Server_CardZone *zone = zoneIterator.next().value();
QList<ServerInfo_Card *> cardList;
ServerInfo_Zone *zoneInfo = playerInfo.add_zone_list();
zoneInfo->set_name(zone->getName().toStdString());
zoneInfo->set_type(zone->getType());
zoneInfo->set_with_coords(zone->hasCoords());
zoneInfo->set_card_count(zone->cards.size());
if (
(((playerWhosAsking == player) || (playerWhosAsking->getSpectator() && spectatorsSeeEverything)) && (zone->getType() != HiddenZone))
|| ((playerWhosAsking != player) && (zone->getType() == PublicZone))
(((playerWhosAsking == player) || (playerWhosAsking->getSpectator() && spectatorsSeeEverything)) && (zone->getType() != ServerInfo_Zone::HiddenZone))
|| ((playerWhosAsking != player) && (zone->getType() == ServerInfo_Zone::PublicZone))
) {
QListIterator<Server_Card *> cardIterator(zone->cards);
while (cardIterator.hasNext()) {
Server_Card *card = cardIterator.next();
ServerInfo_Card *cardInfo = zoneInfo->add_card_list();
QString displayedName = card->getFaceDown() ? QString() : card->getName();
cardInfo->set_id(card->getId());
cardInfo->set_name(displayedName.toStdString());
cardInfo->set_x(card->getX());
cardInfo->set_y(card->getY());
cardInfo->set_face_down(card->getFaceDown());
cardInfo->set_tapped(card->getTapped());
cardInfo->set_attacking(card->getAttacking());
cardInfo->set_color(card->getColor().toStdString());
cardInfo->set_pt(card->getPT().toStdString());
cardInfo->set_annotation(card->getAnnotation().toStdString());
cardInfo->set_destroy_on_zone_change(card->getDestroyOnZoneChange());
cardInfo->set_doesnt_untap(card->getDoesntUntap());
QList<ServerInfo_CardCounter *> cardCounterList;
QMapIterator<int, int> cardCounterIterator(card->getCounters());
while (cardCounterIterator.hasNext()) {
cardCounterIterator.next();
cardCounterList.append(new ServerInfo_CardCounter(cardCounterIterator.key(), cardCounterIterator.value()));
ServerInfo_CardCounter *counterInfo = cardInfo->add_counter_list();
counterInfo->set_id(cardCounterIterator.key());
counterInfo->set_value(cardCounterIterator.value());
}
int attachPlayerId = -1;
QString attachZone;
int attachCardId = -1;
if (card->getParentCard()) {
attachPlayerId = card->getParentCard()->getZone()->getPlayer()->getPlayerId();
attachZone = card->getParentCard()->getZone()->getName();
attachCardId = card->getParentCard()->getId();
cardInfo->set_attach_player_id(card->getParentCard()->getZone()->getPlayer()->getPlayerId());
cardInfo->set_attach_zone(card->getParentCard()->getZone()->getName().toStdString());
cardInfo->set_attach_card_id(card->getParentCard()->getId());
}
cardList.append(new ServerInfo_Card(card->getId(), displayedName, card->getX(), card->getY(), card->getFaceDown(), card->getTapped(), card->getAttacking(), card->getColor(), card->getPT(), card->getAnnotation(), card->getDestroyOnZoneChange(), card->getDoesntUntap(), cardCounterList, attachPlayerId, attachZone, attachCardId));
}
}
zoneList.append(new ServerInfo_Zone(zone->getName(), zone->getType(), zone->hasCoords(), zone->cards.size(), cardList));
}
result.append(new ServerInfo_Player(player->getProperties(), player == playerWhosAsking ? player->getDeck() : 0, zoneList, counterList, arrowList));
result.append(playerInfo);
}
return result;
}
void Server_Game::sendGameEvent(GameEvent *event, GameEventContext *context, Server_Player *exclude)
{
sendGameEventContainer(new GameEventContainer(QList<GameEvent *>() << event, -1, context), exclude);
}
void Server_Game::sendGameEventContainer(GameEventContainer *cont, Server_Player *exclude, bool excludeOmniscient)
void Server_Game::sendGameEventContainer(GameEventContainer *cont, GameEventStorageItem::EventRecipients recipients, int privatePlayerId)
{
QMutexLocker locker(&gameMutex);
cont->setGameId(gameId);
cont->set_game_id(gameId);
QMapIterator<int, Server_Player *> playerIterator(players);
while (playerIterator.hasNext()) {
Server_Player *p = playerIterator.next().value();
if ((p != exclude) && !(excludeOmniscient && p->getSpectator() && spectatorsSeeEverything))
p->sendProtocolItem(cont, false);
}
delete cont;
}
void Server_Game::sendGameEventContainerOmniscient(GameEventContainer *cont, Server_Player *exclude)
{
QMutexLocker locker(&gameMutex);
cont->setGameId(gameId);
QMapIterator<int, Server_Player *> playerIterator(players);
while (playerIterator.hasNext()) {
Server_Player *p = playerIterator.next().value();
if ((p != exclude) && (p->getSpectator() && spectatorsSeeEverything))
p->sendProtocolItem(cont, false);
const bool playerPrivate = (p->getPlayerId() == privatePlayerId) || (p->getSpectator() && spectatorsSeeEverything);
if ((recipients.testFlag(GameEventStorageItem::SendToPrivate) && playerPrivate) || (recipients.testFlag(GameEventStorageItem::SendToOthers) && !playerPrivate))
p->sendGameEvent(cont);
}
delete cont;
}
void Server_Game::sendGameEventToPlayer(Server_Player *player, GameEvent *event)
GameEventContainer *Server_Game::prepareGameEvent(const ::google::protobuf::Message &gameEvent, int playerId, GameEventContext *context)
{
player->sendProtocolItem(new GameEventContainer(QList<GameEvent *>() << event, gameId));
GameEventContainer *cont = new GameEventContainer;
cont->set_game_id(gameId);
if (context)
cont->mutable_context()->CopyFrom(*context);
GameEvent *event = cont->add_event_list();
if (playerId != -1)
event->set_player_id(playerId);
event->GetReflection()->MutableMessage(event, gameEvent.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(gameEvent);
return cont;
}
ServerInfo_Game *Server_Game::getInfo() const
ServerInfo_Game Server_Game::getInfo() const
{
QMutexLocker locker(&gameMutex);
if (players.isEmpty())
// Game is closing
return new ServerInfo_Game(room->getId(), getGameId(), QString(), false, 0, getMaxPlayers(), false, QList<GameTypeId *>(), 0, false, 0);
else {
// Game is open
QList<GameTypeId *> gameTypeList;
ServerInfo_Game result;
result.set_room_id(room->getId());
result.set_game_id(getGameId());
result.set_max_players(getMaxPlayers());
if (!players.isEmpty()) {
for (int i = 0; i < gameTypes.size(); ++i)
gameTypeList.append(new GameTypeId(gameTypes[i]));
result.add_game_types(gameTypes[i]);
return new ServerInfo_Game(
room->getId(),
getGameId(),
getDescription(),
!getPassword().isEmpty(),
getPlayerCount(),
getMaxPlayers(),
gameStarted,
gameTypeList,
new ServerInfo_User(getCreatorInfo(), false),
onlyBuddies,
onlyRegistered,
getSpectatorsAllowed(),
getSpectatorsNeedPassword(),
getSpectatorCount()
);
result.set_description(getDescription().toStdString());
result.set_with_password(!getPassword().isEmpty());
result.set_player_count(getPlayerCount());
result.set_started(gameStarted);
result.mutable_creator_info()->CopyFrom(*getCreatorInfo());
result.set_only_buddies(onlyBuddies);
result.set_only_registered(onlyRegistered);
result.set_spectators_allowed(getSpectatorsAllowed());
result.set_spectators_need_password(getSpectatorsNeedPassword());
result.set_spectators_count(getSpectatorCount());
}
return result;
}