Skip to content

Commit

Permalink
правки
Browse files Browse the repository at this point in the history
  • Loading branch information
HamletSargsyan committed Jul 22, 2024
1 parent 0011e5b commit 8941362
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 122 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Изменено

- Подсказка о том как использовать команду `/craft`

### Удалено

- Кеширование, из-за изменения результата функции ([#35](https://github.com/HamletSargsyan/livebot/issues/35), [#34](https://github.com/HamletSargsyan/livebot/issues/34), [#33](https://github.com/HamletSargsyan/livebot/issues/33))

## [3.6.4] - 2024-07-22

### Исправлено
Expand Down
54 changes: 36 additions & 18 deletions src/base/player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random
from typing import Any, Callable, NoReturn, Union, List
from datetime import datetime, timedelta
from typing import Any, Callable, NoReturn, TypedDict, Union, List
from datetime import timedelta

from telebot.types import (
Message,
Expand All @@ -26,6 +26,7 @@
get_time_difference_string,
get_item_emoji,
get_user_tag,
utcnow,
)

from database.funcs import BaseDB, database, T as ModelsType
Expand Down Expand Up @@ -185,7 +186,7 @@ def generate_quest(user: UserModel):
quest = QuestModel(
name=item.name,
quantity=quantity,
start_time=datetime.utcnow(),
start_time=utcnow(),
xp=xp,
reward=reward,
owner=user._id,
Expand All @@ -195,24 +196,39 @@ def generate_quest(user: UserModel):
return quest


def get_available_crafts(user: UserModel):
available_crafts = []
class CraftResource(TypedDict):
item_name: str
item_count: int
user_item_quantity: int


class AvailableCraftItem(TypedDict):
item_name: str
resources: list[CraftResource]


def get_available_crafts(user: UserModel) -> list[AvailableCraftItem]:
available_crafts: list[AvailableCraftItem] = []

for item in items_list:
if not item.craft:
continue

craft = item.craft
can_craft = True
required_resources = []
required_resources: List[CraftResource] = []

for craft_item_name, craft_item_count in craft.items():
user_item = get_or_add_user_item(user, craft_item_name)
if (user_item.quantity <= 0) or (user_item.quantity < craft_item_count):
can_craft = False
break
required_resources.append(
(craft_item_name, craft_item_count, user_item.quantity)
{
"item_name": craft_item_name,
"item_count": craft_item_count,
"user_item_quantity": user_item.quantity,
}
)

if can_craft:
Expand All @@ -222,7 +238,9 @@ def get_available_crafts(user: UserModel):

available_crafts = sorted(
available_crafts,
key=lambda x: max(x["resources"], key=lambda y: y[2]), # pyright: ignore
key=lambda x: max(x["resources"], key=lambda y: y["user_item_quantity"])[
"user_item_quantity"
],
reverse=True,
)
return available_crafts
Expand Down Expand Up @@ -458,7 +476,7 @@ def street(call: CallbackQuery, user: UserModel):
)
return

current_time = datetime.utcnow()
current_time = utcnow()

if user.state is None:
user.state = "street"
Expand Down Expand Up @@ -560,7 +578,7 @@ def street(call: CallbackQuery, user: UserModel):

user.xp += xp
user.state = None
user.action_time = datetime.utcnow()
user.action_time = utcnow()

user.hunger += random.randint(2, 5)
user.fatigue += random.randint(3, 8)
Expand Down Expand Up @@ -595,11 +613,11 @@ def work(call: CallbackQuery, user: UserModel):
)
return

current_time = datetime.utcnow()
current_time = utcnow()

if user.state is None:
user.state = "work"
user.action_time = datetime.utcnow() + timedelta(hours=3)
user.action_time = utcnow() + timedelta(hours=3)
database.users.update(**user.to_dict())
elif user.state != "work":
bot.answer_callback_query(call.id, "Ты занят чем то другим", show_alert=True)
Expand Down Expand Up @@ -629,7 +647,7 @@ def work(call: CallbackQuery, user: UserModel):

user.xp += xp
user.state = None
user.action_time = datetime.utcnow()
user.action_time = utcnow()
user.fatigue += random.randint(5, 10)
user.hunger += random.randint(3, 6)
user.mood -= random.randint(3, 6)
Expand All @@ -647,7 +665,7 @@ def work(call: CallbackQuery, user: UserModel):


def sleep(call: CallbackQuery, user: UserModel):
current_time = datetime.utcnow()
current_time = utcnow()

if user.state is None:
user.state = "sleep"
Expand All @@ -673,7 +691,7 @@ def sleep(call: CallbackQuery, user: UserModel):
user.fatigue -= fatigue
user.xp += random.uniform(1.5, 2.0)
user.state = None
user.action_time = datetime.utcnow()
user.action_time = utcnow()
database.users.update(**user.to_dict())

mess = "Охх, хорошенько поспал"
Expand All @@ -682,7 +700,7 @@ def sleep(call: CallbackQuery, user: UserModel):


def game(call: CallbackQuery, user: UserModel):
current_time = datetime.utcnow()
current_time = utcnow()

if user.level < 3:
bot.answer_callback_query(call.id, "Доступно с 3 лвла", show_alert=True)
Expand Down Expand Up @@ -716,7 +734,7 @@ def game(call: CallbackQuery, user: UserModel):
if random.randint(1, 100) < user.luck:
user.mood *= 2
user.state = None
user.action_time = datetime.utcnow()
user.action_time = utcnow()
database.users.update(**user.to_dict())

mess = "Как же хорошо было играть 😊"
Expand All @@ -736,6 +754,6 @@ def generate_daily_gift(user: UserModel):
items = random.choices(items, k=random.randint(1, 3))
daily_gift.items = [item.name for item in items]
daily_gift.is_claimed = False
daily_gift.next_claimable_at = datetime.utcnow() + timedelta(days=1)
daily_gift.next_claimable_at = utcnow() + timedelta(days=1)
database.daily_gifts.update(**daily_gift.to_dict())
return daily_gift
2 changes: 0 additions & 2 deletions src/base/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

from helpers.datatypes import WeatherData
from config import OPENWEATHER_API_KEY, weather_region
from helpers.utils import cached


@cached
def get_weather() -> WeatherData:
url = "http://api.openweathermap.org/data/2.5/weather?"

Expand Down
13 changes: 7 additions & 6 deletions src/bot/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
get_time_difference_string,
get_item,
get_user_tag,
utcnow,
)

