Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.contrib.staticfiles.storage import staticfiles_storage
- from django.views.decorators.csrf import csrf_exempt
- from django.http import JsonResponse, FileResponse, HttpResponse
- from json import load
- from ast import literal_eval
- from PIL import Image, ImageDraw, ImageFont
- from webcolors import hex_to_rgb
- def get_data(request, game_set):
- if request.method != 'GET':
- data = {'error': 403}
- return JsonResponse(data, status=403, content_type='application/json', safe=False)
- try:
- file_path = staticfiles_storage.path('maps/' + game_set + '.json')
- data = open(file_path)
- data = load(data)
- return JsonResponse(data, status=200, content_type='application/json', safe=False)
- except FileNotFoundError:
- data = {'error': 404}
- return JsonResponse(data, status=404, content_type='application/json', safe=False)
- def get_map(request, game_set):
- if request.method != 'GET':
- data = {'error': 403}
- return JsonResponse(data, status=403, content_type='application/json', safe=False)
- try:
- file_path = staticfiles_storage.path('maps/' + game_set + '.png')
- file = open(file_path, 'rb')
- return FileResponse(file)
- except FileNotFoundError:
- data = {'error': 404}
- return JsonResponse(data, status=404, content_type='application/json', safe=False)
- @csrf_exempt
- def render_map(request, game_set):
- if request.method != 'POST':
- data = {'error': 403}
- return JsonResponse(data, status=403, content_type='application/json', safe=False)
- try:
- file_path = staticfiles_storage.path('maps/' + game_set + '.png')
- map_file = Image.open(file_path)
- file_path = staticfiles_storage.path('maps/' + game_set + '.json')
- map_data = open(file_path)
- map_data = load(map_data)
- request_data = literal_eval(request.body.decode())
- for tile in request_data['tiles']:
- cords = next(t for t in map_data['tiles'] if t['id'] == tile['id'])
- cords = (cords['x'], cords['y'])
- color = hex_to_rgb(tile['color'])
- color = (color.red, color.green, color.blue, 255)
- ImageDraw.floodfill(map_file, cords, color)
- i = 0
- font = ImageFont.truetype(request_data['font']['name'], request_data['font']['size'])
- for player in request_data['players']:
- slot = map_data['players_slots'][i]
- fill_cords = (slot['fill_cords']['x'], slot['fill_cords']['y'])
- color = hex_to_rgb(player['color'])
- color = (color.red, color.green, color.blue, 255)
- ImageDraw.floodfill(map_file, fill_cords, color)
- name_cords = (slot['name_cords']['x'], slot['name_cords']['y'])
- ImageDraw.Draw(map_file).text(name_cords, player['name'], fill='#000000', font=font)
- i += 1
- if i >= len(map_data['players_slots']):
- break
- response = HttpResponse(content_type='image/png')
- map_file.save(response, "PNG")
- return response
- except FileNotFoundError:
- data = {'error': 404}
- return JsonResponse(data, status=404, content_type='application/json', safe=False)
- except Exception as error:
- data = {'error': str(error)}
- return JsonResponse(data, status=500, content_type='application/json', safe=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement