Esempio n. 1
0
def setup_simulation(config):
    """prepare the simulation"""
    global model
    global queued_players
    print('----------------------CONFIG')
    print(config)

    # only setup a new simulation if there
    # is no existing simluation.
    # to start a new simulation, first hit the reset endpoint
    if model is None:
        if not any(isinstance(h, SocketsHandler) for h in logger.handlers):
            # don't redundantly add the handler
            logger.setLevel(logging.INFO)
            sockets_handler = SocketsHandler()
            logger.addHandler(sockets_handler)

        if not any(
                isinstance(h, logging.FileHandler) for h in logger.handlers):
            file_handler = logging.FileHandler('simulation.log')
            logger.addHandler(file_handler)

        pop = load_population('data/population.json')
        pop = pop[:200]  # limit to 200 for now
        model = City(pop, config)

        # send population to the frontend
        s = socketio()
        s.emit(
            'setup', {
                'existing': False,
                'population': [p.as_json() for p in pop],
                'buildings': [{
                    'id': b.id,
                    'tenants': []
                } for b in model.buildings]
            },
            namespace='/simulation')
        s.emit('government',
               model.government.as_json(),
               namespace='/simulation')

        # setup queued players
        print('QUEUED PLAYERS')
        print(queued_players)
        for id in queued_players:
            players.append(id)
            person = random.choice([p for p in model.people if p.sid == None])
            person.sid = id
            s.emit('person', person.as_json(), namespace='/player', room=id)
            s.emit('joined', person.as_json(), namespace='/simulation')
        queued_players = []
Esempio n. 2
0
def setup_simulation(config):
    """prepare the simulation"""
    global model
    global queued_players
    print('----------------------CONFIG')
    print(config)

    # only setup a new simulation if there
    # is no existing simluation.
    # to start a new simulation, first hit the reset endpoint
    if model is None:
        if not any(isinstance(h, SocketsHandler) for h in logger.handlers):
            # don't redundantly add the handler
            logger.setLevel(logging.INFO)
            sockets_handler = SocketsHandler()
            logger.addHandler(sockets_handler)

        pop = load_population('data/population.json')
        pop = pop[:200] # limit to 200 for now
        model = City(pop, config)

        # send population to the frontend
        s = socketio()
        s.emit('setup', {
            'existing': False,
            'population': [p.as_json() for p in pop],
            'buildings': [{
                'id': b.id,
                'tenants': []
            } for b in model.buildings]
        }, namespace='/simulation')
        s.emit('government', model.government.as_json(), namespace='/simulation')

        # setup queued players
        print('QUEUED PLAYERS')
        print(queued_players)
        for id in queued_players:
            players.append(id)
            person = random.choice([p for p in model.people if p.sid == None])
            person.sid = id
            s.emit('person', person.as_json(), namespace='/player', room=id)
            s.emit('joined', person.as_json(), namespace='/simulation')
        queued_players = []
Esempio n. 3
0
    return jsonify(success=True)


@routes.route('/propose', methods=['POST'])
def propose():
    data = request.get_json()
    proposal = data.get('proposal', None)
    if proposal is None:
        choose_proposer.delay()
    else:
        print(proposal)
        start_vote.delay(proposal)
    return jsonify(success=True)


pop = load_population('data/population.json')
@routes.route('/person/<id>')
def person(id):
    # eh hacky but ok for this scale
    per = None
    for p in pop:
        if p.id == id:
            per = p
            break
    if per is None:
        abort(404)
    return jsonify(**per.as_json())


@routes.route('/reset')
def reset_sim():