From 7629eb6c49375ea9eed85c258734280dcc119d92 Mon Sep 17 00:00:00 2001 From: yudejp Date: Fri, 24 Sep 2021 16:47:45 +0900 Subject: [PATCH] Implement basic user page for Minecraft server --- pages/components/Minecraft/GetName.js | 28 +++++++++++++++++ pages/components/Minecraft/Head.js | 16 ++++++++++ pages/components/Minecraft/LastPlayed.js | 35 ++++++++++++++++++++- pages/components/Minecraft/Online.js | 39 ++++++++++++++++++++++++ pages/components/Minecraft/Players.js | 18 ++++++++--- 5 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 pages/components/Minecraft/GetName.js create mode 100644 pages/components/Minecraft/Head.js create mode 100644 pages/components/Minecraft/Online.js diff --git a/pages/components/Minecraft/GetName.js b/pages/components/Minecraft/GetName.js new file mode 100644 index 0000000..0cbeb82 --- /dev/null +++ b/pages/components/Minecraft/GetName.js @@ -0,0 +1,28 @@ +import React, { useState, useEffect } from 'react'; +import axios from 'axios'; + +function App (props) { + const uuid = props; + const [data, setData] = useState({ hits: [] }); +// const uuid_nohyphen = uuid.uuid.toString().replace(/-/g, ""); + + useEffect(() => { + const fetchData = async () => { + const result = await axios( + 'https://api.ashcon.app/mojang/v2/user/' + uuid.uuid, + ); + setData(result.data); + }; + fetchData(); + }, []); + if (data === undefined){ + console.log("[Minecraft: UUID to player's name] データの取得に失敗しました。 / Failed to retrieve data.") + return + }else { + return {data.username} + }; + + // return

{uuid_nohyphen}

+} + +export default App; \ No newline at end of file diff --git a/pages/components/Minecraft/Head.js b/pages/components/Minecraft/Head.js new file mode 100644 index 0000000..300d708 --- /dev/null +++ b/pages/components/Minecraft/Head.js @@ -0,0 +1,16 @@ +import React, { useState, useEffect } from 'react'; +import Image from 'next/image' + +function App (props) { + const uuid = props; + + return ( + + ) +} + +export default App; \ No newline at end of file diff --git a/pages/components/Minecraft/LastPlayed.js b/pages/components/Minecraft/LastPlayed.js index 9826e90..9515e14 100644 --- a/pages/components/Minecraft/LastPlayed.js +++ b/pages/components/Minecraft/LastPlayed.js @@ -9,6 +9,33 @@ function App (props) { const { locale, locales, defaultLocale, pathname } = router const { t, lang } = useTranslation("common") const [data, setData] = useState({ hits: [] }); + + const timeAgo = (prevDate) => { + const diff = Number(new Date()) - prevDate; + const minute = 60 * 1000; + const hour = minute * 60; + const day = hour * 24; + const month = day * 30; + const year = day * 365; + switch (true) { + case diff < minute: + const seconds = Math.round(diff / 1000); + return `${seconds} 秒前` + case diff < hour: + return Math.round(diff / minute) + ' 分前'; + case diff < day: + return Math.round(diff / hour) + ' 時間前'; + case diff < month: + return Math.round(diff / day) + ' 日前'; + case diff < year: + return Math.round(diff / month) + ' ヶ月前'; + case diff > year: + return Math.round(diff / year) + ' 年前'; + default: + return ""; + } +}; + useEffect(() => { const fetchData = async () => { const result = await axios( @@ -25,7 +52,13 @@ function App (props) { if (data.toString() == "[object Object]") { return

取得中です...

} else { - return

{data.toString()}

+ let dateTime = new Date(parseInt(data.toString()) * 1000); + return ( +
+ {dateTime.toLocaleDateString() + " " + dateTime.toLocaleTimeString()} + 最終ログイン: {timeAgo(dateTime)} +
+ ) } }; } diff --git a/pages/components/Minecraft/Online.js b/pages/components/Minecraft/Online.js new file mode 100644 index 0000000..53fa1e5 --- /dev/null +++ b/pages/components/Minecraft/Online.js @@ -0,0 +1,39 @@ +import React, { useState, useEffect } from 'react'; +import axios from 'axios'; +import useTranslation from 'next-translate/useTranslation' +import { useRouter } from 'next/router' +import LastPlayed from './LastPlayed' + +function App (props) { + const uuid = props; + const router = useRouter() + const { locale, locales, defaultLocale, pathname } = router + const { t, lang } = useTranslation("common") + const [data, setData] = useState({ hits: [] }); + + useEffect(() => { + const fetchData = async () => { + const result = await axios( + 'https://minecraft.yude.jp/online/' + uuid.uuid, + ); + setData(result.data); + }; + fetchData(); + }, []); + if (data === undefined){ + console.log("[Minecraft: オンライン状況] データの取得に失敗しました。 / Failed to retrieve data.") + return

+ }else { + if (data.toString() == "false") { + return + } else { + if (data.toString() == "true") { + return

オンライン

+ } else { + return

取得中...

+ } + } + }; +} + +export default App; \ No newline at end of file diff --git a/pages/components/Minecraft/Players.js b/pages/components/Minecraft/Players.js index a466c4d..2ae24bd 100644 --- a/pages/components/Minecraft/Players.js +++ b/pages/components/Minecraft/Players.js @@ -1,12 +1,22 @@ -import LastPlayed from './LastPlayed' +import Online from './Online' +import Head from './Head' +import GetName from './GetName' function App (props) { const uuid = props; return ( -
-

UUID: {uuid.uuid}

-

Last played:

+
+
+ +
+
+
+

+

UUID: {uuid.uuid}

+

+
+
) }