Lilli98 commited on
Commit
6fabeb3
·
verified ·
1 Parent(s): 95658ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -105
app.py CHANGED
@@ -672,65 +672,67 @@ elif not st.session_state['consent_given']:
672
  st.warning("Please select an option to continue.")
673
 
674
 
675
- # --- Game Setup / Main Interface Logic ---
676
  else:
677
- # --- Game Setup & Instructions ---
678
- if 'game_state' not in st.session_state or not st.session_state.game_state.get('game_running', False):
 
 
 
 
 
 
 
 
 
 
679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
680
  st.markdown("---")
681
- st.header("⚙️ Game Configuration")
682
-
683
- # =============== 1. NEW: Participant ID Input ===============
684
- participant_id = st.text_input("Enter Your Name or Team ID:", key="participant_id_input", placeholder="e.g., Team A")
685
- # =======================================================
686
 
687
- # =============== 1. MODIFIED: 自动分配逻辑 (移除手动选择) ===============
688
- if participant_id:
689
- # 使用哈希和模运算,将用户均匀分配到 4 个实验组
690
- setting_index = hash(participant_id) % len(EXPERIMENT_SETTINGS)
691
- llm_personality, info_sharing = EXPERIMENT_SETTINGS[setting_index]
692
- else:
693
- # 默认值,如果未输入ID
694
- llm_personality, info_sharing = ('human_like', 'local')
 
 
 
695
 
696
- # [修改点 2.2]: 移除启动时显示实验条件的提示
697
- # st.info(f"You will be automatically assigned to the condition: **{llm_personality.replace('_', ' ').title()} / {info_sharing.title()}**.")
698
- # =================================================================
699
-
700
- # 移除原有的 c1, c2 和 selectbox
701
- # c1, c2 = st.columns(2)
702
- # with c1:
703
- # with c2:
704
-
705
- # =============== MODIFIED: Start Game Button ===============
706
- if st.button("🚀 Start Game", type="primary", disabled=(client is None)):
707
- if not participant_id:
708
- st.error("Please enter a Name or Team ID to start!")
709
- else:
710
- existing_data = load_leaderboard_data()
711
- if participant_id in existing_data:
712
- # 如果ID已存在,添加一个session_state标志,要求再次点击
713
- if st.session_state.get('last_id_warning') == participant_id:
714
- # 这是第二次点击,确认覆盖
715
- st.session_state.pop('last_id_warning', None)
716
- init_game_state(llm_personality, info_sharing, participant_id) # 使用自动分配的值
717
- st.rerun()
718
- else:
719
- st.session_state['last_id_warning'] = participant_id
720
- st.warning(f"ID '{participant_id}' already exists! Your score will be overwritten. Click 'Start Game' again to confirm.")
721
- else:
722
- # 新ID,直接开始
723
- if 'last_id_warning' in st.session_state:
724
- del st.session_state['last_id_warning']
725
- init_game_state(llm_personality, info_sharing, participant_id) # 使用自动分配的值
726
- st.rerun()
727
- # ===========================================================
728
-
729
- # =============== NEW: Show Leaderboard on Start Page ===============
730
  show_leaderboard_ui()
731
- # =================================================================
732
- # --- Main Game Interface ---
733
- elif 'game_state' in st.session_state and st.session_state.game_state.get('game_running'):
 
 
 
 
 
 
 
 
 
 
734
  state = st.session_state.game_state
735
  week, human_role, echelons, info_sharing = state['week'], state['human_role'], state['echelons'], state['info_sharing']
736
  echelon_order = ["Retailer", "Wholesaler", "Distributor", "Factory"] # Define here for UI
@@ -895,60 +897,11 @@ else:
895
  del st.session_state.game_state
896
  st.rerun()
897
 
898
- # --- Game Over Interface ---
899
- elif 'game_state' in st.session_state and not st.session_state.game_state.get('game_running', False) and st.session_state.game_state.get('week', 0) > WEEKS:
900
- st.header("🎉 Game Over!")
901
- state = st.session_state.game_state
902
- participant_id = state['participant_id'] # 获取ID
903
-
904
  # --------------------------------------------------------------------------
905
- # [NEW SECTION]: Survey Link Generation
906
  # --------------------------------------------------------------------------
