-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
163 lines (127 loc) · 4.45 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import logging
from google.appengine.ext import webapp
from google.appengine.api import channel
from google.appengine.api import users
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
from django.utils import simplejson
games = {}
class Game:
def __init__(self):
self.player1 = None
self.player2 = None
self.state = {
'units': [
]
}
self.add_unit(20, 20, 'red')
self.add_unit(60, 20, 'red')
self.add_unit(100, 20, 'red')
self.add_unit(140, 20, 'red')
self.add_unit(20, 380, 'blue')
self.add_unit(60, 380, 'blue')
self.add_unit(100, 380, 'blue')
self.add_unit(140, 380, 'blue')
def add_unit(self, x=0, y=0, color='red'):
self.state['units'].append({
'x': x,
'y': y,
'color': color,
'target': {
'x': x,
'y': y,
},
})
def export_to_json(self):
return simplejson.dumps({
'player1': '' if self.player1 is None else self.player1.nickname(),
'player2': '' if self.player2 is None else self.player2.nickname(),
'state': self.state,
})
def send_state_to_clients(self):
json = self.export_to_json()
if (self.player1):
channel.send_message(self.player1.user_id(), json)
if (self.player2):
channel.send_message(self.player2.user_id(), json)
class GamesHandler(webapp.RequestHandler):
def get(self, game_name):
global games
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
return
if not game_name in games:
self.error(404)
self.response.out.write('Could not find game "%s"' % game_name)
return
game = games[game_name]
if game.player1 is None:
game.player1 = user
elif game.player1.user_id() == user.user_id():
pass
elif game.player2 is None:
game.player2 = user
elif game.player2.user_id() == user.user_id():
pass
else:
self.error(500)
self.response.out.write('Game is full')
return
token = channel.create_channel(user.user_id())
self.response.out.write(template.render('ingame.html', {
'token': token,
'nickname': user.nickname(),
}))
def post(self):
""" Create a new game """
global games
user = users.get_current_user()
game = Game()
game.player1 = user
games[self.request.get('game_name')] = game
class MoveHandler(webapp.RequestHandler):
def post(self, game_name, unit_index):
""" Set target position for a unit """
global games
unit_index = int(unit_index)
game = games[game_name]
game.state['units'][unit_index]['target']['x'] = int(self.request.get('x'))
game.state['units'][unit_index]['target']['y'] = int(self.request.get('y'))
game.send_state_to_clients()
class KillHandler(webapp.RequestHandler):
def post(self, game_name, unit_index):
""" Set target position for a unit """
global games
game = games[game_name]
user = users.get_current_user()
new_color = 'red' if user.user_id() == game.player1.user_id() else 'blue'
unit_index = int(unit_index)
game.state['units'][unit_index]['color'] = new_color
game.send_state_to_clients()
class OpenedHandler(webapp.RequestHandler):
def post(self, game_name):
global games
game = games[game_name]
game.send_state_to_clients()
class MainHandler(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
return
self.response.out.write(template.render('index.html', {
'nickname': user.nickname(),
}))
def main():
application = webapp.WSGIApplication([
('/games/(.*)/units/(.*)/move', MoveHandler),
('/games/(.*)/units/(.*)/kill', KillHandler),
('/games/(.*)/opened', OpenedHandler),
('/games/(.*)', GamesHandler),
('/games', GamesHandler),
('/.*', MainHandler),
], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()