RewardApp / trash /logic /logic.py
Mhdeusi's picture
Rename logic/logic.py to trash/logic/logic.py
466b02c verified
from .utils import load_json, save_json
from .reward import RewardSystem
import re
class LearningLogic:
def __init__(self, data_dir="data"):
self.data_dir = data_dir
self.reward_system = RewardSystem()
self.user_progress = {
"total_score": 0,
"completed_lessons": [],
"quiz_scores": {},
"exercise_scores": {}
}
def load_lesson(self, day):
"""بارگذاری محتوای آموزشی"""
return load_json(f"{self.data_dir}/lesson_day{day}.json")
def load_quiz(self, day):
"""بارگذاری سوالات کوییز"""
return load_json(f"{self.data_dir}/quiz_day{day}.json")
def load_exercise(self, day):
"""بارگذاری تمرین"""
return load_json(f"{self.data_dir}/exercise_day{day}.json")
def check_quiz_answers(self, day, user_answers):
"""بررسی پاسخ‌های کوییز"""
quiz_data = self.load_quiz(day)
if not quiz_data:
return None
correct_count = 0
results = []
for i, question in enumerate(quiz_data["questions"]):
is_correct = user_answers[i] == question["correct_index"]
if is_correct:
correct_count += 1
results.append({
"question_id": question["id"],
"user_answer": user_answers[i],
"correct_answer": question["correct_index"],
"is_correct": is_correct
})
total_questions = len(quiz_data["questions"])
reward = self.reward_system.calculate_quiz_reward(correct_count, total_questions)
# به‌روزرسانی پیشرفت کاربر
self.user_progress["total_score"] += reward
self.user_progress["quiz_scores"][f"day_{day}"] = {
"score": reward,
"correct_answers": correct_count,
"total_questions": total_questions
}
return {
"results": results,
"correct_count": correct_count,
"total_questions": total_questions,
"reward": reward,
"feedback": self.reward_system.get_feedback_message(reward, total_questions * 2)
}
def check_exercise_answer(self, day, user_answer):
"""بررسی پاسخ تمرین"""
exercise_data = self.load_exercise(day)
if not exercise_data:
return None
expected_keywords = exercise_data["exercise"]["expected_keywords"]
user_answer_lower = user_answer.lower()
# بررسی وجود کلمات کلیدی در پاسخ کاربر
matches = 0
matched_keywords = []
for keyword in expected_keywords:
if re.search(r'\b' + re.escape(keyword.lower()) + r'\b', user_answer_lower):
matches += 1
matched_keywords.append(keyword)
is_correct = matches >= len(expected_keywords) * 0.6 # حداقل 60% تطابق
reward = self.reward_system.calculate_exercise_reward(
is_correct, matches, len(expected_keywords)
)
# به‌روزرسانی پیشرفت کاربر
self.user_progress["total_score"] += reward
self.user_progress["exercise_scores"][f"day_{day}"] = {
"score": reward,
"is_correct": is_correct,
"keyword_matches": matches,
"total_keywords": len(expected_keywords)
}
return {
"is_correct": is_correct,
"matched_keywords": matched_keywords,
"expected_keywords": expected_keywords,
"reward": reward,
"feedback": self.reward_system.get_feedback_message(reward, 25)
}
def get_user_progress(self):
"""دریافت وضعیت پیشرفت کاربر"""
return self.user_progress
def save_progress(self, file_path="user_progress.json"):
"""ذخیره پیشرفت کاربر"""
return save_json(self.user_progress, file_path)
def load_progress(self, file_path="user_progress.json"):
"""بارگذاری پیشرفت کاربر"""
progress = load_json(file_path)
if progress:
self.user_progress = progress
return True
return False