from database.models import DogModel
Expand Down Expand Up @@ -70,7 +71,7 @@ def dog_callback(call: CallbackQuery):
return
elif data[1] == "friend":
date = datetime.utcfromtimestamp(call.message.date)
current_time = datetime.utcnow()
current_time = utcnow()
time_difference = current_time - date

if time_difference >= timedelta(minutes=1):
Expand Down Expand Up @@ -154,7 +155,7 @@ def dog_callback(call: CallbackQuery):
mess, call.message.chat.id, call.message.id, reply_markup=markup
)
elif data[1] == "sleep" and dog:
current_time = datetime.utcnow()
current_time = utcnow()
time_difference = current_time - dog.sleep_time
if time_difference <= timedelta(minutes=1):
bot.answer_callback_query(
Expand All @@ -164,7 +165,7 @@ def dog_callback(call: CallbackQuery):
)
return

dog.sleep_time = datetime.utcnow()
dog.sleep_time = utcnow()
time_difference = current_time - dog.sleep_time

bot.answer_callback_query(
Expand All @@ -174,7 +175,7 @@ def dog_callback(call: CallbackQuery):
)
elif data[1] == "wakeup" and dog:
bot.answer_callback_query(call.id, f"{dog.name} проснулся", show_alert=True)
dog.sleep_time = datetime.utcnow()
dog.sleep_time = utcnow()

database.users.update(**user.to_dict())
if dog:
Expand Down Expand Up @@ -251,7 +252,7 @@ def finish_quest_callback(call: CallbackQuery):
"Ты выполнил квест за "
)

total_time: timedelta = datetime.utcnow() - quest.start_time
total_time: timedelta = utcnow() - quest.start_time
mess += get_time_difference_string(total_time)

generate_quest(user)
Expand Down Expand Up @@ -789,7 +790,7 @@ def daily_gift_callback(call: CallbackQuery):
show_alert=True,
)
return
now = datetime.utcnow()
now = utcnow()

daily_gift = database.daily_gifts.get(owner=user._id)
if daily_gift.is_claimed:
Expand Down
Loading

0 comments on commit 8941362

Please sign in to comment.