Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from freedomofkeima/improve-daily-greetings
Browse files Browse the repository at this point in the history
Add event type greetings
  • Loading branch information
freedomofkeima committed Oct 2, 2017
2 parents 7e53798 + e3d1f18 commit 61b89be
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
6 changes: 5 additions & 1 deletion docs/offerings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ Maid-chan has a scheduler worker for handling various daily tasks. For example,

Daily offerings contain of two parts: greeting and image. Greetings are taken from `maidchan/constant.py` while offerings are taken from `offerings/stock/` directory.

.. autofunction:: maidchan.offerings.check_event_morning_offerings

Maid-chan supports event type morning messages, e.g.: Halloween, Christmas, etc. If "force" flag is turned on, Maid-chan will use a specific message for the morning greeting. Otherwise, there's a probability that Maid-chan will use seasonal type of messages.

.. autofunction:: maidchan.offerings.get_morning_offerings_text

.. autofunction:: maidchan.offerings.get_night_offerings_text

Offerings text functionality has two types: normal and special with 2% of probability. For example, Maid-chan sends morning greeting more than half an hour later than usual because of overslept. Greeting text on any specific day is the same for everyone, as it is stored in scheduler's metadata.
In general, offerings text functionality has two types: normal and special with 2% of probability. For example, Maid-chan sends morning greeting more than half an hour later than usual because of overslept (2% chance). Greeting text on any specific day is the same for everyone, as it is stored in scheduler's metadata.

.. autofunction:: maidchan.offerings.get_offerings_image

Expand Down
43 changes: 43 additions & 0 deletions maidchan/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,49 @@ class Constants(object):
"The game was so much fun! Are you sleeping already, {}? Good nite~"
]

EVENT_GOOD_MORNING = [
{
"text": "Good morning, {}! It's almost time for Halloween!",
"start_date": 1,
"start_month": 10,
"end_date": 30,
"end_month": 10,
"force": False
},
{
"text": "おはよう! Happy Halloween, {}! Trick or Treat <3",
"start_date": 31,
"start_month": 10,
"end_date": 31,
"end_month": 10,
"force": True
},
{
"text": "Ohaa~ Christmas is coming, {}!",
"start_date": 1,
"start_month": 12,
"end_date": 24,
"end_month": 12,
"force": False
},
{
"text": "Merry Christmas, {}! Santa came to my house last night, nyaa~",
"start_date": 25,
"start_month": 12,
"end_date": 25,
"end_month": 12,
"force": True
},
{
"text": "あけましておめでとう! 今年もよろしく, {}!",
"start_date": 1,
"start_month": 1,
"end_date": 1,
"end_month": 1,
"force": True
}
]

# Base default time, before adding random delta
DEFAULT_MORNING_TIME = "09:00" # UTC+9
DEFAULT_JAPANESE_TIME = "13:00" # UTC+9
Expand Down
42 changes: 40 additions & 2 deletions maidchan/offerings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
import datetime
import logging
import os
import pytz
from random import randint

from maidchan.constant import Constants
Expand All @@ -21,14 +23,50 @@ def get_special_probability():
return False


def check_event_morning_offerings():
result = []
dt_now = datetime.datetime.now(tz=pytz.timezone('Asia/Tokyo')) # UTC+9
for event in Constants.EVENT_GOOD_MORNING:
start_mt = dt_now.replace(
month=event["start_month"],
day=event["start_date"],
hour=0,
minute=0,
second=0
)
end_mt = dt_now.replace(
month=event["end_month"],
day=event["end_date"],
hour=23,
minute=59,
second=59
)
# Ensure start < end
if end_mt < start_mt:
end_mt = end_mt.replace(year=end_mt.year + 1)
# Check range
if start_mt <= dt_now <= end_mt:
if event.get('force', False):
return True, event["text"]
else:
result.append(event["text"])
return False, result


def get_morning_offerings_text():
# Priority: event force > special > normal/event
event_force, event_text = check_event_morning_offerings()
# Force special text, e.g.: Halloween day, Christmas day, etc
if event_force:
return event_text, NORMAL
is_special = get_special_probability()
if is_special:
p = randint(1, len(Constants.SPECIAL_GOOD_MORNING))
return Constants.SPECIAL_GOOD_MORNING[p-1], SPECIAL
else:
p = randint(1, len(Constants.NORMAL_GOOD_MORNING))
return Constants.NORMAL_GOOD_MORNING[p-1], NORMAL
text_list = Constants.NORMAL_GOOD_MORNING + event_text
p = randint(1, len(text_list))
return text_list[p-1], NORMAL


def get_night_offerings_text():
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ oauthlib==2.0.1
pymessenger==0.0.7.0
pymongo==3.4.0
python-twitter==3.2
pytz==2017.2
redis==2.10.5
requests==2.12.4
requests-oauthlib==0.7.0
Expand Down

0 comments on commit 61b89be

Please sign in to comment.