Add consumer for responses by Tunakill Rankingsystem
This commit is contained in:
37
src/App.tsx
37
src/App.tsx
@@ -2,12 +2,41 @@ import React from 'react';
|
||||
import './App.scss';
|
||||
import Mock from "./mock/Mock";
|
||||
import TableEntry from "./models/TableEntry";
|
||||
import {SinusBot} from "@support-pp/sinusbot-ts";
|
||||
import SinusBotService from "./services/SinusBotService";
|
||||
|
||||
export default class App extends React.Component {
|
||||
|
||||
private tableContent: TableEntry[] = Mock.tableContent;
|
||||
//private tableContent: TableEntry[] = Mock.tableContent;
|
||||
|
||||
state = {
|
||||
error: null,
|
||||
isLoaded: false,
|
||||
users: [],
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
fetch('https://sinusbot.humenius.me/api/v1/bot/i/51314e26-13a1-4779-b2ac-e36d493687d4/event/tunakill_rank_all_user')
|
||||
.then(res => res.json())
|
||||
.then((data) => SinusBotService.consumeTunakillResponse(data))
|
||||
.then(
|
||||
(result) => {
|
||||
this.setState({
|
||||
isLoaded: true,
|
||||
users: result
|
||||
});
|
||||
},
|
||||
(error) => {
|
||||
this.setState({
|
||||
isLoaded: true,
|
||||
error: error
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
render() {
|
||||
const { error, isLoaded, users } = this.state;
|
||||
return (
|
||||
<div className="App">
|
||||
<p className="title">Humenius' TeamSpeak 3-Ranking</p>
|
||||
@@ -21,8 +50,8 @@ export default class App extends React.Component {
|
||||
</thead>
|
||||
<tbody>
|
||||
{
|
||||
this.tableContent.map((tableEntry, i) => {
|
||||
let className = "";
|
||||
users.map((tableEntry: TableEntry, i) => {
|
||||
let className;
|
||||
switch (i+1) {
|
||||
case 1: className = "first-place"; break;
|
||||
case 2: className = "second-place"; break;
|
||||
@@ -32,7 +61,7 @@ export default class App extends React.Component {
|
||||
|
||||
return (
|
||||
<tr key={i} className={className}>
|
||||
<td>{tableEntry.placement}</td>
|
||||
<td>{i+1}</td>
|
||||
<td>{tableEntry.name}</td>
|
||||
<td>{tableEntry.onlineTime}</td>
|
||||
</tr>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
interface TableEntry {
|
||||
placement: number;
|
||||
name: string;
|
||||
onlineTime: string;
|
||||
}
|
||||
|
||||
7
src/models/TunaKillUser.ts
Normal file
7
src/models/TunaKillUser.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
interface TunaKillUser {
|
||||
name: string;
|
||||
time: number;
|
||||
uid: string;
|
||||
}
|
||||
|
||||
export default TunaKillUser;
|
||||
40
src/services/SinusBotService.ts
Normal file
40
src/services/SinusBotService.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import TableEntry from "../models/TableEntry";
|
||||
import TunaKillUser from "../models/TunaKillUser";
|
||||
|
||||
class SinusBotService {
|
||||
public static consumeTunakillResponse(data: any): TableEntry[] | undefined {
|
||||
if (!(data !== null || data[0] !== null || data[0].data !== null)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const response = data[0].data;
|
||||
if (!(response.length > 0)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return response
|
||||
.map((user: TunaKillUser) => {
|
||||
if (user.name === "Server Query Admin") { return null; }
|
||||
return {
|
||||
name: user.name,
|
||||
rawTime: user.time,
|
||||
onlineTime: new Date(user.time * 1000)
|
||||
.toISOString()
|
||||
.substr(11, 8)
|
||||
};
|
||||
})
|
||||
.sort((a: any, b: any) => {
|
||||
if (a.rawTime < b.rawTime) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (a.rawTime > b.rawTime) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default SinusBotService;
|
||||
Reference in New Issue
Block a user