short LobbyGame::HandleJoinGameReply(ReadBuffer &theMsg)
{
	short aStatus = LobbyGameStatus_None;
	try
	{
		aStatus = theMsg.ReadShort();

		if(aStatus!=LobbyGameStatus_Success)
			return aStatus;

		unsigned short aNumPlayers = theMsg.ReadShort();
		for(int i=0; i<aNumPlayers; i++)
		{
			unsigned short aClientId = theMsg.ReadShort();
			LobbyClient *aClient = GetClient(aClientId);
			if(aClient!=NULL)
			{
				LobbyPlayerPtr aPlayer = LobbyPlayer::CreatePlayer();
				if(aPlayer->ReadData(theMsg))
					aClient->SetPlayer(aPlayer);
			}
		}

		HandleJoinGameReplyHook(theMsg);
	}
	catch(ReadBufferException&)
	{
		aStatus = LobbyGameStatus_UnpackFailure;
	}

	return aStatus;
}
void StagingLogic::HandlePlayerJoined(ReadBuffer &theMsg, LobbyClient *theSender)
{
	if(!theSender->IsCaptain(true))
		return;

	unsigned short aClientId = theMsg.ReadShort();
	LobbyClient *aClient = mGame->GetClient(aClientId);
	if(aClient==NULL)
		return;

	if(!aClient->IsPlayer()) 
	{
		LobbyPlayerPtr aPlayer = LobbyPlayer::CreatePlayer();
		if(!aPlayer->ReadData(theMsg))
			return;

		aClient->SetPlayer(aPlayer);
	}
	else
	{
		// already know about him (can happen if we're the captain or it's our client right after we joined)
		if(!IAmCaptain()) // already got ourselves in the join game reply
			return;
	}

	LobbyEvent::BroadcastEvent(new PlayerChangedEvent(aClient,LobbyChangeType_Add));
	if(aClient->SameTeam(mMyClient))
		LobbyEvent::BroadcastEvent(new PlayerTeamChangedEvent(aClient,LobbyChangeType_Add));

	mGame->HandlePlayerJoined(aClient);
	if(LobbyPersistentData::GetStagingSoundEffects())
		LobbyMisc::PlaySound(LobbyGlobal_PlayerJoined_Sound);

	if(mEveryoneReadySoundState && !aClient->IsPlayerReady())
		mEveryoneReadySoundState = false;

	UpdateStartButton();
}