void ServerEvents::OnUserQuit(const std::string& nick)
{
	slLogDebugFunc("");
	try {
		User& user = m_serv.GetUser(nick);
		IBattle* userbattle = user.GetBattle();
		if (userbattle) {
			int battleid = userbattle->GetID();
			try {
				if (&userbattle->GetFounder() == &user) {
					for (int i = 0; i < int(userbattle->GetNumUsers()); i++) {
						User& battleuser = userbattle->GetUser(i);
						OnUserLeftBattle(battleid, battleuser.GetNick());
					}
					OnBattleClosed(battleid);
				} else
					OnUserLeftBattle(battleid, user.GetNick());
			} catch (...) {
			}
		}
		ui().OnUserOffline(user);
		m_serv._RemoveUser(nick);
		if (useractions().DoActionOnUser(UserActions::ActNotifLogin, TowxString(nick)))
			actNotifBox(SL_MAIN_ICON, TowxString(nick) + _(" just went offline"));
	} catch (std::runtime_error& except) {
	}
}
void ServerEvents::OnUserStatus( const wxString& nick, UserStatus status )
{
    wxLogDebugFunc( _T("") );
    try
    {
        wxLogMessage( _T("calling m_serv.GetUser( nick ) ") );
        User& user = m_serv.GetUser( nick );
        wxLogMessage( _T("calling user.SetStatus( status ) ") );

        UserStatus oldStatus = user.GetStatus();
        user.SetStatus( status );
        if ( useractions().DoActionOnUser( UserActions::ActNotifStatus, nick ) )
        {
            wxString diffString = status.GetDiffString( oldStatus ) ;
            if ( diffString != wxEmptyString )
                actNotifBox( SL_MAIN_ICON, nick + _(" is now ") + diffString );
        }

        wxLogMessage( _T("calling ui().OnUserStatusChanged( user ) ") );
        ui().OnUserStatusChanged( user );
        wxLogMessage( _T("updating battles ") );

        if ( user.GetBattle() != 0 )
        {
            Battle& battle = *user.GetBattle();
            try
            {
            if ( battle.GetFounder().GetNick() == user.GetNick() )
            {
                if ( status.in_game != battle.GetInGame() )
                {
                    battle.SetInGame( status.in_game );
                    if ( status.in_game ) battle.StartSpring();
					else
						BattleEvents::GetBattleEventSender( BattleEvents::BattleInfoUpdate ).SendEvent( std::make_pair(user.GetBattle(),wxString()) );
                }
            }
            }catch(...){}
        }
    }
    catch (...)
    {
        wxLogWarning( _("OnUserStatus() failed ! (exception)") );
    }
}
void ServerEvents::OnNewUser( const wxString& nick, const wxString& country, int cpu, const wxString& id )
{
    wxLogDebugFunc( _T("") );
    try
    {
        ASSERT_LOGIC( !m_serv.UserExists( nick ), _T("New user from server, but already exists!") );
    }
    catch (...)
    {
        return;
    }
    User& user = m_serv._AddUser( nick );
    if ( useractions().DoActionOnUser( UserActions::ActNotifLogin, nick ) )
        actNotifBox( SL_MAIN_ICON, nick + _(" is online") );
    user.SetCountry( country );
    user.SetCpu( cpu );
		user.SetID( id );
    ui().OnUserOnline( user );
}
void ServerEvents::OnBattleOpened(int id, BattleType type, NatType nat, const std::string& nick,
				  const std::string& host, int port, int maxplayers,
				  bool haspass, int rank, const std::string& maphash, const std::string& engineName, const std::string& engineVersion, const std::string& map,
				  const std::string& title, const std::string& mod)
{
	slLogDebugFunc("");
	try {
		ASSERT_EXCEPTION(!m_serv.BattleExists(id), _T("New battle from server, but already exists!"));
		IBattle& battle = m_serv._AddBattle(id);

		User& user = m_serv.GetUser(nick);
		battle.OnUserAdded(user);

		battle.SetBattleType(type);
		battle.SetNatType(nat);
		battle.SetFounder(nick);
		battle.SetHostIp(host);
		battle.SetHostPort(port);
		battle.SetMaxPlayers(maxplayers);
		battle.SetIsPassworded(haspass);
		battle.SetRankNeeded(rank);
		battle.SetHostMap(map, maphash);
		battle.SetDescription(title);
		battle.SetHostGame(mod, "");
		battle.SetEngineName(engineName);
		battle.SetEngineVersion(engineVersion);

		if (useractions().DoActionOnUser(UserActions::ActNotifBattle, TowxString(user.GetNick()))) {
			actNotifBox(SL_MAIN_ICON, TowxString(user.GetNick()) + _(" opened battle ") + TowxString(title));
		}
		if (!m_serv.IsOnline()) { //login info isn't complete yet
			return;
		}

		ui().OnBattleOpened(battle);
		if (user.Status().in_game) {
			battle.SetInGame(true);
			battle.StartSpring();
		}
	} catch (std::runtime_error& except) {
	}
}
void ServerEvents::OnNewUser(const std::string& nick, const std::string& country, int cpu, int id)
{
	slLogDebugFunc("");
	if (m_serv.UserExists(nick)) {
		wxLogWarning("New user from server, but already exists: %s", nick.c_str());
		return;
	}
	User& user = m_serv._AddUser(nick);
	user.SetCountry(country);
	user.SetCpu(cpu);
	user.SetID(id);
	if (!m_serv.IsOnline()) { //login info isn't complete yet
		return;
	}
	ui().OnUserOnline(user);

	if (useractions().DoActionOnUser(UserActions::ActNotifLogin, TowxString(nick))) {
		actNotifBox(SL_MAIN_ICON, TowxString(nick) + _(" just connected"));
	}
}
void ServerEvents::OnUserStatus(const std::string& nick, UserStatus status)
{
	slLogDebugFunc("");
	try {
		User& user = m_serv.GetUser(nick);

		UserStatus oldStatus = user.GetStatus();
		user.SetStatus(status);
		if (useractions().DoActionOnUser(UserActions::ActNotifStatus, TowxString(nick))) {
			wxString diffString = TowxString(status.GetDiffString(oldStatus));
			if (diffString != wxEmptyString)
				actNotifBox(SL_MAIN_ICON, TowxString(nick) + _(" is now ") + diffString);
		}

		if (m_serv.IsOnline()) { //login info isn't complete yet
			ui().OnUserStatusChanged(user);
		}

		if (user.GetBattle() != 0) {
			IBattle& battle = *user.GetBattle();
			try {
				if (battle.GetFounder().GetNick() == user.GetNick()) {
					if (status.in_game != battle.GetInGame()) {
						battle.SetInGame(status.in_game);
						if (m_serv.IsOnline()) {
							if (status.in_game) {
								battle.StartSpring();
							} else {
								ui().OnBattleInfoUpdated(battle, wxEmptyString);
							}
						}
					}
				}
			} catch (...) {
			}
		}
	} catch (...) {
		wxLogWarning(_("OnUserStatus() failed ! (exception)"));
	}
}
void ServerEvents::OnBattleOpened( int id, BattleType type, NatType nat, const wxString& nick,
                                   const wxString& host, int port, int maxplayers,
                                   bool haspass, int rank, const wxString& maphash, const wxString& map,
                                   const wxString& title, const wxString& mod )
{
    wxLogDebugFunc( _T("") );
    try
    {
        ASSERT_EXCEPTION( !m_serv.BattleExists( id ), _T("New battle from server, but already exists!") );
        Battle& battle = m_serv._AddBattle( id );

        User& user = m_serv.GetUser( nick );
        battle.OnUserAdded( user );

        battle.SetBattleType( type );
        battle.SetNatType( nat );
        battle.SetFounder( nick );
        battle.SetHostIp( host );
        battle.SetHostPort( port );
        battle.SetMaxPlayers( maxplayers );
        battle.SetIsPassworded( haspass );
        battle.SetRankNeeded( rank );
        battle.SetHostMap( map, maphash );
        battle.SetDescription( title );
        battle.SetHostMod( mod, wxEmptyString );

        if ( useractions().DoActionOnUser( UserActions::ActNotifBattle, user.GetNick() ) )
            actNotifBox( SL_MAIN_ICON, user.GetNick() + _(" opened battle ") + title );

        ui().OnBattleOpened( battle );
        if ( user.Status().in_game )
        {
            battle.SetInGame( true );
            battle.StartSpring();
        }
    }
    catch (std::runtime_error &except)
    {
    }
}