cuju-render 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (382) hide show
  1. cuju/render/__init__.py +759 -0
  2. cuju/render/__main__.py +1065 -0
  3. cuju/render/_export/__init__.py +3 -0
  4. cuju/render/_export/feed.py +542 -0
  5. cuju/render/_export/sizes.py +157 -0
  6. cuju/render/_export/video.py +1618 -0
  7. cuju/render/_export/video_jobs.py +408 -0
  8. cuju/render/_format/__init__.py +3 -0
  9. cuju/render/_format/annotations.py +235 -0
  10. cuju/render/_format/codec.py +307 -0
  11. cuju/render/_format/keep.py +121 -0
  12. cuju/render/_format/recording.py +860 -0
  13. cuju/render/_format/writer.py +54 -0
  14. cuju/render/_guide/__init__.py +3 -0
  15. cuju/render/_guide/doctor.py +523 -0
  16. cuju/render/_guide/help.py +62 -0
  17. cuju/render/_guide/tutorial.py +254 -0
  18. cuju/render/_http/__init__.py +3 -0
  19. cuju/render/_http/commands.py +57 -0
  20. cuju/render/_http/headers.py +103 -0
  21. cuju/render/_http/launch.py +500 -0
  22. cuju/render/_http/launchers.py +408 -0
  23. cuju/render/_http/pwa.py +128 -0
  24. cuju/render/_http/server.py +2105 -0
  25. cuju/render/_http/window.py +360 -0
  26. cuju/render/_moments/__init__.py +3 -0
  27. cuju/render/_moments/captions.py +284 -0
  28. cuju/render/_moments/events.py +1123 -0
  29. cuju/render/_moments/search.py +694 -0
  30. cuju/render/_packages/__init__.py +3 -0
  31. cuju/render/_packages/glsl.py +746 -0
  32. cuju/render/_packages/loudness.py +334 -0
  33. cuju/render/_packages/pack.py +1959 -0
  34. cuju/render/_packages/presentation.py +1376 -0
  35. cuju/render/_packages/vocabulary.py +609 -0
  36. cuju/render/_playing/__init__.py +3 -0
  37. cuju/render/_playing/achievements.py +463 -0
  38. cuju/render/_playing/bench.py +609 -0
  39. cuju/render/_playing/challenges.py +1813 -0
  40. cuju/render/_playing/demo.py +323 -0
  41. cuju/render/_playing/draw.py +211 -0
  42. cuju/render/_playing/errors.py +20 -0
  43. cuju/render/_playing/game.py +645 -0
  44. cuju/render/_playing/limits.py +339 -0
  45. cuju/render/_playing/play.py +926 -0
  46. cuju/render/_playing/policy_worker.py +250 -0
  47. cuju/render/_playing/strict.py +810 -0
  48. cuju/render/_registry/__init__.py +3 -0
  49. cuju/render/_registry/clips.py +394 -0
  50. cuju/render/_registry/joints.py +108 -0
  51. cuju/render/_registry/teams.py +2058 -0
  52. cuju/render/_store/__init__.py +3 -0
  53. cuju/render/_store/home.py +70 -0
  54. cuju/render/_store/library.py +604 -0
  55. cuju/render/_store/samples.py +163 -0
  56. cuju/render/_text/__init__.py +3 -0
  57. cuju/render/_text/languages.py +637 -0
  58. cuju/render/_text/messages.py +1396 -0
  59. cuju/render/_tournaments/__init__.py +3 -0
  60. cuju/render/_tournaments/results.py +547 -0
  61. cuju/render/_tournaments/tournament.py +2742 -0
  62. cuju/render/_tournaments/tournament_cli.py +587 -0
  63. cuju/render/_tournaments/tournament_runner.py +1975 -0
  64. cuju/render/_version.py +6 -0
  65. cuju/render/_watch/__init__.py +3 -0
  66. cuju/render/_watch/grid.py +265 -0
  67. cuju/render/_watch/live.py +664 -0
  68. cuju/render/_watch/notebook.py +79 -0
  69. cuju/render/_watch/show.py +171 -0
  70. cuju/render/samples/sample-11v11.cjr +0 -0
  71. cuju/render/samples/sample-5v5.cjr +0 -0
  72. cuju/render/samples/teams/starter/main.py +33 -0
  73. cuju/render/scenarios/benchmark_full_game_11_vs_11_easy.py +46 -0
  74. cuju/render/scenarios/benchmark_full_game_11_vs_11_hard.py +46 -0
  75. cuju/render/scenarios/benchmark_full_game_11_vs_11_medium.py +46 -0
  76. cuju/render/scenarios/benchmark_full_game_5_vs_5_easy.py +34 -0
  77. cuju/render/scenarios/benchmark_full_game_5_vs_5_hard.py +34 -0
  78. cuju/render/scenarios/benchmark_full_game_5_vs_5_medium.py +34 -0
  79. cuju/render/web/assets/adboard_polygon.webp +0 -0
  80. cuju/render/web/assets/adboard_your_ad_here.webp +0 -0
  81. cuju/render/web/assets/animations/kaykit-rig-medium-combatmelee.glb +0 -0
  82. cuju/render/web/assets/animations/kaykit-rig-medium-general.glb +0 -0
  83. cuju/render/web/assets/animations/kaykit-rig-medium-movementadvanced.glb +0 -0
  84. cuju/render/web/assets/animations/kaykit-rig-medium-movementbasic.glb +0 -0
  85. cuju/render/web/assets/animations/kaykit-rig-medium-simulation.glb +0 -0
  86. cuju/render/web/assets/animations/kaykit-rig-medium-special.glb +0 -0
  87. cuju/render/web/assets/ball.glb +0 -0
  88. cuju/render/web/assets/challenges/3_vs_1_with_keeper.mp4 +0 -0
  89. cuju/render/web/assets/challenges/3_vs_1_with_keeper.webp +0 -0
  90. cuju/render/web/assets/challenges/corner.mp4 +0 -0
  91. cuju/render/web/assets/challenges/corner.webp +0 -0
  92. cuju/render/web/assets/challenges/counterattack_easy.mp4 +0 -0
  93. cuju/render/web/assets/challenges/counterattack_easy.webp +0 -0
  94. cuju/render/web/assets/challenges/counterattack_hard.mp4 +0 -0
  95. cuju/render/web/assets/challenges/counterattack_hard.webp +0 -0
  96. cuju/render/web/assets/challenges/empty_goal.mp4 +0 -0
  97. cuju/render/web/assets/challenges/empty_goal.webp +0 -0
  98. cuju/render/web/assets/challenges/empty_goal_close.mp4 +0 -0
  99. cuju/render/web/assets/challenges/empty_goal_close.webp +0 -0
  100. cuju/render/web/assets/challenges/manifest.json +375 -0
  101. cuju/render/web/assets/challenges/one_on_one.mp4 +0 -0
  102. cuju/render/web/assets/challenges/one_on_one.webp +0 -0
  103. cuju/render/web/assets/challenges/one_on_one_hard.mp4 +0 -0
  104. cuju/render/web/assets/challenges/one_on_one_hard.webp +0 -0
  105. cuju/render/web/assets/challenges/pass_and_shoot_with_keeper.mp4 +0 -0
  106. cuju/render/web/assets/challenges/pass_and_shoot_with_keeper.webp +0 -0
  107. cuju/render/web/assets/challenges/run_pass_and_shoot_with_keeper.mp4 +0 -0
  108. cuju/render/web/assets/challenges/run_pass_and_shoot_with_keeper.webp +0 -0
  109. cuju/render/web/assets/challenges/run_to_score.mp4 +0 -0
  110. cuju/render/web/assets/challenges/run_to_score.webp +0 -0
  111. cuju/render/web/assets/challenges/run_to_score_with_keeper.mp4 +0 -0
  112. cuju/render/web/assets/challenges/run_to_score_with_keeper.webp +0 -0
  113. cuju/render/web/assets/challenges/single_goal_versus_lazy.mp4 +0 -0
  114. cuju/render/web/assets/challenges/single_goal_versus_lazy.webp +0 -0
  115. cuju/render/web/assets/crowd.webp +0 -0
  116. cuju/render/web/assets/crowd_normal.webp +0 -0
  117. cuju/render/web/assets/cuju-logo-384.png +0 -0
  118. cuju/render/web/assets/cuju-logo.png +0 -0
  119. cuju/render/web/assets/cuju-logo.svg +46 -0
  120. cuju/render/web/assets/cuju-mark-light.svg +33 -0
  121. cuju/render/web/assets/cuju-mark.svg +33 -0
  122. cuju/render/web/assets/goal.glb +0 -0
  123. cuju/render/web/assets/grass.webp +0 -0
  124. cuju/render/web/assets/hair_black.png +0 -0
  125. cuju/render/web/assets/hair_blonde.png +0 -0
  126. cuju/render/web/assets/hair_brown.png +0 -0
  127. cuju/render/web/assets/hair_darkblonde.png +0 -0
  128. cuju/render/web/assets/hair_long01.glb +0 -0
  129. cuju/render/web/assets/hair_long02.glb +0 -0
  130. cuju/render/web/assets/hair_medium01.glb +0 -0
  131. cuju/render/web/assets/hair_red.png +0 -0
  132. cuju/render/web/assets/hair_short01.glb +0 -0
  133. cuju/render/web/assets/hair_short02.glb +0 -0
  134. cuju/render/web/assets/icons/cuju-192.png +0 -0
  135. cuju/render/web/assets/icons/cuju-512.png +0 -0
  136. cuju/render/web/assets/icons/cuju-maskable-512.png +0 -0
  137. cuju/render/web/assets/kit_goalkeeper.webp +0 -0
  138. cuju/render/web/assets/kit_mask.png +0 -0
  139. cuju/render/web/assets/kit_normal.webp +0 -0
  140. cuju/render/web/assets/manifest.json +601 -0
  141. cuju/render/web/assets/models/kaykit-barbarian.glb +0 -0
  142. cuju/render/web/assets/models/kaykit-barbarian.json +23 -0
  143. cuju/render/web/assets/models/kaykit-knight.glb +0 -0
  144. cuju/render/web/assets/models/kaykit-knight.json +23 -0
  145. cuju/render/web/assets/models/kaykit-mage.glb +0 -0
  146. cuju/render/web/assets/models/kaykit-mage.json +23 -0
  147. cuju/render/web/assets/models/kaykit-mannequin-medium.glb +0 -0
  148. cuju/render/web/assets/models/kaykit-mannequin-medium.json +23 -0
  149. cuju/render/web/assets/models/kaykit-ranger.glb +0 -0
  150. cuju/render/web/assets/models/kaykit-ranger.json +23 -0
  151. cuju/render/web/assets/models/kaykit-rogue-hooded.glb +0 -0
  152. cuju/render/web/assets/models/kaykit-rogue-hooded.json +23 -0
  153. cuju/render/web/assets/models/kaykit-rogue.glb +0 -0
  154. cuju/render/web/assets/models/kaykit-rogue.json +23 -0
  155. cuju/render/web/assets/models/kaykit-skeleton-mage.glb +0 -0
  156. cuju/render/web/assets/models/kaykit-skeleton-mage.json +23 -0
  157. cuju/render/web/assets/models/kaykit-skeleton-minion.glb +0 -0
  158. cuju/render/web/assets/models/kaykit-skeleton-minion.json +23 -0
  159. cuju/render/web/assets/models/kaykit-skeleton-rogue.glb +0 -0
  160. cuju/render/web/assets/models/kaykit-skeleton-rogue.json +23 -0
  161. cuju/render/web/assets/models/kaykit-skeleton-warrior.glb +0 -0
  162. cuju/render/web/assets/models/kaykit-skeleton-warrior.json +23 -0
  163. cuju/render/web/assets/pitch.webp +0 -0
  164. cuju/render/web/assets/pitch_normal.webp +0 -0
  165. cuju/render/web/assets/player.glb +0 -0
  166. cuju/render/web/assets/shoe.webp +0 -0
  167. cuju/render/web/assets/shoe_sole.png +0 -0
  168. cuju/render/web/assets/skin_01.png +0 -0
  169. cuju/render/web/assets/skin_02.png +0 -0
  170. cuju/render/web/assets/skin_03.png +0 -0
  171. cuju/render/web/assets/skin_04.png +0 -0
  172. cuju/render/web/assets/sounds/applause.ogg +0 -0
  173. cuju/render/web/assets/sounds/beach-wave-1.ogg +0 -0
  174. cuju/render/web/assets/sounds/beach-wave-2.ogg +0 -0
  175. cuju/render/web/assets/sounds/beach-wave-3.ogg +0 -0
  176. cuju/render/web/assets/sounds/beach-wave-4.ogg +0 -0
  177. cuju/render/web/assets/sounds/city-horn.ogg +0 -0
  178. cuju/render/web/assets/sounds/city-loop.ogg +0 -0
  179. cuju/render/web/assets/sounds/city-siren.ogg +0 -0
  180. cuju/render/web/assets/sounds/crowd-cheer.ogg +0 -0
  181. cuju/render/web/assets/sounds/crowd-loop.ogg +0 -0
  182. cuju/render/web/assets/sounds/crowd-ooh.ogg +0 -0
  183. cuju/render/web/assets/sounds/gulls.ogg +0 -0
  184. cuju/render/web/assets/sounds/neon-sting.ogg +0 -0
  185. cuju/render/web/assets/sounds/rain-loop.ogg +0 -0
  186. cuju/render/web/assets/sounds/sounds.json +46 -0
  187. cuju/render/web/assets/sounds/space-beep.ogg +0 -0
  188. cuju/render/web/assets/sounds/space-hum.ogg +0 -0
  189. cuju/render/web/assets/sounds/space-radio.ogg +0 -0
  190. cuju/render/web/assets/sounds/whistle-long.ogg +0 -0
  191. cuju/render/web/assets/sounds/whistle-short.ogg +0 -0
  192. cuju/render/web/assets/sounds/wind-loop.ogg +0 -0
  193. cuju/render/web/easy.css +357 -0
  194. cuju/render/web/export.html +23 -0
  195. cuju/render/web/feed.css +262 -0
  196. cuju/render/web/feed.html +28 -0
  197. cuju/render/web/flat.css +120 -0
  198. cuju/render/web/home.css +837 -0
  199. cuju/render/web/index.html +373 -0
  200. cuju/render/web/js/boot.js +50 -0
  201. cuju/render/web/js/broadcast/audio.js +2141 -0
  202. cuju/render/web/js/broadcast/celebrate.js +1238 -0
  203. cuju/render/web/js/broadcast/decisions.js +609 -0
  204. cuju/render/web/js/broadcast/hud.js +352 -0
  205. cuju/render/web/js/broadcast/show.js +3614 -0
  206. cuju/render/web/js/broadcast/transition.js +2231 -0
  207. cuju/render/web/js/broadcast/voices.js +153 -0
  208. cuju/render/web/js/capture.js +101 -0
  209. cuju/render/web/js/core/accessibility.js +90 -0
  210. cuju/render/web/js/core/api.js +429 -0
  211. cuju/render/web/js/core/comfort.js +135 -0
  212. cuju/render/web/js/core/i18n.js +705 -0
  213. cuju/render/web/js/core/icons.js +167 -0
  214. cuju/render/web/js/core/input-settings.js +156 -0
  215. cuju/render/web/js/core/keys.js +277 -0
  216. cuju/render/web/js/core/language-ui.js +125 -0
  217. cuju/render/web/js/core/menu.js +853 -0
  218. cuju/render/web/js/core/names.js +113 -0
  219. cuju/render/web/js/core/page-structure.js +110 -0
  220. cuju/render/web/js/core/scenarios.js +109 -0
  221. cuju/render/web/js/core/spoilers.js +236 -0
  222. cuju/render/web/js/core/typeahead.js +38 -0
  223. cuju/render/web/js/export/dom-picture.js +330 -0
  224. cuju/render/web/js/export/export-host.js +97 -0
  225. cuju/render/web/js/export/feed-dialog.js +191 -0
  226. cuju/render/web/js/export/feed-params.js +266 -0
  227. cuju/render/web/js/export/feed.js +363 -0
  228. cuju/render/web/js/export/mp4-mux.js +295 -0
  229. cuju/render/web/js/export/photo-core.js +313 -0
  230. cuju/render/web/js/export/photo.js +1314 -0
  231. cuju/render/web/js/export/safe-frame.js +59 -0
  232. cuju/render/web/js/export/size-fields.js +103 -0
  233. cuju/render/web/js/export/sizes.js +178 -0
  234. cuju/render/web/js/export/video-boot.js +55 -0
  235. cuju/render/web/js/export/video-browser.js +325 -0
  236. cuju/render/web/js/export/video-export.js +417 -0
  237. cuju/render/web/js/export/video-plan.js +207 -0
  238. cuju/render/web/js/export/video-range.js +185 -0
  239. cuju/render/web/js/export/webm-mux.js +198 -0
  240. cuju/render/web/js/feed-boot.js +151 -0
  241. cuju/render/web/js/feed-frame.js +101 -0
  242. cuju/render/web/js/help/doctor.js +488 -0
  243. cuju/render/web/js/help/firstrun.js +379 -0
  244. cuju/render/web/js/help/help-center.js +438 -0
  245. cuju/render/web/js/help/install-offer.js +164 -0
  246. cuju/render/web/js/help/install-state.js +104 -0
  247. cuju/render/web/js/help/lessons-core.js +207 -0
  248. cuju/render/web/js/help/lessons.js +463 -0
  249. cuju/render/web/js/help/palette.js +584 -0
  250. cuju/render/web/js/help/pwa.js +257 -0
  251. cuju/render/web/js/help/tour-core.js +241 -0
  252. cuju/render/web/js/help/tour.js +461 -0
  253. cuju/render/web/js/main.js +2513 -0
  254. cuju/render/web/js/pages/draw.js +188 -0
  255. cuju/render/web/js/pages/fieldpage.js +530 -0
  256. cuju/render/web/js/pages/home.js +521 -0
  257. cuju/render/web/js/pages/matches.js +779 -0
  258. cuju/render/web/js/pages/moments-ui.js +359 -0
  259. cuju/render/web/js/pages/queue-filters.js +63 -0
  260. cuju/render/web/js/pages/recording-groups.js +80 -0
  261. cuju/render/web/js/pages/recordings.js +1069 -0
  262. cuju/render/web/js/play/bench.js +347 -0
  263. cuju/render/web/js/play/challenges.js +1302 -0
  264. cuju/render/web/js/play/medals.js +127 -0
  265. cuju/render/web/js/play/play.js +1953 -0
  266. cuju/render/web/js/play/teasers.js +196 -0
  267. cuju/render/web/js/play/touch.js +350 -0
  268. cuju/render/web/js/play/tournament-layout.js +487 -0
  269. cuju/render/web/js/play/tournament-results.js +441 -0
  270. cuju/render/web/js/play/tournament-strict.js +560 -0
  271. cuju/render/web/js/play/tournament.js +2585 -0
  272. cuju/render/web/js/players/animate.js +972 -0
  273. cuju/render/web/js/players/clips.js +224 -0
  274. cuju/render/web/js/players/kaykit.js +162 -0
  275. cuju/render/web/js/players/kits.js +676 -0
  276. cuju/render/web/js/players/number-patch.js +82 -0
  277. cuju/render/web/js/players/players.js +2694 -0
  278. cuju/render/web/js/players/recolor.js +893 -0
  279. cuju/render/web/js/players/retarget.js +325 -0
  280. cuju/render/web/js/players/rig-numbers.js +174 -0
  281. cuju/render/web/js/presentation/animator.js +231 -0
  282. cuju/render/web/js/presentation/assign.js +85 -0
  283. cuju/render/web/js/presentation/behaviors.js +440 -0
  284. cuju/render/web/js/presentation/budget.js +209 -0
  285. cuju/render/web/js/presentation/catalog.js +1674 -0
  286. cuju/render/web/js/presentation/clocks.js +72 -0
  287. cuju/render/web/js/presentation/commands.js +301 -0
  288. cuju/render/web/js/presentation/contract.js +240 -0
  289. cuju/render/web/js/presentation/effects.js +904 -0
  290. cuju/render/web/js/presentation/flash.js +605 -0
  291. cuju/render/web/js/presentation/freeze-worklet.js +22 -0
  292. cuju/render/web/js/presentation/glsl.js +464 -0
  293. cuju/render/web/js/presentation/host.js +323 -0
  294. cuju/render/web/js/presentation/index.js +2066 -0
  295. cuju/render/web/js/presentation/loudness-worklet.js +235 -0
  296. cuju/render/web/js/presentation/loudness.js +740 -0
  297. cuju/render/web/js/presentation/moments.js +536 -0
  298. cuju/render/web/js/presentation/post.js +327 -0
  299. cuju/render/web/js/presentation/preview.js +180 -0
  300. cuju/render/web/js/presentation/protocol.js +174 -0
  301. cuju/render/web/js/presentation/reach.js +158 -0
  302. cuju/render/web/js/presentation/rigging.js +130 -0
  303. cuju/render/web/js/presentation/sandbox.js +157 -0
  304. cuju/render/web/js/presentation/shaders.js +235 -0
  305. cuju/render/web/js/presentation/stage.js +402 -0
  306. cuju/render/web/js/presentation/teamaudio.js +722 -0
  307. cuju/render/web/js/presentation/tracks.js +197 -0
  308. cuju/render/web/js/presentation/values.js +81 -0
  309. cuju/render/web/js/recording/annotation-data.js +203 -0
  310. cuju/render/web/js/recording/captions.js +491 -0
  311. cuju/render/web/js/recording/codec.js +185 -0
  312. cuju/render/web/js/recording/decode-worker.js +70 -0
  313. cuju/render/web/js/recording/decode.js +94 -0
  314. cuju/render/web/js/recording/episodes.js +46 -0
  315. cuju/render/web/js/recording/format.js +461 -0
  316. cuju/render/web/js/recording/live.js +348 -0
  317. cuju/render/web/js/recording/moments-query.js +59 -0
  318. cuju/render/web/js/recording/moments.js +149 -0
  319. cuju/render/web/js/recording/pose.js +86 -0
  320. cuju/render/web/js/recording/timeline.js +275 -0
  321. cuju/render/web/js/teams/art.js +50 -0
  322. cuju/render/web/js/teams/clip-editor.js +293 -0
  323. cuju/render/web/js/teams/editor.js +1979 -0
  324. cuju/render/web/js/teams/library.js +344 -0
  325. cuju/render/web/js/teams/painter.js +633 -0
  326. cuju/render/web/js/teams/surprise-ui.js +195 -0
  327. cuju/render/web/js/teams/surprise.js +497 -0
  328. cuju/render/web/js/teams/team-check.js +127 -0
  329. cuju/render/web/js/watch/annotations.js +270 -0
  330. cuju/render/web/js/watch/flat.js +366 -0
  331. cuju/render/web/js/watch/grid.js +436 -0
  332. cuju/render/web/js/watch/pitch2d.js +289 -0
  333. cuju/render/web/js/watch/player-tools.js +192 -0
  334. cuju/render/web/js/watch/presets.js +201 -0
  335. cuju/render/web/js/world/arenas.js +997 -0
  336. cuju/render/web/js/world/ball.js +204 -0
  337. cuju/render/web/js/world/cameras.js +982 -0
  338. cuju/render/web/js/world/crowd.js +376 -0
  339. cuju/render/web/js/world/field.js +227 -0
  340. cuju/render/web/js/world/framing.js +145 -0
  341. cuju/render/web/js/world/occlusion.js +48 -0
  342. cuju/render/web/js/world/pitch.js +703 -0
  343. cuju/render/web/js/world/scene.js +395 -0
  344. cuju/render/web/js/world/sky.js +325 -0
  345. cuju/render/web/js/world/stadium.js +1262 -0
  346. cuju/render/web/js/world/vendor-sky.js +367 -0
  347. cuju/render/web/js/world/weather.js +264 -0
  348. cuju/render/web/languages/en.json +4750 -0
  349. cuju/render/web/lessons.json +137 -0
  350. cuju/render/web/manifest.webmanifest +23 -0
  351. cuju/render/web/style.css +8399 -0
  352. cuju/render/web/sw.js +235 -0
  353. cuju/render/web/vendor/fabric/LICENSE +16 -0
  354. cuju/render/web/vendor/fabric/fabric.min.mjs +1 -0
  355. cuju/render/web/vendor/lucide/LICENSE +43 -0
  356. cuju/render/web/vendor/lucide/icons.js +141 -0
  357. cuju/render/web/vendor/three/LICENSE +21 -0
  358. cuju/render/web/vendor/three/build/three.core.js +60586 -0
  359. cuju/render/web/vendor/three/build/three.module.js +19719 -0
  360. cuju/render/web/vendor/three/examples/jsm/loaders/GLTFLoader.js +4925 -0
  361. cuju/render/web/vendor/three/examples/jsm/postprocessing/EffectComposer.js +365 -0
  362. cuju/render/web/vendor/three/examples/jsm/postprocessing/MaskPass.js +195 -0
  363. cuju/render/web/vendor/three/examples/jsm/postprocessing/OutputPass.js +148 -0
  364. cuju/render/web/vendor/three/examples/jsm/postprocessing/Pass.js +191 -0
  365. cuju/render/web/vendor/three/examples/jsm/postprocessing/RenderPass.js +193 -0
  366. cuju/render/web/vendor/three/examples/jsm/postprocessing/ShaderPass.js +135 -0
  367. cuju/render/web/vendor/three/examples/jsm/postprocessing/UnrealBloomPass.js +541 -0
  368. cuju/render/web/vendor/three/examples/jsm/shaders/CopyShader.js +52 -0
  369. cuju/render/web/vendor/three/examples/jsm/shaders/LuminosityHighPassShader.js +68 -0
  370. cuju/render/web/vendor/three/examples/jsm/shaders/OutputShader.js +103 -0
  371. cuju/render/web/vendor/three/examples/jsm/utils/BufferGeometryUtils.js +1501 -0
  372. cuju/render/web/vendor/three/examples/jsm/utils/SkeletonUtils.js +496 -0
  373. cuju_render-0.1.0.dist-info/METADATA +116 -0
  374. cuju_render-0.1.0.dist-info/RECORD +382 -0
  375. cuju_render-0.1.0.dist-info/WHEEL +4 -0
  376. cuju_render-0.1.0.dist-info/licenses/LICENSE +202 -0
  377. cuju_render-0.1.0.dist-info/licenses/LICENSES/CC-BY-4.0.txt +396 -0
  378. cuju_render-0.1.0.dist-info/licenses/LICENSES/CC0-1.0.txt +127 -0
  379. cuju_render-0.1.0.dist-info/licenses/LICENSES/ISC.txt +16 -0
  380. cuju_render-0.1.0.dist-info/licenses/LICENSES/MIT.txt +24 -0
  381. cuju_render-0.1.0.dist-info/licenses/LICENSES/Unlicense.txt +13 -0
  382. cuju_render-0.1.0.dist-info/licenses/NOTICE +35 -0
