

def parse_packet(packet):
    try:
        _packet = json.loads(packet.strip())
    except:
        logging.error('Debug: Invalid packet.')
        return json.dumps({
            'type': 'text',
            'text': 'Invalid packet from this client.',
        })
    if (_packet['type'] == 'get'):
        if (_packet['what'] == 'stats'):
            _stats = {
                'total_memories': sum([len(l['memory']) for l in LIFE.values()]),
                'active_life': len([l for l in LIFE.values() if (not l['dead'])]),
            }
            return json.dumps(_stats)
        elif (_packet['what'] == 'groups'):
            return json.dumps(WORLD_INFO['groups'])
        elif (_packet['what'] == 'life'):
            _life = LIFE[_packet['value']]
            _knows = {
                
            }
            for entry in _life['know'].values():
                _knows[entry['life']['id']] = {
                    
                }
                for key in entry:
                    if (key == 'heard'):
                        continue
                    if (key == 'life'):
                        _knows[_knows['life']['id']][key] = entry['life']['id']
                        continue
                    _knows[entry['life']['id']][key] = _life['know'][entry['life']['id']][key]
            if _life['job']:
                _job = _life['job']['gist']
            else:
                _job = None
            _sent_life = {
                'name': _life['name'],
                'id': _life['id'],
                'state': _life['state'],
                'flags': _life['flags'],
                'job': _job,
                'group': _life['group'],
                'tempstor': _life['tempstor2'],
                'know': _knows,
                'stats': _life['stats'],
            }
            return json.dumps(_sent_life)
        elif (_packet['what'] == 'memory'):
            _memory = []
            for entry in LIFE[_packet['value']]['memory']:
                if (('question' in entry) and entry['question']):
                    _q = entry.copy()
                    _memory.append(_q)
                    continue
                _memory.append(entry)
            return json.dumps(_memory)
        elif (_packet['what'] == 'life_list'):
            return json.dumps(LIFE.keys())
        elif (_packet['what'] == 'camp_list'):
            return json.dumps(CAMPS.keys())
        elif (_packet['what'] == 'camp'):
            if (not (_packet['value'] in CAMPS)):
                return json.dumps({
                    
                })
            _camp = CAMPS[_packet['value']]
            _send_camp = _camp.copy()
            _send_camp['groups'] = alife.camps.get_controlling_groups(_camp['id'])
            return json.dumps(_send_camp)

dataset/ETHPy150Open flags/Reactor-3/network.py parse_packet/VarMisuse@262/370 `entry`->`_knows`