def build_road(player, vertex1, vertex2): if vertex2 < vertex1: (vertex1, vertex2) = (vertex2, vertex1) if ( #the player has the resources player.hasResourcesFor(BuildTypes.ROAD) and #both vertices are valid v.isvalid(v.decompress(vertex1)) and v.isvalid(v.decompress(vertex2)) and #no other roads overlap Road.overlaps_q(player, vertex1, vertex2).count() == 0 and #the player has a road at one of the vertices already player.road_meets_q(vertex1, vertex2).count() != 0 ) : player.takeResourcesFor(BuildTypes.ROAD) r = Road(player.UserID, vertex1, vertex2) player.game.roads.append(r) player.game.log(Log.road_built(player, r)) # TODO: check if we now have longest road. # It is a longest path problem. Check the rules before implementing db_session.commit() return "success" else: return "failure"
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"