File size: 3,584 Bytes
255efa8
 
 
c0d2247
c35e5b5
 
c0d2247
c35e5b5
 
255efa8
c35e5b5
 
c0d2247
 
 
255efa8
c0d2247
c35e5b5
c0d2247
c35e5b5
 
c0d2247
c35e5b5
 
c0d2247
 
1960505
 
 
 
 
 
 
 
 
c35e5b5
a8c2362
c35e5b5
c0d2247
255efa8
1960505
 
c35e5b5
ae75435
 
c35e5b5
 
1960505
 
c35e5b5
 
1960505
 
c35e5b5
1960505
 
 
 
 
c35e5b5
 
1960505
 
c35e5b5
 
 
a8c2362
c35e5b5
 
255efa8
 
 
 
 
 
 
1960505
 
 
 
c35e5b5
255efa8
 
 
 
 
 
 
 
 
c35e5b5
1960505
 
 
c35e5b5
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const restaurant_no = 5;

function shuffle(array, seed) {
    var m = array.length, t, i;
  
    // While there remain elements to shuffle…
    while (m) {
  
      // Pick a remaining element…
      i = Math.floor(random(seed) * m--);
  
      // And swap it with the current element.
      t = array[m];
      array[m] = array[i];
      array[i] = t;
      ++seed 
    }
  
    return array;
  }
  
function random(seed) {
var x = Math.sin(seed++) * 10000; 
return x - Math.floor(x);
}

async function fetchRestaurants() {
    const response = await fetch('restaurants.json');
    if (!response.ok) {
        throw new Error('Network response was not ok ' + response.statusText);
    }
    const data = await response.json();
    return data;
}


function getRandomRestaurants(restaurants, seed) {
    // Generate three random indices based on the seed
    randomRestaurants = shuffle(restaurants, seed);
    return randomRestaurants.slice(0,restaurant_no);
}

async function displayRestaurants() {
    const today = new Date();
    const day = today.getDay();

    // Format the date
    const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
    const formattedDate = today.toLocaleDateString(undefined, options);

    // Display the date
    document.getElementById('currentDate').innerText = `Today is ${formattedDate}`;

    // Only display suggestions on weekdays
    if (day === 0 || day === 6) {
        document.getElementById('restaurants').innerHTML = "No suggestions available today!";
        return;
    }

    const dateKey = today.toISOString().split('T')[0];

    try {
        const restaurants = await fetchRestaurants();
        console.log('Fetched Restaurants:', restaurants); // Debugging: Print fetched restaurants
        const seed = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate(); // Create a seed from the date
        console.log('Random seed per day: ', seed);
        const randomRestaurants = getRandomRestaurants(restaurants, seed);
        console.log('Random Restaurants:', randomRestaurants); // Debugging: Print random restaurants

        const firstThreeRestaurants = randomRestaurants.slice(0, 3);
        const bonusRestaurants = randomRestaurants.slice(3, 5);

        document.getElementById('restaurants').innerHTML = firstThreeRestaurants.map(restaurant => 
            `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`
        ).join('<br>');
        document.getElementById('bonusRestaurants').innerHTML = bonusRestaurants.map(restaurant => 
            `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`
        ).join('<br>');

    } catch (error) {
        console.error('Error fetching restaurants:', error);
        const bestRestaurants = [
            ["돈비고고 (unlimited 돈까스 + 제육 (Pork) for 8.8K)", "https://naver.me/GGUfwvl9"],
            ["Taksim Kebab (Turkish, small place)", "https://maps.app.goo.gl/6rwGVo5qbT9xKZAMA"],
            ["강남역 파스타 (Pasta, large space)", "https://naver.me/xL1EJLfC"],
        ]
        document.getElementById('restaurants').innerHTML = "Failed to load restaurant suggestions. <br>Below are my personal suggestions based on your review!!";
        document.getElementById('bonusHeader').innerHTML = "";        
        document.getElementById('bonusRestaurants').innerHTML = bestRestaurants.map(restaurant => 
            `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`
        ).join('<br>') + "<br><br>";
    }
}

window.onload = displayRestaurants;