-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsetLog.js
More file actions
80 lines (59 loc) · 1.97 KB
/
Copy pathsetLog.js
File metadata and controls
80 lines (59 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { existsSync, readFileSync, writeFileSync } from 'fs';
function getYesterDayKey() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1; // 월은 0부터 시작하므로 +1
const day = date.getDate();
return `${year}-${month}-${day - 1}`; // 날짜를 문자열로 반환
}
function getTodayKey() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1; // 월은 0부터 시작하므로 +1
const day = date.getDate();
return `${year}-${month}-${day}`; // 날짜를 문자열로 반환
}
function flattenData(array) {
// 하나의 배열로 만듬
let data = [];
array.map((arr) => {
arr.map((each) => {
data.push(each);
});
});
return data;
}
export function saveTeams(teams) {
let data = {};
const filePath = './logs/logs.json';
// 기존 파일이 있으면 데이터를 불러옴
if (existsSync(filePath)) {
const fileData = readFileSync(filePath);
data = JSON.parse(fileData);
}
const todayKey = getTodayKey(); // 오늘 날짜를 키로 사용
if (Object.keys(data).includes(todayKey)) {
return;
}
data[todayKey] = {
date: todayKey,
data: flattenData(teams),
};
writeFileSync(filePath, JSON.stringify(data, null, 2)); // 데이터를 파일에 저장 (2는 JSON 정렬)
console.log('저장 완료');
console.log(teams);
}
export function loadPreviousTeams() {
const filePath = './logs/logs.json';
const dateKey = getYesterDayKey();
if (existsSync(filePath)) {
const fileData = readFileSync(filePath);
const data = JSON.parse(fileData);
if (Object.keys(data).length != 0) {
data[dateKey] && console.log('어제 날짜 발견!! 어제 팀으로 셔플합니다.');
return data[dateKey] || null; // 어제 날짜의 데이터를 반환
}
}
console.log('어제 팀을 발견하지 못했으므로, default data 사용합니다.');
return null; // 파일이 없으면 null 반환
}