/
main.py
89 lines (72 loc) · 2.63 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.api import channel
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from tournament import Game, Player
class BaseHandler(webapp.RequestHandler):
template_values = {}
def render(self, path):
self.template_values.update({'logout_url' : users.create_logout_url('/bye')})
return self.response.out.write(template.render(path,
self.template_values))
class MainPage(BaseHandler):
def get(self):
userid = users.get_current_user().user_id()
player = Player.from_id(userid)
if player.gameroom is not None:
self.template_values.update({'gamekey': player.gameroom})
clientid_for_channel = player.create_channel()
token = channel.create_channel(clientid_for_channel)
self.template_values.update({'token' : token,
'userid' : player.keyname})
return self.render('index.html')
class JoinGame(BaseHandler):
def post(self):
"""
Processes the req from a client/player to join a gameroom
"""
user = users.get_current_user()
if not user:
return self.redirect(users.create_login_url('/'))
player = Player.from_id(user.user_id())
if not player.gameroom:
return Game.join_latest_or_new(player)
return Game.continue_tournament(player)
class Chat(BaseHandler):
"""
Chatting between users
"""
def post(self):
userid = users.get_current_user().user_id()
message = self.request.get('m', '')
Player.from_id(userid).chat(message)
class Bye(BaseHandler):
"""
Handles the Bye
"""
def get(self):
self.response.out.write("""<html><body>
Bye. It was nice to see you here !!<a href="/">Login Again</a>
</body></html>""")
class Leave(BaseHandler):
"""
Handles when the player decides to leave the gameroom
"""
def get(self):
userid = users.get_current_user().user_id()
Player.from_id(userid).leave_tournament()
self.redirect('/bye')
application = webapp.WSGIApplication(
[('/', MainPage),
('/joingame.*', JoinGame),
('/chat', Chat),
('/bye', Bye),
('/leave', Leave),],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()