-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (47 loc) · 2.03 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
"""main.py - This file contains handlers that are called by taskqueue and/or
cronjobs."""
import webapp2
from google.appengine.api import mail, app_identity
from api import TicTacToeApi
from models import Game
class SendReminderEmail(webapp2.RequestHandler):
@staticmethod
def get():
"""Send a reminder email to the user who's turn it is for
each game that is active. Called every hour using a cron job"""
app_id = app_identity.get_application_id()
# Get all in progress games
games = Game.query(Game.game_over == False)
for game in games:
# Get first player or second player based on whos turn
if game.whos_turn == 1:
user = game.player_one.get()
else:
user = game.player_two.get()
# If the user has an email send them a little reminder
if user.email is not None:
subject = 'Freaky TicTacToe Reminder'
body = 'Hello {}, it is currently your turn in the game [ {} ]. Please return to the game.'\
.format(user.name, game.key.urlsafe())
mail.send_mail('noreply@{}.appspotmail.com'.format(app_id),
user.email,
subject,
body)
class IncrementActiveGames(webapp2.RequestHandler):
@staticmethod
def post():
"""This method which is called via the task queue
increments the number of active games in cache"""
TicTacToeApi.increment_active_games()
class DecrementActiveGames(webapp2.RequestHandler):
@staticmethod
def post():
"""This method which is called via the task queue
decrements the number of active games in cache"""
TicTacToeApi.decrement_active_games()
app = webapp2.WSGIApplication([
('/crons/send_reminder', SendReminderEmail),
('/tasks/increment_active_games', IncrementActiveGames),
('/tasks/decrement_active_games', DecrementActiveGames)
], debug=True)