GameEventContainer *Server_Game::prepareGameEvent(const ::google::protobuf::Message &gameEvent, int playerId, GameEventContext *context)
{
	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;
}
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);
}