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

おみくじライブラリを書いた #860

Merged
merged 35 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ae879e1
おみくじライブラリを書いた
fono09 Mar 5, 2022
ebc515a
継承の修正漏
fono09 Mar 5, 2022
43a1986
鳩は唐揚げ!(自動で直してあげたよ!)
github-actions[bot] Mar 5, 2022
ce07d82
テストとコメントの修正
fono09 Mar 5, 2022
7a7d4a6
typo修正
fono09 Mar 5, 2022
5aef79a
auto忘れ
fono09 Mar 5, 2022
f0bfa4a
鳩は唐揚げ!(自動で直してあげたよ!)
github-actions[bot] Mar 5, 2022
c54f7bd
メソッド実行忘れ
fono09 Mar 5, 2022
981a75f
自分で設定した型を破っていた
fono09 Mar 5, 2022
8f4b00e
テストでEnumのautoをimport漏れ
fono09 Mar 5, 2022
52f4577
typo
fono09 Mar 5, 2022
79f4d34
テスト対応
fono09 Mar 5, 2022
89a3089
謎の抽象クラスではなくジェネリクスで良い
fono09 Mar 6, 2022
45f270f
鳩は唐揚げ!(自動で直してあげたよ!)
github-actions[bot] Mar 5, 2022
59c68b7
ジェネリクス周りの修正
fono09 Mar 6, 2022
60db5a3
おみくじの繋ぎこみ
fono09 Mar 6, 2022
9f2ab9c
鳩は唐揚げ!(自動で直してあげたよ!)
github-actions[bot] Mar 6, 2022
68f2d7c
lint対応
fono09 Mar 6, 2022
2388c77
鳩は唐揚げ!(自動で直してあげたよ!)
github-actions[bot] Mar 6, 2022
dcd7d4d
名前空間の衝突に対処
fono09 Mar 6, 2022
cbd5345
assertInのtestの指定ミスに対応
fono09 Mar 6, 2022
01e8f10
凡ミス対応
fono09 Mar 6, 2022
410861e
lint対応
fono09 Mar 6, 2022
8880b83
lint/test対応
fono09 Mar 7, 2022
b8e4036
鳩は唐揚げ!(自動で直してあげたよ!)
github-actions[bot] Mar 7, 2022
e289afc
Merge pull request #872 from dev-hato/fix-format-fono/add-omikuji
fono09 Mar 7, 2022
cbe8113
シンプルにした
fono09 Mar 8, 2022
b57e9ea
Merge branch 'fono/add-omikuji' of github.com:dev-hato/hato-bot into …
fono09 Mar 8, 2022
5cf3463
鳩は唐揚げ!(自動で直してあげたよ!)
github-actions[bot] Mar 8, 2022
896bce0
Merge pull request #873 from dev-hato/fix-format-fono/add-omikuji
fono09 Mar 8, 2022
5efc4c4
lint対応
fono09 Mar 8, 2022
e38753e
脱字対応
fono09 Mar 8, 2022
aaf2067
Merge branch 'develop' into fono/add-omikuji
fono09 Mar 8, 2022
ff6a85e
lint対応
fono09 Mar 8, 2022
2e95cce
Enum&dict継承で型安全にしてみた
fono09 Mar 9, 2022
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
43 changes: 43 additions & 0 deletions library/omikuji.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# coding: utf-8

"""
おみくじを返す
"""

from typing import Tuple, TypeVar
from dataclasses import dataclass
from random import choices


@dataclass
class OmikujiResult():
"""
おみくじの引いた結果を示すデータクラス
出やすさの調整もここで行う
"""
appearance: int
message: str

def __post_init__(self):
"""
初期化後のアサーション
"""
assert self.appearance > 0
assert self.message != ''


TOmikujiEnum = TypeVar('TOmikujiEnum')
OmikujiResults = dict[TOmikujiEnum, OmikujiResult]


def draw(entries: OmikujiResults) -> Tuple[TOmikujiEnum, OmikujiResult]:
"""
おみくじを引く
"""

return choices(
population=list(entries.items()),
weights=list(
map(lambda entry: entry.appearance, entries.values())),
k=1
)[0]
1 change: 1 addition & 0 deletions plugins/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def analyze_message(message: str) -> Callable[[BaseClient], None]:
'標高': lambda m: hato.altitude((m[len('標高'):]).strip()),
'version': lambda m: hato.version,
'にゃーん': lambda m: hato.yoshiyoshi,
'おみくじ': lambda m: hato.omikuji,
}

