22import json
33from datetime import datetime
44
5+ from update_dashboard import archive_current_month
6+
57SCOREBOARD_FILE = "scoreboard.json"
68PR_DATA_FILE = "pr_data.json"
79
1113 "DP" : 5
1214}
1315
14-
1516def initialize_user ():
1617 # 사용자별 초기 스코어보드 구조
1718 return {
1819 ** {ctype : [] for ctype in CHALLENGE_TYPES .keys ()},
1920 "achieved" : {ctype : False for ctype in CHALLENGE_TYPES .keys ()}
2021 }
2122
22-
2323def main ():
24+ # 현재 달 문자열
25+ current_month = datetime .now ().strftime ("%Y-%m" )
26+
2427 # 1. 기존 스코어보드 로드 (없으면 빈 dict로 초기화)
28+ print ("[Step 1] Loading scoreboard file..." )
2529 if os .path .exists (SCOREBOARD_FILE ):
2630 with open (SCOREBOARD_FILE , 'r' , encoding = 'utf-8' ) as f :
2731 try :
@@ -31,28 +35,43 @@ def main():
3135 else :
3236 scoreboard = {}
3337
34- print (f"scorebard : { scoreboard } " )
38+ print (f"[Step 1] Loaded scoreboard data : { scoreboard !r } " )
3539
36- # 2. 새 구조("month", "users")가 없다면 변환
40+ # 2-1. 새 구조("month", "users")가 없다면 변환
41+ print ("[Step 2.1] Verifying scoreboard structure..." )
3742 if "users" not in scoreboard or "month" not in scoreboard :
3843 scoreboard = {
39- "month" : datetime . now (). strftime ( "%Y-%m" ) ,
44+ "month" : current_month ,
4045 "users" : scoreboard # 기존 scoreboard의 내용(사용자 데이터)이 있다면 여기에 넣음
4146 }
47+ else :
48+ # 2-2. month 값이 다르면 현재 달로 덮어쓰기
49+ print ("[Step 2.2] Checking month field..." )
50+ if scoreboard ["month" ] != current_month :
51+ print (f"[Step 2.2] Month mismatch detected (previous: { scoreboard ['month' ]} , current: { current_month } ); archiving..." )
52+
53+ archive_current_month ()
54+ print ("[Step 2.2] Archived previous month data to HISTORY.md" )
55+
56+ scoreboard ["month" ] = current_month
57+ scoreboard ["users" ] = {} # 매달 유저값도 초기화
58+ print (f"[Step 2.2] Reset scoreboard for new month: { scoreboard !r} " )
4259
4360 users = scoreboard ["users" ]
4461
4562 # 3. pr_data.json 파일 로드
63+ print ("[Step 3] Loading PR data file..." )
4664 if not os .path .exists (PR_DATA_FILE ):
47- print (f"{ PR_DATA_FILE } 파일이 존재하지 않습니다. " )
65+ print (f"[Step 3] PR data file not found: { PR_DATA_FILE } " )
4866 exit (1 )
4967
5068 with open (PR_DATA_FILE , 'r' , encoding = 'utf-8' ) as f :
5169 pr_data = json .load (f )
5270
53- print (f"pr_data : { pr_data } " )
71+ print (f"[Step 3] Loaded PR data : { pr_data !r } " )
5472
5573 # 4. pr_data의 각 항목을 순회하며 사용자별 스코어보드 업데이트
74+ print ("[Step 4] Processing PR data entries..." )
5675 for entry in pr_data :
5776 username = entry ["username" ]
5877 algorithm = entry ["algorithm" ]
@@ -61,36 +80,37 @@ def main():
6180 if not username or not algorithm or problem_id is None :
6281 continue
6382
64- print (f"username : { username } , algorithm: { algorithm } , problem_id: { problem_id } " )
65- print (f"users: { users } " )
83+ print (f"[Step 4] Entry details -> user : { username } , algorithm: { algorithm } , problem_id: { problem_id } " )
84+ print (f"[Step 4] Current users state : { users !r } " )
6685 # 챌린지 유형에 포함되어 있는지 확인 (예: "그래프", "DP")
6786 if algorithm not in CHALLENGE_TYPES :
6887 continue # 챌린지 대상이 아니면 무시
6988
70- # 사용자의 기록이 없으면 초기화
71- print ("사용자의 기록이 없으면 초기화" )
89+ # 해당 사용자가 없으면 초기화
7290 if username not in users :
91+ print (f"[Step 4] Initializing user: { username } " )
7392 users [username ] = initialize_user ()
93+ print (f"[Step 4] Users after initialization: { users !r} " )
7494
75- # 해당 유형 문제 번호를 중복 없이 추가
76- print ("해당 유형 문제 번호를 중복 없이 추가 " )
95+ # 해당 문제 ID 중복 없이 추가
96+ print (f"[Step 4] Adding problem ID { problem_id } for user ' { username } ' " )
7797 if problem_id not in users [username ].get (algorithm , []):
7898 users [username ][algorithm ].append (problem_id )
7999
80- print ( f"users: { users } " )
81-
82- # 5. 각 사용자별로 달성 여부 업데이트
83- for username , data in users .items ():
84- for ctype , goal in CHALLENGE_TYPES . items ():
85- count = len ( data . get ( ctype , []))
86- # 목표 수 이상이면 달성 처리
87- data [ "achieved" ][ ctype ] = ( count >= goal )
88-
89- # 5 . 스코어보드 저장
90- with open ( SCOREBOARD_FILE , 'w' , encoding = 'utf-8' ) as f :
91- json . dump ( scoreboard , f , ensure_ascii = False , indent = 2 )
92-
93- print ("scoreboard.json 업데이트 완료! " )
100+ # 5. 각 사용자별로 달성 여부 업데이트
101+ print ( "[Step 5] Updating achievement status for each user..." )
102+ for username , data in users . items ():
103+ for ctype , goal in CHALLENGE_TYPES .items ():
104+ count = len ( data . get ( ctype , []))
105+ # 목표 수 이상이면 달성 처리
106+ data [ "achieved" ][ ctype ] = ( count >= goal )
107+ print ( f"[Step 6] Achievement statuses: { users !r } " )
108+
109+ # 6 . 스코어보드 저장
110+ print ( "[Step 6] Saving updated scoreboard to file..." )
111+ with open ( SCOREBOARD_FILE , 'w' , encoding = 'utf-8' ) as f :
112+ json . dump ( scoreboard , f , ensure_ascii = False , indent = 2 )
113+ print (f"[Step 7] Successfully updated ' { SCOREBOARD_FILE } ' " )
94114
95115if __name__ == '__main__' :
96116 main ()
0 commit comments