Ejemplo n.º 1
0
def get_jobs(life, group_id):
	_group = get_group(life, group_id)
	_jobs = []
	_leader = LIFE[_group['leader']]
	
	if not has_camp(group_id):
		_nearest_camp = camps.get_nearest_known_camp(_leader)
		
		if _leader['known_camps']:
			_j = jobs.create_job(_leader, 'Raid', gist='start_raid', description='Raid camp %s.' % _nearest_camp['id'])
			_pos = lfe.get_current_chunk(_leader)['pos']
			_chunk_key = lfe.get_current_chunk_id(_leader)
		
			jobs.add_task(_j, '0', 'announce_to_group',
			              action.make_small_script(function='announce_to_group',
			                                       kwargs={'group_id': group_id,
			                                               'gist': 'announce_group_job',
			                                               'message': jobs.get_job(_j)['description'],
			                                               'job_id': _j}),
			              player_action=action.make_small_script(function='always'),
			              description='Gather group members.')
			jobs.add_task(_j, '1', 'move_to_chunk',
			              action.make_small_script(function='travel_to_position',
			                                       kwargs={'pos': _pos}),
			              player_action=action.make_small_script(function='is_in_chunk',
	                                           kwargs={'chunk_key': _chunk_key}),
			              description='Travel to position %s, %s' % (_pos[0], _pos[1]),
				          delete_on_finish=False)
			jobs.add_task(_j, '2', 'wait_for_number_of_group_members_in_chunk',
			              action.make_small_script(function='number_of_alife_in_chunk_matching',
			                                       kwargs={'amount': 2,
			                                               'chunk_key': _chunk_key,
			                                               'matching': {'group': _leader['group']}}),
			              description='Wait until everyone arrives.')
			#jobs.add_task(_j, '3', 'talk',
			#              action.make_small_script(function='travel_to_position',
			#                                       kwargs={'pos': chunks.get_nearest_chunk_in_list(_leader['pos'], camps.get_camp(_nearest_camp['id'])['reference'])}),
			#              requires=['1'],
			#              delete_on_finish=False)
			
			_jobs.append(_j)
	
	if len(_leader['known_groups'])>1:
		_lowest = {'score': 0, 'group': None}
		for group_id in [g for g in _leader['known_groups'] if not g==_leader['group']]:
			_score = judgement.judge_group(_leader, group_id)
			
			if not _lowest['group'] or _score < _lowest['score']:
				_lowest['score'] = _score
				_lowest['group'] = group_id
			
		
		print 'RAID', _lowest
	else:
		print 'ony one'
	
	return _jobs
Ejemplo n.º 2
0
def crud_job():
    token = request.headers['Authorization']
    username = jwt.decode(token, app.config['SECRET_KEY_ADMIN'])

    if (request.json['mode'] == "add"):
        resp = job.create_job(username)
        return resp
    elif (request.json['mode'] == "delete"):
        resp = job.delete_job(username)
        return resp
    elif (request.json['mode'] == "update"):
        resp = job.update_job(username)
        return resp
    else:
        resp = jsonify({'message': 'Invalid Request.'})
        return resp
Ejemplo n.º 3
0
def _handle_post_offset_job(limit, offset, job_type):
    # Converting the limit and offset to integers if they were
    # provided and checking if they are non-negative.
    try:
        # These conversions might fail if they aren't None or
        # integer strings.
        if limit is not None:
            limit = int(limit)
        if offset is not None:
            offset = int(offset)
    except ValueError:
        return _make_error(
            'limit and offset, if provided, must be integers.'
        ), 400
    else:
        job_dict = jobs.create_job(redis_client, limit=limit, offset=offset,
                                   job_type=job_type)
        return jsonify(job_dict)
Ejemplo n.º 4
0
def _handle_post_range_job(start, end, job_type):
    # Converting the start and end to integers if they were
    # provided.
    try:
        # These conversions might fail if they aren't None or
        # integer strings.
        if start is not None:
            start = int(start)
        if end is not None:
            end = int(end)
    except ValueError:
        return _make_error(
            'start and end, if provided, must be integers.'
        ), 400
    else:
        job_dict = jobs.create_job(redis_client, start=start, end=end,
                                   job_type=job_type)
        return jsonify(job_dict)