for key, method in conditions.items():
Expand Down
65 changes: 65 additions & 0 deletions plugins/hato.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import os
import re
from enum import Enum, auto
from logging import getLogger
from tempfile import NamedTemporaryFile
from typing import List
Expand All @@ -22,6 +23,7 @@
from library.hatokaraage import hato_ha_karaage
from library.clientclass import BaseClient
from library.jma_amesh import jma_amesh
from library.omikuji import OmikujiResult, OmikujiResults, draw as omikuji_draw
logger = getLogger(__name__)


Expand Down Expand Up @@ -54,6 +56,7 @@ def help_message(client: BaseClient):
'text delete [int] ... 指定した番号[int]のパワーワードを削除する。 ',
'>< [text] ... 文字列[text]を吹き出しで表示する。',
'にゃーん ... 「よしよし」と返す。',
'おみくじ ... おみくじを引いて返す。',
'version ... バージョン情報を表示する。',
'',
'詳細はドキュメント(https://github.com/dev-hato/hato-bot/wiki)も見てくれっぽ!',
Expand Down Expand Up @@ -273,3 +276,65 @@ def yoshiyoshi(client: BaseClient):
logger.debug("%s called 'hato yoshiyoshi'", client.get_send_user())
logger.debug("%s app called 'hato yoshiyoshi'", client.get_type())
client.post('よしよし')


# 以下おみくじの設定
# Refer: dev-hato/hato-bot#876
class OmikujiEnum(Enum):
"""
おみくじの結果一覧
"""
DAI_KICHI = auto()
CHU_KICHI = auto()
SHO_KICHI = auto()
KICHI = auto()
SUE_KICHI = auto()
AGE_KICHI = auto()
KYO = auto()
DAI_KYO = auto()


omikuji_results = OmikujiResults({
OmikujiEnum.DAI_KICHI: OmikujiResult(
12,
":tada: 大吉 何でもうまくいく!!気がする!!"
),
OmikujiEnum.KICHI: OmikujiResult(
100,
":smirk: 吉 まあうまくいくかも!?"
),
OmikujiEnum.CHU_KICHI: OmikujiResult(
100,
":smile: 中吉 そこそこうまくいくかも!?"
),
OmikujiEnum.SHO_KICHI: OmikujiResult(
100,
":smily: 小吉 なんとなくうまくいくかも!?"
),
OmikujiEnum.SUE_KICHI: OmikujiResult(
37,
":expressionless: 末吉 まあ多分うまくいくかもね……!?"
),
OmikujiEnum.AGE_KICHI: OmikujiResult(
2,
":poultry_leg: 揚げ吉 鳩を揚げると良いことあるよ!!"
),
OmikujiEnum.KYO: OmikujiResult(
12,
":cry: 凶 ちょっと慎重にいったほうがいいかも……"
),
OmikujiEnum.DAI_KYO: OmikujiResult(
2,
":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう"
),
})


def omikuji(client: BaseClient):
"""
おみくじ結果を返す
"""

logger.debug("%s called 'hato yoshiyoshi'", client.get_send_user())
logger.debug("%s app called 'hato yoshiyoshi'", client.get_type())
client.post(omikuji_draw(omikuji_results)[1].message)
35 changes: 35 additions & 0 deletions tests/library/test_omikuji.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
omikujiライブラリのテスト
"""

import unittest

from enum import Enum, auto
from library.omikuji import OmikujiResult, OmikujiResults, draw


class TestOmikuji(unittest.TestCase):
"""
おみくじのテスト
"""

def test_omikuji_minimum(self):
"""
おみくじの実装が正常か
"""
class DummyOmikujiEnum(Enum):
"""
おみくじ設定(ダミー)
"""
KICHI = auto()
SUE_KICHI = auto()

dummy_omikuji_results = OmikujiResults({
DummyOmikujiEnum.KICHI: OmikujiResult(1, "吉"),
DummyOmikujiEnum.SUE_KICHI: OmikujiResult(1, "末吉"),
})

self.assertIn(
draw(dummy_omikuji_results)[0],
dummy_omikuji_results.keys()
)
20 changes: 19 additions & 1 deletion tests/plugins/test_hato.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import requests_mock

import slackbot_settings as conf
from plugins.hato import split_command, amesh, altitude, yoshiyoshi
from plugins.hato import split_command, amesh, altitude, yoshiyoshi, omikuji, omikuji_results
from tests.library.test_geo import set_mock
from tests.plugins import TestClient

Expand Down Expand Up @@ -232,5 +232,23 @@ def test_yoshiyoshi(self):
self.assertEqual(client1.get_post_message(), 'よしよし')


class TestOmikuji(unittest.TestCase):
"""
omikujiのテスト
"""

def test_omikuji(self):
"""
設定したおみくじ結果のうち1つが返ってくる
"""

client1 = TestClient()
omikuji(client1)
self.assertIn(
client1.get_post_message(),
map(lambda e: e.message, omikuji_results.values())
)


if __name__ == '__main__':
unittest.main()