Newer
Older
fff / fff / 09_yohou / main.js
const today = new Date();
const month = today.getMonth() + 1;

document.getElementById("date").textContent =
  today.toLocaleDateString("ja-JP", { weekday: "short" });

const wafuuColors = [
  { bg: "#F6F4EF", text: "#2B2B2B" }, 
  { bg: "#E8ECEF", text: "#1F2A44" }, 
  { bg: "#F2E9E4", text: "#5A2A27" }, 
  { bg: "#E6EFE9", text: "#1C3D33" }, 
  { bg: "#EFE6D8", text: "#3B2F2F" }, 
  { bg: "#EDE4E1", text: "#4A1C1A" }  
];

function setRandomBackground() {
  const color =
    wafuuColors[Math.floor(Math.random() * wafuuColors.length)];

  document.body.style.backgroundColor = color.bg;
  document.body.style.color = color.text;
}


// 追加できる
const sakanaName = {
  aji: "アジ",
  saba: "サバ",
  sanma: "サンマ",
  iwashi: "イワシ",
  katsuo: "カツオ",
  sake: "サケ",
  buri: "ブリ",
  ika: "イカ",
  tako: "タコ"
};

// tenki API(Open-Meteo)宮城県座標指定

async function getTenki() {
  const url =
    "https://api.open-meteo.com/v1/forecast" +
    "?latitude=38.3&longitude=141.0" +
    "&daily=weathercode,temperature_2m_max" +
    "&timezone=Asia/Tokyo";

  const res = await fetch(url);
  const data = await res.json();

  const code = data.daily.weathercode[0];
  const temp = data.daily.temperature_2m_max[0];

  return { code, temp };
}

// 天気

function makeCondition(tenki) {
  const list = [];

  const iconEl = document.getElementById("weatherIcon");
  const textEl = document.getElementById("weatherText");
  
   if (tenki.code === 0) {
    list.push("sunny");
    iconEl.innerHTML = "☀"; 
    textEl.textContent = "晴れ";
  } else if (tenki.code >= 61) {
    list.push("rain");
    iconEl.innerHTML = "🌧"; 
    textEl.textContent = "雨";
  } else {
    list.push("cloudy");
    iconEl.innerHTML = "☁"; 
    textEl.textContent = "くもり";
  }

  if (tenki.temp >= 25) list.push("hot");
  if (tenki.temp <= 10) list.push("cold");

  const day = today.getDay();
  if (day === 0 || day === 6) list.push("weekend");
  else list.push("weekday");

  return list;
}

// 魚判定

function isInSeason(m, start, end) {
  if (start <= end) {
    return m >= start && m <= end;
  } else {
    // fuyu wo mataideru sakana
    return m >= start || m <= end;
  }
}

// 魚を選ぶ

function chooseSakana(fishes, conditions) {
  let list = fishes.filter(f =>
    isInSeason(
      month,
      Number(f.season_start),
      Number(f.season_end)
    )
  );

  if (conditions.includes("hot")) {
    list = list.filter(f => f.hot === "1");
  }

  if (conditions.includes("cold")) {
    list = list.filter(f => f.cold === "1");
  }

  if (conditions.includes("rain")) {
    list = list.filter(f => f.easy === "1");
  }

  // shibori sugita toki no nige
  if (list.length === 0) {
    list = fishes;
  }

  return list[Math.floor(Math.random() * list.length)];
}

// csvを取りに行く

function loadCsv(path) {
  return new Promise(resolve => {
    Papa.parse(path, {
      download: true,
      header: true,
      complete: result => resolve(result.data)
    });
  });
}

// main 

async function main() {
  setRandomBackground(); // saisho ni funiki wo kimeru

  try {
    const tenki = await getTenki();
    const conditions = makeCondition(tenki);

    const fishes = await loadCsv("fish.csv");
    const messages = await loadCsv("condition.csv");

    const sakana = chooseSakana(fishes, conditions);

    const displayName =
      sakanaName[sakana.fish] || sakana.fish;

    document.getElementById("fishName").textContent = displayName;

    const reasonHtml = messages
      .filter(m => conditions.includes(m.condition))
      .map(m => `<li>${m.message}</li>`)
      .join("");

    document.getElementById("reason").innerHTML = reasonHtml;

  } catch (e) {
    // umaku ikanai hi mo aru
    document.getElementById("fishName").textContent = "??( ;∀;)??";
    document.getElementById("reason").innerHTML =
      "<li>ごめんなさい。データを取得できないです。</li>";
  }
}

main();