|
| 1 | +from asyncio import gather |
| 2 | +from dataclasses import dataclass |
| 3 | + |
| 4 | +from ttt.application.common.ports.clock import Clock |
| 5 | +from ttt.application.common.ports.transaction import Transaction |
| 6 | +from ttt.application.common.ports.uuids import UUIDs |
| 7 | +from ttt.application.player.ports.player_fsm import PlayerFsm |
| 8 | +from ttt.application.player.ports.player_views import PlayerViews |
| 9 | +from ttt.application.player.ports.players import Players |
| 10 | +from ttt.application.player.ports.stars_purchase_payment_gateway import ( |
| 11 | + StarsPurchasePaymentGateway, |
| 12 | +) |
| 13 | +from ttt.entities.core.player.location import PlayerLocation |
| 14 | +from ttt.entities.core.player.stars_purchase import StarsPurchase |
| 15 | +from ttt.entities.core.stars import NonExchangeableRublesForStarsError |
| 16 | +from ttt.entities.finance.rubles import Rubles |
| 17 | +from ttt.entities.tools.tracking import Tracking |
| 18 | + |
| 19 | + |
| 20 | +@dataclass(frozen=True, unsafe_hash=False) |
| 21 | +class InitiateStarsPurchasePayment: |
| 22 | + fsm: PlayerFsm |
| 23 | + transaction: Transaction |
| 24 | + players: Players |
| 25 | + uuids: UUIDs |
| 26 | + clock: Clock |
| 27 | + player_views: PlayerViews |
| 28 | + payment_gateway: StarsPurchasePaymentGateway |
| 29 | + |
| 30 | + async def __call__( |
| 31 | + self, location: PlayerLocation, rubles: Rubles, |
| 32 | + ) -> None: |
| 33 | + async with self.transaction: |
| 34 | + player, purchase_id, payment_id, current_datetime = await gather( |
| 35 | + self.players.player_with_id(location.player_id), |
| 36 | + self.uuids.random_uuid(), |
| 37 | + self.uuids.random_uuid(), |
| 38 | + self.clock.current_datetime(), |
| 39 | + ) |
| 40 | + |
| 41 | + tracking = Tracking() |
| 42 | + try: |
| 43 | + player.initiate_stars_purchase_payment( |
| 44 | + purchase_id, |
| 45 | + location.chat_id, |
| 46 | + payment_id, |
| 47 | + rubles, |
| 48 | + current_datetime, |
| 49 | + tracking, |
| 50 | + ) |
| 51 | + except NonExchangeableRublesForStarsError: |
| 52 | + await self.fsm.set(None) |
| 53 | + await ( |
| 54 | + self.player_views |
| 55 | + .render_non_exchangeable_rubles_for_stars_view(location) |
| 56 | + ) |
| 57 | + return |
| 58 | + |
| 59 | + await self.fsm.set(None) |
| 60 | + await gather(*[ |
| 61 | + self.payment_gateway.process_payment(it, location) |
| 62 | + for it in tracking.new |
| 63 | + if isinstance(it, StarsPurchase) |
| 64 | + ]) |
0 commit comments