lion-turtle/leadercraft-shopper/main.go

143 lines
3.6 KiB
Go
Raw Normal View History

2020-08-14 14:53:46 -04:00
// NGnius 2020-01-30
package main // leadercraft-server
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"encoding/json"
"strconv"
)
const (
// Version the current version
Version = "0.1"
// Name the program name
Name = "leadercraft-shopper"
)
var (
isClosing bool
printVersionAndExit bool
)
type Criteria struct {
Location [][]float64
GameID int64
Coefficient int64
ScoreMode string
}
func defaultCriteria() Criteria {
return Criteria {
Location: [][]float64 {[]float64{-10000,-10000, -10000}, []float64{10000,10000,10000}},
GameID: 0,
Coefficient: 1_000_000_000,
ScoreMode: "time",
}
}
func init() {
initArgs()
}
func main() {
parseArgs()
sqlInitErr := sqlInit()
if sqlInitErr != nil {
fmt.Printf("Failed to initialise SQL connection: %s\n", sqlInitErr)
os.Exit(1)
}
// handle interrupt (terminate) signal
signalChan := make(chan os.Signal)
signal.Notify(signalChan, os.Interrupt)
go func() {
s := <-signalChan
fmt.Println("Received terminate signal " + s.String())
isClosing = true
sqlClose()
}()
// get popular games
for page := 1; page < 10; page++ {
url := fmt.Sprintf("https://api.steampowered.com/IPublishedFileService/QueryFiles/v1/?key=%s&page=%d&numperpage=%d&appid=%d&requiredtags=%s&return_short_description=true", os.Getenv("STEAM_TOKEN"), page, 100, 1078000, "[\"featured\",\"game\"]")
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
return
}
if resp.StatusCode != 200 {
fmt.Println(resp.Status)
return
}
gamesJSON := make(map[string]interface{})
decoder := json.NewDecoder(resp.Body)
decErr := decoder.Decode(&gamesJSON)
if decErr != nil {
fmt.Println(decErr)
return
}
resp.Body.Close()
games := gamesJSON["response"].(map[string]interface{})["publishedfiledetails"].([]interface{})
//fmt.Println(len(games))
//fmt.Println(gamesJSON["response"].(map[string]interface{})["total"].(float64))
folderErr := os.MkdirAll("criterias/", os.ModeDir | os.ModePerm)
if folderErr != nil {
fmt.Println(folderErr)
}
for _, genGame := range games {
game := genGame.(map[string]interface{})
id, _ := strconv.Atoi(game["publishedfileid"].(string))
id64 := int64(id)
title := game["title"].(string)
desc := game["short_description"].(string)
//fmt.Printf("Game ID:%d title:'%s' desc:'%s'\n", id, title, desc)
gameBoard := LoadBoard(id64)
if gameBoard == nil {
// create new board
gameBoard = &Board{
ID: id64,
Name: title,
Description: desc,
}
gameBoard.Commit()
// create new criteria
crit := defaultCriteria()
crit.GameID = id64
file, createErr := os.Create(fmt.Sprintf("criterias/criteria-%d.json", id64))
if createErr != nil {
fmt.Println(createErr)
} else {
encoder := json.NewEncoder(file)
encoder.Encode(&crit)
file.Close()
}
fmt.Printf("Created new Board ID:%d Name:'%s'\n", id, title)
} else if id64 > 2 {
fmt.Printf("Found existing Board ID:%d Name:'%s'\n", id, gameBoard.Name)
if gameBoard.Name != title || gameBoard.Description != desc {
gameBoard.Name = title
gameBoard.Description = desc
}
gameBoard.Commit()
}
}
}
}
func initArgs() {
flag.BoolVar(&printVersionAndExit, "version", false, "Print version and exit")
flag.StringVar(&sqlConnection, "conn", sqlConnectionDefault, "Database connection string")
flag.StringVar(&sqlServer, "sql", sqlServerDefault, "SQL Database type")
}
func parseArgs() {
flag.Parse()
if printVersionAndExit {
fmt.Println(Name + " v" + Version)
os.Exit(0)
}
}