-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
128 lines (99 loc) · 3.93 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
import webapp2
import os
import logging
from google.appengine.ext.webapp import template
from google.appengine.ext import ndb
from google.appengine.api import memcache
from goseethesun.service import GameService
from goseethesun.service import PlayerService
from goseethesun.core import Game
from goseethesun.core import Entity
from goseethesun.core import Player
from goseethesun.gamecontrollers.BasicGameController import BasicGameController
import json
class WebGamesHandler(webapp2.RequestHandler):
def get(self):
games = GameService.allGames()
template_values = {'games': games}
path = os.path.join(os.path.dirname(__file__), 'web/listGames.html')
self.response.out.write(template.render(path, template_values))
def post(self):
games = Game.query()
for game in games:
game.key.delete()
entities = Entity.query()
for entity in entities:
entity.key.delete()
self.redirect('/')
class JSONGamesHandler(webapp2.RequestHandler):
def get(self):
logging.info(self.request)
self.response.headers['Content-Type'] = 'application/json'
games = GameService.allGames()
self.response.out.write(GameService.toJSON(games))
logging.info(self.response)
def post(self):
self.response.headers['Content-Type'] = 'application/json'
args = json.loads(self.request.body)
name = args['name']
description = args['description']
private = args['private'] != '';
lat = args['lat']
lon = args['lon']
logging.info(name)
logging.info(description)
logging.info(private)
game = GameService.createGame(name, description, private, lat, lon)
games = [game]
self.response.out.write(GameService.toJSON(games))
class JSONPlayerHandler(webapp2.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = 'application/json'
args = json.loads(self.request.body)
gameId = args['gameKeyId']
nickname = args['nickname']
player = GameService.createPlayer(gameId, nickname)
players = [player]
self.response.out.write(PlayerService.toJSON(players))
logging.info(PlayerService.toJSON(players))
class JSONResignHandler(webapp2.RequestHandler):
def post(self):
logging.info(self.request.body)
args = json.loads(self.request.body)
playerKey = args['id']
PlayerService.deletePlayer(playerKey)
class JSONPositionHandler(webapp2.RequestHandler):
def post(self):
args = json.loads(self.request.body)
lat = args['lat']
lon = args['lon']
playerId = int(args['id'])
PlayerService.updateCachedPosition(playerId, lat, lon)
players = GameService.getBuddies(playerId)
#GameService.executeGameLogicForPlayer(player)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(PlayerService.toJSON(players))
logging.info(self.response)
class JSONActionHandler(webapp2.RequestHandler):
def post(self):
args = json.loads(self.request.body)
playerId = int(args['id'])
action = args['action']
target = args['target']
game = GameService.getGameForPlayer(playerId)
controller = BasicGameController.get_by_id(game.gameControllerId, parent=None)
getattr(controller, action)(target, playerId)
class JSONStaticsHandler(webapp2.RequestHandler):
pass
class JSONDynamicsHandler(webapp2.RequestHandler):
pass
app = webapp2.WSGIApplication([
('/', WebGamesHandler),
('/play.json' , JSONPositionHandler),
('/games.json' , JSONGamesHandler ),
('/player.json' , JSONPlayerHandler ),
('/resign.json' , JSONResignHandler ),
('/statics.json' , JSONStaticsHandler ),
('/dynamics.json', JSONDynamicsHandler),
('/action.json' , JSONActionHandler),
], debug=True)