Create logic.py
Browse files- logic/logic.py +122 -0
logic/logic.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .utils import load_json, save_json
|
| 2 |
+
from .reward import RewardSystem
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
class LearningLogic:
|
| 6 |
+
def __init__(self, data_dir="data"):
|
| 7 |
+
self.data_dir = data_dir
|
| 8 |
+
self.reward_system = RewardSystem()
|
| 9 |
+
self.user_progress = {
|
| 10 |
+
"total_score": 0,
|
| 11 |
+
"completed_lessons": [],
|
| 12 |
+
"quiz_scores": {},
|
| 13 |
+
"exercise_scores": {}
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
def load_lesson(self, day):
|
| 17 |
+
"""بارگذاری محتوای آموزشی"""
|
| 18 |
+
return load_json(f"{self.data_dir}/lesson_day{day}.json")
|
| 19 |
+
|
| 20 |
+
def load_quiz(self, day):
|
| 21 |
+
"""بارگذاری سوالات کوییز"""
|
| 22 |
+
return load_json(f"{self.data_dir}/quiz_day{day}.json")
|
| 23 |
+
|
| 24 |
+
def load_exercise(self, day):
|
| 25 |
+
"""بارگذاری تمرین"""
|
| 26 |
+
return load_json(f"{self.data_dir}/exercise_day{day}.json")
|
| 27 |
+
|
| 28 |
+
def check_quiz_answers(self, day, user_answers):
|
| 29 |
+
"""بررسی پاسخهای کوییز"""
|
| 30 |
+
quiz_data = self.load_quiz(day)
|
| 31 |
+
if not quiz_data:
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
correct_count = 0
|
| 35 |
+
results = []
|
| 36 |
+
|
| 37 |
+
for i, question in enumerate(quiz_data["questions"]):
|
| 38 |
+
is_correct = user_answers[i] == question["correct_index"]
|
| 39 |
+
if is_correct:
|
| 40 |
+
correct_count += 1
|
| 41 |
+
results.append({
|
| 42 |
+
"question_id": question["id"],
|
| 43 |
+
"user_answer": user_answers[i],
|
| 44 |
+
"correct_answer": question["correct_index"],
|
| 45 |
+
"is_correct": is_correct
|
| 46 |
+
})
|
| 47 |
+
|
| 48 |
+
total_questions = len(quiz_data["questions"])
|
| 49 |
+
reward = self.reward_system.calculate_quiz_reward(correct_count, total_questions)
|
| 50 |
+
|
| 51 |
+
# بهروزرسانی پیشرفت کاربر
|
| 52 |
+
self.user_progress["total_score"] += reward
|
| 53 |
+
self.user_progress["quiz_scores"][f"day_{day}"] = {
|
| 54 |
+
"score": reward,
|
| 55 |
+
"correct_answers": correct_count,
|
| 56 |
+
"total_questions": total_questions
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
return {
|
| 60 |
+
"results": results,
|
| 61 |
+
"correct_count": correct_count,
|
| 62 |
+
"total_questions": total_questions,
|
| 63 |
+
"reward": reward,
|
| 64 |
+
"feedback": self.reward_system.get_feedback_message(reward, total_questions * 2)
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
def check_exercise_answer(self, day, user_answer):
|
| 68 |
+
"""بررسی پاسخ تمرین"""
|
| 69 |
+
exercise_data = self.load_exercise(day)
|
| 70 |
+
if not exercise_data:
|
| 71 |
+
return None
|
| 72 |
+
|
| 73 |
+
expected_keywords = exercise_data["exercise"]["expected_keywords"]
|
| 74 |
+
user_answer_lower = user_answer.lower()
|
| 75 |
+
|
| 76 |
+
# بررسی وجود کلمات کلیدی در پاسخ کاربر
|
| 77 |
+
matches = 0
|
| 78 |
+
matched_keywords = []
|
| 79 |
+
|
| 80 |
+
for keyword in expected_keywords:
|
| 81 |
+
if re.search(r'\b' + re.escape(keyword.lower()) + r'\b', user_answer_lower):
|
| 82 |
+
matches += 1
|
| 83 |
+
matched_keywords.append(keyword)
|
| 84 |
+
|
| 85 |
+
is_correct = matches >= len(expected_keywords) * 0.6 # حداقل 60% تطابق
|
| 86 |
+
|
| 87 |
+
reward = self.reward_system.calculate_exercise_reward(
|
| 88 |
+
is_correct, matches, len(expected_keywords)
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# بهروزرسانی پیشرفت کاربر
|
| 92 |
+
self.user_progress["total_score"] += reward
|
| 93 |
+
self.user_progress["exercise_scores"][f"day_{day}"] = {
|
| 94 |
+
"score": reward,
|
| 95 |
+
"is_correct": is_correct,
|
| 96 |
+
"keyword_matches": matches,
|
| 97 |
+
"total_keywords": len(expected_keywords)
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
return {
|
| 101 |
+
"is_correct": is_correct,
|
| 102 |
+
"matched_keywords": matched_keywords,
|
| 103 |
+
"expected_keywords": expected_keywords,
|
| 104 |
+
"reward": reward,
|
| 105 |
+
"feedback": self.reward_system.get_feedback_message(reward, 25)
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
def get_user_progress(self):
|
| 109 |
+
"""دریافت وضعیت پیشرفت کاربر"""
|
| 110 |
+
return self.user_progress
|
| 111 |
+
|
| 112 |
+
def save_progress(self, file_path="user_progress.json"):
|
| 113 |
+
"""ذخیره پیشرفت کاربر"""
|
| 114 |
+
return save_json(self.user_progress, file_path)
|
| 115 |
+
|
| 116 |
+
def load_progress(self, file_path="user_progress.json"):
|
| 117 |
+
"""بارگذاری پیشرفت کاربر"""
|
| 118 |
+
progress = load_json(file_path)
|
| 119 |
+
if progress:
|
| 120 |
+
self.user_progress = progress
|
| 121 |
+
return True
|
| 122 |
+
return False
|