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

「つづきから」で前回の続きからプレイできるようにする #18

Merged
merged 4 commits into from Sep 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 34 additions & 8 deletions domain.py
Expand Up @@ -29,19 +29,45 @@

DATASTORE_DEFAULT_LAST_WORD = u'シリトリ'

PARAM_START_MODE_NEW_GAME = u'NEW_GAME'
PARAM_START_MODE_CONTINUE = u'CONTINUE'


def ask_continue(obj):
originalDetectIntentRequest = obj['originalDetectIntentRequest']
userId = originalDetectIntentRequest[u'payload'][u'user'][u'userId']
hayatedayon marked this conversation as resolved.
Show resolved Hide resolved
queryResult = obj[u'queryResult']
return {
u'followupEventInput': {
u'name': ASK_CONTINUE_EVENT,
u'languageCode': queryResult[u'languageCode'],

if infra.contains_user(userId):
return {
u'followupEventInput': {
u'name': ASK_CONTINUE_EVENT,
u'languageCode': queryResult[u'languageCode'],
}
}
else:
return {
u'followupEventInput': {
u'name': ASK_WORD_EVENT,
u'languageCode': queryResult[u'languageCode'],
}
}
}


def set_continue(obj):
queryResult = obj[u'queryResult']
startMode = queryResult[u'parameters'][u'startMode']
if startMode == PARAM_START_MODE_NEW_GAME:
logging.info(PARAM_START_MODE_NEW_GAME)
originalDetectIntentRequest = obj['originalDetectIntentRequest']
userId = originalDetectIntentRequest[u'payload'][u'user'][u'userId']
user = infra.load_user(userId, DATASTORE_DEFAULT_LAST_WORD)
infra.reset_datastore(user)
elif startMode == PARAM_START_MODE_CONTINUE:
logging.info(PARAM_START_MODE_CONTINUE)
else:
Exception(u"Unknown startMode: " + startMode)
Copy link
Collaborator

Choose a reason for hiding this comment

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

raiseしていないのはなぜ?


return {
u'followupEventInput': {
u'name': ASK_WORD_EVENT,
Expand All @@ -59,10 +85,9 @@ def response_word(obj):
user = infra.load_user(userId, DATASTORE_DEFAULT_LAST_WORD)

if queryText == ASK_WORD_EVENT:
infra.reset_datastore(user)
user = infra.load_user(userId, DATASTORE_DEFAULT_LAST_WORD)
return {
u'fulfillmentText': u'しりとり、の、リ',
u'fulfillmentText': user.last_word + u'、の、' + user.last_word[-1],
# FIXME: #414, #427 ^^^^^^^^^^^^^^^^^^
}
else:
logging.info(queryText)
Expand Down Expand Up @@ -119,6 +144,7 @@ def response_lose_word(obj):

user = infra.load_user(userId, DATASTORE_DEFAULT_LAST_WORD)
reading_end = user.last_word[-1]
# FIXME: #414, #427 ^^^^^^^^^^^^

word_record = infra.search_lose_word_record_from_dic(
user, reading_end)
Expand Down
12 changes: 12 additions & 0 deletions infra.py
Expand Up @@ -38,6 +38,17 @@ class User(ndb.Model):
date = ndb.DateTimeProperty(auto_now_add=True)


def contains_user(user_id):
try:
user = User.get_by_id(user_id)
if user:
hayatedayon marked this conversation as resolved.
Show resolved Hide resolved
return True
except Exception:
pass

return False


def load_user(user_id, default_last_word):
try:
user = User.get_by_id(user_id)
Expand Down Expand Up @@ -69,6 +80,7 @@ def reset_datastore(user):
def get_last_word_datastore(user):
try:
return user.last_word[-1]
# FIXME: #414, #427 ^^^^^
except Exception:
pass

Expand Down
7 changes: 5 additions & 2 deletions tests/test_main.py
Expand Up @@ -74,7 +74,7 @@ def test_post_google_assistant_welcome_intent(self):
obj = json.loads(response.body)

assert response.status_int == 200
assert obj[u'followupEventInput'][u'name'] == u'ASK_CONTINUE_EVENT'
assert obj[u'followupEventInput'][u'name'] == u'ASK_WORD_EVENT'
assert obj[u'followupEventInput'][u'languageCode'] == u'ja'

def test_post_ask_continue_intent(self):
Expand All @@ -89,6 +89,9 @@ def test_post_ask_continue_intent(self):
u'displayName': u'Ask Continue Intent',
},
u'queryText': u'test code',
u'parameters': {
u'startMode': u'NEW_GAME'
},
u'languageCode': u'ja',
},
u'originalDetectIntentRequest':
Expand Down Expand Up @@ -138,7 +141,7 @@ def test_post_ask_word_intent_1(self):
obj = json.loads(response.body)

assert response.status_int == 200
assert obj[u'fulfillmentText'] == u'しりとり、の、リ'
assert obj[u'fulfillmentText'] == u'シリトリ、の、リ'

def test_post_ask_word_intent_2(self):
app = webtest.TestApp(main.app)
Expand Down