Skip to content

Commit

Permalink
✨ add trade accepted message
Browse files Browse the repository at this point in the history
  • Loading branch information
YousefEZ committed Sep 14, 2024
1 parent 1ad1617 commit 3e121b8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
9 changes: 6 additions & 3 deletions host/nation/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TradeAcceptResponses(IntEnum):
SUCCESS = auto()
TOO_MANY_ACTIVE_AGREEMENTS = auto()
TRADE_PARTNER_FULL = auto()
NOT_FOUND = auto()


class TradeSentResponses(IntEnum):
Expand Down Expand Up @@ -247,9 +248,11 @@ def _accept(self, trade_request: TradeRequest) -> None:
self._session.commit()
trade_request.invalidate()

def accept(self, trade_request: TradeRequest) -> TradeAcceptResponses:
if self._identifier != trade_request.recipient:
raise TradeError("not a recipient of this request")
def accept(self, sponsor: base_types.UserId) -> TradeAcceptResponses:
requests = list(filter(lambda x: x.sponsor == sponsor, self.requests))
if not requests:
return TradeAcceptResponses.NOT_FOUND
trade_request = requests[0]

Check warning on line 255 in host/nation/trade.py

View check run for this annotation

Codecov / codecov/patch

host/nation/trade.py#L252-L255

Added lines #L252 - L255 were not covered by tests

if len(self.active_agreements) >= GameplaySettings.trade.maximum_active_agreements:
return TradeAcceptResponses.TOO_MANY_ACTIVE_AGREEMENTS

Check warning on line 258 in host/nation/trade.py

View check run for this annotation

Codecov / codecov/patch

host/nation/trade.py#L257-L258

Added lines #L257 - L258 were not covered by tests
Expand Down
24 changes: 23 additions & 1 deletion templates/trade.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,29 @@
<view></view>
</message>
<message key="trade_accepted">

<embed>
<title>:currency_exchange: Trade Accepted</title>
<colour>teal</colour>
<description>You {{recipient.metadata.emoji}}**{{recipient.metadata.nation_name}}** accepted the trade from {{sponsor.metadata.emoji}}**{{sponsor.metadata.nation_name}}**.</description>
<fields>
<field>
<name>:outbox_tray: Your offering</name>
<value>
{% for resource in sponsor.trade.resources -%}
- {{ Resources[resource].emoji }} {{resource}}
{% endfor -%}
</value>
</field>
<field>
<name>:inbox_tray: Receiving</name>
<value>
{%- for resource in recipient.trade.resources -%}
- {{ Resources[resource].emoji }} {{resource}}
{% endfor -%}
</value>
</field>
</fields>
</embed>
</message>
<message key="trade_declined">
</message>
Expand Down
26 changes: 21 additions & 5 deletions view/cogs/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from qalib.template_engines.jinja2 import Jinja2

Check warning on line 8 in view/cogs/trade.py

View check run for this annotation

Codecov / codecov/patch

view/cogs/trade.py#L1-L8

Added lines #L1 - L8 were not covered by tests

from host.nation import Nation
from host.nation.trade import TradeSelectResponses, TradeSentResponses
from host.nation.trade import TradeAcceptResponses, TradeSelectResponses, TradeSentResponses
from lon import LeagueOfNations, interaction_morph
from view.cogs.custom_jinja2 import ENVIRONMENT

Check warning on line 13 in view/cogs/trade.py

View check run for this annotation

Codecov / codecov/patch

view/cogs/trade.py#L10-L13

Added lines #L10 - L13 were not covered by tests

Expand All @@ -31,6 +31,11 @@
TradeSelectResponses.ACTIVE_AGREEMENT: "select_trade_active_agreement",
}

TradeAcceptMapping: Dict[TradeAcceptResponses, str] = {

Check warning on line 34 in view/cogs/trade.py

View check run for this annotation

Codecov / codecov/patch

view/cogs/trade.py#L34

Added line #L34 was not covered by tests
TradeAcceptResponses.SUCCESS: "trade_accept",
TradeAcceptResponses.NOT_FOUND: "trade_not_found",
}


class Trade(commands.Cog):
def __init__(self, bot: LeagueOfNations):
Expand Down Expand Up @@ -114,14 +119,25 @@ async def decline(i: discord.Interaction):
async def requests(self, ctx: qalib.interaction.QalibInteraction) -> None:
nation = self.bot.get_nation(ctx.user.id)

Check warning on line 120 in view/cogs/trade.py

View check run for this annotation

Codecov / codecov/patch

view/cogs/trade.py#L117-L120

Added lines #L117 - L120 were not covered by tests

def on_select_request(request: Nation):
async def callback(select: discord.ui.Select, interaction): ...

return callback
async def on_trade_request_select(

Check warning on line 122 in view/cogs/trade.py

View check run for this annotation

Codecov / codecov/patch

view/cogs/trade.py#L122

Added line #L122 was not covered by tests
select: discord.ui.Select, interaction: discord.Interaction
):
await interaction.response.defer()
target = self.bot.get_nation(int(select.values[0]))
response = nation.trade.accept(target.identifier)
if response != TradeAcceptResponses.SUCCESS:
await ctx.display(TradeAcceptMapping[response])
return

Check warning on line 130 in view/cogs/trade.py

View check run for this annotation

Codecov / codecov/patch

view/cogs/trade.py#L125-L130

Added lines #L125 - L130 were not covered by tests

await ctx.display(

Check warning on line 132 in view/cogs/trade.py

View check run for this annotation

Codecov / codecov/patch

view/cogs/trade.py#L132

Added line #L132 was not covered by tests
TradeAcceptMapping[response], keywords={"recipient": nation, "sponsor": target}
)
# TODO: send notification to the sponsor of the agreement that it has been accepted

await ctx.display(

Check warning on line 137 in view/cogs/trade.py

View check run for this annotation

Codecov / codecov/patch

view/cogs/trade.py#L137

Added line #L137 was not covered by tests
"trade_requests",
keywords={"nation": nation, "Resources": Resources},
callables={"trade_identifier": on_trade_request_select},
)


Expand Down

0 comments on commit 3e121b8

Please sign in to comment.