Ejemplo n.º 5
0
def jobs_index():
    """Handle the root jobs collection."""

    if request.method == 'POST':
        # Parse if this is a range or offset case (or neither), and
        # send an error to the client if they chose both.
        try:
            body = request.get_json(force=True) or {}
        except Exception as e:
            return _make_error(f'Invalid JSON: {e}'), 400

        start = body.get('start')
        end = body.get('end')
        limit = body.get('limit')
        offset = body.get('offset')
        job_type = body.get('job_type', 'line')

        if job_type not in ('line', 'fun_facts', 'histogram', 'box_plot'):
            return _make_error(
                'job_type must be line, fun_facts, histogram, box_plot, or not'
                ' given (defaulting to line)'
            ), 400

        is_range_case = start is not None or end is not None
        is_offset_case = limit is not None or offset is not None

        if is_range_case and is_offset_case:
            return _make_error(
                'limit and/or offset cannot be combined with start and/or end'
            ), 400
        elif is_range_case:
            return _handle_post_range_job(start, end, job_type)
        elif is_offset_case:
            return _handle_post_offset_job(limit, offset, job_type)
        else:
            job_dict = jobs.create_job(redis_client, job_type=job_type)
            return jsonify(job_dict)
    elif request.method == 'GET':
        job_dicts = jobs.get_all_jobs(redis_client)
        return jsonify(job_dicts)
Ejemplo n.º 6
0
def order_to_loot(life, group_id, add_leader=False):
	#TODO: We should really consider moving the needs portion of this code outside of this function
	#Because this function really only does something on the first run, rendering it into just another
	#announce loop...
	
	_group = get_group(life, group_id)
	
	_requirements = [action.make_small_script(function='has_number_of_items_matching',
	                                          args={'matching': [{'type': 'drink'}], 'amount': 1})]
	
	_j = jobs.create_job(life, 'Loot for group %s.' % life['group'],
	                     gist='loot_for_group',
	                     description='Collect loot for group.',
	                     group=life['group'],
	                     requirements=_requirements)
	
	if _j:
		for member in _group['members']:
			if member == _group['leader'] and not add_leader:
				continue
			
			survival.add_needed_item(LIFE[member],
				                    {'type': 'drink'},
				                    amount=1,
			                         pass_if=_requirements,
				                    satisfy_if=action.make_small_script(function='group_needs_resources',
				                                                        args={'group_id': group_id}),
				                    satisfy_callback=action.make_small_script(return_function='pass'))
		
		jobs.add_task(_j, '0', 'bring_back_loot',
		              action.make_small_script(function='find_target',
		                                       kwargs={'target': _group['leader'],
		                                               'distance': 5,
		                                               'follow': False}),
		              player_action=action.make_small_script(function='can_see_target',
		                                                     kwargs={'target_id': _group['leader']}),
		              description='Drop the item off at the camp',
		              delete_on_finish=False)
		
		jobs.add_task(_j, '1', 'flag_item',
		              action.make_small_script(function='flag_item_matching',
		                                       kwargs={'matching': {'type': 'drink'},
		                                               'flag': 'ignore'}),
		              player_action=action.make_small_script(function='always'),
		              description='Ignore this',
		              delete_on_finish=False)
		
		jobs.add_task(_j, '2', 'drop_item',
		              action.make_small_script(function='drop_item_matching',
		                                       kwargs={'matching': {'type': 'drink'}}),
		              player_action=action.make_small_script(function='never'),
		              description='Drop the item off at the camp',
		              delete_on_finish=False)
		
		flag(group_id, 'loot', _j)
	
	if lfe.ticker(life, 'resource_announce', 10):
		_job_id = get_flag(group_id, 'loot')
		
		announce(life, life['group'],
			     'job',
			     'We need more resources.',
			     job_id=_job_id,
		          order=True,
			     filter_if=[action.make_small_script(function='has_needs_to_meet')])
