Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generates cards stats after payment step #170

Merged
merged 6 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions kaori/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker

from kaori.plugins.kkreds import KKredsTransaction
from kaori.plugins.users import User
from .adapters.slack import SlackMessage, SlackAdapter
from .skills import DB
Expand Down Expand Up @@ -89,6 +90,7 @@ def fake_user(test_db: DB) -> User:
@pytest.fixture()
def make_fake_user(test_db: DB) -> callable:
def make_user():
session: Session
with test_db.session_scope() as session:
name = str(uuid4())

Expand All @@ -98,12 +100,25 @@ def make_user():

session.add(u)
session.commit()
session.refresh(u)
session.expunge(u)

return u

return make_user


@pytest.fixture()
def grant_kkreds(test_db: DB) -> callable:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be useful to testing payment later

def fn(user: User, kkreds: int):
session: Session
with test_db.session_scope() as session:
session.add(KKredsTransaction(to_user=user, amount=kkreds))
session.commit()

return fn


_project_root = Path(__file__).parent.parent


Expand Down
16 changes: 10 additions & 6 deletions kaori/plugins/gacha/commands/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from ..engine.core import RarityName, NatureName
from ..models.Card import InvalidCardName, Card
from ..models.Image import Image
from ..tui import render_card, instructions_blocks, query_rarity_blocks, query_nature_blocks, create_are_you_sure_blocks
from ..tui import render_card, instructions_blocks, query_rarity_blocks, query_nature_blocks, \
create_are_you_sure_blocks, create_confirmation_blocks
from ..utils import tmp_prefix


Expand Down Expand Up @@ -185,6 +186,7 @@ def initialize_card(message: SlackMessage, user: User) -> Card:
# TODO: Once the functionality is totally built out we can break this into multiple functions etc.
# BODY: Oh god break this into some functions please, this should be a dispatcher or something.
# This function is now officially out of control
# TODO: AGAIN, this is wayyyy out of control. It's got good test coverage but FFS
def next_card_creation_step(card: Card,
session: Session,
user_input: str) -> Tuple[Card, List[Union[str, dict]]]:
Expand Down Expand Up @@ -245,14 +247,14 @@ def next_card_creation_step(card: Card,
if rarity:
card.set_rarity(rarity)
replies.append(':+1:')
cursor = 'do_stats_roll'
cursor = 'query_confirm_price'
else:
replies.append('Need to specify a rarity')
elif cursor == 'set_confirm_price':
# todo: make utility for capturing english affirmatives
if re.search(r'yes+|yep+|ye+|yeah+', user_input, re.IGNORECASE):
card.published = True
cursor = 'done'
replies.append(':+1:')
cursor = 'do_stats_roll'
# todo: make utility for capturing english negatives
elif re.search(r'no+|nope+|', user_input, re.IGNORECASE):
replies.append("Okay. If you change your mind:\n"
Expand All @@ -263,7 +265,8 @@ def next_card_creation_step(card: Card,

if cursor == 'do_stats_roll':
card.roll_stats()
cursor = 'query_confirm_price'
card.published = True
cursor = 'done'

if cursor.startswith('query_'):
if cursor == 'query_name':
Expand All @@ -287,7 +290,8 @@ def next_card_creation_step(card: Card,
cursor = 'set_confirm_price'

if cursor == 'done':
replies.append('*Congrats!* Your card is created. Enjoy.')
are_you_sure_blocks = create_confirmation_blocks(card)
replies.append({'blocks': are_you_sure_blocks})

card.creation_cursor = cursor
return card, replies
Expand Down
19 changes: 7 additions & 12 deletions kaori/plugins/gacha/commands/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from time import time
from unittest.mock import Mock, MagicMock

from kaori.plugins.gacha.engine import RarityName
from slackclient import SlackClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.orm import sessionmaker

import kaori.plugins.gacha as gacha_plugin
from kaori import test_config
from kaori.adapters.slack import SlackAdapter
from kaori.plugins.gacha.engine import RarityName
from kaori.plugins.gacha.models.Card import Card
from kaori.plugins.users import User
from kaori.skills import DB, LocalFileUploader
Expand All @@ -33,7 +33,7 @@ def test_rarity_extract():
assert user_extract_rarity("I'm thinking a S-tier card") is RarityName.S


def test_card_creation_state_happy():
def test_card_creation_state_happy(make_fake_user, grant_kkreds):
config = test_config
db_engine = create_engine(config.DATABASE_URL)
make_session = sessionmaker(bind=db_engine, autoflush=False)
Expand Down Expand Up @@ -63,16 +63,11 @@ def test_card_creation_state_happy():
gacha_plugin,
}

slack_id = token_hex(5)
u: User = make_fake_user()
slack_id = u.slack_id
user_id = u.id

session: Session
with db.session_scope() as session:
u = User(name='Ridwan',
slack_id=slack_id,
api_key=token_hex(5))
session.add(u)
session.commit()
user_id = u.id
# grant_kkreds(u, 1e10)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I make S-tier cards this test will fail until this is uncommented


def handle(msg):
k.handle('slack', msg)
Expand Down
14 changes: 13 additions & 1 deletion kaori/plugins/gacha/tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ def battle_blocks(attacker: Card, defender: Card, battle_url: str):

def create_are_you_sure_blocks(card):
return [
*render_card(card)['blocks'],
{
"type": "section",
"text": {
Expand All @@ -311,6 +310,19 @@ def create_are_you_sure_blocks(card):
]


def create_confirmation_blocks(card):
return [
*render_card(card)['blocks'],
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Congrats!* Your card was created. Enjoy."
},
},
]


def card_stats_blocks(card_total: int):
return [
{
Expand Down