@@ -0,0 +1,1813 @@
1
+ # Copyright 2026 The cuju Authors
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ """Challenges, short drills with medals that people on the Play page and agents headless both try.
5
+
6
+ Every academy scenario of GRF is a challenge, and the one on one scenario is two more, at the
7
+ scenario's ability and against a right side at ability 1.0. A challenge plays a fixed number of
8
+ attempts, and attempt `i` always plays the seed `seed_i`, for people and agents alike, so every
9
+ result is comparable. The measure of a challenge counts the attempts in which the left side
10
+ scored, the attempts in which it scored within a limit of seconds, or the goals for minus the goals
11
+ against, and its bronze, silver and gold targets read that value, higher being better. The total
12
+ seconds to score break ties, lower being better, and then the earlier run.
13
+
14
+ Each attempt becomes a recording of the library, and one function, `score_attempt`, scores every
15
+ attempt from the frames of its recording, so a person's game and an agent's run use the same code.
16
+ The recordings of one run form a group, which the Recordings page shows as one row, and a run that
17
+ keeps no recordings scores the same frames and then leaves the library as it was. An attempt that
18
+ ends before its episode does counts its goals against and no goals for, so quitting never helps.
19
+ A full run reaches the leaderboard of its challenge, a JSON file in the folder `challenges` of the
20
+ renderer's home that keeps the best run of each competitor, and a run of fewer attempts is
21
+ practice, scored and kept as recordings but off the leaderboard.
22
+
23
+ The server hands its routes under `/api/challenges` to `get`, `post` and `delete`, and plays the
24
+ headless runs that the page asks for in a child process each,
25
+ `python -m cuju.render._playing.challenges`, one at a time, which reports one JSON line at a time as
26
+ a requested match does. A policy that fails in such a run ends its attempt alone, which counts as
27
+ failed, and the run goes on. The benchmarks of `bench` play their runs in the same child process.
28
+ """
29
+
30
+ from __future__ import annotations
31
+
32
+ import argparse
33
+ import collections
34
+ import contextlib
35
+ import json
36
+ import logging
37
+ import math
38
+ import os
39
+ import struct
40
+ import subprocess
41
+ import sys
42
+ import threading
43
+ import time
44
+ import zlib
45
+ from collections.abc import Callable
46
+ from pathlib import Path
47
+ from typing import Any, TextIO
48
+
49
+ from cuju import __version__
50
+
51
+ from .._format import recording as _recording
52
+ from .._registry import teams as _teams
53
+ from .._store import home as _home
54
+ from .._store import library as _library
55
+ from .._text import languages as _languages
56
+ from .._text import messages as _messages
57
+ from .._text.messages import MessageError
58
+ from . import achievements as _achievements
59
+
60
+ _LOGGER = logging.getLogger('cuju')
61
+ STEPS_PER_SECOND = 10
62
+ MEDALS = ('bronze', 'silver', 'gold')
63
+ # The medals from the best down, the order in which a challenge lists them as items.
64
+ MEDAL_ORDER = ('gold', 'silver', 'bronze')
65
+ MEASURES = ('scored', 'within', 'margin')
66
+ # The kinds of challenge, which give each card of the Play page its color and symbol, and how hard
67
+ # a challenge is for a person, from its targets and the defenders it faces.
68
+ CATEGORIES = (
69
+ 'finishing',
70
+ 'dribbling',
71
+ 'combination',
72
+ 'set_piece',
73
+ 'counterattack',
74
+ 'breakthrough',
75
+ 'duel',
76
+ )
77
+ DIFFICULTIES = ('easy', 'medium', 'hard')
78
+ # The leaderboard of a challenge keeps the best run of this many competitors.
79
+ BOARD_SIZE = 100
80
+ # A competitor's name on a leaderboard holds at most this many characters.
81
+ NAME_LENGTH = 40
82
+ # A policy has this long to load and to answer its first observation, and this long for every
83
+ # later answer before the run ends as stuck.
84
+ LOAD_SECONDS = 120.0
85
+ FIRST_CALL_SECONDS = 120.0
86
+ HANG_SECONDS = 60.0
87
+ # A headless run on the server ends after this many seconds.
88
+ _JOB_SECONDS = 30 * 60
89
+ # The server keeps this many ended runs of the page in its list.
90
+ _KEPT_JOBS = 20
91
+ _SCORE = struct.Struct('<2i')
92
+ _FLAGS = struct.Struct('<i')
93
+ _ACADEMY = 'academy_'
94
+
95
+
96
+ def __getattr__(name: str) -> Any:
97
+ """Returns `BatchEnvironment` on first use, so listing the challenges leaves PyTorch out."""
98
+ if name == 'BatchEnvironment':
99
+ from cuju import BatchEnvironment
100
+
101
+ return BatchEnvironment
102
+ raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
103
+
104
+
105
+ def _academy(
106
+ identifier: str, title: str, measure: str, targets: tuple[int, int, int], **extra: Any
107
+ ):
108
+ """Returns the entry of a challenge on an academy scenario of the same name."""
109
+ return {
110
+ 'id': identifier,
111
+ 'title': title,
112
+ 'scenario': _ACADEMY + identifier,
113
+ 'ability': [None, None],
114
+ 'attempts': 5,
115
+ 'measure': measure,
116
+ 'targets': list(targets),
117
+ **extra,
118
+ }
119
+
120
+
121
+ # The catalog. Each goal says what counts, and the targets give the value of a bronze, silver and
122
+ # gold medal. The targets come from runs of the built-in AI and of trivial agents, as the
123
+ # renderer's guide describes under Challenges. The category and the difficulty describe the
124
+ # challenge on its card.
125
+ CATALOG: tuple[dict[str, Any], ...] = (
126
+ _academy(
127
+ 'empty_goal_close',
128
+ 'Empty goal from close range',
129
+ 'within',
130
+ (3, 4, 5),
131
+ limit=3.0,
132
+ category='finishing',
133
+ difficulty='easy',
134
+ goal='Score into the empty goal within 3 seconds of play in each attempt.',
135
+ ),
136
+ _academy(
137
+ 'empty_goal',
138
+ 'Empty goal',
139
+ 'within',
140
+ (3, 4, 5),
141
+ limit=8.0,
142
+ category='finishing',
143
+ difficulty='easy',
144
+ goal='Run from the middle and score into the empty goal within 8 seconds of play.',
145
+ ),
146
+ _academy(
147
+ 'run_to_score',
148
+ 'Run to score',
149
+ 'within',
150
+ (1, 3, 5),
151
+ limit=6.5,
152
+ category='dribbling',
153
+ difficulty='medium',
154
+ goal='Outrun five defenders and score into the empty goal within 6.5 seconds of play.',
155
+ ),
156
+ _academy(
157
+ 'run_to_score_with_keeper',
158
+ 'Run to score past a keeper',
159
+ 'scored',
160
+ (1, 3, 5),
161
+ category='dribbling',
162
+ difficulty='medium',
163
+ goal='Outrun five defenders and beat the goalkeeper.',
164
+ ),
165
+ _academy(
166
+ 'pass_and_shoot_with_keeper',
167
+ 'Pass and shoot past a keeper',
168
+ 'scored',
169
+ (1, 3, 5),
170
+ category='combination',
171
+ difficulty='medium',
172
+ goal='Free the unmarked attacker with a pass and score past the goalkeeper.',
173
+ ),
174
+ _academy(
175
+ 'run_pass_and_shoot_with_keeper',
176
+ 'Run, pass and shoot past a keeper',
177
+ 'scored',
178
+ (1, 3, 5),
179
+ category='combination',
180
+ difficulty='hard',
181
+ goal='Run with the ball, pass when the defender commits and score past the goalkeeper.',
182
+ ),
183
+ _academy(
184
+ '3_vs_1_with_keeper',
185
+ 'Three against one',
186
+ 'scored',
187
+ (1, 3, 5),
188
+ category='combination',
189
+ difficulty='medium',
190
+ goal='Combine three attackers against one defender and score past the goalkeeper.',
191
+ ),
192
+ _academy(
193
+ 'corner',
194
+ 'Corner',
195
+ 'scored',
196
+ (1, 2, 4),
197
+ category='set_piece',
198
+ difficulty='hard',
199
+ goal='Score from the corner before the ball goes out or the clock runs down.',
200
+ ),
201
+ _academy(
202
+ 'counterattack_easy',
203
+ 'Counterattack',
204
+ 'scored',
205
+ (1, 3, 5),
206
+ category='counterattack',
207
+ difficulty='medium',
208
+ goal='Finish a counterattack of four attackers against one defender.',
209
+ ),
210
+ _academy(
211
+ 'counterattack_hard',
212
+ 'Counterattack, hard',
213
+ 'scored',
214
+ (1, 2, 4),
215
+ category='counterattack',
216
+ difficulty='hard',
217
+ goal='Finish a counterattack of four attackers against two defenders.',
218
+ ),
219
+ _academy(
220
+ 'single_goal_versus_lazy',
221
+ 'Single goal against a lazy team',
222
+ 'within',
223
+ (2, 4, 5),
224
+ limit=30.0,
225
+ category='breakthrough',
226
+ difficulty='easy',
227
+ goal='Score against outfield players who stand still, within 30 seconds of play.',
228
+ ),
229
+ {
230
+ 'id': 'one_on_one',
231
+ 'title': 'One on one',
232
+ 'scenario': '1_vs_1_easy',
233
+ 'ability': [None, None],
234
+ 'attempts': 3,
235
+ 'measure': 'margin',
236
+ 'targets': [1, 3, 6],
237
+ 'category': 'duel',
238
+ 'difficulty': 'medium',
239
+ 'goal': 'Outscore a single opponent over three short matches, goals for minus against.',
240
+ },
241
+ {
242
+ 'id': 'one_on_one_hard',
243
+ 'title': 'One on one, hard',
244
+ 'scenario': '1_vs_1_easy',
245
+ 'ability': [None, 1.0],
246
+ 'attempts': 3,
247
+ 'measure': 'margin',
248
+ 'targets': [1, 2, 4],
249
+ 'category': 'duel',
250
+ 'difficulty': 'hard',
251
+ 'goal': 'Outscore a single opponent at ability 1.0 over three short matches.',
252
+ },
253
+ )
254
+
255
+
256
+ def seeds(identifier: str, count: int) -> list[int]:
257
+ """Returns the seeds of the first `count` attempts of a challenge.
258
+
259
+ Attempt `i` plays `(crc32(identifier) + 7919 * i) % 2**31`, with the identifier in UTF 8.
260
+ """
261
+ base = zlib.crc32(identifier.encode())
262
+ return [(base + 7919 * index) % 2**31 for index in range(count)]
263
+
264
+
265
+ def find(identifier: Any) -> dict[str, Any]:
266
+ """Returns the catalog entry of a challenge, with its seeds and played abilities.
267
+
268
+ Raises:
269
+ ArgumentError: If no challenge has that identifier.
270
+ """
271
+ for entry in CATALOG:
272
+ if entry['id'] == identifier:
273
+ return {
274
+ **entry,
275
+ 'targets': list(entry['targets']),
276
+ 'ability': list(entry['ability']),
277
+ 'limit': entry.get('limit'),
278
+ 'seeds': seeds(entry['id'], entry['attempts']),
279
+ 'played_ability': _played_ability(entry['scenario'], tuple(entry['ability'])),
280
+ 'medals': medal_items(entry),
281
+ }
282
+ raise MessageError(
283
+ 'server.challenges.unknown',
284
+ challenges=', '.join(entry['id'] for entry in CATALOG),
285
+ identifier=repr(identifier),
286
+ )
287
+
288
+
289
+ def challenges() -> list[dict[str, Any]]:
290
+ """Returns every challenge of the catalog, with its seeds and the abilities it plays."""
291
+ return [find(entry['id']) for entry in CATALOG]
292
+
293
+
294
+ _PLAYED: dict[tuple[str, Any], list[float]] = {}
295
+
296
+
297
+ def _played_ability(scenario: str, ability: tuple[Any, Any]) -> list[float]:
298
+ """Returns the ability of each side that a challenge plays, the scenario's where none is set."""
299
+ key = (scenario, ability)
300
+ if key not in _PLAYED:
301
+ from cuju.scenarios import Builder, load_scenario
302
+
303
+ builder = Builder(0)
304
+ load_scenario(_teams.scenario_path(scenario))(builder)
305
+ config = builder.config()
306
+ defaults = (config.left_team_difficulty, config.right_team_difficulty)
307
+ _PLAYED[key] = [
308
+ round(float(default if value is None else value), 4)
309
+ for value, default in zip(ability, defaults, strict=True)
310
+ ]
311
+ return list(_PLAYED[key])
312
+
313
+
314
+ def medal(challenge: dict[str, Any], value: float) -> str | None:
315
+ """Returns the best medal whose target `value` reaches, or `None`."""
316
+ won = None
317
+ for name, target in zip(MEDALS, challenge['targets'], strict=True):
318
+ if value >= target:
319
+ won = name
320
+ return won
321
+
322
+
323
+ def medal_items(challenge: dict[str, Any], value: float | None = None) -> list[dict[str, Any]]:
324
+ """Returns the medals of a challenge as items, gold, silver and bronze in that order.
325
+
326
+ Each item names its medal, the value its target asks for, whether that target asks for a goal
327
+ in every attempt, and whether `value` reached that medal, so a run or a competitor's best marks
328
+ the one item of the best medal it won and none when it won no medal.
329
+
330
+ Args:
331
+ challenge: The challenge, as `find` returns it.
332
+ value: The value of a run or a best run, or `None` to mark no item.
333
+ """
334
+ won = None if value is None else medal(challenge, value)
335
+ targets = dict(zip(MEDALS, challenge['targets'], strict=True))
336
+ return [
337
+ {
338
+ 'medal': name,
339
+ 'target': targets[name],
340
+ 'every': challenge['measure'] != 'margin' and targets[name] >= challenge['attempts'],
341
+ 'reached': won == name,
342
+ }
343
+ for name in MEDAL_ORDER
344
+ ]
345
+
346
+
347
+ def target_words(challenge: dict[str, Any], item: dict[str, Any]) -> str:
348
+ """Returns the target of a medal item in plain English, as the command line prints it."""
349
+ target = item['target']
350
+ if challenge['measure'] == 'margin':
351
+ return f'{_messages.counted(target, "goal")} or more ahead'
352
+ within = '' if challenge['measure'] == 'scored' else f' within {challenge["limit"]:g} seconds'
353
+ if item['every']:
354
+ return f'a goal{within} in all {challenge["attempts"]} attempts'
355
+ return f'a goal{within} in {_messages.counted(target, "attempt")} or more'
356
+
357
+
358
+ def medal_text(challenge: dict[str, Any], value: float | None = None) -> str:
359
+ """Returns the medal items of a challenge in one line, the item that `value` reached marked."""
360
+ items = [
361
+ f'{item["medal"]}, {target_words(challenge, item)}{", reached" if item["reached"] else ""}'
362
+ for item in medal_items(challenge, value)
363
+ ]
364
+ return 'medals ' + '; '.join(items)
365
+
366
+
367
+ def score_attempt(header: dict[str, Any], frames: list[bytes], side: int = 0) -> dict[str, Any]:
368
+ """Scores one attempt from the frames of its recording.
369
+
370
+ The seconds of play run from the first frame to the frame of the goal, at ten steps a second,
371
+ whatever the game clock shows. An attempt whose last frame lacks the episode end flag ended
372
+ early, so it counts its goals against and no goals for.
373
+
374
+ Args:
375
+ header: The header of the recording.
376
+ frames: Its frames, as `_recording.frames` yields them.
377
+ side: The side that the challenge controls, the left one.
378
+
379
+ Returns:
380
+ Whether the episode finished, the goals for and against, whether the side scored and the
381
+ seconds of play to its first goal, or `None` without a goal.
382
+ """
383
+ if not frames:
384
+ return {'finished': False, 'goals': [0, 0], 'scored': False, 'seconds': None}
385
+ steps = float(header.get('steps_per_second') or STEPS_PER_SECOND)
386
+ first = _SCORE.unpack_from(frames[0], 8)
387
+ last = _SCORE.unpack_from(frames[-1], 8)
388
+ finished = bool(_FLAGS.unpack_from(frames[-1], 28)[0] & 1)
389
+ goals_for = max(0, last[side] - first[side]) if finished else 0
390
+ against = max(0, last[1 - side] - first[1 - side])
391
+ seconds = None
392
+ if goals_for:
393
+ for index, frame in enumerate(frames):
394
+ if _SCORE.unpack_from(frame, 8)[side] > first[side]:
395
+ seconds = round(index / steps, 1)
396
+ break
397
+ return {
398
+ 'finished': finished,
399
+ 'goals': [goals_for, against],
400
+ 'scored': goals_for > 0,
401
+ 'seconds': seconds,
402
+ }
403
+
404
+
405
+ def _counts(challenge: dict[str, Any], result: dict[str, Any]) -> bool:
406
+ """Returns whether an attempt counts towards the value of a challenge of attempts."""
407
+ if not result['scored']:
408
+ return False
409
+ return challenge['measure'] != 'within' or result['seconds'] <= challenge['limit']
410
+
411
+
412
+ def score_run(challenge: dict[str, Any], results: list[dict[str, Any]]) -> dict[str, Any]:
413
+ """Returns the value, the seconds and the medal of a run of attempts.
414
+
415
+ The seconds add up the seconds to score of the attempts that count, every scoring attempt for
416
+ a measure of goals, and break ties between runs of the same value.
417
+ """
418
+ if challenge['measure'] == 'margin':
419
+ value = sum(result['goals'][0] - result['goals'][1] for result in results)
420
+ counted = [result for result in results if result['scored']]
421
+ else:
422
+ counted = [result for result in results if _counts(challenge, result)]
423
+ value = len(counted)
424
+ seconds = round(sum(result['seconds'] for result in counted), 1)
425
+ return {'value': value, 'seconds': seconds, 'medal': medal(challenge, value)}
426
+
427
+
428
+ def _attempt_result(
429
+ challenge: dict[str, Any],
430
+ index: int,
431
+ match: str | None,
432
+ scored: dict[str, Any],
433
+ failure: BaseException | None = None,
434
+ ) -> dict[str, Any]:
435
+ """Returns the stored result of one attempt, its index, seed and recording with its score.
436
+
437
+ An attempt whose policy failed says so in `failed` and carries the policy's error as `error`,
438
+ `error_id` and `error_values`, and its score is that of an attempt that ended early.
439
+ """
440
+ result = {
441
+ 'index': index,
442
+ 'seed': challenge['seeds'][index],
443
+ 'match': match,
444
+ **scored,
445
+ 'counts': _counts(challenge, scored),
446
+ 'failed': failure is not None,
447
+ }
448
+ if failure is not None:
449
+ result.update(_messages.entry_fields(failure))
450
+ return result
451
+
452
+
453
+ # Leaderboards.
454
+
455
+
456
+ def _number(value: Any) -> bool:
457
+ """Returns whether `value` is a finite JSON number, which excludes true and false."""
458
+ return isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value)
459
+
460
+
461
+ def _whole(value: Any) -> bool:
462
+ return isinstance(value, int) and not isinstance(value, bool)
463
+
464
+
465
+ def _valid_attempt(value: Any) -> bool:
466
+ return (
467
+ isinstance(value, dict)
468
+ and _whole(value.get('index'))
469
+ and _whole(value.get('seed'))
470
+ and (value.get('match') is None or isinstance(value.get('match'), str))
471
+ and isinstance(value.get('finished'), bool)
472
+ and isinstance(value.get('goals'), list)
473
+ and len(value['goals']) == 2
474
+ and all(_whole(goal) and goal >= 0 for goal in value['goals'])
475
+ and isinstance(value.get('scored'), bool)
476
+ and (value.get('seconds') is None or (_number(value['seconds']) and value['seconds'] >= 0))
477
+ )
478
+
479
+
480
+ # Every field of a stored entry with its check, so that an entry edited by hand or written by a
481
+ # later version is skipped with a warning and the rest of the board stays.
482
+ _ENTRY_FIELDS: dict[str, Callable[[Any], bool]] = {
483
+ 'kind': lambda value: value in ('person', 'agent'),
484
+ 'name': lambda value: isinstance(value, str) and 0 < len(value) <= NAME_LENGTH,
485
+ 'team': lambda value: value is None or isinstance(value, str),
486
+ 'value': _number,
487
+ 'seconds': lambda value: _number(value) and value >= 0,
488
+ 'medal': lambda value: value is None or value in MEDALS,
489
+ 'attempts': lambda value: isinstance(value, list) and all(map(_valid_attempt, value)),
490
+ 'time': _number,
491
+ 'cuju_version': lambda value: isinstance(value, str),
492
+ }
493
+
494
+
495
+ def _folder() -> Path:
496
+ return _home.folder('challenges')
497
+
498
+
499
+ def _board_path(identifier: str) -> Path:
500
+ return _folder() / f'{identifier}.json'
501
+
502
+
503
+ _THREAD_LOCK = threading.RLock()
504
+ # A lock file older than this belongs to a process that stopped while it held the lock.
505
+ _STALE_LOCK_SECONDS = 30.0
506
+
507
+
508
+ @contextlib.contextmanager
509
+ def _locked() -> Any:
510
+ """Holds the lock of the leaderboards, within this process and across processes.
511
+
512
+ The server, a command and a Python call may each write a board, so a lock file created
513
+ exclusively keeps their writes apart, and one left behind by a stopped process expires.
514
+ """
515
+ with _THREAD_LOCK:
516
+ path = _folder() / '.lock'
517
+ deadline = time.monotonic() + 2 * _STALE_LOCK_SECONDS
518
+ while True:
519
+ try:
520
+ handle = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
521
+ break
522
+ except FileExistsError:
523
+ with contextlib.suppress(OSError):
524
+ if time.time() - path.stat().st_mtime > _STALE_LOCK_SECONDS:
525
+ path.unlink()
526
+ continue
527
+ if time.monotonic() > deadline:
528
+ raise OSError(f'expected the lock of the leaderboards {path} to free') from None
529
+ time.sleep(0.05)
530
+ os.close(handle)
531
+ try:
532
+ yield
533
+ finally:
534
+ with contextlib.suppress(OSError):
535
+ path.unlink()
536
+
537
+
538
+ def _read_board(identifier: str) -> list[dict[str, Any]]:
539
+ """Returns the valid entries of a challenge's board, best first, warning once of others."""
540
+ path = _board_path(identifier)
541
+ if not path.is_file():
542
+ return []
543
+ try:
544
+ document = json.loads(path.read_text(encoding='utf-8'))
545
+ except (OSError, ValueError) as error:
546
+ _LOGGER.warning('cuju render skips the leaderboard file %s, %s', path.name, error)
547
+ return []
548
+ if (
549
+ not isinstance(document, dict)
550
+ or document.get('challenge') != identifier
551
+ or not isinstance(document.get('entries'), list)
552
+ ):
553
+ _LOGGER.warning(
554
+ 'cuju render skips the leaderboard file %s, which holds no board of %s',
555
+ path.name,
556
+ identifier,
557
+ )
558
+ return []
559
+ entries, skipped = [], 0
560
+ for entry in document['entries']:
561
+ if isinstance(entry, dict) and all(
562
+ key in entry and check(entry[key]) for key, check in _ENTRY_FIELDS.items()
563
+ ):
564
+ kept = {key: entry[key] for key in _ENTRY_FIELDS}
565
+ if isinstance(entry.get('default_name'), bool):
566
+ kept['default_name'] = entry['default_name']
567
+ if _playing_pair(entry.get('playing')):
568
+ kept['playing'] = entry['playing']
569
+ entries.append(kept)
570
+ else:
571
+ skipped += 1
572
+ if skipped:
573
+ _LOGGER.warning(
574
+ 'cuju render skips %d invalid entries of the leaderboard file %s', skipped, path.name
575
+ )
576
+ entries.sort(key=_rank_key)
577
+ # People under default names of two languages are one competitor, whose best run counts.
578
+ seen: set[tuple[str, str]] = set()
579
+ best = []
580
+ for entry in entries:
581
+ if _competitor(entry) not in seen:
582
+ seen.add(_competitor(entry))
583
+ best.append(entry)
584
+ return best
585
+
586
+
587
+ def _playing_pair(value: Any) -> bool:
588
+ """Returns whether a value is the players that played and those a team controls, fewer first."""
589
+ return (
590
+ isinstance(value, list)
591
+ and len(value) == 2
592
+ and all(_whole(number) for number in value)
593
+ and 1 <= value[0] < value[1] <= 11
594
+ )
595
+
596
+
597
+ def _write_board(identifier: str, entries: list[dict[str, Any]]) -> None:
598
+ """Writes a board through a temporary file, so no reader ever sees half of it."""
599
+ document = {'challenge': identifier, 'entries': entries}
600
+ _teams._replace(_board_path(identifier), json.dumps(document).encode())
601
+
602
+
603
+ def _rank_key(entry: dict[str, Any]) -> tuple[float, float, float]:
604
+ return (-entry['value'], entry['seconds'], entry['time'])
605
+
606
+
607
+ def default_person(entry: dict[str, Any]) -> bool:
608
+ """Returns whether an entry is a person's under a default name, by its flag or its name.
609
+
610
+ A person who types no name plays under the default name of the page's language, and a new
611
+ entry says so in `default_name`. An older one names the default of a language that the
612
+ renderer knows, which counts the same.
613
+ """
614
+ if entry.get('kind') != 'person':
615
+ return False
616
+ return entry.get('default_name') is True or entry.get('name') in _languages.default_names()
617
+
618
+
619
+ def _competitor(entry: dict[str, Any]) -> tuple[str, str]:
620
+ """Returns who an entry belongs to, a person by name, an agent by team or else by name.
621
+
622
+ Every person under a default name is the same competitor, whatever the language of the name.
623
+ """
624
+ if entry['kind'] == 'agent' and entry['team'] is not None:
625
+ return ('team', entry['team'])
626
+ if default_person(entry):
627
+ return ('person', '')
628
+ return (entry['kind'], entry['name'])
629
+
630
+
631
+ def challenge_value(spec: dict[str, Any]) -> dict[str, Any]:
632
+ """Returns a challenge as a value of a message, which the page shows by its own title."""
633
+ return {'kind': 'challenge', 'name': spec['id'], 'text': spec['title']}
634
+
635
+
636
+ def competitor_name(entry: dict[str, Any]) -> dict[str, Any] | str:
637
+ """Returns the name of a competitor as a message value, a label for a name that cuju made.
638
+
639
+ The built-in AI under its default name, a policy without a name of its own and a person under
640
+ a default name are labels, which the page shows in its language, and any other name stays the
641
+ text it is.
642
+ """
643
+ name = entry.get('name')
644
+ if default_person(entry):
645
+ return _messages.label('server.names.default_person')
646
+ builtin = _messages.label('server.names.builtin')
647
+ made = entry.get('kind') == 'agent' and entry.get('team') == _teams.BUILTIN
648
+ if made and name == builtin['text']:
649
+ return builtin
650
+ if entry.get('kind') == 'agent' and entry.get('team') is None and name == POLICY_NAME:
651
+ return _messages.label('server.names.python_policy')
652
+ return str(name)
653
+
654
+
655
+ def _named(entry: dict[str, Any]) -> dict[str, Any]:
656
+ """Returns an entry of a board or a run with `name_id` and `name_values` for a made name."""
657
+ return {**entry, **_messages.named('name', competitor_name(entry))}
658
+
659
+
660
+ def board(identifier: str) -> list[dict[str, Any]]:
661
+ """Returns the leaderboard of a challenge, best first, each entry with its rank."""
662
+ find(identifier)
663
+ return [
664
+ _named({**entry, 'rank': rank}) for rank, entry in enumerate(_read_board(identifier), 1)
665
+ ]
666
+
667
+
668
+ def record(identifier: str, entry: dict[str, Any]) -> dict[str, Any]:
669
+ """Adds a run to a challenge's board, which keeps each competitor's best run.
670
+
671
+ Returns:
672
+ The competitor's best run with its rank, `None` for a rank outside the board, whether
673
+ this run improved on the best one before it, and whether it is the competitor's first run
674
+ on the board, which improves on nothing before it.
675
+ """
676
+ with _locked():
677
+ entries = _read_board(identifier)
678
+ who = _competitor(entry)
679
+ before = next((item for item in entries if _competitor(item) == who), None)
680
+ improved = before is None or _rank_key(entry) < _rank_key(before)
681
+ if improved:
682
+ entries = [item for item in entries if _competitor(item) != who] + [entry]
683
+ entries.sort(key=_rank_key)
684
+ entries = entries[:BOARD_SIZE]
685
+ _write_board(identifier, entries)
686
+ best = next((item for item in entries if _competitor(item) == who), entry)
687
+ rank = next(
688
+ (rank for rank, item in enumerate(entries, 1) if _competitor(item) == who), None
689
+ )
690
+ return {
691
+ 'personal_best': _named({**best, 'rank': rank}),
692
+ 'rank': rank,
693
+ 'improved': improved,
694
+ 'first': before is None,
695
+ }
696
+
697
+
698
+ def personal_bests(kind: str, name: str | None = None, team: str | None = None) -> dict[str, Any]:
699
+ """Returns a competitor's best run of every challenge whose board holds one, by challenge."""
700
+ who = _competitor({'kind': kind, 'name': name, 'team': team})
701
+ bests = {}
702
+ for entry in CATALOG:
703
+ for rank, item in enumerate(_read_board(entry['id']), 1):
704
+ if _competitor(item) == who:
705
+ bests[entry['id']] = {**item, 'rank': rank}
706
+ break
707
+ return bests
708
+
709
+
710
+ def _summary(identifier: str, person: str | None) -> dict[str, Any]:
711
+ """Returns the size and leaders of a board, with a person's own best run when named."""
712
+ entries = board(identifier)
713
+ summary = {
714
+ 'entries': len(entries),
715
+ 'people': sum(entry['kind'] == 'person' for entry in entries),
716
+ 'agents': sum(entry['kind'] == 'agent' for entry in entries),
717
+ 'top': [_short(entry) for entry in entries[:3]],
718
+ 'mine': None,
719
+ }
720
+ if person:
721
+ who = _competitor({'kind': 'person', 'name': person, 'team': None})
722
+ mine = next((entry for entry in entries if _competitor(entry) == who), None)
723
+ summary['mine'] = None if mine is None else _short(mine)
724
+ return summary
725
+
726
+
727
+ def _short(entry: dict[str, Any]) -> dict[str, Any]:
728
+ keys = ('rank', 'kind', 'name', 'team', 'value', 'seconds', 'medal')
729
+ short = {key: entry[key] for key in keys}
730
+ if 'playing' in entry:
731
+ short['playing'] = entry['playing']
732
+ return _named(short)
733
+
734
+
735
+ def _entry(
736
+ kind: str,
737
+ name: str,
738
+ team: str | None,
739
+ totals: dict[str, Any],
740
+ results: list[dict[str, Any]],
741
+ default_name: bool = False,
742
+ playing: list[int] | None = None,
743
+ ) -> dict[str, Any]:
744
+ """Returns an entry of a board. A team that played with players out keeps `playing`, the
745
+ players that played and the players it controls, so the board says so beside its result."""
746
+ entry = {
747
+ 'kind': kind,
748
+ 'name': name,
749
+ 'team': team,
750
+ 'value': totals['value'],
751
+ 'seconds': totals['seconds'],
752
+ 'medal': totals['medal'],
753
+ 'attempts': [
754
+ {
755
+ **{
756
+ key: result[key]
757
+ for key in ('index', 'seed', 'match', 'finished', 'goals', 'scored', 'seconds')
758
+ },
759
+ 'failed': bool(result.get('failed')),
760
+ }
761
+ for result in results
762
+ ],
763
+ 'time': time.time(),
764
+ 'cuju_version': __version__,
765
+ }
766
+ if kind == 'person':
767
+ entry['default_name'] = default_name
768
+ if playing is not None:
769
+ entry['playing'] = list(playing)
770
+ return entry
771
+
772
+
773
+ def _default_flag(value: Any, name: str) -> bool:
774
+ """Returns whether a person's name is a default one, as the page says or the name shows.
775
+
776
+ Raises:
777
+ ArgumentError: If the flag is no boolean.
778
+ """
779
+ if not isinstance(value, bool):
780
+ raise MessageError('server.challenges.default_name', value=repr(value))
781
+ return value or name in _languages.default_names()
782
+
783
+
784
+ # The name of a policy whose function has no name of its own, such as a lambda.
785
+ POLICY_NAME = 'Python policy'
786
+
787
+
788
+ def _policy_name(policy: Any) -> str:
789
+ """Returns the default name of a policy, its function's name, or `Python policy` for none."""
790
+ name = getattr(policy, '__name__', None)
791
+ if not isinstance(name, str) or not name.isidentifier():
792
+ return POLICY_NAME
793
+ return name
794
+
795
+
796
+ def _name(value: Any) -> str:
797
+ """Returns a competitor's name, printable and trimmed, or raises `ArgumentError`."""
798
+ if not isinstance(value, str):
799
+ raise MessageError('server.challenges.name_text', value=repr(value))
800
+ name = ''.join(character for character in value if character.isprintable()).strip()
801
+ if not 0 < len(name) <= NAME_LENGTH:
802
+ raise MessageError('server.challenges.name_length', most=NAME_LENGTH, value=repr(value))
803
+ return name
804
+
805
+
806
+ # People.
807
+
808
+
809
+ class UnknownRecordingError(MessageError):
810
+ """Raised for a recording that the library does not hold outside the Trash."""
811
+
812
+
813
+ def _recording_of(match: Any) -> tuple[dict[str, Any], list[bytes]]:
814
+ """Returns the header and frames of a recording of the library.
815
+
816
+ Raises:
817
+ UnknownRecordingError: If the library holds no such recording outside the Trash.
818
+ """
819
+ path = _library.path_of(match) if isinstance(match, str) else None
820
+ if path is None:
821
+ raise UnknownRecordingError('server.challenges.recording', match=repr(match))
822
+ return _recording.read_header(path), list(_recording.frames(path))
823
+
824
+
825
+ def _close(first: Any, second: Any) -> bool:
826
+ return _number(first) and _number(second) and abs(first - second) < 1e-3
827
+
828
+
829
+ def check_person(
830
+ challenge: dict[str, Any], index: int, header: dict[str, Any]
831
+ ) -> dict[str, Any] | None:
832
+ """Returns why a recording cannot be a person's attempt `index` of a challenge, or `None`.
833
+
834
+ A person's attempt is a game of the Play page with a person on the left alone, the built-in AI
835
+ everywhere else, the challenge's scenario and abilities and the seed of the attempt. The reason
836
+ is a label of `_messages.label`, which the page shows in its own language.
837
+ """
838
+ provenance = header.get('provenance')
839
+ if not isinstance(provenance, dict):
840
+ return _messages.label('server.challenges.no_provenance')
841
+ expected = {
842
+ 'source': 'human_game',
843
+ 'scenario': challenge['scenario'],
844
+ 'seed': challenge['seeds'][index],
845
+ 'humans': [True, False],
846
+ 'teams': [_teams.BUILTIN, _teams.BUILTIN],
847
+ 'agent_counts': [1, 0],
848
+ }
849
+ for key, value in expected.items():
850
+ found = provenance.get(key)
851
+ if found != value:
852
+ return _messages.label(
853
+ 'server.challenges.provenance',
854
+ key=key,
855
+ value=repr(value),
856
+ attempt=index + 1,
857
+ found=repr(found),
858
+ )
859
+ played = provenance.get('ability')
860
+ if not (
861
+ isinstance(played, list)
862
+ and len(played) == 2
863
+ and all(map(_close, played, challenge['played_ability']))
864
+ ):
865
+ return _messages.label(
866
+ 'server.challenges.ability',
867
+ ability=repr(challenge['played_ability']),
868
+ found=repr(played),
869
+ )
870
+ return None
871
+
872
+
873
+ def score_recording(identifier: str, match: str, attempt: Any) -> dict[str, Any]:
874
+ """Scores a recording as a person's attempt of a challenge, and says why it may not count.
875
+
876
+ Raises:
877
+ ArgumentError: If the challenge, the attempt or the recording is unknown.
878
+ """
879
+ challenge = find(identifier)
880
+ index = _attempt_index(challenge, attempt)
881
+ header, frames = _recording_of(match)
882
+ problem = check_person(challenge, index, header)
883
+ result = _attempt_result(challenge, index, match, score_attempt(header, frames))
884
+ return {'challenge': identifier, 'result': result, **_messages.named('problem', problem)}
885
+
886
+
887
+ def _attempt_index(challenge: dict[str, Any], attempt: Any) -> int:
888
+ if isinstance(attempt, str) and attempt.isdigit():
889
+ attempt = int(attempt)
890
+ if not _whole(attempt) or not 0 <= attempt < challenge['attempts']:
891
+ raise MessageError(
892
+ 'server.challenges.attempt', last=challenge['attempts'] - 1, attempt=repr(attempt)
893
+ )
894
+ return attempt
895
+
896
+
897
+ def person_run(
898
+ identifier: str,
899
+ person: Any,
900
+ matches: Any,
901
+ keep_recordings: Any = True,
902
+ default_name: Any = False,
903
+ ) -> dict[str, Any]:
904
+ """Checks, scores and records a person's run, the recordings of its attempts in order.
905
+
906
+ Every recording is scored again from its frames and must match its attempt, as
907
+ `check_person` describes, and each one counts once. An attempt that a person left before its
908
+ first step has no recording, `None`, and counts as missed. The recordings of the run become a
909
+ group, or with `keep_recordings` false leave the library for good once the run is scored, and
910
+ its attempts then name no recording. A person who typed no name plays under the default name
911
+ of the page's language with `default_name` true, and the run's entry keeps that flag, so the
912
+ page shows the default of its own language wherever the name shows.
913
+
914
+ Raises:
915
+ ArgumentError: If the name, the number of recordings or any recording does not fit.
916
+ """
917
+ challenge = find(identifier)
918
+ name = _name(person)
919
+ keep = _keep_flag(keep_recordings)
920
+ default = _default_flag(default_name, name)
921
+ if not isinstance(matches, list) or len(matches) != challenge['attempts']:
922
+ raise MessageError(
923
+ 'server.challenges.matches', count=challenge['attempts'], matches=repr(matches)
924
+ )
925
+ named = [match for match in matches if match is not None]
926
+ if len(set(named)) != len(named):
927
+ raise MessageError('server.challenges.distinct')
928
+ results, recordings = [], []
929
+ for index, match in enumerate(matches):
930
+ if match is None:
931
+ missed = {'finished': False, 'goals': [0, 0], 'scored': False, 'seconds': None}
932
+ results.append(_attempt_result(challenge, index, None, missed))
933
+ continue
934
+ header, frames = _recording_of(match)
935
+ problem = check_person(challenge, index, header)
936
+ if problem is not None:
937
+ raise MessageError(
938
+ 'server.challenges.attempt_recording',
939
+ match=match,
940
+ attempt=index + 1,
941
+ reason=problem,
942
+ )
943
+ results.append(_attempt_result(challenge, index, match, score_attempt(header, frames)))
944
+ recordings.append((header, frames))
945
+ if not keep:
946
+ results = [{**result, 'match': None} for result in results]
947
+ totals = score_run(challenge, results)
948
+ entry = _entry('person', name, None, totals, results, default)
949
+ outcome = {
950
+ 'challenge': identifier,
951
+ 'kind': 'person',
952
+ **_messages.named('name', competitor_name(entry)),
953
+ 'team': None,
954
+ 'default_name': default,
955
+ 'practice': False,
956
+ 'attempts': results,
957
+ **totals,
958
+ 'medals': medal_items(challenge, totals['value']),
959
+ **record(identifier, entry),
960
+ }
961
+ outcome['achievements'] = _achievements_of(
962
+ lambda: _achievements.for_person_run(recordings, outcome)
963
+ )
964
+ shown = competitor_name(entry)
965
+ outcome['group'] = _end_person_run(challenge, shown, matches, keep, 'done', outcome)
966
+ return outcome
967
+
968
+
969
+ def leave_run(
970
+ identifier: str,
971
+ person: Any,
972
+ matches: Any,
973
+ keep_recordings: Any = True,
974
+ default_name: Any = False,
975
+ ) -> dict[str, Any]:
976
+ """Keeps the attempts of a run that a person left as a group, or removes them for good.
977
+
978
+ Each recording must match its attempt, as `check_person` describes, so only the attempts of
979
+ the challenge leave the library.
980
+
981
+ Args:
982
+ identifier: The challenge.
983
+ person: The person's name.
984
+ matches: The recordings of the attempts played, in order, `None` for a missed one.
985
+ keep_recordings: Whether the library keeps them, as a group of a left run.
986
+ default_name: Whether the person plays under the default name of the page's language.
987
+
988
+ Returns:
989
+ The challenge, the group or `None`, and the recordings that left the library.
990
+
991
+ Raises:
992
+ ArgumentError: If the name, the recordings or any recording does not fit.
993
+ """
994
+ challenge = find(identifier)
995
+ name = _name(person)
996
+ keep = _keep_flag(keep_recordings)
997
+ default = _default_flag(default_name, name)
998
+ if not isinstance(matches, list) or not 0 < len(matches) <= challenge['attempts']:
999
+ raise MessageError(
1000
+ 'server.challenges.left_matches', count=challenge['attempts'], matches=repr(matches)
1001
+ )
1002
+ named = [match for match in matches if match is not None]
1003
+ if len(set(named)) != len(named):
1004
+ raise MessageError('server.challenges.distinct')
1005
+ for index, match in enumerate(matches):
1006
+ if match is None:
1007
+ continue
1008
+ path = _library.path_of(match) if isinstance(match, str) else None
1009
+ if path is None:
1010
+ raise UnknownRecordingError('server.challenges.recording', match=repr(match))
1011
+ problem = check_person(challenge, index, _recording.read_header(path))
1012
+ if problem is not None:
1013
+ raise MessageError(
1014
+ 'server.challenges.attempt_recording',
1015
+ match=match,
1016
+ attempt=index + 1,
1017
+ reason=problem,
1018
+ )
1019
+ shown = competitor_name({'kind': 'person', 'name': name, 'default_name': default})
1020
+ group = _end_person_run(challenge, shown, matches, keep, 'left', None)
1021
+ return {'challenge': identifier, 'group': group, 'removed': [] if keep else named}
1022
+
1023
+
1024
+ def _end_person_run(
1025
+ challenge: dict[str, Any],
1026
+ name: dict[str, Any] | str,
1027
+ matches: list[Any],
1028
+ keep: bool,
1029
+ state: str,
1030
+ outcome: dict[str, Any] | None,
1031
+ ) -> str | None:
1032
+ """Groups the recordings of a person's run, or removes them for good, and returns the group."""
1033
+ named = [match for match in matches if match is not None]
1034
+ if not keep:
1035
+ for match in named:
1036
+ _library.remove(match)
1037
+ return None
1038
+ if not named:
1039
+ return None
1040
+ group = _home.new_identifier('g')
1041
+ count = challenge['attempts']
1042
+ _keep_group(_group(challenge, group, name, count, state, len(matches), outcome), matches)
1043
+ return group
1044
+
1045
+
1046
+ def _achievements_of(award: Callable[[], list[str]]) -> list[str]:
1047
+ """Returns the achievements that `award` finds, and none with a warning if it fails."""
1048
+ try:
1049
+ return award()
1050
+ except Exception as error:
1051
+ _LOGGER.warning('cuju render could not read the achievements of a challenge, %s', error)
1052
+ return []
1053
+
1054
+
1055
+ # Groups of recordings, one for each run whose attempts the library keeps.
1056
+
1057
+
1058
+ def _keep_flag(value: Any) -> bool:
1059
+ """Returns whether a run keeps its recordings, or raises `ArgumentError` for no boolean."""
1060
+ if not isinstance(value, bool):
1061
+ raise MessageError('server.challenges.keep_recordings', value=repr(value))
1062
+ return value
1063
+
1064
+
1065
+ def _result_label(
1066
+ spec: dict[str, Any], count: int, state: str, played: int, outcome: dict[str, Any] | None
1067
+ ) -> dict[str, Any]:
1068
+ """Returns the result of a run as a label, its value and medal, or how far it went."""
1069
+ if outcome is None:
1070
+ message = {'playing': 'server.names.run_playing', 'left': 'server.names.run_left'}
1071
+ return _messages.label(
1072
+ message.get(state, 'server.names.run_stopped'), played=played, attempts=count
1073
+ )
1074
+ value = outcome['value']
1075
+ if spec['measure'] == 'margin':
1076
+ shown = _messages.label('server.names.run_margin', value=f'{value:+d}' if value else '0')
1077
+ else:
1078
+ shown = _messages.label('server.names.run_attempts', value=value, attempts=count)
1079
+ if outcome['practice']:
1080
+ return _messages.label('server.names.run_practice', value=shown)
1081
+ if outcome['medal']:
1082
+ medal = _messages.label(f'server.names.medal_{outcome["medal"]}')
1083
+ return _messages.label('server.names.run_medal', value=shown, medal=medal)
1084
+ return _messages.label('server.names.run_no_medal', value=shown)
1085
+
1086
+
1087
+ def _group(
1088
+ spec: dict[str, Any],
1089
+ identifier: str,
1090
+ competitor: dict[str, Any] | str,
1091
+ count: int,
1092
+ state: str,
1093
+ played: int,
1094
+ outcome: dict[str, Any] | None = None,
1095
+ ) -> dict[str, Any]:
1096
+ """Returns the group of the recordings of a run, as the summaries of the library keep it.
1097
+
1098
+ The state is `playing` while a headless run plays, `done` once it is scored, `left` for a run
1099
+ that a person left and `stopped` for a run that ended on a failure.
1100
+ """
1101
+ title = _messages.label(
1102
+ 'server.names.challenge_run', challenge=challenge_value(spec), competitor=competitor
1103
+ )
1104
+ return {
1105
+ 'id': identifier,
1106
+ 'kind': 'challenge',
1107
+ 'challenge': spec['id'],
1108
+ **_messages.named('title', title),
1109
+ **_messages.named('competitor', competitor),
1110
+ **_messages.named('result', _result_label(spec, count, state, played, outcome)),
1111
+ 'state': state,
1112
+ 'attempts': count,
1113
+ 'value': None if outcome is None else outcome['value'],
1114
+ 'seconds': None if outcome is None else outcome['seconds'],
1115
+ 'medal': None if outcome is None else outcome['medal'],
1116
+ 'practice': False if outcome is None else outcome['practice'],
1117
+ }
1118
+
1119
+
1120
+ def _keep_group(group: dict[str, Any], matches: list[Any]) -> None:
1121
+ """Writes a group into the summaries of the recordings of its attempts, by attempt."""
1122
+ members = {match: index for index, match in enumerate(matches) if match is not None}
1123
+ if members:
1124
+ _library.set_group(group, members)
1125
+
1126
+
1127
+ # Agents.
1128
+
1129
+
1130
+ def run(
1131
+ challenge: str,
1132
+ team: str | None = None,
1133
+ policy: Callable[..., Any] | None = None,
1134
+ name: str | None = None,
1135
+ control: dict[str, Any] | None = None,
1136
+ attempts: int | None = None,
1137
+ device: str = 'cpu',
1138
+ progress: Callable[[dict[str, Any]], None] | None = None,
1139
+ keep_recordings: bool = True,
1140
+ fail_attempts: bool = False,
1141
+ ) -> dict[str, Any]:
1142
+ """Plays a challenge headless with a team of the registry or a Python policy, and scores it.
1143
+
1144
+ Each attempt plays in a batch of one environment, `{scenario}_l{agents}_r0` with the seed of
1145
+ the attempt and the challenge's abilities, as fast as the policy answers. A team of one
1146
+ dynamic agent therefore faces the very episode that a person faces on the Play page. Every
1147
+ attempt is saved as a recording of the run's group, and a full run reaches the leaderboard.
1148
+
1149
+ Args:
1150
+ challenge: The identifier of the challenge, such as `empty_goal_close`.
1151
+ team: A team of the registry, `builtin` for GRF's built-in AI on the whole side.
1152
+ policy: A Kaggle agent function, `agent(obs)` or `agent(obs, configuration)`, in place of
1153
+ a team.
1154
+ name: The name of a policy on the leaderboard, by default the function's name, or
1155
+ `Python policy` for a function without one, such as a lambda.
1156
+ control: The control of a policy, as a team's control settings, by default one player of
1157
+ dynamic assignment.
1158
+ attempts: The attempts to play, from 1 to the challenge's count, which is the default. A
1159
+ run of fewer attempts is practice and stays off the leaderboard.
1160
+ device: `cpu` or `cuda`.
1161
+ progress: A function called with the result of each attempt as it ends.
1162
+ keep_recordings: Whether the library keeps the attempts as recordings. A run without
1163
+ them scores, ranks and earns from the same frames, and its attempts name no
1164
+ recording.
1165
+ fail_attempts: Whether a policy that fails ends its attempt alone, as the page, the
1166
+ command line and the benchmarks play. The attempt then ends where the policy failed
1167
+ and counts as one that ended early, its goals against and no goals for, it says so in
1168
+ `failed` with the policy's error, and the next attempt loads the policy again. A
1169
+ policy that cannot load fails every attempt left. By default a failing policy raises.
1170
+
1171
+ Returns:
1172
+ The run, its value, seconds, medal, rank and best run on the leaderboard, its medals as
1173
+ `medal_items` gives them, the count of its `failed_attempts`, `playing`, the players that
1174
+ played and those the team controls for a team that played with players out, else `None`,
1175
+ the result and recording of each attempt, the `group` of its recordings or `None`, and the
1176
+ achievements that a team of the registry keeps, none for the built-in AI and for a Python
1177
+ policy, which keep none.
1178
+
1179
+ Raises:
1180
+ ArgumentError: If the challenge, the team, the control or the attempts do not fit.
1181
+ PolicyError: If the policy fails, answers with anything but actions, or stops answering,
1182
+ unless `fail_attempts` is true. Nothing then reaches the leaderboard.
1183
+ """
1184
+ from cuju.kaggle import AgentRunner
1185
+
1186
+ from .play import Policies, PolicyRunner, _default_actions
1187
+
1188
+ spec = find(challenge)
1189
+ scenario = spec['scenario']
1190
+ keep = _keep_flag(keep_recordings)
1191
+ if (team is None) == (policy is None):
1192
+ raise MessageError('server.challenges.team_or_policy')
1193
+ count = spec['attempts'] if attempts is None else attempts
1194
+ if not _whole(count) or not 1 <= count <= spec['attempts']:
1195
+ raise MessageError(
1196
+ 'server.challenges.attempts', most=spec['attempts'], attempts=repr(attempts)
1197
+ )
1198
+ builtin = _teams.entry(_teams.BUILTIN)
1199
+ if team is not None:
1200
+ side = _teams.entry(team)
1201
+ competitor = side['name']
1202
+ control = None if side['kind'] == 'builtin' else side.get('control')
1203
+ else:
1204
+ if not callable(policy):
1205
+ raise MessageError('server.challenges.policy', policy=repr(policy))
1206
+ competitor = _name(name if name is not None else _policy_name(policy))
1207
+ side = {'id': None, 'kind': 'policy', 'name': competitor, 'format': '11v11'}
1208
+ side['control'] = _teams.check_control(control, side['format'])
1209
+ control = side['control']
1210
+ fit = _teams.challenge_fit(side, scenario)
1211
+ agents = fit['agents']
1212
+ # A team that controls more players than the side holds plays with the others out.
1213
+ playing = None if fit['slots'] is None else [fit['playing'], fit['players']]
1214
+ checked = None if side['kind'] == 'builtin' else _teams.check_control(control, side['format'])
1215
+ extended = checked is not None and (
1216
+ checked['assignment'] == 'fixed' or checked['action_set'] == 'v2'
1217
+ )
1218
+ ability = tuple(spec['ability'])
1219
+
1220
+ def factory(batch: Any) -> Any:
1221
+ if side['kind'] != 'policy':
1222
+ return Policies(
1223
+ batch,
1224
+ [side, builtin],
1225
+ scenario,
1226
+ [agents, 0],
1227
+ extended,
1228
+ [0, agents],
1229
+ fits=[fit, None],
1230
+ )
1231
+ policies = Policies(batch, [builtin, builtin], scenario, [0, 0], extended, [0, 0])
1232
+ function = policy
1233
+ if extended and checked['action_set'] == 'default':
1234
+ function = _default_actions(policy)
1235
+ policies.players.append(
1236
+ {
1237
+ 'side': 0,
1238
+ 'name': competitor,
1239
+ 'runner': AgentRunner(function),
1240
+ 'columns': list(range(agents)),
1241
+ 'selected': fit['selected'],
1242
+ 'slots': fit['slots'],
1243
+ 'held': {},
1244
+ 'calls': 0,
1245
+ }
1246
+ )
1247
+ return policies
1248
+
1249
+ runner = PolicyRunner(hang=HANG_SECONDS) if agents else None
1250
+ # The failure of policies that could not load, which every attempt left shares.
1251
+ unloadable: BaseException | None = None
1252
+ kind_team = None if side['kind'] == 'policy' else side['id']
1253
+ shown = competitor_name({'kind': 'agent', 'name': competitor, 'team': kind_team})
1254
+ group = _home.new_identifier('g') if keep else None
1255
+ results: list[dict[str, Any]] = []
1256
+ recordings: list[tuple[dict[str, Any], list[bytes]]] = []
1257
+
1258
+ def keep_group(state: str, outcome: dict[str, Any] | None = None) -> None:
1259
+ if group is not None:
1260
+ entry = _group(spec, group, shown, count, state, len(results), outcome)
1261
+ _keep_group(entry, [result['match'] for result in results])
1262
+
1263
+ try:
1264
+ for index in range(count):
1265
+ failure = unloadable
1266
+ if failure is not None:
1267
+ match, header, frames = None, {}, []
1268
+ else:
1269
+ match, header, frames, failure = _play_attempt(
1270
+ spec,
1271
+ index,
1272
+ side,
1273
+ competitor,
1274
+ agents,
1275
+ extended,
1276
+ ability,
1277
+ device,
1278
+ runner,
1279
+ factory,
1280
+ keep,
1281
+ fail_attempts,
1282
+ )
1283
+ if failure is not None:
1284
+ if runner.policies is None:
1285
+ unloadable = failure
1286
+ # The next attempt loads the policies again on a thread of their own, since
1287
+ # a call that never returned may still hold the old one.
1288
+ runner.close()
1289
+ runner = PolicyRunner(hang=HANG_SECONDS)
1290
+ result = _attempt_result(spec, index, match, score_attempt(header, frames), failure)
1291
+ results.append(result)
1292
+ recordings.append((header, frames))
1293
+ keep_group('playing')
1294
+ if progress is not None:
1295
+ progress(dict(result))
1296
+ except BaseException:
1297
+ keep_group('stopped')
1298
+ raise
1299
+ finally:
1300
+ if runner is not None:
1301
+ runner.close()
1302
+ totals = score_run(spec, results)
1303
+ practice = count < spec['attempts']
1304
+ outcome: dict[str, Any] = {
1305
+ 'challenge': challenge,
1306
+ 'kind': 'agent',
1307
+ **_messages.named(
1308
+ 'name', competitor_name({'kind': 'agent', 'name': competitor, 'team': kind_team})
1309
+ ),
1310
+ 'team': kind_team,
1311
+ 'practice': practice,
1312
+ 'attempts': results,
1313
+ **totals,
1314
+ 'medals': medal_items(spec, None if practice else totals['value']),
1315
+ 'failed_attempts': sum(result['failed'] for result in results),
1316
+ 'playing': playing,
1317
+ }
1318
+ if practice:
1319
+ # Practice keeps its value but wins no medal and stays off the board.
1320
+ outcome.update(medal=None, rank=None, personal_best=None, improved=False, first=False)
1321
+ else:
1322
+ entry = _entry('agent', competitor, kind_team, totals, results, playing=playing)
1323
+ outcome.update(record(challenge, entry))
1324
+ outcome['achievements'] = _achievements_of(
1325
+ lambda: _achievements.for_agent_run(outcome, recordings)
1326
+ )
1327
+ keep_group('done', outcome)
1328
+ outcome['group'] = group
1329
+ return outcome
1330
+
1331
+
1332
+ def _play_attempt(
1333
+ spec: dict[str, Any],
1334
+ index: int,
1335
+ side: dict[str, Any],
1336
+ competitor: str,
1337
+ agents: int,
1338
+ extended: bool,
1339
+ ability: tuple[Any, Any],
1340
+ device: str,
1341
+ runner: Any,
1342
+ factory: Callable[[Any], Any],
1343
+ keep: bool = True,
1344
+ fail_attempts: bool = False,
1345
+ ) -> tuple[str | None, dict[str, Any], list[bytes], BaseException | None]:
1346
+ """Plays one attempt and returns its recording's identifier, header, frames and failure.
1347
+
1348
+ The recording goes to the library when `keep` is true, and its identifier is `None` otherwise.
1349
+ With `fail_attempts`, a policy that fails ends the attempt where it failed, which keeps the
1350
+ frames played so far and returns the failure, and an attempt that never stepped keeps no
1351
+ recording. The failure is `None` otherwise.
1352
+ """
1353
+ from .play import PolicyError, PolicyMessageError
1354
+
1355
+ scenario, seed = spec['scenario'], spec['seeds'][index]
1356
+ # A test may put a batch of its own in the module's place of `BatchEnvironment`.
1357
+ make = globals().get('BatchEnvironment') or __getattr__('BatchEnvironment')
1358
+ batch = make(
1359
+ f'{scenario}_l{agents}_r0',
1360
+ agent_counts=[(agents, 0)],
1361
+ environment_count=1,
1362
+ representation='raw',
1363
+ seed=seed,
1364
+ auto_reset=False,
1365
+ device=device,
1366
+ difficulty=None if ability == (None, None) else [ability],
1367
+ action_set='v2' if extended else 'default',
1368
+ )
1369
+ failure = None
1370
+ try:
1371
+ reader = _recording.StateReader(int(batch.left_players[0] + batch.right_players[0]))
1372
+ batch.reset()
1373
+ state = batch.get_state(0)
1374
+ lineup = reader.lineup(state)
1375
+ timing = _recording.scenario_timing(state)
1376
+ frames = [reader.frame(state)]
1377
+ try:
1378
+ first_call = runner is not None and runner.policies is None
1379
+ if runner is not None:
1380
+ if runner.policies is None:
1381
+ runner.load(lambda: factory(batch), LOAD_SECONDS)
1382
+ else:
1383
+ # The same policies play every attempt, and each sees its episode from step 0.
1384
+ runner.policies.batch = batch
1385
+ for player in runner.policies.players:
1386
+ player.update(calls=0, held={})
1387
+ done = False
1388
+ while not done:
1389
+ if runner is not None:
1390
+ runner.request()
1391
+ limit = FIRST_CALL_SECONDS if first_call else HANG_SECONDS
1392
+ first_call = False
1393
+ if not runner.wait(limit):
1394
+ raise PolicyMessageError('server.policy.silent', count=f'{limit:g}')
1395
+ actions = runner.actions(None)
1396
+ else:
1397
+ import torch
1398
+
1399
+ actions = torch.zeros((1, 0), dtype=torch.int32)
1400
+ _, _, finished, _ = batch.step(actions)
1401
+ done = bool(finished[0])
1402
+ frames.append(reader.frame(batch.get_state(0), 1 if done else 0))
1403
+ except PolicyError as error:
1404
+ if not fail_attempts:
1405
+ raise
1406
+ # The attempt ends here, so its last frame lacks the end of the episode.
1407
+ failure = error
1408
+ played = [round(float(value), 4) for value in batch.difficulty[0].tolist()]
1409
+ finally:
1410
+ batch.close()
1411
+ shown = competitor_name({'kind': 'agent', 'name': competitor, 'team': side['id']})
1412
+ if side['kind'] == 'policy':
1413
+ teams = [
1414
+ {'id': None, 'name': shown, 'ability': played[0]},
1415
+ {**_teams.header_team(_teams.BUILTIN), 'ability': played[1]},
1416
+ ]
1417
+ else:
1418
+ teams = [
1419
+ {**team, 'ability': played[number]}
1420
+ for number, team in enumerate(_teams.match_teams(side['id'], _teams.BUILTIN))
1421
+ ]
1422
+ provenance = {
1423
+ 'source': 'challenge',
1424
+ 'challenge': spec['id'],
1425
+ 'attempt': index,
1426
+ 'seed': seed,
1427
+ 'scenario': scenario,
1428
+ 'teams': [side['id'], _teams.BUILTIN],
1429
+ 'competitor': competitor,
1430
+ 'agent_counts': [agents, 0],
1431
+ 'action_set': 'v2' if extended else 'default',
1432
+ 'ability': played,
1433
+ }
1434
+ if side['kind'] == 'policy':
1435
+ provenance['policy'] = competitor
1436
+ title = _messages.label(
1437
+ 'server.names.challenge_attempt',
1438
+ challenge=challenge_value(spec),
1439
+ attempt=index + 1,
1440
+ competitor=shown,
1441
+ )
1442
+ if failure is not None:
1443
+ provenance['failed'] = True
1444
+ head = _recording.header(title, scenario, lineup, teams, len(frames), timing, provenance)
1445
+ stepped = failure is None or len(frames) > 1
1446
+ match = _library.save(head, frames).stem if keep and stepped else None
1447
+ return match, json.loads(head[len(_recording.MAGIC) + 4 :]), frames, failure
1448
+
1449
+
1450
+ # The runs that the page asks for, played one at a time in child processes.
1451
+
1452
+
1453
+ class _Jobs:
1454
+ """The headless runs of the page, queued and played one at a time, each in a child process.
1455
+
1456
+ The list lives in memory, the newest 20 ended runs with every waiting or playing one, and a
1457
+ run that the page cancels ends its process, so nothing of it reaches the leaderboard.
1458
+ """
1459
+
1460
+ def __init__(self) -> None:
1461
+ self._lock = threading.Lock()
1462
+ self._entries: list[dict[str, Any]] = []
1463
+ self._processes: dict[str, subprocess.Popen] = {}
1464
+ self._wake = threading.Event()
1465
+ threading.Thread(target=self._run, daemon=True, name='cuju-challenges').start()
1466
+
1467
+ def add(
1468
+ self, identifier: str, team: Any, attempts: Any, keep_recordings: Any = True
1469
+ ) -> dict[str, Any]:
1470
+ """Queues a run of a team and returns its entry.
1471
+
1472
+ Raises:
1473
+ ArgumentError: If the challenge, the team, the attempts or the keeping do not fit.
1474
+ """
1475
+ challenge = find(identifier)
1476
+ keep = _keep_flag(keep_recordings)
1477
+ if not isinstance(team, str):
1478
+ raise MessageError('server.challenges.team', team=repr(team))
1479
+ entry = _teams.entry(team)
1480
+ _teams.challenge_fit(entry, challenge['scenario'])
1481
+ if attempts is not None and (
1482
+ not _whole(attempts) or not 1 <= attempts <= challenge['attempts']
1483
+ ):
1484
+ raise MessageError(
1485
+ 'server.challenges.attempts', most=challenge['attempts'], attempts=repr(attempts)
1486
+ )
1487
+ job = {
1488
+ 'id': _home.new_identifier('j'),
1489
+ 'challenge': identifier,
1490
+ 'team': team,
1491
+ **_messages.named('name', _teams.shown_name(entry)),
1492
+ 'attempts': challenge['attempts'] if attempts is None else attempts,
1493
+ 'keep_recordings': keep,
1494
+ 'state': 'queued',
1495
+ 'progress': 0.0,
1496
+ 'results': [],
1497
+ 'result': None,
1498
+ 'error': None,
1499
+ 'error_id': None,
1500
+ 'error_values': None,
1501
+ 'created': time.time(),
1502
+ }
1503
+ with self._lock:
1504
+ self._entries.append(job)
1505
+ ended = [item for item in self._entries if item['state'] in ('done', 'failed')]
1506
+ for item in ended[: max(0, len(ended) - _KEPT_JOBS)]:
1507
+ self._entries.remove(item)
1508
+ self._wake.set()
1509
+ return dict(job)
1510
+
1511
+ def entries(self) -> list[dict[str, Any]]:
1512
+ """Returns a copy of every run, newest first."""
1513
+ with self._lock:
1514
+ return [dict(job) for job in reversed(self._entries)]
1515
+
1516
+ def cancel(self, identifier: str) -> bool:
1517
+ """Cancels a waiting run or ends a playing one, and returns whether there was one."""
1518
+ with self._lock:
1519
+ job = next((item for item in self._entries if item['id'] == identifier), None)
1520
+ if job is None or job['state'] not in ('queued', 'running'):
1521
+ return False
1522
+ job.update(state='failed', **_messages.fields('server.requests.cancelled'))
1523
+ process = self._processes.get(identifier)
1524
+ if process is not None:
1525
+ process.kill()
1526
+ return True
1527
+
1528
+ def _run(self) -> None:
1529
+ while True:
1530
+ self._wake.wait()
1531
+ self._wake.clear()
1532
+ while True:
1533
+ with self._lock:
1534
+ job = next((item for item in self._entries if item['state'] == 'queued'), None)
1535
+ if job is None:
1536
+ break
1537
+ try:
1538
+ self._play(job)
1539
+ except Exception as error:
1540
+ with self._lock:
1541
+ job['state'] = 'failed'
1542
+ job.update(_messages.entry_fields(error, _messages.described(error)))
1543
+
1544
+ def _play(self, job: dict[str, Any]) -> None:
1545
+ command = child_command(
1546
+ job['challenge'], job['team'], job['attempts'], job['keep_recordings']
1547
+ )
1548
+ with self._lock:
1549
+ if job['state'] != 'queued':
1550
+ return
1551
+ job['state'] = 'running'
1552
+
1553
+ def started(process: subprocess.Popen) -> None:
1554
+ with self._lock:
1555
+ self._processes[job['id']] = process
1556
+ cancelled = job['state'] != 'running'
1557
+ if cancelled:
1558
+ process.kill()
1559
+
1560
+ def report(line: dict[str, Any]) -> None:
1561
+ with self._lock:
1562
+ if isinstance(line.get('attempt'), dict):
1563
+ job['results'] = [*job['results'], line['attempt']]
1564
+ job['progress'] = len(job['results']) / job['attempts']
1565
+ if isinstance(line.get('result'), dict):
1566
+ job['result'] = line['result']
1567
+ if isinstance(line.get('error'), str):
1568
+ job.update(_messages.report_fields(line))
1569
+
1570
+ try:
1571
+ code, tail = play_child(command, report, started)
1572
+ finally:
1573
+ with self._lock:
1574
+ self._processes.pop(job['id'], None)
1575
+ with self._lock:
1576
+ if job['state'] != 'running':
1577
+ return
1578
+ if code == 0 and job['result'] is not None:
1579
+ job.update(state='done', progress=1.0)
1580
+ else:
1581
+ job['state'] = 'failed'
1582
+ if not job['error']:
1583
+ job.update(_messages.exit_fields('server.challenges.exit', code, tail))
1584
+
1585
+ def get(self, identifier: str) -> dict[str, Any] | None:
1586
+ """Returns a copy of one run, or `None` for a run that the list no longer holds."""
1587
+ with self._lock:
1588
+ job = next((item for item in self._entries if item['id'] == identifier), None)
1589
+ return None if job is None else dict(job)
1590
+
1591
+
1592
+ def child_command(
1593
+ identifier: str,
1594
+ team: str,
1595
+ attempts: int | None = None,
1596
+ keep: bool = True,
1597
+ device: str = 'cpu',
1598
+ ) -> list[str]:
1599
+ """Returns the command of the child process that plays one headless run of a team."""
1600
+ command = [
1601
+ *_home.python_command(),
1602
+ '-m',
1603
+ 'cuju.render._playing.challenges',
1604
+ identifier,
1605
+ '--team',
1606
+ team,
1607
+ ]
1608
+ if attempts is not None:
1609
+ command += ['--attempts', str(attempts)]
1610
+ if device != 'cpu':
1611
+ command += ['--device', device]
1612
+ if not keep:
1613
+ command.append('--no-recordings')
1614
+ return command
1615
+
1616
+
1617
+ def play_child(
1618
+ command: list[str],
1619
+ report: Callable[[dict[str, Any]], None],
1620
+ started: Callable[[subprocess.Popen], None] | None = None,
1621
+ seconds: float = _JOB_SECONDS,
1622
+ ) -> tuple[int, str]:
1623
+ """Plays a headless run in a child process and hands each of its reports to `report`.
1624
+
1625
+ The child runs in the renderer's folder, as a requested match does, and ends after `seconds`.
1626
+ Every run of the page, of a benchmark and of a benchmark on the command line plays this way,
1627
+ so a submission runs in a process of its own and no run sees another.
1628
+
1629
+ Args:
1630
+ command: The command, as `child_command` returns it.
1631
+ report: A function called with each report of the child, a JSON object.
1632
+ started: A function called with the process once it has started, which may end it.
1633
+ seconds: The seconds after which the run ends.
1634
+
1635
+ Returns:
1636
+ The exit code of the child and the last lines it wrote to standard error.
1637
+ """
1638
+ process = subprocess.Popen(
1639
+ command,
1640
+ stdin=subprocess.DEVNULL,
1641
+ stdout=subprocess.PIPE,
1642
+ stderr=subprocess.PIPE,
1643
+ text=True,
1644
+ encoding='utf-8',
1645
+ errors='replace',
1646
+ cwd=_home.process_folder(),
1647
+ env=_home.process_environment(),
1648
+ )
1649
+ if started is not None:
1650
+ started(process)
1651
+ stderr: collections.deque[str] = collections.deque(maxlen=20)
1652
+ reader = threading.Thread(target=lambda: stderr.extend(process.stderr), daemon=True)
1653
+ reader.start()
1654
+ timer = threading.Timer(seconds, process.kill)
1655
+ timer.daemon = True
1656
+ timer.start()
1657
+ try:
1658
+ for line in process.stdout:
1659
+ try:
1660
+ parsed = json.loads(line)
1661
+ except ValueError:
1662
+ continue
1663
+ if isinstance(parsed, dict):
1664
+ report(parsed)
1665
+ code = process.wait()
1666
+ finally:
1667
+ timer.cancel()
1668
+ reader.join(1.0)
1669
+ return code, ''.join(stderr).strip()
1670
+
1671
+
1672
+ _JOBS: _Jobs | None = None
1673
+ _JOBS_LOCK = threading.Lock()
1674
+
1675
+
1676
+ def jobs() -> _Jobs:
1677
+ """Returns the server's queue of headless runs, started on first use."""
1678
+ global _JOBS
1679
+ with _JOBS_LOCK:
1680
+ if _JOBS is None:
1681
+ _JOBS = _Jobs()
1682
+ return _JOBS
1683
+
1684
+
1685
+ # The routes of the server under /api/challenges, each returning a status and a JSON value.
1686
+
1687
+
1688
+ def get(parts: list[str], query: dict[str, str]) -> tuple[Any, int]:
1689
+ """Answers `GET /api/challenges/...`, the catalog, a board, a score, the runs or benchmarks."""
1690
+ if not parts:
1691
+ person = query.get('person') or None
1692
+ return [{**entry, 'board': _summary(entry['id'], person)} for entry in challenges()], 200
1693
+ if parts == ['jobs']:
1694
+ return jobs().entries(), 200
1695
+ if parts == ['names']:
1696
+ return {'default_names': sorted(_languages.default_names())}, 200
1697
+ if len(parts) == 2 and parts[0] == 'jobs':
1698
+ job = jobs().get(parts[1])
1699
+ if job is None:
1700
+ return _messages.fields('server.challenges.no_run'), 404
1701
+ return job, 200
1702
+ if parts[0] == 'benches':
1703
+ from . import bench as _bench
1704
+
1705
+ return _bench.get(parts[1:])
1706
+ identifier = parts[0]
1707
+ if identifier not in {entry['id'] for entry in CATALOG}:
1708
+ return _messages.fields('server.challenges.no_challenge', identifier=repr(identifier)), 404
1709
+ if len(parts) == 1:
1710
+ return {'challenge': find(identifier), 'entries': board(identifier)}, 200
1711
+ if parts[1:] == ['score']:
1712
+ try:
1713
+ return score_recording(identifier, query.get('match', ''), query.get('attempt')), 200
1714
+ except UnknownRecordingError as error:
1715
+ return _messages.error_fields(error), 404
1716
+ return _messages.fields('server.no_resource'), 404
1717
+
1718
+
1719
+ def post(parts: list[str], document: dict[str, Any]) -> tuple[Any, int]:
1720
+ """Answers `POST /api/challenges/<id>/runs`, `POST /api/challenges/<id>/leave` and
1721
+ `POST /api/challenges/benches`.
1722
+
1723
+ A run is a person's run to record or a team's to play, leave takes the attempts of a run that
1724
+ a person left, to keep as a group or to remove, and benches starts a benchmark of models.
1725
+ """
1726
+ if parts == ['benches']:
1727
+ from . import bench as _bench
1728
+
1729
+ return _bench.post(document)
1730
+ if len(parts) != 2 or parts[1] not in ('runs', 'leave'):
1731
+ return _messages.fields('server.no_resource'), 404
1732
+ if parts[0] not in {entry['id'] for entry in CATALOG}:
1733
+ return _messages.fields('server.challenges.no_challenge', identifier=repr(parts[0])), 404
1734
+ keep = document.get('keep_recordings', True)
1735
+ if 'person' in document or parts[1] == 'leave':
1736
+ unknown = set(document) - {'person', 'matches', 'keep_recordings', 'default_name'}
1737
+ if unknown:
1738
+ raise MessageError('server.challenges.person_fields', unknown=repr(sorted(unknown)))
1739
+ default = document.get('default_name', False)
1740
+ person, matches = document.get('person'), document.get('matches')
1741
+ if parts[1] == 'leave':
1742
+ return leave_run(parts[0], person, matches, keep, default), 200
1743
+ return person_run(parts[0], person, matches, keep, default), 201
1744
+ unknown = set(document) - {'team', 'attempts', 'keep_recordings'}
1745
+ if unknown or 'team' not in document:
1746
+ raise MessageError('server.challenges.run_fields')
1747
+ return jobs().add(parts[0], document['team'], document.get('attempts'), keep), 201
1748
+
1749
+
1750
+ def delete(parts: list[str]) -> tuple[Any, int]:
1751
+ """Answers `DELETE /api/challenges/jobs/<job>` and `DELETE /api/challenges/benches/<bench>`,
1752
+ which cancel a run or a benchmark of the page."""
1753
+ if len(parts) == 2 and parts[0] == 'benches':
1754
+ from . import bench as _bench
1755
+
1756
+ return _bench.delete(parts[1])
1757
+ if len(parts) == 2 and parts[0] == 'jobs' and jobs().cancel(parts[1]):
1758
+ return {'cancelled': parts[1]}, 200
1759
+ return _messages.fields('server.challenges.no_run'), 404
1760
+
1761
+
1762
+ # The child process of a headless run, and the command line.
1763
+
1764
+ # The channel of the reports, standard output until `main` moves everything else away.
1765
+ _reports: TextIO = sys.stdout
1766
+
1767
+
1768
+ def _report(**entries: Any) -> None:
1769
+ _reports.write(json.dumps(entries) + '\n')
1770
+ _reports.flush()
1771
+
1772
+
1773
+ def main(arguments: list[str] | None = None) -> None:
1774
+ """Plays the run that the command line names and reports each attempt and the result.
1775
+
1776
+ A policy that fails ends its attempt alone, which counts as failed, so the run always ends
1777
+ with a result unless its challenge, team or attempts do not fit.
1778
+ """
1779
+ global _reports
1780
+ from .play import PolicyError
1781
+
1782
+ parser = argparse.ArgumentParser(prog='python -m cuju.render._playing.challenges')
1783
+ parser.add_argument('challenge')
1784
+ parser.add_argument('--team', required=True)
1785
+ parser.add_argument('--attempts', type=int, default=None)
1786
+ parser.add_argument('--device', default='cpu')
1787
+ parser.add_argument('--no-recordings', action='store_true')
1788
+ options = parser.parse_args(arguments)
1789
+ sys.stdout.flush()
1790
+ _reports = os.fdopen(os.dup(1), 'w', encoding='utf-8')
1791
+ os.dup2(2, 1)
1792
+ sys.stdout = sys.stderr
1793
+ try:
1794
+ outcome = run(
1795
+ options.challenge,
1796
+ team=options.team,
1797
+ attempts=options.attempts,
1798
+ device=options.device,
1799
+ progress=lambda result: _report(attempt=result),
1800
+ keep_recordings=not options.no_recordings,
1801
+ fail_attempts=True,
1802
+ )
1803
+ except PolicyError as error:
1804
+ _report(error=str(error), **_messages.id_fields(error))
1805
+ sys.exit(1)
1806
+ except Exception as error:
1807
+ _report(error=_messages.described(error), **_messages.id_fields(error))
1808
+ sys.exit(1)
1809
+ _report(result=outcome)
1810
+
1811
+
1812
+ if __name__ == '__main__':
1813
+ main()