Пример #1
0
	def __end_turn(self, game_id):
		"""Function that is called at the end of every turn

		The purpose of this function is to handle pieces moving after a completed turn
		The piece will be moves, rotated, and scored
		(Right now is very long, should probably be broken down)
		"""
		# Makes sure change are in the databse
		# TODO: consider persist object through instead of flushing to db
		db.session.flush()

		#Get all cards that have potential to move
		cards = Card.query.filter_by(game_id=game_id).\
				filter_by(value="P").\
				filter(Card.board_space_id != None).\
				all()
		#Move all the cards
		for card in cards:
			x_y = self.__get_movement(card.direction)
			new_space = BoardSpace.query.filter_by(game_id=game_id).\
				filter_by(x_loc = card.board_space.x_loc + x_y[0]).\
				filter_by(y_loc = card.board_space.y_loc + x_y[1]).\
				first()

			#if no space, flip in opposite direction
			if not new_space:
				card.direction = self.__get_opposite_direction(card.direction)
			else:
				previous_space = card.board_space
				card.board_space = new_space

				#if current space, rotate
				current_space = Card.query.filter_by(game_id=game_id).\
					filter_by(board_space=new_space).filter(Card.value != "P").\
					filter(Card.id != card.id).first()
				if current_space:
					card.direction = current_space.value
					current_space.board_space = None
					current_space.finished = True

				#Grab any meeples at current space, if they exist add points to score, then remove
				meeple = Meeple.query.filter_by(game_id=game_id).\
					filter_by(board_space=new_space).first()
				
				if meeple:
					meeple.board_space = None
					meeple.finished = True
					card.board_space = None
					card.finished = True

			#flush so duplicate distruction of meeple does not happen
			db.session.flush()

		#End Turn 
		Game.change(game_id)