示例#1
0
void Server_Game::getInfo(ServerInfo_Game &result) const
{
	QMutexLocker locker(&gameMutex);
	
	result.set_room_id(room->getId());
	result.set_game_id(gameId);
	if (gameClosed)
		result.set_closed(true);
	else {
		for (int i = 0; i < gameTypes.size(); ++i)
			result.add_game_types(gameTypes[i]);
		
		result.set_max_players(getMaxPlayers());
		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_can_chat(spectatorsCanTalk);
		result.set_spectators_omniscient(spectatorsSeeEverything);
		result.set_spectators_count(getSpectatorCount());
		result.set_start_time(startTime.toTime_t());
	}
}
示例#2
0
void Server_Game::removePlayer(Server_Player *player)
{
	room->getServer()->removePersistentPlayer(QString::fromStdString(player->getUserInfo()->name()), room->getId(), gameId, player->getPlayerId());
	players.remove(player->getPlayerId());
	
	GameEventStorage ges;
	removeArrowsRelatedToPlayer(ges, player);
	unattachCards(ges, player);
	ges.enqueueGameEvent(Event_Leave(), player->getPlayerId());
	ges.sendToGame(this);
	
	bool playerActive = activePlayer == player->getPlayerId();
	bool playerHost = hostId == player->getPlayerId();
	bool spectator = player->getSpectator();
	player->prepareDestroy();
	
	if (!getPlayerCount()) {
		gameClosed = true;
		deleteLater();
		return;
	} else if (!spectator) {
		if (playerHost) {
			int newHostId = -1;
			QMapIterator<int, Server_Player *> playerIterator(players);
			while (playerIterator.hasNext()) {
				Server_Player *p = playerIterator.next().value();
				if (!p->getSpectator()) {
					newHostId = p->getPlayerId();
					break;
				}
			}
			if (newHostId != -1) {
				hostId = newHostId;
				sendGameEventContainer(prepareGameEvent(Event_GameHostChanged(), hostId));
			}
		}
		stopGameIfFinished();
		if (gameStarted && playerActive)
			nextTurn();
	}
	
	ServerInfo_Game gameInfo;
	gameInfo.set_room_id(room->getId());
	gameInfo.set_game_id(gameId);
	gameInfo.set_player_count(getPlayerCount());
	gameInfo.set_spectators_count(getSpectatorCount());
	emit gameInfoChanged(gameInfo);
}
示例#3
0
void Server_Game::addPlayer(Server_AbstractUserInterface *userInterface, ResponseContainer &rc, bool spectator, bool broadcastUpdate)
{
	QMutexLocker locker(&gameMutex);
	
	Server_Player *newPlayer = new Server_Player(this, nextPlayerId++, userInterface->copyUserInfo(true), spectator, userInterface);
	newPlayer->moveToThread(thread());
	
	Event_Join joinEvent;
	newPlayer->getProperties(*joinEvent.mutable_player_properties(), true);
	sendGameEventContainer(prepareGameEvent(joinEvent, -1));
	
	const QString playerName = QString::fromStdString(newPlayer->getUserInfo()->name());
	if (spectator)
		allSpectatorsEver.insert(playerName);
	else
		allPlayersEver.insert(playerName);
	players.insert(newPlayer->getPlayerId(), newPlayer);
	if (newPlayer->getUserInfo()->name() == creatorInfo->name()) {
		hostId = newPlayer->getPlayerId();
		sendGameEventContainer(prepareGameEvent(Event_GameHostChanged(), hostId));
	}
	
	if (broadcastUpdate) {
		ServerInfo_Game gameInfo;
		gameInfo.set_room_id(room->getId());
		gameInfo.set_game_id(gameId);
		gameInfo.set_player_count(getPlayerCount());
		gameInfo.set_spectators_count(getSpectatorCount());
		emit gameInfoChanged(gameInfo);
	}
	
	if ((newPlayer->getUserInfo()->user_level() & ServerInfo_User::IsRegistered) && !spectator)
		room->getServer()->addPersistentPlayer(playerName, room->getId(), gameId, newPlayer->getPlayerId());
	
	userInterface->playerAddedToGame(gameId, room->getId(), newPlayer->getPlayerId());
	
	createGameJoinedEvent(newPlayer, rc, false);
}