Advertisement
Guest User

api бота

a guest
Aug 25th, 2022
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. from django.contrib.staticfiles.storage import staticfiles_storage
  2. from django.views.decorators.csrf import csrf_exempt
  3. from django.http import JsonResponse, FileResponse, HttpResponse
  4. from json import load
  5. from ast import literal_eval
  6. from PIL import Image, ImageDraw, ImageFont
  7. from webcolors import hex_to_rgb
  8.  
  9.  
  10. def get_data(request, game_set):
  11.  
  12.     if request.method != 'GET':
  13.         data = {'error': 403}
  14.         return JsonResponse(data, status=403, content_type='application/json', safe=False)
  15.  
  16.     try:
  17.         file_path = staticfiles_storage.path('maps/' + game_set + '.json')
  18.         data = open(file_path)
  19.         data = load(data)
  20.         return JsonResponse(data, status=200, content_type='application/json', safe=False)
  21.  
  22.     except FileNotFoundError:
  23.         data = {'error': 404}
  24.         return JsonResponse(data, status=404, content_type='application/json', safe=False)
  25.  
  26.  
  27. def get_map(request, game_set):
  28.  
  29.     if request.method != 'GET':
  30.         data = {'error': 403}
  31.         return JsonResponse(data, status=403, content_type='application/json', safe=False)
  32.  
  33.     try:
  34.         file_path = staticfiles_storage.path('maps/' + game_set + '.png')
  35.         file = open(file_path, 'rb')
  36.         return FileResponse(file)
  37.  
  38.     except FileNotFoundError:
  39.         data = {'error': 404}
  40.         return JsonResponse(data, status=404, content_type='application/json', safe=False)
  41.  
  42.  
  43. @csrf_exempt
  44. def render_map(request, game_set):
  45.  
  46.     if request.method != 'POST':
  47.         data = {'error': 403}
  48.         return JsonResponse(data, status=403, content_type='application/json', safe=False)
  49.  
  50.     try:
  51.         file_path = staticfiles_storage.path('maps/' + game_set + '.png')
  52.         map_file = Image.open(file_path)
  53.  
  54.         file_path = staticfiles_storage.path('maps/' + game_set + '.json')
  55.         map_data = open(file_path)
  56.         map_data = load(map_data)
  57.         request_data = literal_eval(request.body.decode())
  58.  
  59.         for tile in request_data['tiles']:
  60.             cords = next(t for t in map_data['tiles'] if t['id'] == tile['id'])
  61.             cords = (cords['x'], cords['y'])
  62.             color = hex_to_rgb(tile['color'])
  63.             color = (color.red, color.green, color.blue, 255)
  64.             ImageDraw.floodfill(map_file, cords, color)
  65.  
  66.         i = 0
  67.         font = ImageFont.truetype(request_data['font']['name'], request_data['font']['size'])
  68.         for player in request_data['players']:
  69.             slot = map_data['players_slots'][i]
  70.  
  71.             fill_cords = (slot['fill_cords']['x'], slot['fill_cords']['y'])
  72.             color = hex_to_rgb(player['color'])
  73.             color = (color.red, color.green, color.blue, 255)
  74.  
  75.             ImageDraw.floodfill(map_file, fill_cords, color)
  76.             name_cords = (slot['name_cords']['x'], slot['name_cords']['y'])
  77.             ImageDraw.Draw(map_file).text(name_cords, player['name'], fill='#000000', font=font)
  78.  
  79.             i += 1
  80.             if i >= len(map_data['players_slots']):
  81.                 break
  82.  
  83.         response = HttpResponse(content_type='image/png')
  84.         map_file.save(response, "PNG")
  85.  
  86.         return response
  87.  
  88.     except FileNotFoundError:
  89.         data = {'error': 404}
  90.         return JsonResponse(data, status=404, content_type='application/json', safe=False)
  91.  
  92.     except Exception as error:
  93.         data = {'error': str(error)}
  94.         return JsonResponse(data, status=500, content_type='application/json', safe=False)
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement