0
0
mirror of https://github.com/yude-jp/yude.jp synced 2024-11-17 11:02:40 +09:00
yude.jp/pages/components/Minecraft.js

65 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-05-30 12:55:44 +09:00
import React, { useState, useEffect } from 'react';
import useTranslation from 'next-translate/useTranslation'
import { useRouter } from 'next/router'
import axios from 'axios';
import Image from 'next/image'
import { list } from 'postcss';
2021-05-30 12:55:44 +09:00
export default function Minecraft(props) {
const router = useRouter()
const { locale, locales, defaultLocale, pathname } = router
const { t, lang } = useTranslation("minecraft")
const [data, setData] = useState({ hits: [] });
2021-06-14 12:36:18 +09:00
2021-05-30 12:55:44 +09:00
useEffect(() => {
const fetchData = async () => {
const result = await axios(
2021-05-31 10:03:55 +09:00
'https://api.mcsrvstat.us/2/yude.jp',
2021-05-30 12:55:44 +09:00
);
setData(result.data);
};
fetchData();
}, []);
if (data === undefined){
console.log("[Minecraft Query] データの取得に失敗しました。 / Failed to retrieve data.")
return (
2021-06-14 12:36:18 +09:00
<p>{t('minecraft:fail')}</p>
2021-05-30 12:55:44 +09:00
)
}else{
2021-05-30 13:31:17 +09:00
const status = data.online
2021-05-30 13:37:20 +09:00
const player = data.players && data.players.online
2021-06-14 12:36:18 +09:00
2021-05-30 12:55:44 +09:00
return (
2021-06-02 08:23:02 +09:00
<div className="text-center">
2021-05-30 12:55:44 +09:00
{(() => {
2021-05-31 09:59:14 +09:00
if (status == true) {
2021-05-30 13:37:20 +09:00
if (player == undefined || player == 0) {
2021-06-14 12:36:18 +09:00
return <span>{t('minecraft:no_one')}</span>
2021-05-30 12:55:44 +09:00
} else {
2021-06-14 12:36:18 +09:00
return <span>{t('minecraft:playing', {count: player})}</span>
2021-05-31 09:59:14 +09:00
}}else if (status == false) {
2021-06-14 12:36:18 +09:00
return <span>{t('minecraft:offline')}</span>
2021-05-31 09:59:14 +09:00
}else {
2021-06-14 12:36:18 +09:00
return <span>{t('minecraft:loading')}</span>
2021-05-30 12:55:44 +09:00
}
})()}
2021-06-02 08:23:02 +09:00
{(() => {
if (data.players && data.players.list){
var list = [];
var url = [];
for (var i in data.players.list){
url.push('https://mc-heads.net/avatar/' + data.players.list[i])
}
for (var i in url){
2021-06-02 08:23:02 +09:00
list.push(<Image src={url[i]} key={i} className="inline" height="50" width="50" />)
}
return <div>{list}</div>
}
})()}
2021-06-02 08:23:02 +09:00
</div>
2021-05-30 12:55:44 +09:00
)
}
}