907
- # Your Qualtrics URL and PID field name
908
- personalized_survey_url = f"{QUALTRICS_BASE_URL}?{PID_FIELD_NAME}={participant_id}"
909
-
910
- st.markdown("---")
911
- st.header("📋 Experiment Wrap-up: Please Complete the Survey")
912
- st.warning(f"Your Experiment ID is: **{participant_id}**. Please click the link below to complete the final survey. **This survey will automatically link to your experiment data.**")
913
-
914
- st.markdown(
915
- f"""
916
- <a href="{personalized_survey_url}" target="_blank">
917
- <button style="background-color: #4CAF50; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border: none; border-radius: 8px;">
918
- Click Here to Start the Survey
919
- </button>
920
- </a>
921
- """,
922
- unsafe_allow_html=True
923
- )
924
-
925
- st.markdown("---")
926
- # --------------------------------------------------------------------------
927
-
928
- try:
929
- logs_df = pd.json_normalize(state['logs'])
930
- fig = plot_results(
931
- logs_df,
932
- f"Beer Game (Human: {state['human_role']})",
933
- state['human_role']
934
- )
935
- st.pyplot(fig)
936
- save_logs_and_upload(state) # This now also updates the leaderboard
937
- except Exception as e:
938
- st.error(f"Error generating final report: {e}")
939
- show_leaderboard_ui()
940
- if st.button("✨ Start a New Game"):
941
- # 在重置游戏时清除所有与实验相关的时间戳
942
- if 'consent_timestamp' in st.session_state: del st.session_state['consent_timestamp']
943
- if 'consent_given' in st.session_state: del st.session_state['consent_given']
944
- del st.session_state.game_state
945
- st.rerun()
946
-
947
- # --- Fallback: Game Setup (Default Screen) ---
948
- else:
949
- # This is the original Game Setup logic when game_state does not exist or is just cleared.
950
- # This covers the scenarios: Before Week 1, or after a full game reset.
951
-
952
  st.markdown("---")
953
  st.header("⚙️ Game Configuration")
954
 
 
672
  st.warning("Please select an option to continue.")
673
 
674
 
675
+ # --- Main Application Flow ---
676
  else:
677
+ # 1. 检查 Game Over 状态 (最高优先级)
678
+ is_game_state_present = st.session_state.get('game_state') is not None
679
+ is_game_running = st.session_state.get('game_state', {}).get('game_running', False)
680
+ is_game_over = is_game_state_present and not is_game_running and st.session_state.get('game_state', {}).get('week', 0) > WEEKS
681
+
682
+ if is_game_over:
683
+ # --------------------------------------------------------------------------
684
+ # --- Game Over Interface ---
685
+ # --------------------------------------------------------------------------
686
+ st.header("🎉 Game Over!")
687
+ state = st.session_state.game_state
688
+ participant_id = state['participant_id'] # 获取ID
689
 
690
+ # [NEW]: Survey Link Generation (英文显示)
691
+ personalized_survey_url = f"{QUALTRICS_BASE_URL}?{PID_FIELD_NAME}={participant_id}"
692
+
693
+ st.markdown("---")
694
+ st.header("📋 Experiment Wrap-up: Please Complete the Survey")
695
+ st.warning(f"Your Experiment ID is: **{participant_id}**. Please click the link below to complete the final survey. **This survey will automatically link to your experiment data.**")
696
+
697
+ st.markdown(
698
+ f"""
699
+ <a href="{personalized_survey_url}" target="_blank">
700
+ <button style="background-color: #4CAF50; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border: none; border-radius: 8px;">
701
+ Click Here to Start the Survey
702
+ </button>
703
+ </a>
704
+ """,
705
+ unsafe_allow_html=True
706
+ )
707
+
708
  st.markdown("---")
 
 
 
 
 
709
 
710
+ try:
711
+ logs_df = pd.json_normalize(state['logs'])
712
+ fig = plot_results(
713
+ logs_df,
714
+ f"Beer Game (Human: {state['human_role']})",
715
+ state['human_role']
716
+ )
717
+ st.pyplot(fig)
718
+ save_logs_and_upload(state) # Uploads data/updates leaderboard
719
+ except Exception as e:
720
+ st.error(f"Error generating final report or saving data: {e}")
721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
722
  show_leaderboard_ui()
723
+ if st.button("✨ Start a New Game"):
724
+ # 在重置游戏时清除所有与实验相关的时间戳
725
+ if 'consent_timestamp' in st.session_state: del st.session_state['consent_timestamp']
726
+ if 'consent_given' in st.session_state: del st.session_state['consent_given']
727
+ del st.session_state.game_state
728
+ st.rerun()
729
+
730
+
731
+ # 2. 检查 Game Running 状态 (第二优先级)
732
+ elif is_game_running:
733
+ # --------------------------------------------------------------------------
734
+ # --- Main Game Interface ---
735
+ # --------------------------------------------------------------------------
736
  state = st.session_state.game_state
737
  week, human_role, echelons, info_sharing = state['week'], state['human_role'], state['echelons'], state['info_sharing']
738
  echelon_order = ["Retailer", "Wholesaler", "Distributor", "Factory"] # Define here for UI
 
897
  del st.session_state.game_state
898
  st.rerun()
899
 
900
+ # 3. Game Setup (默认屏幕 / 游戏已重置)
901
+ else:
 
 
 
 
902
  # --------------------------------------------------------------------------
903
+ # --- Game Setup & Instructions ---
904
  # --------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905
  st.markdown("---")
906
  st.header("⚙️ Game Configuration")
907