1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-09 16:42:48 +00:00

wip ui for pubg

This commit is contained in:
2017-08-02 23:26:39 -05:00
parent 84c2425c6d
commit e425ada97e
10 changed files with 216 additions and 19 deletions

View File

@@ -39,18 +39,22 @@ func Connect(token string) {
// Start - blocking function that starts a websocket listenting for discord callbacks
func Start() {
// Open the websocket and begin listening.
err := Session.Open()
if err != nil {
fmt.Println("error opening connection,", err)
// start new non blocking go routine
go func() {
// 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
}
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{}) {

View File

@@ -20,6 +20,11 @@ type configFile struct {
SoundsPath string `json:"SoundsPath"`
UploadPassword string `json:"UploadPassword"`
ServerAddr string `json:"ServerAddr`
Pubg struct {
Enabled bool `json:"enabled"`
APIKey string `json:"apiKey"`
Players []string `json:"players"`
} `json:"pubg"`
}
type configFlags struct {

View 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)
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/go-chi/chi/middleware"
"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/pubg"
)
func getRouter() *chi.Mux {
@@ -40,12 +41,19 @@ func getRouter() *chi.Mux {
r.Get("/soundlist", handlers.SoundList)
r.Put("/upload", handlers.FileUpload)
r.Get("/ytdownloader", handlers.Downloader)
r.Get("/stats/pubg", pubg.Handler)
return r
}
// 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()
if config.Flags.TLS {