void Server_Game::sendGameEventContainer(GameEventContainer *cont, GameEventStorageItem::EventRecipients recipients, int privatePlayerId)
{
	QMutexLocker locker(&gameMutex);
	
	cont->set_game_id(gameId);
	QMapIterator<int, Server_Player *> playerIterator(players);
	while (playerIterator.hasNext()) {
		Server_Player *p = playerIterator.next().value();
		const bool playerPrivate = (p->getPlayerId() == privatePlayerId) || (p->getSpectator() && spectatorsSeeEverything);
		if ((recipients.testFlag(GameEventStorageItem::SendToPrivate) && playerPrivate) || (recipients.testFlag(GameEventStorageItem::SendToOthers) && !playerPrivate))
			p->sendGameEvent(*cont);
	}
	if (recipients.testFlag(GameEventStorageItem::SendToPrivate)) {
		cont->set_seconds_elapsed(secondsElapsed - startTimeOfThisGame);
		cont->clear_game_id();
		currentReplay->add_event_list()->CopyFrom(*cont);
	}
	
	delete cont;
}
void GameEventStorage::sendToGame(Server_Game *game)
{
	if (gameEventList.isEmpty())
		return;
	
	GameEventContainer *contPrivate = new GameEventContainer;
	GameEventContainer *contOthers = new GameEventContainer;
	for (int i = 0; i < gameEventList.size(); ++i) {
		const GameEvent &event = gameEventList[i]->getGameEvent();
		const GameEventStorageItem::EventRecipients recipients = gameEventList[i]->getRecipients();
		if (recipients.testFlag(GameEventStorageItem::SendToPrivate))
			contPrivate->add_event_list()->CopyFrom(event);
		if (recipients.testFlag(GameEventStorageItem::SendToOthers))
			contOthers->add_event_list()->CopyFrom(event);
	}
	if (gameEventContext) {
		contPrivate->mutable_context()->CopyFrom(*gameEventContext);
		contOthers->mutable_context()->CopyFrom(*gameEventContext);
	}
	game->sendGameEventContainer(contPrivate, GameEventStorageItem::SendToPrivate, privatePlayerId);
	game->sendGameEventContainer(contOthers, GameEventStorageItem::SendToOthers, privatePlayerId);
}