Compare commits

..

No commits in common. "fd9792aa9583ab960f9697f318c74fa7467c49b6" and "f9ac8c343b01e511f9ac582b3c832f20a2602aa2" have entirely different histories.

74 changed files with 20 additions and 1519 deletions

View File

@ -1,25 +0,0 @@
[tool.poetry]
name = "servicemanager"
version = "0.0.1"
description = "Computer equipment prevention and maintenance management system"
authors = ["Sergey Vanyushkin <pi3c@yandex.ru>"]
license = "MIT"
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.10"
fastapi = {extras = ["all"], version = "^0.108.0"}
SQLAlchemy = "^2.0.25"
asyncpg = "^0.29.0"
pydantic-settings = "^2.1.0"
uvicorn = "^0.25.0"
[tool.poetry.group.dev.dependencies]
pytest = "^7.4.4"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
app = "servicemanager.main:run_app"

View File

@ -1,4 +0,0 @@
[pytest]
pythonpath = . servicemanager
; env_files =
; .test.env

View File

@ -1,11 +0,0 @@
from fastapi import APIRouter
api_router = APIRouter()
@api_router.get("/ping")
async def pong():
"""Тестовый роут"""
return {"ping": "pong!"}

View File

@ -1,21 +0,0 @@
import os
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
site_router = APIRouter()
print(os.path.abspath(os.curdir))
site_router.mount("/static", StaticFiles(directory="servicemanager/frontend/static"), name="static")
templates = Jinja2Templates(directory="servicemanager/frontend/templates")
@site_router.get("/testpage", response_class=HTMLResponse)
async def read_item(request: Request):
return templates.TemplateResponse(
request=request, name="test.html"
)

View File

@ -1,8 +0,0 @@
<html>
<head>
<title>test page</title>
</head>
<body>
<h1>test_data</h1>
</body>
</html>

View File

@ -1,44 +0,0 @@
import asyncio
import sys
from fastapi import FastAPI
import uvicorn
from api.routes import api_router
from frontend.routes import site_router
async def generate_test_data():
"""
Создание БД и наполнение ее данными
"""
print('generating data')
def create_app():
"""
Создание экземпляра приложения FastAPI и врзврат его
"""
app = FastAPI()
app.include_router(api_router)
app.include_router(site_router)
return app
def run_app():
"""
Запуск локального вебсервера для тестов и проверки
"""
uvicorn.run(
app="main:create_app",
reload=True,
factory=True,
)
if __name__ == "__main__":
if "--configure" in sys.argv:
asyncio.run(generate_test_data())
if "--webserver" in sys.argv:
run_app()

View File

@ -1,20 +0,0 @@
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
DB_HOST: str
DB_PORT: int
DB_USER: str
DB_PASS: str
DB_NAME: str
@property
def DATABASE_URL_asyncpg(self):
"""
Возвращает строку подключения к БД необходимую для SQLAlchemy
"""
return f"postgresql+asyncpg://{self.DB_USER}:{self.DB_PASS}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
model_config = SettingsConfigDict(env_file=".env")
settings = Settings()

View File

@ -1,10 +0,0 @@
from starlette.testclient import TestClient
from servicemanager.main import create_app
client = TestClient(create_app())
def test_testpage():
response = client.get("/testpage")
assert response.status_code == 200

View File

@ -1,11 +0,0 @@
from starlette.testclient import TestClient
from servicemanager.main import create_app
client = TestClient(create_app())
def test_ping():
response = client.get("/ping")
assert response.status_code == 200
assert response.json() == {"ping": "pong!"}

View File

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 6.3 KiB

View File