Ejemplo n.º 7
0
def get_jobs(life, group_id):
    _group = get_group(life, group_id)
    _jobs = []
    _leader = LIFE[_group['leader']]

    if not has_camp(group_id):
        _nearest_camp = camps.get_nearest_known_camp(_leader)

        if _leader['known_camps']:
            _j = jobs.create_job(_leader,
                                 'Raid',
                                 gist='start_raid',
                                 description='Raid camp %s.' %
                                 _nearest_camp['id'])
            _pos = lfe.get_current_chunk(_leader)['pos']
            _chunk_key = lfe.get_current_chunk_id(_leader)

            jobs.add_task(
                _j,
                '0',
                'announce_to_group',
                action.make_small_script(function='announce_to_group',
                                         kwargs={
                                             'group_id':
                                             group_id,
                                             'gist':
                                             'announce_group_job',
                                             'message':
                                             jobs.get_job(_j)['description'],
                                             'job_id':
                                             _j
                                         }),
                player_action=action.make_small_script(function='always'),
                description='Gather group members.')
            jobs.add_task(
                _j,
                '1',
                'move_to_chunk',
                action.make_small_script(function='travel_to_position',
                                         kwargs={'pos': _pos}),
                player_action=action.make_small_script(
                    function='is_in_chunk', kwargs={'chunk_key': _chunk_key}),
                description='Travel to position %s, %s' % (_pos[0], _pos[1]),
                delete_on_finish=False)
            jobs.add_task(_j,
                          '2',
                          'wait_for_number_of_group_members_in_chunk',
                          action.make_small_script(
                              function='number_of_alife_in_chunk_matching',
                              kwargs={
                                  'amount': 2,
                                  'chunk_key': _chunk_key,
                                  'matching': {
                                      'group': _leader['group']
                                  }
                              }),
                          description='Wait until everyone arrives.')
            #jobs.add_task(_j, '3', 'talk',
            #              action.make_small_script(function='travel_to_position',
            #                                       kwargs={'pos': chunks.get_nearest_chunk_in_list(_leader['pos'], camps.get_camp(_nearest_camp['id'])['reference'])}),
            #              requires=['1'],
            #              delete_on_finish=False)

            _jobs.append(_j)

    if len(_leader['known_groups']) > 1:
        _lowest = {'score': 0, 'group': None}
        for group_id in [
                g for g in _leader['known_groups'] if not g == _leader['group']
        ]:
            _score = judgement.judge_group(_leader, group_id)

            if not _lowest['group'] or _score < _lowest['score']:
                _lowest['score'] = _score
                _lowest['group'] = group_id

        print 'RAID', _lowest
    else:
        print 'ony one'

    return _jobs
Ejemplo n.º 8
0
def order_to_loot(life, group_id, add_leader=False):
    #TODO: We should really consider moving the needs portion of this code outside of this function
    #Because this function really only does something on the first run, rendering it into just another
    #announce loop...

    _group = get_group(life, group_id)

    _requirements = [
        action.make_small_script(function='has_number_of_items_matching',
                                 args={
                                     'matching': [{
                                         'type': 'drink'
                                     }],
                                     'amount': 1
                                 })
    ]

    _j = jobs.create_job(life,
                         'Loot for group %s.' % life['group'],
                         gist='loot_for_group',
                         description='Collect loot for group.',
                         group=life['group'],
                         requirements=_requirements)

    if _j:
        for member in _group['members']:
            if member == _group['leader'] and not add_leader:
                continue

            survival.add_needed_item(LIFE[member], {'type': 'drink'},
                                     amount=1,
                                     pass_if=_requirements,
                                     satisfy_if=action.make_small_script(
                                         function='group_needs_resources',
                                         args={'group_id': group_id}),
                                     satisfy_callback=action.make_small_script(
                                         return_function='pass'))

        jobs.add_task(_j,
                      '0',
                      'bring_back_loot',
                      action.make_small_script(function='find_target',
                                               kwargs={
                                                   'target': _group['leader'],
                                                   'distance': 5,
                                                   'follow': False
                                               }),
                      player_action=action.make_small_script(
                          function='can_see_target',
                          kwargs={'target_id': _group['leader']}),
                      description='Drop the item off at the camp',
                      delete_on_finish=False)

        jobs.add_task(
            _j,
            '1',
            'flag_item',
            action.make_small_script(function='flag_item_matching',
                                     kwargs={
                                         'matching': {
                                             'type': 'drink'
                                         },
                                         'flag': 'ignore'
                                     }),
            player_action=action.make_small_script(function='always'),
            description='Ignore this',
            delete_on_finish=False)

        jobs.add_task(_j,
                      '2',
                      'drop_item',
                      action.make_small_script(
                          function='drop_item_matching',
                          kwargs={'matching': {
                              'type': 'drink'
                          }}),
                      player_action=action.make_small_script(function='never'),
                      description='Drop the item off at the camp',
                      delete_on_finish=False)

        flag(group_id, 'loot', _j)

    if lfe.ticker(life, 'resource_announce', 10):
        _job_id = get_flag(group_id, 'loot')

        announce(
            life,
            life['group'],
            'job',
            'We need more resources.',
            job_id=_job_id,
            order=True,
            filter_if=[action.make_small_script(function='has_needs_to_meet')])