def setup(player, settlement_vertex, road_to): game = player.game settlement_v = v.decompress(settlement_vertex) road_v = v.decompress(road_to) if ( #we're setting up game.State in [Game.States.SETUP_FORWARD, Game.States.SETUP_BACKWARD] and #it's the player's turn player.UserID == game.CurrentPlayerID and #the settlement vertex is valid v.isvalid(settlement_v) and #the new settlement conforms to the distance rule Settlement.distance_rule(player.GameID, settlement_v) and #the road vertex is adjacent to the settlement (and valid) road_v in v.adjacent(settlement_v) #we don't have to check for existing roads, #because there can't be any, by the distance rule ): s = player.add_settlement(settlement_vertex) r = player.add_road(settlement_vertex, road_to) player.Score += 1 game.log(Log.setup(player, settlement_vertex, road_to)) if game.State == Game.States.SETUP_FORWARD: if game.CurrentIndex + 1 == len(game.players): #time to go backwards game.State = Game.States.SETUP_BACKWARD else: game.CurrentIndex += 1 elif game.State == Game.States.SETUP_BACKWARD: #TODO: A naming schema for compressed/uncompressed vertices (Hungarian Notation FTW!) adjacent = map(v.compress, v.adjacent_hexes(settlement_v)) cards = db_session.query(Hex.Type, func.count()). \ filter_by(GameID=game.GameID). \ filter(Hex.Vertex.in_(adjacent)). \ filter(Hex.Type != Terrain.DESERT). \ group_by(Hex.Type). \ all() player.add_cards([(j, i) for (i, j) in cards]) if game.CurrentIndex == 0: #setup is over, welcome to the real world, Neo game.State = Game.States.NORMAL_PLAY else: game.CurrentIndex -= 1 game.CurrentPlayerID = game.players[game.CurrentIndex].UserID log_state_change(game) db_session.commit() return "success" else: return "failure"
def distance_rule(gameid, vertex): checkvertices = map(v.compress, [vertex] + v.adjacent(vertex)) return Settlement.query. \ filter_by(GameID=gameid). \ filter(Settlement.Vertex.in_(checkvertices)). \ count() == 0