ガチャを回す…
Loading...
const SUPABASE_URL = "https://auubbsvvualpndlcogrf.supabase.co"
const SUPABASE_KEY = "sb_publishable_ufmeIHt5zQ01FrI8oT8Pcg_9caFeP3_" // ← 改行絶対ダメ
const supabase = window.supabase.createClient(SUPABASE_URL, SUPABASE_KEY)
async function runGacha() {
const url = new URL(location.href)
const code = url.searchParams.get("code")
if (!code) {
document.getElementById("result").innerText = "コードがありません"
return
}
// ① コード確認
const { data: c } = await supabase
.from("codes")
.select("*")
.eq("code", code)
.single()
if (!c || c.used) {
document.getElementById("result").innerText = "このコードは使用できません"
return
}
// ② 先に使用済みにする(超重要)
await supabase
.from("codes")
.update({ used: true })
.eq("code", code)
// ③ ガチャ抽選(レア度付き)
const { data: items } = await supabase.from("gacha_items").select("*")
// ★ レア度確率
const rand = Math.random()
let rarity = "N"
if (rand < 0.001) rarity = "Secret" // 0.1%
else if (rand < 0.011) rarity = "SSR" // 1%
else if (rand < 0.031) rarity = "SR" // 2%
else if (rand < 0.15) rarity = "R" // 11.9%
else rarity = "N" // 85%
// レア度に合うアイテムだけ抽出
const filtered = items.filter(i => i.rarity === rarity)
// fallback(なければ全体)
const pool = filtered.length > 0 ? filtered : items
const item = pool[Math.floor(Math.random() * pool.length)]
// ④ 保存
await supabase.from("user_contents").insert({
user_id: code,
content: item.content,
rarity: rarity
})
document.getElementById("result").innerHTML = `
${rarity} GET!
マイページ
`
}
runGacha()