@ -2,7 +2,8 @@ import os
import time import time
import pygame as pg import pygame as pg
import pygame_menu from pygame.key import name
import pygame_menu as pgm
from .snake import Snake from .snake import Snake
from .food import Food from .food import Food
@ -27,30 +28,27 @@ class Game:
self.menu = None self.menu = None
self.snake = Snake() self.snake = Snake()
self.foods_array = [] self.foods_array = []
self.game_speed = 0
def game_init(self): def game_init(self):
pg.init() pg.init()
def get_menu(self): def get_menu(self):
self.set_bg() self.set_bg()
self.menu = pygame_menu.Menu('Snakessss game', 400, 300, theme=pygame_menu.themes.THEME_BLUE) self.menu = pgm.Menu('Snakessss game', 400, 300, theme=pgm.pygame_menu.themes.THEME_DARK)
self.menu.add.button('Start new game', self.start_new_game) self.menu.add.button('Start new game', self.start_new_game)
if self.started: if self.started:
self.menu.add.button('Restore game', self.restore_game) self.menu.add.button('Restore game', self.restore_game)
self.menu.add.selector('Speed', [('Slow', 0), ('Medium', 1), ('Fast', 2)], onchange=self.set_speed, onreturn=self.restore_game) self.menu.add.selector('Speed', [('Slow', 40), ('Medium', 25), ('Fast', 10)], onchange=self.set_speed)
self.menu._widgets[-1].set_value(self.game_speed) self.menu.add.button('Exit', pgm.pygame_menu.events.EXIT)
self.menu.add.button('Exit', pygame_menu.events.EXIT)
self.menu.mainloop(self.screen) self.menu.mainloop(self.screen)
def disable_menu(self): def disable_menu(self):
if self.menu is not None: self.menu.disable()
self.menu.disable()
def set_speed(self, tuple_celected, difficulty): def set_speed(self, value, difficulty):
self.game_speed = difficulty self.snake.speed = difficulty
self.snake.set_speed(speed=self.game_speed)
def set_title(self): def set_title(self):
pg.display.set_caption(self.title) pg.display.set_caption(self.title)
@ -58,19 +56,14 @@ class Game:
self.screen.blit(self.bg, (0, 0)) self.screen.blit(self.bg, (0, 0))
def start_new_game(self): def start_new_game(self):
print(self.game_speed, self.snake.speed)
self.started = True self.started = True
self.paused = False self.paused = False
self.disable_menu() self.disable_menu()
self.need_reset = True self.need_reset = True
def restore_game(self, *args, **kwargs): def restore_game(self):
if self.started: self.disable_menu()
self.disable_menu() self.paused = False
self.paused = False
else:
self.start_new_game()
def save_game(self): def save_game(self):
pass pass
@ -79,14 +72,10 @@ class Game:
pass pass
def gameover(self): def gameover(self):
surf = self.font.render('GAME OVER', True, (20, 20, 20)) surf = self.font.render('Game over', True, (20, 20, 20))
rect = surf.get_rect() rect = surf.get_rect()
score = self.font.render(f'Score: {self.score}', True, (20, 20, 20))
rect2 = score.get_rect()
rect.midtop = (390, 250) rect.midtop = (390, 250)
rect2.midtop = (390, 320)
self.screen.blit(surf, rect) self.screen.blit(surf, rect)
self.screen.blit(score, rect2)
pg.display.flip() pg.display.flip()
time.sleep(3) time.sleep(3)
@ -100,7 +89,6 @@ class Game:
for f in self.foods_array: for f in self.foods_array:
if self.snake.get_head_coords() == f.get_coords(): if self.snake.get_head_coords() == f.get_coords():
self.score += 5 * self.snake.length
self.foods_array.remove(f) self.foods_array.remove(f)
self.snake.length += 1 self.snake.length += 1
@ -111,24 +99,23 @@ class Game:
pg.display.update() pg.display.update()
if self.snake.impacted: if self.snake.impacted:
self.started = False
self.gameover() self.gameover()
self.started = False
def mainloop(self): def mainloop(self):
while not self.done: while not self.done:
if self.need_reset: if self.need_reset:
self.snake.reset(speed=self.game_speed) self.snake.reset()
self.start_new_game() self.start_new_game()
self.need_reset = False self.need_reset = False
for event in pg.event.get(): for event in pg.event.get():
if event.type == pg.QUIT: if event.type == pg.QUIT:
self.done = True self.done = True
pressed = pg.key.get_pressed() pressed = pg.key.get_pressed()
if pressed[pg.K_ESCAPE]: if pressed[pg.K_ESCAPE]:
self.paused = False if self.paused else True self.paused = False if self.paused else True
if self.paused: if self.paused:
@ -137,15 +124,12 @@ class Game:
if pressed[pg.K_UP]: if pressed[pg.K_UP]:
if self.snake.direction != 'down': if self.snake.direction != 'down':
self.snake.direction = 'up' self.snake.direction = 'up'
if pressed[pg.K_DOWN]: if pressed[pg.K_DOWN]:
if self.snake.direction != 'up': if self.snake.direction != 'up':
self.snake.direction = 'down' self.snake.direction = 'down'
if pressed[pg.K_LEFT]: if pressed[pg.K_LEFT]:
if self.snake.direction != 'right': if self.snake.direction != 'right':
self.snake.direction = 'left' self.snake.direction = 'left'
if pressed[pg.K_RIGHT]: if pressed[pg.K_RIGHT]:
if self.snake.direction != 'left': if self.snake.direction != 'left':
self.snake.direction = 'right' self.snake.direction = 'right'

View File

Before

Width:  |  Height:  |  Size: 1013 KiB

After

Width:  |  Height:  |  Size: 1013 KiB

View File

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

View File

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View File

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@ -24,16 +24,13 @@ class Snake:
self.direction = 'right' self.direction = 'right'
self.direction_prev = 'right' self.direction_prev = 'right'
self.length = 2 self.length = 10
self.speed = 30 # скорость движения змейки чем меньше, тем быстрее self.speed = 40 # скорость движения змейки чем меньше, тем быстрее
self.counter = 0 # Просто счетчик, для регулировки скорости нужен self.counter = 0 # Просто счетчик, для регулировки скорости нужен
self.body = kwargs.get('body', [('h', 60, 300), ('h', 30, 300)]) self.body = kwargs.get('body', [('h', 60, 300), ('h', 30, 300)])
self.impacted = False self.impacted = False
def set_speed(self, **kwargs): def reset(self):
self.speed = (30, 20, 10)[kwargs.get('speed', 30)]
def reset(self, **kwargs):
self.x = 90 self.x = 90
self.y = 300 self.y = 300
self.body = [('h', 60, 300), ('h', 30, 300)] self.body = [('h', 60, 300), ('h', 30, 300)]
@ -41,7 +38,7 @@ class Snake:
self.direction_prev = 'right' self.direction_prev = 'right'
self.impacted = False self.impacted = False
self.length = 2 self.length = 2
self.set_speed(**kwargs) self.speed = 40
def get_head_coords(self): def get_head_coords(self):
return self.x, self.y return self.x, self.y