def post(self): """Handles post requests.""" user = users.get_current_user() builder_purchased = PurchasedItem.gql(("WHERE federated_identity = '%s' " "AND item_name = '%s'") % (user.federated_identity(), 'Builder')) can_build = (builder_purchased.count() > 0) level_name = self.request.get('level') if level_name in ['1', '2', '3', '4', '5']: self.SendError('%s is already a level' % level_name, can_build) return else: existing = Level.gql("WHERE owner = USER('%s') AND level='%s'" % (user.email(), level_name)) if existing.count(): self.SendError('%s is already a level' % level_name, can_build) return # Validate base_rows max_column = 23 max_row = 9 base_rows = self.request.get('base_rows', None) try: base_rows = int(base_rows) if base_rows < 0 or base_rows > max_row: self.SendError(('%s is invalid for rows at bottom ' 'of screen' % base_rows), can_build) return except (ValueError, TypeError): self.SendError(('%s is invalid for rows at bottom ' 'of screen' % base_rows), can_build) return # Validate static static = self.ParseRowsFromRequest('static') if static is None: self.SendError(('Static Blocks invalid. Please use ' 'integers and separate rows by commas'), can_build) return # Validate moveable moveable = self.ParseRowsFromRequest('moveable') if moveable is None: self.SendError(('Moveable Blocks invalid. Please use ' 'integers and separate rows by commas'), can_build) return # Update static or fail if moveable and static share blocks; # also fail if a moveable block would end up hovering for column, row_list in moveable.iteritems(): destination = static[column] if column in static else [] for row in row_list: if row in destination: self.SendError(('Moveable and Static Blocks can\'t occupy ' 'the same place'), can_build) return elif row > base_rows: # if in row base_rows, block will be supported if row - 1 not in row_list + destination: self.SendError('Moveable can\'t hover', can_build) return # Validate door door_column = self.request.get('door_column', '') door_row = self.request.get('door_row', '') try: door_column = int(door_column) door_row = int(door_row) # Door must be on screen; # Door has height of 2 blocks, so can't start in the top row if (door_row < base_rows or door_row > max_row - 1 or door_column < 0 or door_column > max_column): self.SendError('Door out of bounds', can_build) return # Door must not conflict with squares if door_column in static: if (door_row in static[door_column] or door_row + 1 in static[door_column]): self.SendError('Door conflicts with Static Blocks', can_build) return elif door_column in moveable: if (door_row in moveable[door_column] or door_row + 1 in moveable[door_column]): self.SendError('Door conflicts with Moveable Blocks', can_build) return except (ValueError, TypeError): self.SendError('Door values invalid', can_build) return door = {'row': door_row, 'column': door_column} # Validate player player_column = self.request.get('player_column', '') player_row = self.request.get('player_row', '') try: player_column = int(player_column) player_row = int(player_row) # Player must start on screen if (player_row < base_rows or player_row > max_row or player_column < 0 or player_column > max_column): self.SendError('Player out of bounds', can_build) return # Make sure no collisions with obstacles; though # Player *CAN* start in the door if the creator wants it if player_column in static and player_row in static[player_column]: self.SendError('Player conflicts with Static Blocks', can_build) return elif player_column in moveable and player_row in moveable[player_column]: self.SendError('Player conflicts with Moveable Blocks', can_build) return # Player must start on ground if player_row != base_rows: block_below = False if player_column in static: if player_row - 1 in static[player_column]: block_below = True if player_column in moveable: if player_row - 1 in moveable[player_column]: block_below = True if not block_below: self.SendError('Player must start grounded', can_build) return except (ValueError, TypeError): self.SendError('Player values invalid', can_build) return player_start = {'x': player_column*64, # default entity_size 'y': 64*(10 - (player_row + 1))} # default canvas_height new_level = Level(owner=user, level=level_name, base_rows=base_rows, static_blocks=DBPIckle(static), move_blocks=DBPIckle(moveable), door=DBPIckle(door), player_start=DBPIckle(player_start)) new_level.put() self.redirect('/play?level=%s' % level_name)
from main import DBPIckle from models import Level level_one = Level(level='1', next_level='2', base_rows=4, static_blocks=DBPIckle({7: [4, 5], 13: [4], 17: [4, 5]}), move_blocks=DBPIckle({2: [4], 10: [4]}), door=DBPIckle({'row': 4, 'column': 23}), player_start=DBPIckle({'x': 256, 'y': 320})) level_one.put() level_two = Level(level='2', next_level='3', base_rows=4, static_blocks=DBPIckle({12: [4, 5], 23: [4, 5, 6]}), move_blocks=DBPIckle({0: [4, 5], 3: [4], 22: [4]}), door=DBPIckle({'row': 7, 'column': 23}), player_start=DBPIckle({'x': 256, 'y': 320})) level_two.put() level_three = Level(level='3', next_level='4', base_rows=3, static_blocks=DBPIckle( {0: [3, 4], 1: [3, 4], 2: [3, 4], 3: [3, 4], 4: [3, 4], 5: [3, 4], 11: [3, 4], 12: [3, 4], 13: [3, 4], 14: [3, 4], 15: [3, 4], 16: [3, 4], 17: [3, 4], 18: [3, 4], 19: [3, 4], 20: [3, 4], 21: [3, 4], 22: [3, 4], 23: [3, 4]}), move_blocks=DBPIckle({0: [5]}), door=DBPIckle({'row': 5, 'column': 23}), player_start=DBPIckle({'x': 256, 'y': 256})) level_three.put() level_four = Level(level='4', next_level='5', base_rows=2, static_blocks=DBPIckle(
def get(self): level = Level() level.grid = "abcdefghijklmnopqrstuvwxyzaabcdefghijklmnopqrstuvwxyzaabcdefghijklmnopqrstuvwxyzaabcdefghi" level.word_bank = ["abc", "def", "ghi", "klmnop"] level.time = datetime.datetime.now() level.put()