Skip to content

Commit

Permalink
feat: ready to merge
Browse files Browse the repository at this point in the history
  • Loading branch information
AiroPi committed Jan 7, 2025
1 parent 375bee4 commit c2a612e
Show file tree
Hide file tree
Showing 33 changed files with 68 additions and 35 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/build-publish-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build and Publish Docker Image

on:
workflow_dispatch:
push:
branches:
- "master"

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# https://github.com/docker/build-push-action#usage
- name: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: true
target: production
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2 changes: 1 addition & 1 deletion deploy/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ services:
readme-backend:
image: ghcr.io/airopi/readme-backend:master
volumes:
- data:/app/data
- ./data:/app/data
environment:
GITHUB_PROFILE_URL: "https://github.com/AiroPi"
46 changes: 24 additions & 22 deletions python/src/routers/minesweeper_router.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import hashlib
import os
from datetime import datetime
from pathlib import Path

import minesweeper
from fastapi import APIRouter, Response
from fastapi.responses import FileResponse, RedirectResponse
from fastapi.responses import RedirectResponse

GITHUB_PROFILE_URL = os.environ["GITHUB_PROFILE_URL"]
BASE_PATH = Path("./resources/images/minesweeper/png/")
Expand Down Expand Up @@ -35,20 +33,24 @@ def load_image(path: Path):
DIGITS = {i: load_image(BASE_PATH / f"{i}.png") for i in range(9)}


def get_header(etag: str = ""):
def get_header():
return {
"Cache-Control": "no-cache, max-age=0",
# "Last-Modified": datetime.now().strftime("%a, %d %b %Y %H:%M:%S GMT"),
"ETag": etag,
# "ETag": etag,
}


def response_img(img: str):
return RedirectResponse(f"/static/ms/{img}.png")
# return Response("abc")
etag = hashlib.md5(img).hexdigest() # noqa: S324

return Response(img, media_type="image/png", headers=get_header(etag))
def response_img(img: bytes):
"""
Maybe some optimisations could be done here.
I'm pretty sure that the request *could* be cached but without being cached.
Like -> if the content didn't change, don't send it again, otherwise send it.
Also, the tiles are "shared" between the image, so maybe there is a way for the browser to reuse the same image.
I don't want to dig more into this, but it's a thought.
"""
# etag = hashlib.md5(img).hexdigest()
return Response(img, media_type="image/png", headers=get_header())


def redirect_to_github():
Expand All @@ -59,32 +61,32 @@ def redirect_to_github():
def get_board_img(i: int, j: int):
if (i, j) in game.revealed:
if game.board[i][j] == -1:
return response_img("mine_red")
return response_img(str(game.board[i][j]))
return response_img(RED_MINE)
return response_img(DIGITS[game.board[i][j]])
if game.game_over and game.board[i][j] == -1:
return response_img("mine")
return response_img(MINE)
if (i, j) in game.flags:
return response_img("flag")
return response_img("closed")
return response_img(FLAG)
return response_img(CLOSED)


@router.get("/img/{text}")
def get_img(text: str):
match text:
case "flag-toggle":
if flag_mode:
return response_img("flag.png")
return response_img(FLAG)
else:
return response_img("deactivated_flag.png")
return response_img(DEACTIVATED_FLAG)
case "header":
return response_img("header.png")
return response_img(HEADER)
case "undo":
return response_img("undo.png")
return response_img(UNDO)
case "face":
if game.game_over:
return response_img("face_lose.png")
return response_img(FACE_LOSE)
else:
return response_img("face.png")
return response_img(FACE)
case _:
return 404

Expand Down
14 changes: 2 additions & 12 deletions scripts/readme_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def generate_minesweeper_markdown() -> str:
markdown: list[list[str]] = [
[
f'[<img height="50px" src="{BASE_URL}/ms/img/flag-toggle"/>]({BASE_URL}/ms/toggle-flag)',
f'<img height="50px" width="75px" src="{BASE_URL}/ms/img/header"/>',
'<img height="50px" width="75px" src="/static/ms/header"/>',
f'[<img height="50px" src="{BASE_URL}/ms/img/face"/>]({BASE_URL}/ms/reset)',
f'<img height="50px" width="75px" src="{BASE_URL}/ms/img/header"/>',
'<img height="50px" width="75px" src="/static/ms/header"/>',
f'[<img height="50px" src="{BASE_URL}/ms/img/undo"/>]({BASE_URL}/ms/undo)',
]
]
Expand Down Expand Up @@ -51,16 +51,6 @@ def generate_connect4_markdown() -> str:
return " \n".join("".join(row) for row in markdown)


# &nbsp;[1️⃣](https://readme.airopi.dev/connect4/play?column=0)&nbsp;&nbsp;&nbsp;
# [2️⃣](https://readme.airopi.dev/connect4/play?column=1)&nbsp;&nbsp;&nbsp;
# [3️⃣](https://readme.airopi.dev/connect4/play?column=2)&nbsp;&nbsp;&nbsp;
# [4️⃣](https://readme.airopi.dev/connect4/play?column=3)&nbsp;&nbsp;&nbsp;
# [5️⃣](https://readme.airopi.dev/connect4/play?column=4)&nbsp;&nbsp;&nbsp;
# [6️⃣](https://readme.airopi.dev/connect4/play?column=5)&nbsp;&nbsp;&nbsp;
# [7️⃣](https://readme.airopi.dev/connect4/play?column=6)

# <img src="https://readme.airopi.dev/connect4/image" width="240"/>

# Restart : [🔄](https://readme.airopi.dev/connect4/reset)


Expand Down
Binary file removed static/ms/0.png
Binary file not shown.
Binary file removed static/ms/1.png
Binary file not shown.
Binary file removed static/ms/2.png
Binary file not shown.
Binary file removed static/ms/3.png
Binary file not shown.
Binary file removed static/ms/4.png
Binary file not shown.
Binary file removed static/ms/5.png
Binary file not shown.
Binary file removed static/ms/6.png
Binary file not shown.
Binary file removed static/ms/7.png
Binary file not shown.
Binary file removed static/ms/8.png
Binary file not shown.
Binary file removed static/ms/closed.png
Binary file not shown.
Binary file removed static/ms/d0.png
Binary file not shown.
Binary file removed static/ms/d1.png
Binary file not shown.
Binary file removed static/ms/d2.png
Binary file not shown.
Binary file removed static/ms/d3.png
Binary file not shown.
Binary file removed static/ms/d4.png
Binary file not shown.
Binary file removed static/ms/d5.png
Binary file not shown.
Binary file removed static/ms/d6.png
Binary file not shown.
Binary file removed static/ms/d7.png
Binary file not shown.
Binary file removed static/ms/d8.png
Binary file not shown.
Binary file removed static/ms/d9.png
Binary file not shown.
Binary file removed static/ms/deactivated_flag.png
Binary file not shown.
Binary file removed static/ms/dnull.png
Binary file not shown.
Binary file removed static/ms/face.png
Binary file not shown.
Binary file removed static/ms/face_lose.png
Binary file not shown.
Binary file removed static/ms/face_win.png
Binary file not shown.
Binary file removed static/ms/flag.png
Diff not rendered.
Binary file removed static/ms/mine.png
Diff not rendered.
Binary file removed static/ms/mine_red.png
Diff not rendered.
Binary file removed static/ms/undo.png
Diff not rendered.

0 comments on commit c2a612e

Please sign in to comment.