From 4a37b4a4aea358b9e03f755644208ab9537b9e21 Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Thu, 29 Jun 2017 17:59:07 +0300 Subject: [PATCH] [partners_api] taxi engine city restrictions --- map/CMakeLists.txt | 2 + map/framework.cpp | 20 +++ map/framework.hpp | 7 +- map/map.pro | 2 + map/place_page_info.cpp | 7 +- map/place_page_info.hpp | 7 +- map/taxi_delegate.cpp | 32 ++++ map/taxi_delegate.hpp | 25 +++ partners_api/CMakeLists.txt | 4 + partners_api/partners_api.pro | 4 + .../partners_api_tests/taxi_engine_tests.cpp | 58 ++++--- partners_api/taxi_base.cpp | 32 ++++ partners_api/taxi_base.hpp | 28 +++- partners_api/taxi_countries.cpp | 30 ++++ partners_api/taxi_countries.hpp | 27 ++++ partners_api/taxi_engine.cpp | 78 ++++----- partners_api/taxi_engine.hpp | 48 +++--- partners_api/taxi_places.hpp | 149 ++++++++++++++++++ partners_api/uber_api.hpp | 3 +- partners_api/yandex_api.cpp | 2 + partners_api/yandex_api.hpp | 5 +- xcode/map/map.xcodeproj/project.pbxproj | 12 ++ .../partners_api.xcodeproj/project.pbxproj | 16 ++ 23 files changed, 504 insertions(+), 94 deletions(-) create mode 100644 map/taxi_delegate.cpp create mode 100644 map/taxi_delegate.hpp create mode 100644 partners_api/taxi_base.cpp create mode 100644 partners_api/taxi_countries.cpp create mode 100644 partners_api/taxi_countries.hpp create mode 100644 partners_api/taxi_places.hpp diff --git a/map/CMakeLists.txt b/map/CMakeLists.txt index 096f553145..5d35873ce1 100644 --- a/map/CMakeLists.txt +++ b/map/CMakeLists.txt @@ -58,6 +58,8 @@ set( routing_manager.hpp routing_mark.cpp routing_mark.hpp + taxi_delegate.cpp + taxi_delegate.hpp track.cpp track.hpp traffic_manager.cpp diff --git a/map/framework.cpp b/map/framework.cpp index 2ef27fa5c7..e2401da891 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -5,6 +5,7 @@ #include "map/geourl_process.hpp" #include "map/gps_tracker.hpp" #include "map/mwm_tree.hpp" +#include "map/taxi_delegate.hpp" #include "map/user_mark.hpp" #include "defines.hpp" @@ -354,6 +355,7 @@ void Framework::Migrate(bool keepDownloaded) m_selectedFeature = FeatureID(); m_searchEngine.reset(); m_infoGetter.reset(); + m_taxiEngine.reset(); TCountriesVec existedCountries; GetStorage().DeleteAllLocalMaps(&existedCountries); DeregisterAllMaps(); @@ -362,6 +364,7 @@ void Framework::Migrate(bool keepDownloaded) InitCountryInfoGetter(); InitSearchEngine(); RegisterAllMaps(); + InitTaxiEngine(); m_trafficManager.SetCurrentDataVersion(GetStorage().GetCurrentDataVersion()); if (m_drapeEngine && m_isRenderingEnabled) @@ -475,6 +478,7 @@ Framework::Framework(FrameworkParams const & params) InitTransliteration(); LOG(LDEBUG, ("Transliterators initialized")); + InitTaxiEngine(); } Framework::~Framework() @@ -921,6 +925,10 @@ void Framework::FillInfoFromFeatureType(FeatureType const & ft, place_page::Info { info.m_localAdsStatus = place_page::LocalAdsStatus::NotAvailable; } + + auto const latlon = MercatorBounds::ToLatLon(feature::GetCenter(ft)); + ASSERT(m_taxiEngine, ()); + info.m_reachableByProviders = m_taxiEngine->GetProvidersAtPos(latlon); } void Framework::FillApiMarkInfo(ApiMarkPoint const & api, place_page::Info & info) const @@ -3295,3 +3303,15 @@ void Framework::RegisterCountryFilesOnRoute(std::shared_ptr m_storage.ForEachCountryFile( [&ptr](platform::CountryFile const & file) { ptr->RegisterFile(file); }); } + +void Framework::InitTaxiEngine() +{ + ASSERT(!m_taxiEngine, ()); + ASSERT(m_infoGetter, ()); + ASSERT(m_cityFinder, ()); + + m_taxiEngine = my::make_unique(); + + m_taxiEngine->SetDelegate( + my::make_unique(GetStorage(), *m_infoGetter, *m_cityFinder)); +} diff --git a/map/framework.hpp b/map/framework.hpp index 60c3f9bfc4..343f23972a 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -181,7 +181,6 @@ protected: unique_ptr m_bookingApi = make_unique(); unique_ptr m_viatorApi = make_unique(); - unique_ptr m_taxiEngine = make_unique(); df::DrapeApi m_drapeApi; @@ -836,4 +835,10 @@ public: private: std::unique_ptr m_cityFinder; unique_ptr m_adsEngine; + // The order matters here: storage::CountryInfoGetter and + // search::CityFinder must be initialized before + // taxi::Engine and, therefore, destroyed after taxi::Engine. + unique_ptr m_taxiEngine; + + void InitTaxiEngine(); }; diff --git a/map/map.pro b/map/map.pro index 9c6d62d46d..9bdb9778d4 100644 --- a/map/map.pro +++ b/map/map.pro @@ -33,6 +33,7 @@ HEADERS += \ reachable_by_taxi_checker.hpp \ routing_manager.hpp \ routing_mark.hpp \ + taxi_delegate.hpp \ track.hpp \ traffic_manager.hpp \ user_mark.hpp \ @@ -64,6 +65,7 @@ SOURCES += \ reachable_by_taxi_checker.cpp \ routing_manager.cpp \ routing_mark.cpp \ + taxi_delegate.cpp \ track.cpp \ traffic_manager.cpp \ user_mark.cpp \ diff --git a/map/place_page_info.cpp b/map/place_page_info.cpp index 5f3731e4b4..dd9b29948b 100644 --- a/map/place_page_info.cpp +++ b/map/place_page_info.cpp @@ -215,9 +215,12 @@ vector Info::GetBanners() const return m_adsEngine->GetBanners(m_types, m_topmostCountryIds, languages::GetCurrentNorm()); } -bool Info::IsReachableByTaxi() const +std::vector Info::ReachableByTaxiProviders() const { - return IsReachableByTaxiChecker::Instance()(m_types); + if (!IsReachableByTaxiChecker::Instance()(m_types)) + return {}; + + return m_reachableByProviders; } void Info::SetMercator(m2::PointD const & mercator) { m_mercator = mercator; } diff --git a/map/place_page_info.hpp b/map/place_page_info.hpp index 442f871e75..1b2e2a3eac 100644 --- a/map/place_page_info.hpp +++ b/map/place_page_info.hpp @@ -1,6 +1,9 @@ #pragma once #include "map/bookmark.hpp" + +#include "partners_api/taxi_provider.hpp" + #include "map/routing_mark.hpp" #include "storage/index.hpp" @@ -102,7 +105,7 @@ public: bool HasBanner() const; vector GetBanners() const; - bool IsReachableByTaxi() const; + std::vector ReachableByTaxiProviders() const; void SetMercator(m2::PointD const & mercator); @@ -169,5 +172,7 @@ public: LocalAdsStatus m_localAdsStatus = LocalAdsStatus::NotAvailable; string m_localAdsUrl; + + std::vector m_reachableByProviders; }; } // namespace place_page diff --git a/map/taxi_delegate.cpp b/map/taxi_delegate.cpp new file mode 100644 index 0000000000..2e9ea9a978 --- /dev/null +++ b/map/taxi_delegate.cpp @@ -0,0 +1,32 @@ +#include "map/taxi_delegate.hpp" + +#include "map/city_finder.hpp" + +#include "storage/country_info_getter.hpp" +#include "storage/index.hpp" +#include "storage/storage.hpp" + +#include "coding/multilang_utf8_string.hpp" + +#include "geometry/mercator.hpp" + +TaxiDelegate::TaxiDelegate(storage::Storage const & st, storage::CountryInfoGetter const & ig, + CityFinder & cf) + : m_storage(st), m_infoGetter(ig), m_cityFinder(cf) +{ +} + +storage::TCountriesVec TaxiDelegate::GetCountryIds(ms::LatLon const & latlon) +{ + m2::PointD const point = MercatorBounds::FromLatLon(latlon); + auto const countryId = m_infoGetter.GetRegionCountryId(point); + storage::TCountriesVec topmostCountryIds; + m_storage.GetTopmostNodesFor(countryId, topmostCountryIds); + return topmostCountryIds; +} + +std::string TaxiDelegate::GetCityName(ms::LatLon const & latlon) +{ + m2::PointD const point = MercatorBounds::FromLatLon(latlon); + return m_cityFinder.GetCityName(point, StringUtf8Multilang::kEnglishCode); +} diff --git a/map/taxi_delegate.hpp b/map/taxi_delegate.hpp new file mode 100644 index 0000000000..8b2c08f456 --- /dev/null +++ b/map/taxi_delegate.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "partners_api/taxi_engine.hpp" + +namespace storage +{ +class Storage; +class CountryInfoGetter; +} + +class CityFinder; + +class TaxiDelegate : public taxi::Delegate +{ +public: + TaxiDelegate(storage::Storage const & st, storage::CountryInfoGetter const & ig, CityFinder & cf); + + storage::TCountriesVec GetCountryIds(ms::LatLon const & latlon) override; + std::string GetCityName(ms::LatLon const & latlon) override; + +private: + storage::Storage const & m_storage; + storage::CountryInfoGetter const & m_infoGetter; + CityFinder & m_cityFinder; +}; diff --git a/partners_api/CMakeLists.txt b/partners_api/CMakeLists.txt index 72569e7b53..751f38831b 100644 --- a/partners_api/CMakeLists.txt +++ b/partners_api/CMakeLists.txt @@ -19,9 +19,13 @@ set( opentable_api.hpp rb_ads.cpp rb_ads.hpp + taxi_base.cpp taxi_base.hpp + taxi_countries.cpp + taxi_countries.hpp taxi_engine.cpp taxi_engine.hpp + taxi_places.hpp taxi_provider.hpp uber_api.cpp uber_api.hpp diff --git a/partners_api/partners_api.pro b/partners_api/partners_api.pro index 2ef7c68cea..bda026f3b6 100644 --- a/partners_api/partners_api.pro +++ b/partners_api/partners_api.pro @@ -16,6 +16,8 @@ SOURCES += \ mopub_ads.cpp \ opentable_api.cpp \ rb_ads.cpp \ + taxi_base.cpp \ + taxi_countries.cpp \ taxi_engine.cpp \ uber_api.cpp \ viator_api.cpp \ @@ -31,7 +33,9 @@ HEADERS += \ opentable_api.hpp \ rb_ads.hpp \ taxi_base.hpp \ + taxi_countries.hpp \ taxi_engine.hpp \ + taxi_places.hpp \ taxi_provider.hpp \ uber_api.hpp \ viator_api.cpp \ diff --git a/partners_api/partners_api_tests/taxi_engine_tests.cpp b/partners_api/partners_api_tests/taxi_engine_tests.cpp index 99ba26a908..d672053d84 100644 --- a/partners_api/partners_api_tests/taxi_engine_tests.cpp +++ b/partners_api/partners_api_tests/taxi_engine_tests.cpp @@ -6,10 +6,18 @@ #include "geometry/latlon.hpp" -#include "base/scope_guard.hpp" +#include "base/stl_add.hpp" namespace { +class TaxiDelegateForTrsting : public taxi::Delegate +{ +public: + storage::TCountriesVec GetCountryIds(ms::LatLon const & latlon) override { return {"Belarus"}; } + + std::string GetCityName(ms::LatLon const & latlon) override { return "Minsk"; } +}; + std::vector GetUberSynchronous(ms::LatLon const & from, ms::LatLon const & to, std::string const & url) { @@ -46,13 +54,24 @@ std::vector GetYandexSynchronous(ms::LatLon const & from, ms::Lat return yandexProducts; } -taxi::ProvidersContainer GetProvidersSynchronous(ms::LatLon const & from, ms::LatLon const & to, +taxi::ProvidersContainer GetProvidersSynchronous(taxi::Engine const & engine, + ms::LatLon const & from, ms::LatLon const & to, std::string const & url) { taxi::ProvidersContainer providers; - providers.emplace_back(taxi::Provider::Type::Uber, GetUberSynchronous(from, to, url)); - providers.emplace_back(taxi::Provider::Type::Yandex, GetYandexSynchronous(from, to, url)); + for (auto const provider : engine.GetProvidersAtPos(from)) + { + switch (provider) + { + case taxi::Provider::Type::Uber: + providers.emplace_back(taxi::Provider::Type::Uber, GetUberSynchronous(from, to, url)); + break; + case taxi::Provider::Type::Yandex: + providers.emplace_back(taxi::Provider::Type::Yandex, GetYandexSynchronous(from, to, url)); + break; + } + } return providers; } @@ -236,33 +255,36 @@ UNIT_TEST(TaxiEngine_Smoke) testing::StopEventLoop(); }; - taxi::ProvidersContainer const synchronousProviders = GetProvidersSynchronous(from, to, kTesturl); + taxi::Engine engine( + {{taxi::Provider::Type::Uber, kTesturl}, {taxi::Provider::Type::Yandex, kTesturl}}); + + engine.SetDelegate(my::make_unique()); + + taxi::ProvidersContainer const synchronousProviders = + GetProvidersSynchronous(engine, from, to, kTesturl); { - taxi::Engine engine( - {{taxi::Provider::Type::Uber, kTesturl}, {taxi::Provider::Type::Yandex, kTesturl}}); { lock_guard lock(resultsMutex); - reqId = engine.GetAvailableProducts( - ms::LatLon(55.753960, 37.624513), ms::LatLon(55.765866, 37.661270), - {"Brazil", "Russian Federation"}, standardCallback, errorPossibleCallback); + reqId = engine.GetAvailableProducts(ms::LatLon(55.753960, 37.624513), + ms::LatLon(55.765866, 37.661270), standardCallback, + errorPossibleCallback); } { lock_guard lock(resultsMutex); - reqId = engine.GetAvailableProducts( - ms::LatLon(59.922445, 30.367201), ms::LatLon(59.943675, 30.361123), - {"Brazil", "Russian Federation"}, standardCallback, errorPossibleCallback); + reqId = engine.GetAvailableProducts(ms::LatLon(59.922445, 30.367201), + ms::LatLon(59.943675, 30.361123), standardCallback, + errorPossibleCallback); } { lock_guard lock(resultsMutex); - reqId = engine.GetAvailableProducts( - ms::LatLon(52.509621, 13.450067), ms::LatLon(52.510811, 13.409490), - {"Brazil", "Russian Federation"}, standardCallback, errorPossibleCallback); + reqId = engine.GetAvailableProducts(ms::LatLon(52.509621, 13.450067), + ms::LatLon(52.510811, 13.409490), standardCallback, + errorPossibleCallback); } { lock_guard lock(resultsMutex); - reqId = engine.GetAvailableProducts(from, to, {"Brazil", "Russian Federation"}, lastCallback, - errorCallback); + reqId = engine.GetAvailableProducts(from, to, lastCallback, errorCallback); } } diff --git a/partners_api/taxi_base.cpp b/partners_api/taxi_base.cpp new file mode 100644 index 0000000000..80402aacdd --- /dev/null +++ b/partners_api/taxi_base.cpp @@ -0,0 +1,32 @@ +#include "partners_api/taxi_base.hpp" + +namespace taxi +{ +bool ApiItem::AreAllCountriesDisabled(storage::TCountriesVec const & countryIds, + std::string const & city) const +{ + if (m_disabledCountries.IsEmpty()) + return false; + + bool isCountryDisabled = true; + for (auto const & countryId : countryIds) + isCountryDisabled = isCountryDisabled && m_disabledCountries.Has(countryId, city); + + return isCountryDisabled; +} + +bool ApiItem::IsAnyCountryEnabled(storage::TCountriesVec const & countryIds, + std::string const & city) const +{ + if (m_enabledCountries.IsEmpty()) + return true; + + for (auto const & countryId : countryIds) + { + if (m_enabledCountries.Has(countryId, city)) + return true; + } + + return false; +} +} // namespace taxi diff --git a/partners_api/taxi_base.hpp b/partners_api/taxi_base.hpp index 29c73e6bb7..9d1a7cead2 100644 --- a/partners_api/taxi_base.hpp +++ b/partners_api/taxi_base.hpp @@ -1,5 +1,6 @@ #pragma once +#include "partners_api/taxi_countries.hpp" #include "partners_api/taxi_provider.hpp" #include @@ -29,6 +30,7 @@ struct RideRequestLinks class ApiBase { public: + ApiBase(std::string const & baseUrl) : m_baseUrl(baseUrl) {} virtual ~ApiBase() = default; /// Requests list of available products. Returns request identificator immediately. @@ -41,7 +43,31 @@ public: ms::LatLon const & from, ms::LatLon const & to) const = 0; -private: +protected: std::string const m_baseUrl; }; + +using ApiPtr = std::unique_ptr; + +struct ApiItem +{ + ApiItem(Provider::Type type, ApiPtr && api, Countries const & enabled, Countries const & disabled) + : m_type(type) + , m_api(std::move(api)) + , m_enabledCountries(enabled) + , m_disabledCountries(disabled) + { + } + + bool AreAllCountriesDisabled(storage::TCountriesVec const & countryIds, + std::string const & city) const; + bool IsAnyCountryEnabled(storage::TCountriesVec const & countryIds, + std::string const & city) const; + + Provider::Type m_type; + ApiPtr m_api; + + Countries m_enabledCountries; + Countries m_disabledCountries; +}; } // namespace taxi diff --git a/partners_api/taxi_countries.cpp b/partners_api/taxi_countries.cpp new file mode 100644 index 0000000000..3c40e24fab --- /dev/null +++ b/partners_api/taxi_countries.cpp @@ -0,0 +1,30 @@ +#include "partners_api/taxi_countries.hpp" + +#include + +namespace taxi +{ +Countries::Countries(std::vector const & countries) : m_countries(countries) {} + +bool Countries::IsEmpty() const { return m_countries.empty(); } + +bool Countries::Has(storage::TCountryId id, std::string const & city) const +{ + auto const countryIt = + std::find_if(m_countries.cbegin(), m_countries.cend(), + [&id](Country const & country) { return country.m_id == id; }); + + if (countryIt == m_countries.cend()) + return false; + + auto const & cities = countryIt->m_cities; + + if (cities.empty()) + return true; + + auto const cityIt = std::find_if(cities.cbegin(), cities.cend(), + [&city](std::string const & c) { return c == city; }); + + return cityIt != cities.cend(); +} +} // namespace taxi diff --git a/partners_api/taxi_countries.hpp b/partners_api/taxi_countries.hpp new file mode 100644 index 0000000000..e043c6bea7 --- /dev/null +++ b/partners_api/taxi_countries.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "storage/index.hpp" + +#include +#include + +namespace taxi +{ +class Countries +{ +public: + struct Country + { + storage::TCountryId m_id; + std::vector m_cities; + }; + + Countries(std::vector const & countries); + + bool IsEmpty() const; + bool Has(storage::TCountryId id, std::string const & city) const; + +private: + std::vector m_countries; +}; +} // namespace taxi diff --git a/partners_api/taxi_engine.cpp b/partners_api/taxi_engine.cpp index 719f428a22..8a285be737 100644 --- a/partners_api/taxi_engine.cpp +++ b/partners_api/taxi_engine.cpp @@ -1,4 +1,5 @@ #include "partners_api/taxi_engine.hpp" +#include "partners_api/taxi_places.hpp" #include "partners_api/uber_api.hpp" #include "partners_api/yandex_api.hpp" @@ -8,6 +9,7 @@ #include #include #include +#include namespace { @@ -109,21 +111,20 @@ void ResultMaker::DecrementRequestCount() // Engine ----------------------------------------------------------------------------------------- Engine::Engine(std::vector urls /* = {} */) { - m_enabledCountries = {{Provider::Type::Yandex, {"Russian Federation"}}}; - m_disabledCountries = {{Provider::Type::Uber, {"Russian Federation"}}}; - - AddApi(urls, Provider::Type::Yandex); - AddApi(urls, Provider::Type::Uber); + AddApi(urls, Provider::Type::Yandex, places::kPlaces, {{{}}}); + AddApi(urls, Provider::Type::Uber, {{{}}}, places::kPlaces); } +void Engine::SetDelegate(std::unique_ptr delegate) { m_delegate = std::move(delegate); } + /// Requests list of available products. Returns request identificator immediately. uint64_t Engine::GetAvailableProducts(ms::LatLon const & from, ms::LatLon const & to, - storage::TCountriesVec const & countryIds, SuccessCallback const & successFn, ErrorCallback const & errorFn) { ASSERT(successFn, ()); ASSERT(errorFn, ()); + ASSERT(m_delegate, ()); auto const reqId = ++m_requestId; auto const maker = m_maker; @@ -133,7 +134,7 @@ uint64_t Engine::GetAvailableProducts(ms::LatLon const & from, ms::LatLon const { auto type = api.m_type; - if (AreAllCountriesDisabled(type, countryIds) || !IsAnyCountryEnabled(type, countryIds)) + if (!IsAvailableAtPos(type, from)) { maker->DecrementRequestCount(reqId); maker->MakeResult(reqId); @@ -169,50 +170,51 @@ RideRequestLinks Engine::GetRideRequestLinks(Provider::Type type, std::string co return it->m_api->GetRideRequestLinks(productId, from, to); } -bool Engine::AreAllCountriesDisabled(Provider::Type type, - storage::TCountriesVec const & countryIds) const +std::vector Engine::GetProvidersAtPos(ms::LatLon const & pos) const { - auto const it = - FindByProviderType(type, m_disabledCountries.cbegin(), m_disabledCountries.cend()); + std::vector result; - if (it == m_disabledCountries.end()) - return false; - - auto const & disabledCountries = it->m_countries; - bool isCountryDisabled = true; - for (auto const & countryId : countryIds) + for (auto const & api : m_apis) { - auto const countryIt = - std::find(disabledCountries.cbegin(), disabledCountries.cend(), countryId); - - isCountryDisabled = isCountryDisabled && countryIt != disabledCountries.cend(); + if (IsAvailableAtPos(api.m_type, pos)) + result.push_back(api.m_type); } - return isCountryDisabled; + return result; } -bool Engine::IsAnyCountryEnabled(Provider::Type type, - storage::TCountriesVec const & countryIds) const +bool Engine::IsAvailableAtPos(Provider::Type type, ms::LatLon const & pos) const { - auto const it = FindByProviderType(type, m_enabledCountries.cbegin(), m_enabledCountries.cend()); + return !AreAllCountriesDisabled(type, pos) && IsAnyCountryEnabled(type, pos); +} - if (it == m_enabledCountries.end()) - return true; +bool Engine::AreAllCountriesDisabled(Provider::Type type, ms::LatLon const & latlon) const +{ + auto const it = FindByProviderType(type, m_apis.cbegin(), m_apis.cend()); - auto const & enabledCountries = it->m_countries; - for (auto const & countryId : countryIds) - { - auto const countryIt = std::find(enabledCountries.cbegin(), enabledCountries.cend(), countryId); + CHECK(it != m_apis.cend(), ()); - if (countryIt != enabledCountries.cend()) - return true; - } + auto const countryIds = m_delegate->GetCountryIds(latlon); + auto const city = m_delegate->GetCityName(latlon); - return false; + return it->AreAllCountriesDisabled(countryIds, city); +} + +bool Engine::IsAnyCountryEnabled(Provider::Type type, ms::LatLon const & latlon) const +{ + auto const it = FindByProviderType(type, m_apis.cbegin(), m_apis.cend()); + + CHECK(it != m_apis.cend(), ()); + + auto const countryIds = m_delegate->GetCountryIds(latlon); + auto const city = m_delegate->GetCityName(latlon); + + return it->IsAnyCountryEnabled(countryIds, city); } template -void Engine::AddApi(std::vector const & urls, Provider::Type type) +void Engine::AddApi(std::vector const & urls, Provider::Type type, + Countries const & enabled, Countries const & disabled) { auto const it = std::find_if(urls.cbegin(), urls.cend(), [type](ProviderUrl const & item) { @@ -220,8 +222,8 @@ void Engine::AddApi(std::vector const & urls, Provider::Type type) }); if (it != urls.cend()) - m_apis.emplace_back(type, my::make_unique(it->m_url)); + m_apis.emplace_back(type, my::make_unique(it->m_url), enabled, disabled); else - m_apis.emplace_back(type, my::make_unique()); + m_apis.emplace_back(type, my::make_unique(), enabled, disabled); } } // namespace taxi diff --git a/partners_api/taxi_engine.hpp b/partners_api/taxi_engine.hpp index df63d83977..85d8a4d453 100644 --- a/partners_api/taxi_engine.hpp +++ b/partners_api/taxi_engine.hpp @@ -1,7 +1,5 @@ #pragma once -#include "storage/index.hpp" - #include "partners_api/taxi_base.hpp" #include @@ -16,6 +14,15 @@ using SuccessCallback = using ErrorCallback = std::function; +class Delegate +{ +public: + virtual ~Delegate() = default; + + virtual storage::TCountriesVec GetCountryIds(ms::LatLon const & latlon) = 0; + virtual std::string GetCityName(ms::LatLon const & latlon) = 0; +}; + /// This class is used to collect replies from all taxi apis and to call callback when all replies /// are collected. The methods are called in callbacks on different threads, so synchronization is /// required. @@ -60,48 +67,35 @@ class Engine final public: explicit Engine(std::vector urls = {}); + void SetDelegate(std::unique_ptr delegate); + /// Requests list of available products. Returns request identificator immediately. uint64_t GetAvailableProducts(ms::LatLon const & from, ms::LatLon const & to, - storage::TCountriesVec const & countryIds, SuccessCallback const & successFn, ErrorCallback const & errorFn); /// Returns link which allows you to launch some taxi app. RideRequestLinks GetRideRequestLinks(Provider::Type type, std::string const & productId, ms::LatLon const & from, ms::LatLon const & to) const; + std::vector GetProvidersAtPos(ms::LatLon const & pos) const; + private: - bool AreAllCountriesDisabled(Provider::Type type, - storage::TCountriesVec const & countryIds) const; - bool IsAnyCountryEnabled(Provider::Type type, storage::TCountriesVec const & countryIds) const; + bool IsAvailableAtPos(Provider::Type type, ms::LatLon const & pos) const; + bool AreAllCountriesDisabled(Provider::Type type, ms::LatLon const & latlon) const; + bool IsAnyCountryEnabled(Provider::Type type, ms::LatLon const & latlon) const; template - void AddApi(std::vector const & urls, Provider::Type type); + void AddApi(std::vector const & urls, Provider::Type type, Countries const & enabled, + Countries const & disabled); - using ApiPtr = std::unique_ptr; - - struct ApiContainerItem - { - ApiContainerItem(Provider::Type type, ApiPtr && api) : m_type(type), m_api(std::move(api)) {} - - Provider::Type m_type; - ApiPtr m_api; - }; - - struct SupportedCountriesItem - { - Provider::Type m_type; - storage::TCountriesVec m_countries; - }; - - std::vector m_apis; - - std::vector m_enabledCountries; - std::vector m_disabledCountries; + std::vector m_apis; // Id for currently processed request. uint64_t m_requestId = 0; // Use single instance of maker for all requests, for this reason, // all outdated requests will be ignored. std::shared_ptr m_maker = std::make_shared(); + + std::unique_ptr m_delegate; }; } // namespace taxi diff --git a/partners_api/taxi_places.hpp b/partners_api/taxi_places.hpp new file mode 100644 index 0000000000..c61cff4391 --- /dev/null +++ b/partners_api/taxi_places.hpp @@ -0,0 +1,149 @@ +#pragma once + +#include "partners_api/taxi_countries.hpp" + +namespace taxi +{ +namespace places +{ +// Places which are supported by yandex taxi and in the same time are not supported by uber taxi. +Countries const kPlaces = { + {{"Armenia", {"Yerevan"}}, + {"Belarus", {"Minsk"}}, + {"Georgia Region", {"Tbilisi"}}, + {"Kazakhstan", {"Almaty", "Astana", "Karaganda", "Pavlodar", "Shymkent"}}, + {"Russian Federation", + {"Almetyevsk", + "Anapa", + "Aprelevka", + "Arzamas", + "Arkhangelsk", + "Astrakhan", + "Balakovo", + "Balashikha", + "Barnaul", + "Belgorod", + "Blagoveshchensk", + "Bryansk", + "Vidnoye", + "Vladivostok", + "Vladikavkaz", + "Vladimir", + "Volgograd", + "Volgodonsk", + "Vologda", + "Voronezh", + "Gelendzhik", + "Golitsyno", + "Dedovsk", + "Dzerzhinsky", + "Dmitrov", + "Dolgoprudny", + "Domodedovo", + "Dubna", + "Yekaterinburg", + "Zheleznodorozhny", + "Zhukovsky", + "Zelenograd", + "Ivanovo", + "Ivanteyevka", + "Izhevsk", + "Irkutsk", + "Yoshkar-Ola", + "Kazan", + "Kaliningrad", + "Kaluga", + "Kamensk-Uralsky", + "Kashira", + "Kemerovo", + "Kirov", + "Kislovodsk", + "Kolomna", + "Komsomolsk-on-Amur", + "Korolyov", + "Kotelniki", + "Krasnogorsk", + "Krasnodar", + "Krasnoznamensk", + "Krasnoyarsk", + "Kurgan", + "Kursk", + "Lipetsk", + "Lobnya", + "Lytkarino", + "Lyubertsy", + "Magnitogorsk", + "Makhachkala", + "Mozhaisk", + "Moscow", + "Murmansk", + "Mytishchi", + "Naberezhnye Chelny", + "Naro-Fominsk", + "Nakhabino", + "Nizhnekamsk", + "Nizhny Novgorod", + "Nizhny Tagil", + "Novokuznetsk", + "Novorossiysk", + "Novosibirsk", + "Novy Urengoy", + "Noginsk", + "Noyabrsk", + "Obninsk", + "Odintsovo", + "Ozyory", + "Omsk", + "Oryol", + "Orenburg", + "Orsk", + "Penza", + "Perm", + "Petrozavodsk", + "Podolsk", + "Pushkino", + "Ramenskoye", + "Reutov", + "Rostov-on-Don", + "Ryazan", + "Samara", + "Saint Petersburg", + "Saransk", + "Saratov", + "Sergiyev Posad", + "Serpukhov", + "Smolensk", + "Sochi", + "Stavropol", + "Staraya Kupavna", + "Sterlitamak", + "Stupino", + "Surgut", + "Syzran", + "Syktyvkar", + "Taganrog", + "Tambov", + "Tver", + "Tolyatti", + "Tomsk", + "Troitsk", + "Tula", + "Tyumen", + "Ulan-Ude", + "Ulyanovsk", + "Ufa", + "Fryazino", + "Khabarovsk", + "Khimki", + "Cheboksary", + "Chelyabinsk", + "Cherepovets", + "Chekhov", + "Chita", + "Shchyolkovo", + "Elektrostal", + "Elektrougli", + "Yakutsk", + "Yaroslavl"}}}}; +} // namespace places +} // namespace taxi diff --git a/partners_api/uber_api.hpp b/partners_api/uber_api.hpp index ededd975b4..62d906c346 100644 --- a/partners_api/uber_api.hpp +++ b/partners_api/uber_api.hpp @@ -71,7 +71,7 @@ private: class Api : public ApiBase { public: - explicit Api(std::string const & baseUrl = kEstimatesUrl) : m_baseUrl(baseUrl) {} + explicit Api(std::string const & baseUrl = kEstimatesUrl) : ApiBase(baseUrl) {} // ApiBase overrides: /// Requests list of available products from Uber. void GetAvailableProducts(ms::LatLon const & from, ms::LatLon const & to, @@ -85,7 +85,6 @@ public: private: shared_ptr m_maker = make_shared(); uint64_t m_requestId = 0; - string const m_baseUrl; }; void SetUberUrlForTesting(string const & url); diff --git a/partners_api/yandex_api.cpp b/partners_api/yandex_api.cpp index dbb2521447..c7cbdd26af 100644 --- a/partners_api/yandex_api.cpp +++ b/partners_api/yandex_api.cpp @@ -49,6 +49,8 @@ namespace taxi { namespace yandex { +Countries const kEnabledCountries = {{{}}}; + bool RawApi::GetTaxiInfo(ms::LatLon const & from, ms::LatLon const & to, std::string & result, std::string const & baseUrl /* = kTaxiInfoUrl */) { diff --git a/partners_api/yandex_api.hpp b/partners_api/yandex_api.hpp index 06791ebe78..cd1969eaca 100644 --- a/partners_api/yandex_api.hpp +++ b/partners_api/yandex_api.hpp @@ -26,7 +26,7 @@ public: class Api : public ApiBase { public: - explicit Api(std::string const & baseUrl = kTaxiInfoUrl) : m_baseUrl(baseUrl) {} + explicit Api(std::string const & baseUrl = kTaxiInfoUrl) : ApiBase(baseUrl) {} // ApiBase overrides: /// Requests list of available products from Yandex. void GetAvailableProducts(ms::LatLon const & from, ms::LatLon const & to, @@ -36,9 +36,6 @@ public: /// Returns link which allows you to launch the Yandex app. RideRequestLinks GetRideRequestLinks(std::string const & productId, ms::LatLon const & from, ms::LatLon const & to) const override; - -private: - std::string const m_baseUrl; }; void MakeFromJson(std::string const & src, std::vector & products); diff --git a/xcode/map/map.xcodeproj/project.pbxproj b/xcode/map/map.xcodeproj/project.pbxproj index fc6def5c55..2ae90a91bc 100644 --- a/xcode/map/map.xcodeproj/project.pbxproj +++ b/xcode/map/map.xcodeproj/project.pbxproj @@ -20,6 +20,9 @@ 34921F661BFA0A6900737D6E /* api_mark_point.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 34921F611BFA0A6900737D6E /* api_mark_point.hpp */; }; 34DDA1811DBE5DF40088A609 /* libpartners_api.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 34DDA17F1DBE5DF40088A609 /* libpartners_api.a */; }; 34DDA1821DBE5DF40088A609 /* libtracking.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 34DDA1801DBE5DF40088A609 /* libtracking.a */; }; + 3D47B2931F054BC5000828D2 /* taxi_delegate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D47B2901F054BC5000828D2 /* taxi_delegate.cpp */; }; + 3D47B2941F054BC5000828D2 /* taxi_delegate.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D47B2911F054BC5000828D2 /* taxi_delegate.hpp */; }; + 3D47B2951F054BC5000828D2 /* city_finder.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D47B2921F054BC5000828D2 /* city_finder.hpp */; }; 3D74ABBE1EA76F1D0063A898 /* local_ads_supported_types.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D74ABBD1EA76F1D0063A898 /* local_ads_supported_types.cpp */; }; 45201E931CE4AC90008A4842 /* api_mark_point.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 45201E921CE4AC90008A4842 /* api_mark_point.cpp */; }; 45580ABE1E2CBD5E00CD535D /* benchmark_tools.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 45580ABC1E2CBD5E00CD535D /* benchmark_tools.cpp */; }; @@ -143,6 +146,9 @@ 34AF87EA1DBE5AD000E5E7DC /* common-release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = "common-release.xcconfig"; path = "../common-release.xcconfig"; sourceTree = ""; }; 34DDA17F1DBE5DF40088A609 /* libpartners_api.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libpartners_api.a; path = "/Users/igrechuhin/Repo/omim/xcode/partners_api/../../../omim-xcode-build/Debug/libpartners_api.a"; sourceTree = ""; }; 34DDA1801DBE5DF40088A609 /* libtracking.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libtracking.a; path = "/Users/igrechuhin/Repo/omim/xcode/tracking/../../../omim-xcode-build/Debug/libtracking.a"; sourceTree = ""; }; + 3D47B2901F054BC5000828D2 /* taxi_delegate.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = taxi_delegate.cpp; sourceTree = ""; }; + 3D47B2911F054BC5000828D2 /* taxi_delegate.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = taxi_delegate.hpp; sourceTree = ""; }; + 3D47B2921F054BC5000828D2 /* city_finder.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = city_finder.hpp; sourceTree = ""; }; 3D74ABBD1EA76F1D0063A898 /* local_ads_supported_types.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = local_ads_supported_types.cpp; sourceTree = ""; }; 45201E921CE4AC90008A4842 /* api_mark_point.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = api_mark_point.cpp; sourceTree = ""; }; 45580ABC1E2CBD5E00CD535D /* benchmark_tools.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = benchmark_tools.cpp; sourceTree = ""; }; @@ -419,6 +425,9 @@ 675345BD1A4054AD00A0A8C3 /* map */ = { isa = PBXGroup; children = ( + 3D47B2901F054BC5000828D2 /* taxi_delegate.cpp */, + 3D47B2911F054BC5000828D2 /* taxi_delegate.hpp */, + 3D47B2921F054BC5000828D2 /* city_finder.hpp */, 3D74ABBD1EA76F1D0063A898 /* local_ads_supported_types.cpp */, 56B6EAF61EA4BAF00037D963 /* mwm_tree.cpp */, 56B6EAF71EA4BAF00037D963 /* mwm_tree.hpp */, @@ -489,6 +498,7 @@ 56B6EAF91EA4BAF00037D963 /* mwm_tree.hpp in Headers */, 347B60771DD9926D0050FA24 /* traffic_manager.hpp in Headers */, 6753466B1A4054E800A0A8C3 /* geourl_process.hpp in Headers */, + 3D47B2951F054BC5000828D2 /* city_finder.hpp in Headers */, F6B283081C1B03320081957A /* gps_track_storage.hpp in Headers */, 675346671A4054E800A0A8C3 /* ge0_parser.hpp in Headers */, 675346A21A4054E800A0A8C3 /* user_mark.hpp in Headers */, @@ -497,6 +507,7 @@ 34921F661BFA0A6900737D6E /* api_mark_point.hpp in Headers */, 675346751A4054E800A0A8C3 /* mwm_url.hpp in Headers */, 6753464B1A4054E800A0A8C3 /* bookmark.hpp in Headers */, + 3D47B2941F054BC5000828D2 /* taxi_delegate.hpp in Headers */, 348AB57D1D7EE0C6009F8301 /* chart_generator.hpp in Headers */, F63421F91DF9BF9100A96868 /* reachable_by_taxi_checker.hpp in Headers */, 6753469E1A4054E800A0A8C3 /* user_mark_container.hpp in Headers */, @@ -616,6 +627,7 @@ files = ( F6B283051C1B03320081957A /* gps_track_filter.cpp in Sources */, 675346481A4054E800A0A8C3 /* bookmark_manager.cpp in Sources */, + 3D47B2931F054BC5000828D2 /* taxi_delegate.cpp in Sources */, 675346741A4054E800A0A8C3 /* mwm_url.cpp in Sources */, 347B60761DD9926D0050FA24 /* traffic_manager.cpp in Sources */, F6B283091C1B03320081957A /* gps_track.cpp in Sources */, diff --git a/xcode/partners_api/partners_api.xcodeproj/project.pbxproj b/xcode/partners_api/partners_api.xcodeproj/project.pbxproj index d5c4535982..d62ff19472 100644 --- a/xcode/partners_api/partners_api.xcodeproj/project.pbxproj +++ b/xcode/partners_api/partners_api.xcodeproj/project.pbxproj @@ -20,6 +20,10 @@ 3488B0381E9D11890068AFD8 /* rb_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 346E889E1E9D088200D4CE9B /* rb_tests.cpp */; }; 3488B0391E9D118D0068AFD8 /* facebook_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DBC1C501E4B14810016897F /* facebook_tests.cpp */; }; 3D47B2811F00F94D000828D2 /* mopub_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D47B2801F00F94D000828D2 /* mopub_tests.cpp */; }; + 3D47B29A1F054C89000828D2 /* taxi_base.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D47B2961F054C89000828D2 /* taxi_base.cpp */; }; + 3D47B29B1F054C89000828D2 /* taxi_countries.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D47B2971F054C89000828D2 /* taxi_countries.cpp */; }; + 3D47B29C1F054C89000828D2 /* taxi_countries.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D47B2981F054C89000828D2 /* taxi_countries.hpp */; }; + 3D47B29D1F054C89000828D2 /* taxi_places.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D47B2991F054C89000828D2 /* taxi_places.hpp */; }; 3DBC1C541E4B14920016897F /* facebook_ads.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DBC1C521E4B14920016897F /* facebook_ads.cpp */; }; 3DBC1C551E4B14920016897F /* facebook_ads.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3DBC1C531E4B14920016897F /* facebook_ads.hpp */; }; 3DFEBF851EF82BEA00317D5C /* viator_api.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DFEBF831EF82BEA00317D5C /* viator_api.cpp */; }; @@ -68,6 +72,10 @@ 3475E0E11DBF581B004C7E69 /* common-debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = "common-debug.xcconfig"; path = "../common-debug.xcconfig"; sourceTree = ""; }; 3475E0E21DBF581B004C7E69 /* common-release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = "common-release.xcconfig"; path = "../common-release.xcconfig"; sourceTree = ""; }; 3D47B2801F00F94D000828D2 /* mopub_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = mopub_tests.cpp; sourceTree = ""; }; + 3D47B2961F054C89000828D2 /* taxi_base.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = taxi_base.cpp; sourceTree = ""; }; + 3D47B2971F054C89000828D2 /* taxi_countries.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = taxi_countries.cpp; sourceTree = ""; }; + 3D47B2981F054C89000828D2 /* taxi_countries.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = taxi_countries.hpp; sourceTree = ""; }; + 3D47B2991F054C89000828D2 /* taxi_places.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = taxi_places.hpp; sourceTree = ""; }; 3DBC1C501E4B14810016897F /* facebook_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = facebook_tests.cpp; sourceTree = ""; }; 3DBC1C521E4B14920016897F /* facebook_ads.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = facebook_ads.cpp; sourceTree = ""; }; 3DBC1C531E4B14920016897F /* facebook_ads.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = facebook_ads.hpp; sourceTree = ""; }; @@ -154,6 +162,10 @@ F6B5363B1DA520B20067EEA5 /* partners_api */ = { isa = PBXGroup; children = ( + 3D47B2961F054C89000828D2 /* taxi_base.cpp */, + 3D47B2971F054C89000828D2 /* taxi_countries.cpp */, + 3D47B2981F054C89000828D2 /* taxi_countries.hpp */, + 3D47B2991F054C89000828D2 /* taxi_places.hpp */, 3DFEBF831EF82BEA00317D5C /* viator_api.cpp */, 3DFEBF841EF82BEA00317D5C /* viator_api.hpp */, 3DFEBF941EFBFC1500317D5C /* taxi_base.hpp */, @@ -232,7 +244,9 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + 3D47B29C1F054C89000828D2 /* taxi_countries.hpp in Headers */, 346E88971E9D087400D4CE9B /* ads_base.hpp in Headers */, + 3D47B29D1F054C89000828D2 /* taxi_places.hpp in Headers */, F67E75261DB8F06F00D6741F /* opentable_api.hpp in Headers */, F6B536411DA520E40067EEA5 /* booking_api.hpp in Headers */, 3430643D1E9FBCF500DC7665 /* mopub_ads.hpp in Headers */, @@ -351,9 +365,11 @@ 346E889B1E9D087400D4CE9B /* rb_ads.cpp in Sources */, 3DBC1C541E4B14920016897F /* facebook_ads.cpp in Sources */, 346E88981E9D087400D4CE9B /* ads_engine.cpp in Sources */, + 3D47B29B1F054C89000828D2 /* taxi_countries.cpp in Sources */, F67E75251DB8F06F00D6741F /* opentable_api.cpp in Sources */, 3DFEBFA31EFBFC2300317D5C /* taxi_engine_tests.cpp in Sources */, F6B536401DA520E40067EEA5 /* booking_api.cpp in Sources */, + 3D47B29A1F054C89000828D2 /* taxi_base.cpp in Sources */, 3DFEBF9E1EFBFC1500317D5C /* yandex_api.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0;