mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 01:22:48 +00:00
wip ui for pubg
This commit is contained in:
19
client/app/pages/Pubg/Pubg.scss
Normal file
19
client/app/pages/Pubg/Pubg.scss
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
@import "../../scss/variables";
|
||||||
|
|
||||||
|
.pubg__container {
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pubg__table {
|
||||||
|
border-collapse: collapse;
|
||||||
|
width: 100%;
|
||||||
|
text-align: left;
|
||||||
|
|
||||||
|
tr {
|
||||||
|
border-top: 1px solid $gray3;
|
||||||
|
}
|
||||||
|
|
||||||
|
td, th {
|
||||||
|
padding: 5px;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,108 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import axios from 'axios';
|
||||||
|
import * as _ from 'lodash';
|
||||||
|
import './Pubg.scss';
|
||||||
|
|
||||||
export class Pubg extends React.Component<any, any> {
|
interface Props {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
players: Player[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Player {
|
||||||
|
PlayerName: string;
|
||||||
|
agg?: any;
|
||||||
|
as?: any;
|
||||||
|
na?: any;
|
||||||
|
sa?: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Pubg extends React.Component<Props, State> {
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.state = {
|
||||||
|
players: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
axios.get("/stats/pubg").then((res) => {
|
||||||
|
|
||||||
|
console.log(res.data);
|
||||||
|
this.setState({
|
||||||
|
players: this.filterData(res.data),
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(this.state.players);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
filterData(data: any): Player[] {
|
||||||
|
return _.map(_.values(data), (data: any) => {
|
||||||
|
|
||||||
|
let regions: any = _.chain(data.Stats).groupBy('Region')
|
||||||
|
/*
|
||||||
|
.mapValues((val: any) => {
|
||||||
|
return _.groupBy(val, 'Match');
|
||||||
|
})
|
||||||
|
*/
|
||||||
|
.value();
|
||||||
|
|
||||||
|
|
||||||
|
_.forIn(regions, (val: any, key: string) => {
|
||||||
|
regions[key] = _.groupBy(val, 'Match');
|
||||||
|
|
||||||
|
_.forIn(regions[key], (val2: any, key2: string) => {
|
||||||
|
//regions[key][key2] = _.groupBy(regions[key][key2][0].Stats, 'field');
|
||||||
|
regions[key][key2] = _.groupBy(_.flatten(regions[key][key2][0].Stats), 'field');
|
||||||
|
_.each(regions[key][key2], s => s = _.flatten(s));
|
||||||
|
|
||||||
|
//console.log(regions[key][key2][0]);
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
//console.log(regions);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...{ PlayerName: data.PlayerName },
|
||||||
|
...regions,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
insertTableData() {
|
||||||
|
|
||||||
|
return this.state.players.map((stat: any, index: number) => {
|
||||||
|
return (
|
||||||
|
<tr key={index}>
|
||||||
|
<td>{stat.PlayerName}</td>
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div>test 123</div>
|
<div className="pubg__container">
|
||||||
|
<div className="card">
|
||||||
|
<div className="card__header">PUBG Stats</div>
|
||||||
|
<table className="pubg__table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Name</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{this.insertTableData()}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
"author": "Mitchell Gerber",
|
"author": "Mitchell Gerber",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/lodash": "^4.14.71",
|
||||||
"@types/react": "^16.0.0",
|
"@types/react": "^16.0.0",
|
||||||
"@types/react-dom": "^15.5.1",
|
"@types/react-dom": "^15.5.1",
|
||||||
"@types/react-dropzone": "^3.13.1",
|
"@types/react-dropzone": "^3.13.1",
|
||||||
@@ -29,6 +30,7 @@
|
|||||||
"extract-text-webpack-plugin": "2.0.0-rc.1",
|
"extract-text-webpack-plugin": "2.0.0-rc.1",
|
||||||
"file-loader": "^0.10.0",
|
"file-loader": "^0.10.0",
|
||||||
"html-webpack-plugin": "^2.24.1",
|
"html-webpack-plugin": "^2.24.1",
|
||||||
|
"lodash": "^4.17.4",
|
||||||
"node-sass": "^4.5.3",
|
"node-sass": "^4.5.3",
|
||||||
"postcss-loader": "^1.2.1",
|
"postcss-loader": "^1.2.1",
|
||||||
"react": "15.6.1",
|
"react": "15.6.1",
|
||||||
|
|||||||
@@ -6,6 +6,10 @@
|
|||||||
version "3.2.1"
|
version "3.2.1"
|
||||||
resolved "https://registry.yarnpkg.com/@types/history/-/history-3.2.1.tgz#0039ab0e0be2a0cc22bac171d27a44588103d123"
|
resolved "https://registry.yarnpkg.com/@types/history/-/history-3.2.1.tgz#0039ab0e0be2a0cc22bac171d27a44588103d123"
|
||||||
|
|
||||||
|
"@types/lodash@^4.14.71":
|
||||||
|
version "4.14.71"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.71.tgz#0dc383f78981216ac76e2f2c3afd998e0450e4c1"
|
||||||
|
|
||||||
"@types/react-dom@^15.5.1":
|
"@types/react-dom@^15.5.1":
|
||||||
version "15.5.1"
|
version "15.5.1"
|
||||||
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-15.5.1.tgz#f3c3e14c682785923c7d64583537df319442dec1"
|
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-15.5.1.tgz#f3c3e14c682785923c7d64583537df319442dec1"
|
||||||
@@ -2955,7 +2959,7 @@ lodash.uniq@^4.3.0:
|
|||||||
version "4.5.0"
|
version "4.5.0"
|
||||||
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
|
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
|
||||||
|
|
||||||
lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.2.0, lodash@^4.3.0:
|
lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0:
|
||||||
version "4.17.4"
|
version "4.17.4"
|
||||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
|
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
|
||||||
|
|
||||||
|
|||||||
@@ -3,5 +3,10 @@
|
|||||||
"BotPrefix": "#",
|
"BotPrefix": "#",
|
||||||
"SoundsPath": "./sounds/",
|
"SoundsPath": "./sounds/",
|
||||||
"UploadPassword": "",
|
"UploadPassword": "",
|
||||||
"ServerAddr": ":80"
|
"ServerAddr": ":80",
|
||||||
|
"pubg": {
|
||||||
|
"apiKey": "",
|
||||||
|
"enabled": true,
|
||||||
|
"players": ["player1"]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
main.go
6
main.go
@@ -16,11 +16,9 @@ func main() {
|
|||||||
|
|
||||||
//add handlers
|
//add handlers
|
||||||
bot.AddHandler(bothandlers.SoundsHandler)
|
bot.AddHandler(bothandlers.SoundsHandler)
|
||||||
// remove gif functionality for not
|
|
||||||
//bot.AddHandler(bothandlers.GifHandler)
|
|
||||||
|
|
||||||
// start new go routine for the discord websockets
|
// start the bot
|
||||||
go bot.Start()
|
bot.Start()
|
||||||
|
|
||||||
// start the web server
|
// start the web server
|
||||||
webserver.Start()
|
webserver.Start()
|
||||||
|
|||||||
@@ -39,18 +39,22 @@ func Connect(token string) {
|
|||||||
|
|
||||||
// Start - blocking function that starts a websocket listenting for discord callbacks
|
// Start - blocking function that starts a websocket listenting for discord callbacks
|
||||||
func Start() {
|
func Start() {
|
||||||
// Open the websocket and begin listening.
|
|
||||||
err := Session.Open()
|
// start new non blocking go routine
|
||||||
if err != nil {
|
go func() {
|
||||||
fmt.Println("error opening connection,", err)
|
// Open the websocket and begin listening.
|
||||||
|
err := Session.Open()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error opening connection,", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Bot is now running...")
|
||||||
|
|
||||||
|
// Simple way to keep program running until CTRL-C is pressed.
|
||||||
|
<-make(chan struct{})
|
||||||
return
|
return
|
||||||
}
|
}()
|
||||||
|
|
||||||
fmt.Println("Bot is now running...")
|
|
||||||
|
|
||||||
// Simple way to keep program running until CTRL-C is pressed.
|
|
||||||
<-make(chan struct{})
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddHandler(handler interface{}) {
|
func AddHandler(handler interface{}) {
|
||||||
|
|||||||
@@ -20,6 +20,11 @@ type configFile struct {
|
|||||||
SoundsPath string `json:"SoundsPath"`
|
SoundsPath string `json:"SoundsPath"`
|
||||||
UploadPassword string `json:"UploadPassword"`
|
UploadPassword string `json:"UploadPassword"`
|
||||||
ServerAddr string `json:"ServerAddr`
|
ServerAddr string `json:"ServerAddr`
|
||||||
|
Pubg struct {
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
APIKey string `json:"apiKey"`
|
||||||
|
Players []string `json:"players"`
|
||||||
|
} `json:"pubg"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type configFlags struct {
|
type configFlags struct {
|
||||||
|
|||||||
54
server/webserver/pubg/pubg.go
Normal file
54
server/webserver/pubg/pubg.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package pubg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mgerb/chi_auth_server/response"
|
||||||
|
pubgClient "github.com/mgerb/go-pubg"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
apiKey string
|
||||||
|
stats = map[string]*pubgClient.Player{}
|
||||||
|
mut = &sync.Mutex{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Start -
|
||||||
|
func Start(key string, players []string) {
|
||||||
|
|
||||||
|
apiKey = key
|
||||||
|
|
||||||
|
log.Println("Gathering pubg data...")
|
||||||
|
|
||||||
|
go fetchStats(players)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchStats(players []string) {
|
||||||
|
|
||||||
|
api := pubgClient.New(apiKey)
|
||||||
|
|
||||||
|
// fetch new stats every 30 seconds
|
||||||
|
for {
|
||||||
|
|
||||||
|
for _, player := range players {
|
||||||
|
newStats := api.GetPlayer(player)
|
||||||
|
|
||||||
|
mut.Lock()
|
||||||
|
stats[player] = newStats
|
||||||
|
mut.Unlock()
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 30)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler - returns the pubg stats
|
||||||
|
func Handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
response.JSON(w, stats)
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/go-chi/chi/middleware"
|
"github.com/go-chi/chi/middleware"
|
||||||
"github.com/mgerb/go-discord-bot/server/config"
|
"github.com/mgerb/go-discord-bot/server/config"
|
||||||
"github.com/mgerb/go-discord-bot/server/webserver/handlers"
|
"github.com/mgerb/go-discord-bot/server/webserver/handlers"
|
||||||
|
"github.com/mgerb/go-discord-bot/server/webserver/pubg"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getRouter() *chi.Mux {
|
func getRouter() *chi.Mux {
|
||||||
@@ -40,12 +41,19 @@ func getRouter() *chi.Mux {
|
|||||||
r.Get("/soundlist", handlers.SoundList)
|
r.Get("/soundlist", handlers.SoundList)
|
||||||
r.Put("/upload", handlers.FileUpload)
|
r.Put("/upload", handlers.FileUpload)
|
||||||
r.Get("/ytdownloader", handlers.Downloader)
|
r.Get("/ytdownloader", handlers.Downloader)
|
||||||
|
r.Get("/stats/pubg", pubg.Handler)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start -
|
// Start -
|
||||||
func Start() {
|
func Start() {
|
||||||
|
|
||||||
|
// start gathering pubg data from the api
|
||||||
|
if config.Config.Pubg.Enabled {
|
||||||
|
pubg.Start(config.Config.Pubg.APIKey, config.Config.Pubg.Players)
|
||||||
|
}
|
||||||
|
|
||||||
router := getRouter()
|
router := getRouter()
|
||||||
|
|
||||||
if config.Flags.TLS {
|
if config.Flags.TLS {
|
||||||
|
|||||||
Reference in New Issue
Block a user