async def _(request, userId, token): data = request.json.get('data') attributes = data.get('attributes') new_username = attributes.get('new_username') if not new_username: error = Error(title='Update Username Error', detail='Missing new_username attribute.', status=403) return jsonapi({'errors': [error.serialize()]}, status=403) user = await User.find_by_id(userId) if not user: error = Error(title='Update Username Error', detail='User not found.', status=403) return jsonapi({'errors': [error.serialize()]}, status=403) user.username = new_username try: await user.save() except Exception as e: error = Error(title='Update Username Error', detail=str(e), status=403) return jsonapi({'errors': [error.serialize()]}, status=403) return jsonapi({'data': { 'attributes': { 'status': 'Username updated.' } }}, status=200)
async def _(request, userId, token): data = request.json.get('data') attributes = data.get('attributes') current_password = attributes.get('current_password') if not current_password: error = Error(title='Update Password Error', detail='Missing current_password attribute.', status=403) return jsonapi({'errors': [error.serialize()]}, status=403) new_password = attributes.get('new_password') if not new_password: error = Error(title='Update Password Error', detail='Missing new_password attribute.', status=403) return jsonapi({'errors': [error.serialize()]}, status=403) user = await User.find_by_id(userId) if not user: error = Error(title='Update Password Error', detail='User not found.', status=403) return jsonapi({'errors': [error.serialize()]}, status=403) digest = f'hashed-{hashlib.sha256(current_password.encode()).hexdigest()}' if not digest == user.password: error = Error(title='Update Password Error', detail='Current password does not match.', status=403) return jsonapi({'errors': [error.serialize()]}, status=403) user.password = new_password try: await user.save() except Exception as e: error = Error(title='Update Password Error', detail=str(e), status=403) return jsonapi({'errors': [error.serialize()]}, status=403) return jsonapi({'data': { 'attributes': { 'status': 'Password updated.' } }}, status=200)
async def scan(request, longitude, latitude, radius, units, token): redis = await Redis.connect(host='redis://localhost', minsize=1, maxsize=1) result = await redis.georadius('location', longitude, latitude, float(radius), unit=units) oids = [] for key in result: split = key.split(b':') shard = split[0] oid = split[1] oids.append(oid) return jsonapi({'data': {'attributes': {'characters': oids}}})
async def professions(request, token): return jsonapi({ 'data': { 'attributes': WorldCache.professions } })
async def create(request, token): if not token: error = Error(title='Create Character Error', detail='You are not logged in.', status=403) return jsonapi({"errors": [error.serialize()]}, status=403) token = Document(token) data = request.json.get('data') attributes = data.get('attributes') character = await Character.find_one({'profile': token.data.id}) if character: error = Error(title='Create Character Error', detail='You already have a character.', status=403) return jsonapi({"errors": [error.serialize()]}, status=403) try: character = Character(attributes) except: error = Error(title='Create Character Error', detail='Invalid attributes.', status=403) return jsonapi({"errors": [error.serialize()]}, status=403) character.profile = token.data.id if not character.race in WorldCache.races: error = Error(title='Create Character Error', detail='Invalid race.', status=403) return jsonapi({"errors": [error.serialize()]}, status=403) if not len(character.name.first) > 5: error = Error(title='Create Character Error', detail='Invalid first name.', status=403) return jsonapi({"errors": [error.serialize()]}, status=403) character.level = {'current': 1, 'experience': 0, 'next': 1000} character.attributes = WorldCache.races[character.race]['attributes'] character.health = 0 character.state = { 'target': None, 'hostile': False, 'retaliate': False, 'dead': False, 'casting': False } await character.save() return jsonapi({ 'data': { 'attributes': { 'message': 'Character created.', 'url': f'/v1/characters/{character.id}' } } })