__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import asyncio
  2. import collections
  3. import logging
  4. import random
  5. from itertools import groupby
  6. from typing import List, Tuple, Dict, Union
  7. from .random_street_view import call_random_street_view, VALID_COUNTRIES as RSV_COUNTRIES
  8. from .urban_centers import urban_coord, VALID_COUNTRIES as URBAN_COUNTRIES
  9. from .shared import aiohttp_client, reverse_geocode
  10. from ..schemas import GameConfig, GenMethodEnum, CountryCode, CacheInfo, GeneratorInfo
  11. logger = logging.getLogger(__name__)
  12. generator_info = [
  13. GeneratorInfo(
  14. generation_method=GenMethodEnum.rsv,
  15. country_locks=RSV_COUNTRIES
  16. ),
  17. GeneratorInfo(
  18. generation_method=GenMethodEnum.urban,
  19. country_locks=URBAN_COUNTRIES
  20. ),
  21. ]
  22. cache_names = {
  23. GenMethodEnum.rsv: "RSV",
  24. GenMethodEnum.urban: "Urban",
  25. }
  26. class ExhaustedSourceError(Exception):
  27. pass
  28. DIFFICULTY_1 = [
  29. # Singapore - very small, pretty obvious from lang combo
  30. "sg",
  31. # Israel, Taiwan, Japan, South Korea, Greece, Poland - immediately obvious from language
  32. "il", "tw", "jp", "kr", "gr", "pl",
  33. # Hong Kong - distraction from Taiwan
  34. "hk",
  35. ]
  36. DIFFICULTY_2 = [
  37. # USA! USA! USA! USA! (suck it Europe)
  38. "us", "us", "us", "us",
  39. # Western Europe minus a few more interesting ones
  40. "ie", "gb", "es", "fr",
  41. "be", "nl", "lu",
  42. "de", "ch", "li", "at",
  43. "it", "mt",
  44. # Southern Africa (b/c English)
  45. "za", "ls", "sz", "na", "bw", "zw",
  46. # New Zealand (b/c English)
  47. "nz",
  48. ]
  49. DIFFICULTY_3 = [
  50. # Nordic languages
  51. "is", "fo", "se", "no", "dk", "gl",
  52. # Finno-urgic
  53. "fi", "ee",
  54. # Other Baltics
  55. "lv", "lt",
  56. # Central + Eastern Europe + Balkans (non-Cyrillic, non-Polish confusable)
  57. "cz", "sk", "hu", "ro", "si", "hr", "ba", "al", "md",
  58. # Cyrillic Balkans
  59. "bg", "rs", "me", "mk",
  60. # Turkey can also have its language confused with some of the above
  61. "tr",
  62. # Caucasus
  63. "am", "az", "ge",
  64. # SE Asia (partial - mainly the ones with non-Latin scripts)
  65. "bt", "np", "bd",
  66. "mm", "kh", "vn", "th", "la",
  67. ]
  68. DIFFICULTY_4 = [
  69. # SE Asia (partial - mainly the ones with harder to differentiate languages)
  70. "id", "my",
  71. # Middle East
  72. "iq", "jo", "lb", "sa", "ae", "om",
  73. # North Africa
  74. "eg", "dz", "tn", "ma",
  75. # West Africa
  76. "sn", "gi", "ng",
  77. # East Africa
  78. "ke", "et",
  79. # Mexico + Central America + South America minus Brazil (i.e., all Spanish)
  80. "mx", "gt", "ni", "pa", "co", "ec", "pe", "bo", "ar", "cl",
  81. ]
  82. DIFFICULTY_5 = [
  83. # Canada + Australia
  84. "ca", "au",
  85. # Brazil + Portugal (lol)
  86. "br", "pt",
  87. # Russia + other Cyrillic + Mongolia
  88. "ru",
  89. "ua", "by",
  90. "kz", "kg", "tj", "tm", "uz",
  91. "mn",
  92. # India (basically all photo orbs)
  93. "in",
  94. ]
  95. DIFFICULTY_X = [
  96. # tropical/subtropical island nations
  97. "lk", "cv", "cu", "do", "jm", "mg", "mv", "pg", "ph", "ws", "tt", "pr",
  98. ]
  99. DIFFICULTY_TIER_ORDER = (
  100. DIFFICULTY_1, DIFFICULTY_2, DIFFICULTY_3, DIFFICULTY_4,
  101. DIFFICULTY_5,
  102. DIFFICULTY_4, DIFFICULTY_3, DIFFICULTY_2, DIFFICULTY_1,
  103. DIFFICULTY_X,
  104. )
  105. class PointStore:
  106. def __init__(self,
  107. cache_targets: Dict[Tuple[GenMethodEnum, CountryCode], int],
  108. rsv_country_retries: int = 5,
  109. urban_country_pool_size: int = 30,
  110. urban_country_retries: int = 30,
  111. urban_city_retries: int = 50,
  112. urban_city_retries_per_random_country: int = 10):
  113. self.cache_targets = cache_targets
  114. self.rsv_country_retries = rsv_country_retries
  115. self.urban_country_pool_size = urban_country_pool_size
  116. self.urban_country_retries = urban_country_retries
  117. self.urban_city_retries = urban_city_retries
  118. self.urban_city_retries_per_random_country = urban_city_retries_per_random_country
  119. self.store = collections.defaultdict(collections.deque)
  120. async def _gen_rsv_point(self, country: CountryCode):
  121. # RSV point function returns a collection of points, which should be cached
  122. for actual_country, points in groupby(await call_random_street_view(country), key=lambda p: p[0]):
  123. # but these points need to be cached according to the actual reverse geocoded country they are in
  124. self.store[(GenMethodEnum.rsv, actual_country)].extend(points)
  125. stock = self.store[(GenMethodEnum.rsv, country)]
  126. if len(stock) > 0:
  127. return stock.popleft()
  128. async def _gen_urban_point(self, countries: List[CountryCode], city_retries: int):
  129. for country in countries:
  130. logger.info(f"Selecting urban centers from {country}")
  131. pt = await urban_coord(country, city_retries=city_retries)
  132. if pt is not None:
  133. if pt[0] == country:
  134. return pt
  135. else:
  136. # TODO technically this is slightly wasted effort in rare edge cases
  137. self.store[(GenMethodEnum.urban, pt[0])].append(pt)
  138. # TODO I think all of this logic still gets stuck in the trap of generating points even when there's a stock in some edge cases
  139. # this needs a rewrite but I'm not doing that now
  140. async def get_point(self, generator: GenMethodEnum, country: Union[CountryCode, None], force_generate: bool = False) -> Tuple[str, float, float]:
  141. if country is None:
  142. # generating points across the whole world
  143. # for current generators, this means selecting a country at random
  144. if generator == GenMethodEnum.rsv:
  145. for _ in range(self.rsv_country_retries):
  146. # try a few countries before giving up, just in case one has no data
  147. country = random.choice(RSV_COUNTRIES)
  148. point = await self._gen_rsv_point(country)
  149. if point is not None:
  150. return point
  151. elif generator == GenMethodEnum.urban:
  152. # try many countries since finding an urban center point is harder
  153. countries = random.sample(URBAN_COUNTRIES, k=min(self.urban_country_pool_size, len(URBAN_COUNTRIES)))
  154. point = await self._gen_urban_point(countries, self.urban_city_retries_per_random_country)
  155. if point is not None:
  156. return point
  157. # if nothing could be done - inform the caller
  158. raise ExhaustedSourceError
  159. # generating points for a specific country
  160. # if we already have a point ready, just return it immediately
  161. if not force_generate:
  162. stock = self.store[(generator, country)]
  163. if len(stock) > 0:
  164. return stock.popleft()
  165. # otherwise, need to actually generate a new point
  166. if generator == GenMethodEnum.rsv:
  167. point = await self._gen_rsv_point(country)
  168. if point is not None:
  169. return point
  170. elif generator == GenMethodEnum.urban:
  171. point = await self._gen_urban_point((country for _ in range(self.urban_country_retries)), self.urban_city_retries)
  172. if point is not None:
  173. return point
  174. # finally, if all that fails, just inform the caller
  175. raise ExhaustedSourceError
  176. async def get_points(self, config: GameConfig) -> List[Tuple[str, float, float]]:
  177. """
  178. Provide points according to the GameConfig.
  179. Return a list of valid geo points, as
  180. (2 character country code, latitude, longitude) tuples.
  181. In the event that the configured source cannot reasonably supply enough points,
  182. most likely due to time constraints, this will raise an ExhaustedSourceError.
  183. """
  184. try:
  185. if config.generation_method == GenMethodEnum.diff_tiered:
  186. # in the case of using the "difficulty tiered" generator there is some special logic
  187. # assume that, in general, we want 10 points (4 normal rounds going up in difficulty, 1 max difficulty round, 4 normal going down, 1 nightmare tier)
  188. # if more are requested, it repeats. if less, it only goes that far.
  189. def make_point_task(tier, attempts):
  190. if attempts <= 0:
  191. raise ExhaustedSourceError
  192. try:
  193. country_lock = random.choice(tier)
  194. if country_lock in random_street_view.VALID_COUNTRIES:
  195. return self.get_point(GenMethodEnum.rsv, country_lock)
  196. elif country_lock in urban_centers.VALID_COUNTRIES:
  197. return self.get_point(GenMethodEnum.urban, country_lock)
  198. else:
  199. raise ExhaustedSourceError
  200. except:
  201. return make_point_task(tier, attempts - 1)
  202. point_tasks = [make_point_task(DIFFICULTY_TIER_ORDER[i % len(DIFFICULTY_TIER_ORDER)], 3) for i in range(config.rounds)]
  203. else:
  204. point_tasks = [self.get_point(config.generation_method, config.country_lock) for _ in range(config.rounds)]
  205. gathered = asyncio.gather(*point_tasks)
  206. return await asyncio.wait_for(gathered, 60)
  207. # TODO - it would be nice to keep partially generated sets around if there's a timeout or exhaustion
  208. except asyncio.TimeoutError:
  209. raise ExhaustedSourceError
  210. def get_cache_info(self) -> List[CacheInfo]:
  211. """
  212. Get CacheInfo for all caches.
  213. """
  214. return [CacheInfo(cache_name=f"{cache_names[g]}-{c}", size=len(ps)) for (g, c), ps in self.store.items()]
  215. async def _restock_source_impl(self, generator: GenMethodEnum, country: CountryCode):
  216. key = (generator, country)
  217. target = self.cache_targets.get(key, 0)
  218. stock = self.store[key]
  219. while len(stock) < target: # this check allows for RSV to do its multi-point restock
  220. stock.append(await self.get_point(*key, force_generate=True))
  221. async def restock_source(self, config: GameConfig):
  222. """
  223. Restock any caches associated with the GameConfig.
  224. """
  225. if config.country_lock is None:
  226. return
  227. try:
  228. await self._restock_source_impl(config.generation_method, config.country_lock)
  229. except ExhaustedSourceError:
  230. # if the cache can't be restocked, that is bad, but not fatal
  231. logger.exception(f"Failed to fully restock point cache for {config}")
  232. async def restock_all(self, timeout: Union[int, float, None] = None):
  233. """
  234. Restock all caches.
  235. """
  236. restock_tasks = [self._restock_source_impl(gen, cc) for (gen, cc) in self.cache_targets.keys()]
  237. gathered = asyncio.gather(*restock_tasks)
  238. try:
  239. await asyncio.wait_for(gathered, timeout)
  240. except (asyncio.TimeoutError, ExhaustedSourceError):
  241. # if this task times out, it's fine, as it's just intended to be a best effort
  242. logger.exception("Failed to fully restock a point cache!")
  243. points = PointStore({
  244. (GenMethodEnum.urban, "us"): 10,
  245. })