示例#1
0
def move_robber(player, to, stealfrom):
    game = player.game
    if not (
        #We're supposed to be moving the robber
        game.State == Game.States.MOVE_ROBBER and

        #this player is supposed to be moving the robber
        player.UserID == game.CurrentPlayerID and

        #the robber isn't being moved nowhere
        game.RobberHex != to and

        #the place where the robber is being moved is valid
        to in h.valid_hexes
    ) :
        return "failure"
 
    v1 = v.decompress(to)
    adjacent_vertices = map(v.compress, h.adjacent(v1))

    vulnerable_players = db_session.query(Settlement.UserID). \
        filter_by(GameID=game.GameID). \
        filter(Settlement.Vertex.in_(adjacent_vertices)). \
        filter(Settlement.UserID != player.UserID). \
        all()

    if (len(vulnerable_players) == 0 and stealfrom is None) or (stealfrom,) in vulnerable_players:
        game.RobberHex = to
        game.State = Game.States.NORMAL_PLAY

        if stealfrom is not None:
            pass #TODO

        game.log(Log.robber_moved(player.UserID, to))
        game.log(Log.req_turn(player.UserID))
        db_session.commit()
        return "success"
    else:
        return "failure"
示例#2
0
文件: models.py 项目: 341-catan/catan
            def give_cards(rolled):
                #gets hexes that have just yielded stuff
                rolled_hexes = db_session.query(Hex.Vertex, Hex.Type). \
                    filter_by(GameID=self.GameID). \
                    filter(Hex.Chit == rolled). \
                    filter(Hex.Vertex != self.RobberHex). \
                    all()

                """
                Creates a dict of the form:
                (type -> (vertex -> count)
                where:
                    type is the type of hex
                    vertex is a vertex number adjacent to the hex
                    count is the number of times that vertex would
                        receive cards of type *type*

                It may be better to do sort | uniq here
                """
                types = {}
                for (hex, type) in rolled_hexes:
                    if not type in types:
                        types[type] = {}

                    adjacent = map(v.compress, h.adjacent(v.decompress(hex)))
                    for i in adjacent:
                        if not i in types[type]:
                            types[type][i] = 0
                        types[type][i] += 1

                """
                Creates a dict of the form:
                (userid -> (type -> count))
                where:
                    userid is self explanatory
                    type if the type of resource
                        (equivalent to the type of hex)
                    count is the number of cards of that resource type

                """
                users = {}
                for t in types:
                    #get all settlements that are adjacent to hexes
                    #of type *type*
                    settlements = Settlement.query. \
                        filter_by(GameID=self.GameID). \
                        filter(Settlement.Vertex.in_(types[t].keys())). \
                        all()

                    for s in settlements:
                        if not s.UserID in users:
                            users[s.UserID] = {}
                        if not t in users[s.UserID]:
                            users[s.UserID][t] = 0
                        #give double for cities
                        users[s.UserID][t] += \
                            (2 if s.Type == Settlement.CITY else 1) * \
                            types[t][s.Vertex]

                for u in users:
                    allocated = list(map(lambda type: (users[u][type], type), users[u].keys()))

                    GamePlayer.query. \
                        filter_by(GameID=self.GameID). \
                        filter_by(UserID=u).one(). \
                        add_cards(allocated)