artemisapi/chunithm/13/getRoutes.js

669 lines
20 KiB
JavaScript
Executable File

const express = require("express");
const pool1 = require("../../db");
const { zonedTimeToUtc, format } = require("date-fns-tz");
const { RequireNotGuest } = require("../../middleware/auth");
const router = express.Router();
router.get("/user", RequireNotGuest, getUserHandler);
router.get("/access-code", RequireNotGuest, getAccessCodeHandler);
router.get("/items", RequireNotGuest, getItemsHandler);
router.get("/chuni-avatar-items", RequireNotGuest, getChuniAvatarItems);
router.get("/profile-data", RequireNotGuest, getProfileDataHandler);
router.get("/chuni-score-playlog", RequireNotGuest, getChuniScorePlaylogHandler);
router.get("/chuni-best-and-top", RequireNotGuest, getChuniBestAndTopHandler);
router.get("/chuni-player-team", RequireNotGuest, getChuniPlayerTeamHandler);
router.get("/chuni-duel-items", RequireNotGuest, getChuniDuelItems);
router.get("/chuni-nameplate-items", RequireNotGuest, getChuniNamePlateItems);
router.get("/chuni-map-items", RequireNotGuest, getChuniMapItems);
router.get("/listFriends", RequireNotGuest, listFriendsHandler);
router.get("/listRivals", RequireNotGuest, listRivalsHandler);
router.get("/chuni-static-music", RequireNotGuest, getChuniStaticMusic);
router.get("/get-trophies", RequireNotGuest, getTrophiesHandler);
router.get("/list-favorite-songs", RequireNotGuest, listFavoriteSongsHandler);
router.get("/chuni-recent", RequireNotGuest, getChuniRecentHandler);
function getUserHandler(req, res) {
return res
.status(200)
.json({
success: true,
message: "Found user.",
body: req.session.cozynet,
});
}
function getAccessCodeHandler(req, res) {
const { userId, version } = req.session.cozynet;
if (!userId || !version) {
return res
.status(400)
.json({ error: "User and version parameters are required" });
}
// Retrieve the access code for the given user ID and version
pool1.query(
`
SELECT ac.access_code
FROM aime_card ac
JOIN chuni_profile_data pd ON ac.user = pd.user
WHERE ac.user = ? AND pd.version = ?
`,
[userId, version],
(error, results) => {
if (error) {
console.error("Error retrieving access code:", error);
return res.status(500).json({ error: "Error retrieving access code" });
}
// Ensure that the access code is found
if (results.length === 0 || !results[0].access_code) {
return res.status(404).json({
error: "Access code not found for the given user and version",
});
}
const accessCode = results[0].access_code;
// Send the access code in the response
res.json({ accessCode });
},
);
}
function getItemsHandler(req, res) {
const { userId, version } = req.session.cozynet;
pool1.query(
`
WITH UserAimeId AS (
SELECT aime_user.id AS aimeUserId
FROM aime_user
JOIN aime_card ON aime_user.id = aime_card.user
WHERE aime_card.user = ?
)
SELECT i.*
FROM chuni_item_item i
JOIN UserAimeId uai ON i.user = uai.aimeUserId
JOIN chuni_profile_data pd ON uai.aimeUserId = pd.user
WHERE pd.version = ?
`,
[userId, version],
(error, results, fields) => {
if (error) {
console.error("Error retrieving items:", error);
return res.status(500).json({ error: "Error retrieving items" });
}
res.json(results);
},
);
}
function getProfileDataHandler(req, res) {
const { userId, version } = req.session.cozynet;
pool1.query(
`
WITH UserAimeId AS (
SELECT aime_user.id AS aimeUserId
FROM aime_user
JOIN aime_card ON aime_user.id = aime_card.user
WHERE aime_card.user = ?
),
UserProfile AS (
SELECT p.*, t.teamName, cnst.str as trophyname
FROM chuni_profile_data p
LEFT JOIN chuni_profile_team t ON p.teamId = t.id
LEFT JOIN cozynet_chuni_static_trophies cnst ON p.trophyId = cnst.id
JOIN UserAimeId ON p.user = UserAimeId.aimeUserId
)
SELECT *
FROM UserProfile
WHERE version = ?
`,
[userId, version],
(error, results, fields) => {
if (error) {
console.error("Error retrieving profile data:", error);
return res.status(500).json({ error: "Error retrieving profile data" });
}
results.forEach((row) => {
let lastPlayDateUTC = zonedTimeToUtc(row.lastPlayDate, "Asia/Tokyo");
row.lastPlayDate = format(lastPlayDateUTC, "yyyy-MM-dd");
});
res.json(results);
},
);
}
function getChuniScorePlaylogHandler(req, res) {
const { userId, version } = req.session.cozynet;
pool1.query(
`
WITH RankedScores AS (
SELECT
csp.maxCombo,
csp.isFullCombo,
csp.userPlayDate,
csp.playerRating,
csp.isAllJustice,
csp.score,
csp.judgeHeaven,
csp.judgeGuilty,
csp.judgeJustice,
csp.judgeAttack,
csp.judgeCritical,
csp.isClear,
csp.skillId,
csp.isNewRecord,
csm.chartId,
csm.title,
csm.level,
csm.genre,
csm.jacketPath,
csm.artist,
IF(csp.score > LAG(csp.score, 1) OVER (ORDER BY csp.userPlayDate), 'Increase',
IF(csp.score < LAG(csp.score, 1) OVER (ORDER BY csp.userPlayDate), 'Decrease', 'Same')) AS score_change,
IF(csp.playerRating > LAG(csp.playerRating, 1) OVER (ORDER BY csp.userPlayDate), 'Increase',
IF(csp.playerRating < LAG(csp.playerRating, 1) OVER (ORDER BY csp.userPlayDate), 'Decrease', 'Same')) AS rating_change
FROM
chuni_score_playlog csp
JOIN chuni_profile_data d ON csp.user = d.user
JOIN chuni_static_music csm ON csp.musicId = csm.songId AND csp.level = csm.chartId AND csm.version = d.version
JOIN aime_card a ON d.user = a.user
WHERE
a.user = ? AND d.version = ? -- Use the version parameter here
)
SELECT
maxCombo,
isFullCombo,
userPlayDate,
playerRating,
isAllJustice,
score,
judgeHeaven,
judgeGuilty,
judgeJustice,
judgeAttack,
judgeCritical,
isClear,
skillId,
isNewRecord,
chartId,
title,
level,
genre,
jacketPath,
artist,
score_change,
rating_change,
CASE
WHEN score >= 1009000 THEN CAST(level AS DECIMAL(10,2)) * 100 + 215
WHEN score >= 1007500 THEN CAST(level AS DECIMAL(10,2)) * 100 + 200 + (score - 1007500) / 100
WHEN score >= 1005000 THEN CAST(level AS DECIMAL(10,2)) * 100 + 150 + (score - 1005000) / 50
WHEN score >= 1000000 THEN CAST(level AS DECIMAL(10,2)) * 100 + 100 + (score - 1000000) / 100
WHEN score >= 975000 THEN CAST(level AS DECIMAL(10,2)) * 100 + (score - 975000) / 250
WHEN score >= 925000 THEN CAST(level AS DECIMAL(10,2)) * 100 - 300 + (score - 925000) * 3 / 500
WHEN score >= 900000 THEN CAST(level AS DECIMAL(10,2)) * 100 - 500 + (score - 900000) * 4 / 500
WHEN score >= 800000 THEN ((CAST(level AS DECIMAL(10,2)) * 100 - 500) / 2 + (score - 800000) * ((CAST(level AS DECIMAL(10,2)) - 500) / 2) / 100000)
ELSE 0
END AS rating
FROM
RankedScores
ORDER BY
userPlayDate DESC;
`,
[userId, version], // Pass version as a parameter
(error, results, fields) => {
if (error) throw error;
results.forEach((row) => {
let userPlayDateUTC = zonedTimeToUtc(row.userPlayDate, "Asia/Tokyo");
row.userPlayDate = format(userPlayDateUTC, "yyyy-MM-dd ");
});
res.json(results);
},
);
}
function getChuniRecentHandler(req, res) {
const { userId, version } = req.session.cozynet;
pool1.query(
`
SELECT
jt.score,
jt.musicId,
jt.difficultId,
jt.romVersionCode,
sm.title,
sm.level,
sm.artist,
sm.genre,
sm.jacketPath,
CASE
WHEN jt.score >= 1009000 THEN CAST(sm.level AS DECIMAL(10,2)) * 100 + 215
WHEN jt.score >= 1007500 THEN CAST(sm.level AS DECIMAL(10,2)) * 100 + 200 + (jt.score - 1007500) / 100
WHEN jt.score >= 1005000 THEN CAST(sm.level AS DECIMAL(10,2)) * 100 + 150 + (jt.score - 1005000) / 50
WHEN jt.score >= 1000000 THEN CAST(sm.level AS DECIMAL(10,2)) * 100 + 100 + (jt.score - 1000000) / 100
WHEN jt.score >= 975000 THEN CAST(sm.level AS DECIMAL(10,2)) * 100 + (jt.score - 975000) / 250
WHEN jt.score >= 925000 THEN CAST(sm.level AS DECIMAL(10,2)) * 100 - 300 + (jt.score - 925000) * 3 / 500
WHEN jt.score >= 900000 THEN CAST(sm.level AS DECIMAL(10,2)) * 100 - 500 + (jt.score - 900000) * 4 / 500
WHEN jt.score >= 800000 THEN ((CAST(sm.level AS DECIMAL(10,2)) * 100 - 500) / 2 + (jt.score - 800000) * ((CAST(sm.level AS DECIMAL(10,2)) - 500) / 2) / 100000)
END AS rating
FROM
chuni_profile_recent_rating
JOIN
JSON_TABLE(recentRating, '$[*]' COLUMNS (
score VARCHAR(255) PATH '$.score',
musicId VARCHAR(255) PATH '$.musicId',
difficultId VARCHAR(255) PATH '$.difficultId',
romVersionCode VARCHAR(255) PATH '$.romVersionCode'
)) AS jt
JOIN
chuni_static_music sm ON jt.musicId = sm.songId AND jt.difficultId = sm.chartId
WHERE
user = ? AND sm.version = ?
ORDER BY
rating DESC;
`,
[userId, version], // Pass version as a parameter
(error, results, fields) => {
if (error) throw error;
res.json(results);
},
);
}
function getProfileDataHandler(req, res) {
const { userId, version } = req.session.cozynet;
pool1.query(
`
WITH UserAimeId AS (
SELECT aime_user.id AS aimeUserId
FROM aime_user
JOIN aime_card ON aime_user.id = aime_card.user
WHERE aime_card.user = ?
),
UserProfile AS (
SELECT p.*, t.teamName, cnst.str as trophyname
FROM chuni_profile_data p
LEFT JOIN chuni_profile_team t ON p.teamId = t.id
LEFT JOIN cozynet_chuni_static_trophies cnst ON p.trophyId = cnst.id
JOIN UserAimeId ON p.user = UserAimeId.aimeUserId
)
SELECT *
FROM UserProfile
WHERE version = ?
`,
[userId, version],
(error, results, fields) => {
if (error) {
console.error("Error retrieving profile data:", error);
return res.status(500).json({ error: "Error retrieving profile data" });
}
results.forEach((row) => {
let lastPlayDateUTC = zonedTimeToUtc(row.lastPlayDate, "Asia/Tokyo");
row.lastPlayDate = format(lastPlayDateUTC, "yyyy-MM-dd");
});
res.json(results);
},
);
}
function getChuniStaticMusic(req, res) {
const version = req.session.cozynet.version; // Get version from URL parameter
pool1.query(
`
SELECT DISTINCT csm.songId, csm.title, csm.artist, csm.jacketPath
FROM chuni_static_music csm
WHERE csm.version = ?
`,
[version], // Pass version as a parameter
(error, results, fields) => {
if (error) {
console.error("Error retrieving static music:", error);
return res
.status(500)
.json({ error: "Error retrieving best and top data" });
}
res.json(results);
},
);
}
function getChuniBestAndTopHandler(req, res) {
const { userId, version } = req.session.cozynet;
pool1.query(
`
SELECT
csp.maxCombo,
csp.isFullCombo,
csp.userPlayDate,
csp.playerRating,
csp.isAllJustice,
csp.score,
csp.isNewRecord,
csp.judgeHeaven,
csp.judgeGuilty,
csp.judgeJustice,
csp.judgeAttack,
csp.judgeCritical,
csp.isClear,
csp.skillId,
csp.isNewRecord,
csm.chartId,
csm.title,
csm.level,
csm.genre,
csm.jacketPath,
csm.artist,
CASE
WHEN csp.score > LAG(csp.score, 1) OVER (ORDER BY csp.userPlayDate) THEN 'Increase'
WHEN csp.score < LAG(csp.score, 1) OVER (ORDER BY csp.userPlayDate) THEN 'Decrease'
ELSE 'Same'
END AS score_change,
CASE
WHEN csp.playerRating > LAG(csp.playerRating, 1) OVER (ORDER BY csp.userPlayDate) THEN 'Increase'
WHEN csp.playerRating < LAG(csp.playerRating, 1) OVER (ORDER BY csp.userPlayDate) THEN 'Decrease'
ELSE 'Same'
END AS rating_change,
CASE
WHEN csp.score >= 1009000 THEN CAST(csm.level AS DECIMAL(10,2)) * 100 + 215
WHEN csp.score >= 1007500 THEN CAST(csm.level AS DECIMAL(10,2)) * 100 + 200 + (csp.score - 1007500) / 100
WHEN csp.score >= 1005000 THEN CAST(csm.level AS DECIMAL(10,2)) * 100 + 150 + (csp.score - 1005000) / 50
WHEN csp.score >= 1000000 THEN CAST(csm.level AS DECIMAL(10,2)) * 100 + 100 + (csp.score - 1000000) / 100
WHEN csp.score >= 975000 THEN CAST(csm.level AS DECIMAL(10,2)) * 100 + (csp.score - 975000) / 250
WHEN csp.score >= 925000 THEN CAST(csm.level AS DECIMAL(10,2)) * 100 - 300 + (csp.score - 925000) * 3 / 500
WHEN csp.score >= 900000 THEN CAST(csm.level AS DECIMAL(10,2)) * 100 - 500 + (csp.score - 900000) * 4 / 500
WHEN csp.score >= 800000 THEN ((CAST(csm.level AS DECIMAL(10,2)) * 100 - 500) / 2 + (csp.score - 800000) * ((CAST(csm.level AS DECIMAL(10,2)) - 500) / 2) / 100000)
ELSE 0
END AS rating
FROM
chuni_score_playlog csp
JOIN
chuni_static_music csm ON csp.musicId = csm.songId AND csp.level = csm.chartId
JOIN
chuni_profile_data d ON csp.user = d.user
JOIN
aime_card a ON d.user = a.user
WHERE
a.user = ? AND csm.version = ?
ORDER BY
csp.userPlayDate DESC
`,
[userId, version],
(error, results, fields) => {
if (error) {
console.error("Error retrieving best and top data:", error);
return res
.status(500)
.json({ error: "Error retrieving best and top data" });
}
res.json(results);
},
);
}
function getChuniPlayerTeamHandler(req, res) {
const user = req.session.cozynet.userId;
pool1.query(
`SELECT *
FROM chuni_profile_team
`,
[user],
(error, results, fields) => {
if (error) throw error;
res.json(results);
},
);
}
function getChuniAvatarItems(req, res) {
const user = req.session.cozynet.userId;
pool1.query(
`
SELECT chuni_item_item.itemId as item_id, cozynet_chuni_static_accessory.category, cozynet_chuni_static_accessory.str, cozynet_chuni_static_accessory.sortName
FROM chuni_item_item
LEFT JOIN cozynet_chuni_static_accessory ON chuni_item_item.itemId = cozynet_chuni_static_accessory.id
WHERE chuni_item_item.user = ? AND chuni_item_item.itemKind = 11
`,
[user],
(error, results, fields) => {
if (error) throw error;
res.json(results);
},
);
}
function getChuniNamePlateItems(req, res) {
const { userId, version } = req.session.cozynet;
pool1.query(
`
SELECT
chuni_item_item.itemId AS nameplateId,
cozynet_chuni_static_nameplate.str,
cozynet_chuni_static_nameplate.imagePath,
cozynet_chuni_static_nameplate.sortName
FROM
chuni_item_item
LEFT JOIN
cozynet_chuni_static_nameplate ON chuni_item_item.itemId = cozynet_chuni_static_nameplate.id
JOIN
chuni_profile_data ON chuni_profile_data.user = chuni_item_item.user
WHERE
chuni_item_item.user = ?
AND chuni_item_item.itemKind = 1
AND chuni_profile_data.version = ?
AND cozynet_chuni_static_nameplate.str IS NOT NULL
ORDER BY
chuni_item_item.id ASC;
`,
[userId, version],
(error, results, fields) => {
if (error) throw error;
res.json(results);
},
);
}
function getChuniMapItems(req, res) {
const { userId, version } = req.session.cozynet;
pool1.query(
`
SELECT
chuni_item_item.itemId AS itemId,
cozynet_chuni_static_mapicon.str,
cozynet_chuni_static_mapicon.sortName
FROM
chuni_item_item
LEFT JOIN
cozynet_chuni_static_mapicon ON chuni_item_item.itemId = cozynet_chuni_static_mapicon.id
JOIN
chuni_profile_data ON chuni_profile_data.user = chuni_item_item.user
WHERE
chuni_profile_data.user = ?
AND chuni_item_item.itemKind = 8
AND chuni_profile_data.version = ?
`,
[userId, version],
(error, results, fields) => {
if (error) throw error;
res.json(results);
},
);
}
function getChuniDuelItems(req, res) {
const { userId, version } = req.session.cozynet;
pool1.query(
` SELECT
chuni_item_item.itemId AS duelId,
cozynet_chuni_static_systemvoice.str AS str
FROM
chuni_item_item
LEFT JOIN
cozynet_chuni_static_systemvoice ON chuni_item_item.itemId = cozynet_chuni_static_systemvoice.id
JOIN
chuni_profile_data ON chuni_profile_data.user = chuni_item_item.user
WHERE
chuni_profile_data.user = ?
AND chuni_item_item.itemKind = 9
AND chuni_profile_data.version = ?
`,
[userId, version],
(error, results, fields) => {
if (error) throw error;
res.json(results);
},
);
}
function listRivalsHandler(req, res) {
const { userId, version } = req.session.cozynet;
if (!userId || !version) {
return res
.status(400)
.json({ error: "User and version parameters are required" });
}
pool1.query(
`
SELECT cf.favId AS favId, au.username AS username
FROM chuni_item_favorite cf
JOIN aime_user au ON cf.favId = au.id
JOIN chuni_profile_data pd ON au.id = pd.user
WHERE cf.user = ? AND pd.version = ?
`,
[userId, version],
(error, results) => {
if (error) {
console.error("Error retrieving rivals:", error);
return res.status(500).json({ error: "Error retrieving rivals" });
}
res.json(results);
},
);
}
function listFavoriteSongsHandler(req, res) {
const { userId, version } = req.session.cozynet;
if (!userId || !version) {
return res
.status(400)
.json({ error: "User and version parameters are required" });
}
pool1.query(
`
SELECT DISTINCT
cf.favId AS songId,
cf.favKind,
cf.version,
sm.jacketPath,
sm.title
FROM
chuni_item_favorite cf
JOIN
chuni_static_music sm ON cf.favId = sm.songId
WHERE
cf.user = ?
AND cf.version = ?
`,
[userId, version],
(error, results) => {
if (error) {
console.error("Error retrieving rivals:", error);
return res.status(500).json({ error: "Error retrieving rivals" });
}
res.json(results);
},
);
}
function listFriendsHandler(req, res) {
const { userId, version } = req.session.cozynet;
if (!userId || !version) {
return res
.status(400)
.json({ error: "User and version parameters are required" });
}
pool1.query(
`
SELECT cf.favId AS favId, au.username AS username
FROM chuni_item_favorite cf
JOIN aime_user au ON cf.favId = au.id
JOIN chuni_profile_data pd ON au.id = pd.user
WHERE cf.user = ? AND pd.version = ?
`,
[userId, version],
(error, results) => {
if (error) {
console.error("Error retrieving rivals:", error);
return res.status(500).json({ error: "Error retrieving rivals" });
}
res.json(results);
},
);
}
function getTrophiesHandler(req, res) {
const { userId, version } = req.session.cozynet;
pool1.query(
`
SELECT
cst.str AS name, cst.id AS trophyId, cst.rareType
FROM
cozynet_chuni_static_trophies cst
`,
[userId, version],
(error, results, fields) => {
if (error) {
console.error("Error retrieving items:", error);
return res.status(500).json({ error: "Error retrieving items" });
}
res.json(results);
},
);
}
module.exports = router;