Update app.py
Browse files
app.py
CHANGED
@@ -2370,6 +2370,137 @@ def show_main_interface():
|
|
2370 |
# Save options
|
2371 |
show_save_options()
|
2372 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2373 |
# === 6. MAIN APPLICATION LOGIC ===
|
2374 |
def main():
|
2375 |
try:
|
|
|
2370 |
# Save options
|
2371 |
show_save_options()
|
2372 |
|
2373 |
+
def show_completion_options():
|
2374 |
+
"""Display options and summary when story is completed"""
|
2375 |
+
try:
|
2376 |
+
# Show completion celebration
|
2377 |
+
st.balloons()
|
2378 |
+
|
2379 |
+
# Create completion message
|
2380 |
+
st.markdown("""
|
2381 |
+
<div style="
|
2382 |
+
background-color: #e8f5e9;
|
2383 |
+
padding: 20px;
|
2384 |
+
border-radius: 10px;
|
2385 |
+
text-align: center;
|
2386 |
+
margin: 20px 0;
|
2387 |
+
border: 2px solid #4caf50;
|
2388 |
+
">
|
2389 |
+
<h2 style="color: #2e7d32; margin-bottom: 15px;">
|
2390 |
+
🎉 ยินดีด้วย! คุณเขียนเรื่องราวจบสมบูรณ์แล้ว
|
2391 |
+
</h2>
|
2392 |
+
<p style="color: #1b5e20;">
|
2393 |
+
เรื่องราวของคุณจบลงด้วย: {st.session_state.ending_type}
|
2394 |
+
</p>
|
2395 |
+
</div>
|
2396 |
+
""", unsafe_allow_html=True)
|
2397 |
+
|
2398 |
+
# Show story statistics
|
2399 |
+
st.markdown("### 📊 สรุปเรื่องราว")
|
2400 |
+
col1, col2 = st.columns(2)
|
2401 |
+
|
2402 |
+
with col1:
|
2403 |
+
st.metric(
|
2404 |
+
"จำนวนประโยคทั้งหมด",
|
2405 |
+
len(st.session_state.story),
|
2406 |
+
help="จำนวนประโยคในเรื่องทั้งหมด"
|
2407 |
+
)
|
2408 |
+
st.metric(
|
2409 |
+
"คำศัพท์ที่ใช้",
|
2410 |
+
len(st.session_state.stats['vocabulary_used']),
|
2411 |
+
help="จำนวนคำศัพท์ที่ไม่ซ้ำกัน"
|
2412 |
+
)
|
2413 |
+
|
2414 |
+
with col2:
|
2415 |
+
st.metric(
|
2416 |
+
"คะแนนรวม",
|
2417 |
+
st.session_state.points['total'],
|
2418 |
+
help="คะแนนรวมที่ได้รับ"
|
2419 |
+
)
|
2420 |
+
st.metric(
|
2421 |
+
"ความแม่นยำ",
|
2422 |
+
f"{st.session_state.stats['accuracy_rate']:.1f}%",
|
2423 |
+
help="อัตราการเขียนถูกต้อง"
|
2424 |
+
)
|
2425 |
+
|
2426 |
+
# Show action buttons
|
2427 |
+
st.markdown("### 🎯 ต้องการทำอะไรต่อ?")
|
2428 |
+
col1, col2 = st.columns(2)
|
2429 |
+
|
2430 |
+
with col1:
|
2431 |
+
if st.button("💾 บันทึกเรื่องราว", use_container_width=True):
|
2432 |
+
show_save_dialog()
|
2433 |
+
|
2434 |
+
with col2:
|
2435 |
+
if st.button("🔄 เริ่มเรื่องใหม่", use_container_width=True):
|
2436 |
+
reset_story_with_confirmation()
|
2437 |
+
|
2438 |
+
except Exception as e:
|
2439 |
+
logging.error(f"Error showing completion options: {str(e)}")
|
2440 |
+
st.error("เกิดข้อผิดพลาดในการแสดงตัวเลือกหลังจบเรื่อง")
|
2441 |
+
|
2442 |
+
def show_save_dialog():
|
2443 |
+
"""Display save options dialog"""
|
2444 |
+
try:
|
2445 |
+
st.markdown("### 💾 บันทึกเรื่องราว")
|
2446 |
+
|
2447 |
+
col1, col2 = st.columns(2)
|
2448 |
+
|
2449 |
+
with col1:
|
2450 |
+
# PDF Export
|
2451 |
+
if st.button("📑 บันทึกเป็น PDF", use_container_width=True):
|
2452 |
+
try:
|
2453 |
+
pdf = create_story_pdf()
|
2454 |
+
st.download_button(
|
2455 |
+
"📥 ดาวน์โหลด PDF",
|
2456 |
+
data=pdf,
|
2457 |
+
file_name=f"story_{datetime.now().strftime('%Y%m%d')}.pdf",
|
2458 |
+
mime="application/pdf"
|
2459 |
+
)
|
2460 |
+
except Exception as e:
|
2461 |
+
logging.error(f"Error creating PDF: {str(e)}")
|
2462 |
+
st.error("เกิดข้อผิดพลาดในการสร้างไฟล์ PDF")
|
2463 |
+
|
2464 |
+
with col2:
|
2465 |
+
# JSON Save
|
2466 |
+
if st.button("💾 บันทึกความก้าวหน้า", use_container_width=True):
|
2467 |
+
try:
|
2468 |
+
story_data = {
|
2469 |
+
'timestamp': datetime.now().isoformat(),
|
2470 |
+
'level': st.session_state.level,
|
2471 |
+
'story': st.session_state.story,
|
2472 |
+
'achievements': st.session_state.achievements,
|
2473 |
+
'stats': {
|
2474 |
+
key: list(value) if isinstance(value, set) else value
|
2475 |
+
for key, value in st.session_state.stats.items()
|
2476 |
+
},
|
2477 |
+
'points': st.session_state.points
|
2478 |
+
}
|
2479 |
+
|
2480 |
+
st.download_button(
|
2481 |
+
"📥 ดาวน์โหลดไฟล์บันทึก",
|
2482 |
+
data=json.dumps(story_data, ensure_ascii=False, indent=2),
|
2483 |
+
file_name=f"story_progress_{datetime.now().strftime('%Y%m%d')}.json",
|
2484 |
+
mime="application/json"
|
2485 |
+
)
|
2486 |
+
except Exception as e:
|
2487 |
+
logging.error(f"Error saving progress: {str(e)}")
|
2488 |
+
st.error("เกิดข้อผิดพลาดในการบันทึกความก้าวหน้า")
|
2489 |
+
|
2490 |
+
def reset_story_with_confirmation():
|
2491 |
+
"""Reset story with confirmation dialog"""
|
2492 |
+
try:
|
2493 |
+
if st.session_state.story:
|
2494 |
+
if st.checkbox("✅ ยืนยันการเริ่มใหม่"):
|
2495 |
+
st.warning("การเริ่มใหม่จะลบเรื่องราวปัจจุบันทั้งหมด")
|
2496 |
+
if st.button("🔄 ยืนยันการเริ่มใหม่", use_container_width=True):
|
2497 |
+
reset_story()
|
2498 |
+
st.success("เริ่มต้นใหม่เรียบร้อยแล้ว!")
|
2499 |
+
st.rerun()
|
2500 |
+
except Exception as e:
|
2501 |
+
logging.error(f"Error in reset confirmation: {str(e)}")
|
2502 |
+
st.error("เกิดข้อผิดพลาดในการรีเซ็ตเรื่อง")
|
2503 |
+
|
2504 |
# === 6. MAIN APPLICATION LOGIC ===
|
2505 |
def main():
|
2506 |
try:
|