-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci cd , record games and players to db (#5)
* fixing deploy ci/cd * SQLITE_DB_FILE_PATH in ci/cd * Poll retract handling * playrooms and games recorded to db
- Loading branch information
1 parent
4d3e8e3
commit b38aa4b
Showing
18 changed files
with
221 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__pycache__ | ||
.venv | ||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: CI/CD with Docker Compose | ||
env: | ||
REPO_NAME: ${{ github.event.repository.name }} | ||
|
||
on: | ||
push: | ||
branches: | ||
- develop | ||
|
||
jobs: | ||
build: | ||
runs-on: [self-hosted] | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
- name: executing remote ssh commands using password | ||
uses: appleboy/ssh-action@master | ||
with: | ||
host: ${{ secrets.HOST }} | ||
username: ${{ secrets.USERNAME }} | ||
password: ${{ secrets.PASSWORD }} | ||
key: ${{ secrets.PRIVATE_KEY }} | ||
script: | | ||
cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} | ||
docker compose build --no-cache | ||
deploy: | ||
needs: [build] | ||
runs-on: [self-hosted] | ||
steps: | ||
- name: SSH into production server and deploy | ||
uses: appleboy/ssh-action@master | ||
env: | ||
TELEGRAM_BOT_TOKEN: ${{ secrets.DEV_TELEGRAM_BOT_TOKEN }} | ||
DEVELOPER_CHAT_ID: ${{ secrets.DEVELOPER_CHAT_ID }} | ||
SQLITE_DB_FILE_PATH: ${{ vars.SQLITE_DB_FILE_PATH }} | ||
with: | ||
host: ${{ secrets.HOST }} | ||
username: ${{ secrets.USERNAME }} | ||
password: ${{ secrets.PASSWORD }} | ||
key: ${{ secrets.PRIVATE_KEY }} | ||
script: | | ||
cd ./_work/${{ env.REPO_NAME }}/${{ env.REPO_NAME }} | ||
docker compose down | ||
docker compose up -d | ||
docker compose ps | ||
envs: TELEGRAM_BOT_TOKEN,DEVELOPER_CHAT_ID,SQLITE_DB_FILE_PATH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,40 @@ | ||
import logging | ||
|
||
from telegram import Update | ||
from telegram.ext import ContextTypes | ||
|
||
from src.data_models.Player import Player | ||
from src.services.db_service import save_player | ||
|
||
|
||
async def receive_poll_answer( | ||
update: Update, context: ContextTypes.DEFAULT_TYPE | ||
) -> None: | ||
# TODO save polls to db and query them instead saving them to bot_data | ||
"""Summarize a users poll vote""" | ||
answer = update.poll_answer | ||
if not answer.option_ids: | ||
return # It's retake poll action, ignore it | ||
if not answer.option_ids: # Poll retract, delete previous vote | ||
del context.bot_data[answer.poll_id]["results"][update.effective_user.id] | ||
return | ||
if context.bot_data: | ||
answered_poll = context.bot_data[answer.poll_id] | ||
user_id = update.effective_user.id | ||
result = answered_poll["questions"][answer.option_ids[0]] | ||
context.bot_data[answer.poll_id]["results"][user_id] = result | ||
await save_player( | ||
Player( | ||
telegram_user_id=user_id, | ||
username=update.effective_user.username, | ||
first_name=update.effective_user.first_name, | ||
full_name=update.effective_user.full_name, | ||
last_name=update.effective_user.last_name, | ||
is_bot=update.effective_user.is_bot, | ||
language_code=update.effective_user.language_code, | ||
) | ||
) | ||
else: | ||
# failed to save poll answer. | ||
# Ussually happens for polls that are sent to the bot before it started and not updated | ||
# TODO save polls to db and query them instead saving them to bot_data | ||
return | ||
logging.error( | ||
"Failed to save poll answer. Usually happens for polls that are sent to the bot before it " | ||
"started and not updated", | ||
exc_info=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
from datetime import datetime | ||
from typing import Literal | ||
|
||
from pydantic import BaseModel | ||
from pydantic import BaseModel, field_validator | ||
|
||
|
||
class Game(BaseModel): | ||
playroom_id: int | ||
end_time: datetime | ||
result: Literal["Hitler Canceler", "Fascist Law", "Hitler Death", "Liberal Law"] | ||
poll_id: int | ||
chat_id: int | ||
results: dict # Literal["Hitler Canceler", "Fascist Law", "Hitler Death", "Liberal Law"] | ||
creator_id: int | ||
|
||
@field_validator("results", mode="after") | ||
@classmethod | ||
def validate_results(cls, v: dict) -> Literal["CH", "DH", "FW", "LW"]: | ||
outcomes = set(v.values()) | ||
if "I'm Canceler Hitler" in outcomes: | ||
return "CH" | ||
if "I'm Dead Hitler" in outcomes: | ||
return "DH" | ||
if "I'm Liberal Winner" in outcomes: | ||
return "LW" | ||
if "I'm Fascistic Winner" in outcomes: | ||
return "FW" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
from pydantic import BaseModel, field_validator | ||
|
||
|
||
class Player(BaseModel): | ||
telegram_user_id: int | ||
username: str | ||
first_name: Optional[str] = None | ||
last_name: Optional[str] = None | ||
is_bot: bool = False | ||
first_name: Optional[str | None] | ||
full_name: Optional[str | None] | ||
last_name: Optional[str | None] | ||
is_bot: Optional[bool] = False | ||
language_code: Optional[str | None] | ||
|
||
@field_validator("is_bot", mode="after") | ||
@classmethod | ||
def validate_bot(cls, v: bool) -> str: | ||
return "TRUE" if v else "FALSE" # sqlite3 does not support boolean type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.