Refactor to include avatar service

This commit is contained in:
Brandon Sturgeon 2021-02-16 01:01:42 -08:00 committed by Brandon Sturgeon
parent 92a93624ee
commit 9374550419
19 changed files with 153 additions and 3 deletions

View File

@ -52,7 +52,7 @@ ChatTransit.ReceiveMessage = (ply, text, teamChat) =>
teamName = ply\Team!
rankColor = @GetTeamColorCode teamName
avatar = ply.PlayerSummary.response.players[1].avatar
avatar = ply.PlayerSummary.response.players[1].avatarfull
steamName = ply\Nick!
steamId = ply\SteamID64!
irisId = "none"

View File

@ -0,0 +1,41 @@
import TableToJSON from util
HTTP = HTTP
AvatarServiceAddress = file.Read "cfc/cfc_avatar_service_address.txt", "DATA"
AvatarServiceAddress = string.Replace AvatarServiceAddress, "\r", ""
AvatarServiceAddress = string.Replace AvatarServiceAddress, "\n", ""
class AvatarService
new: (@Logger) =>
sendAvatar: (avatarUrl, success, failed) =>
HTTP
:success
:failed
url: "http://#{AvatarServiceAddress}/outline"
method: "POST"
type: "application/json"
body: TableToJSON :avatarUrl
setOutlinedAvatar: (ply, avatarUrl) =>
data = ply.response.players[1]
data.originalAvatarFull or= data.avatarfull
data.avatarfull = avatarUrl
outlineAvatar: (ply, data) =>
avatar = data.response.players[1].avatarfull
success = (code, body) -> setOutlinedAvatar ply, response
failed = (err) -> @Logger\error err
@sendAvatar avatar, success, failed
ChatTransit.AvatarService = AvatarService ChatTransit.Logger
hook.Add "CFC_SteamLookup_SuccessfulPlayerData", "CFC_ChatTransit_AvatarService", (dataName, ply, data) ->
return unless dataName == "PlayerSummary"
ChatTransit.AvatarService\outlineAvatar ply, data
return nil

View File

@ -0,0 +1,12 @@
FROM python:3.9.0
RUN mkdir /avatar-service
WORKDIR /avatar-service
COPY . /avatar-service
RUN pip install -U --upgrade pip
RUN pip install --upgrade -r ./requirements.txt
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]

View File

@ -0,0 +1,32 @@
worker_processes 1;
pid /run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
http {
sendfile on;
sendfile_max_chunk 10m;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 25;
types_hash_max_size 2048;
open_file_cache max=100 inactive=300s;
open_file_cache_valid 360s;
open_file_cache_min_uses 1;
open_file_cache_errors off;
server {
listen 80;
location /avatars/ {
root /usr/share/nginx/html;
autoindex on;
}
}
}

View File

@ -0,0 +1 @@
yeet

View File

@ -0,0 +1,25 @@
version: "3.8"
services:
nginx:
image: nginx:1.19.6-alpine
ports:
- "$API_PORT:80"
volumes:
- ./avatars.conf:/etc/nginx/nginx.conf
- ./avatars:/usr/share/nginx/html/avatars
service:
build:
context: .
dockerfile: Dockerfile.service
command: flask run --host 0.0.0.0 --port 8080
container_name: "${REALM}_chat_transit_avatar_service"
ports:
- "$PORT:8080"
environment:
FLASK_APP: "service.py"
env_file:
- .env
volumes:
- ./:/build/src
- ./avatars:/avatars

View File

@ -0,0 +1,7 @@
#!/bin/bash
set -e
pip install --upgrade -U --upgrade pip
pip install --upgrade --upgrade -r requirements.txt
exec "$@"

View File

@ -0,0 +1,3 @@
Pillow
requests
flask

View File

@ -0,0 +1,29 @@
from PIL import Image, ImageDraw
from flask import Flask, request
import requests
app = Flask(__name__)
@app.route("/outline", methods=["POST"])
def outline():
# TODO:
# - Convert received image to RGBA
# - Figure out the circle drawing
# - Figure out how to convert the color to R,G,B,A
# - Place image in directory with a uuid or something, respond with full link to that file
content = request.json
image_url = content["avatarUrl"]
outline_color = content["outlineColor"]
avatar = Image.open(requests.get(image_url, stream=True).raw)
x, y = avatar.size
eX, eY = 30, 60 #Size of Bounding Box for ellipse
bbox = (x/2 - eX/2, y/2 - eY/2, x/2 + eX/2, y/2 + eY/2)
draw = ImageDraw.Draw(avatar)
draw.ellipse(bbox, fill=(255,255,255,0), outline=(255,0,0,255), width=25)
del draw

View File

@ -2,10 +2,10 @@ version: "3.8"
services:
web:
build: .
container_name: "${REALM}_chat_transit_web"
container_name: "${REALM}_chat_transit_discord_relay"
ports:
- "$PORT:8080"
env_file:
- .env
- ../.env
volumes:
- ./:/build/src