A collection of worker programs for server tasks
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

143 строки
3.6KB

  1. // NGnius 2020-01-30
  2. package main // leadercraft-server
  3. import (
  4. "flag"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "os/signal"
  9. "encoding/json"
  10. "strconv"
  11. )
  12. const (
  13. // Version the current version
  14. Version = "0.1"
  15. // Name the program name
  16. Name = "leadercraft-shopper"
  17. )
  18. var (
  19. isClosing bool
  20. printVersionAndExit bool
  21. )
  22. type Criteria struct {
  23. Location [][]float64
  24. GameID int64
  25. Coefficient int64
  26. ScoreMode string
  27. }
  28. func defaultCriteria() Criteria {
  29. return Criteria {
  30. Location: [][]float64 {[]float64{-10000,-10000, -10000}, []float64{10000,10000,10000}},
  31. GameID: 0,
  32. Coefficient: 1_000_000_000,
  33. ScoreMode: "time",
  34. }
  35. }
  36. func init() {
  37. initArgs()
  38. }
  39. func main() {
  40. parseArgs()
  41. sqlInitErr := sqlInit()
  42. if sqlInitErr != nil {
  43. fmt.Printf("Failed to initialise SQL connection: %s\n", sqlInitErr)
  44. os.Exit(1)
  45. }
  46. // handle interrupt (terminate) signal
  47. signalChan := make(chan os.Signal)
  48. signal.Notify(signalChan, os.Interrupt)
  49. go func() {
  50. s := <-signalChan
  51. fmt.Println("Received terminate signal " + s.String())
  52. isClosing = true
  53. sqlClose()
  54. }()
  55. // get popular games
  56. for page := 1; page < 10; page++ {
  57. 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\"]")
  58. resp, err := http.Get(url)
  59. if err != nil {
  60. fmt.Println(err)
  61. return
  62. }
  63. if resp.StatusCode != 200 {
  64. fmt.Println(resp.Status)
  65. return
  66. }
  67. gamesJSON := make(map[string]interface{})
  68. decoder := json.NewDecoder(resp.Body)
  69. decErr := decoder.Decode(&gamesJSON)
  70. if decErr != nil {
  71. fmt.Println(decErr)
  72. return
  73. }
  74. resp.Body.Close()
  75. games := gamesJSON["response"].(map[string]interface{})["publishedfiledetails"].([]interface{})
  76. //fmt.Println(len(games))
  77. //fmt.Println(gamesJSON["response"].(map[string]interface{})["total"].(float64))
  78. folderErr := os.MkdirAll("criterias/", os.ModeDir | os.ModePerm)
  79. if folderErr != nil {
  80. fmt.Println(folderErr)
  81. }
  82. for _, genGame := range games {
  83. game := genGame.(map[string]interface{})
  84. id, _ := strconv.Atoi(game["publishedfileid"].(string))
  85. id64 := int64(id)
  86. title := game["title"].(string)
  87. desc := game["short_description"].(string)
  88. //fmt.Printf("Game ID:%d title:'%s' desc:'%s'\n", id, title, desc)
  89. gameBoard := LoadBoard(id64)
  90. if gameBoard == nil {
  91. // create new board
  92. gameBoard = &Board{
  93. ID: id64,
  94. Name: title,
  95. Description: desc,
  96. }
  97. gameBoard.Commit()
  98. // create new criteria
  99. crit := defaultCriteria()
  100. crit.GameID = id64
  101. file, createErr := os.Create(fmt.Sprintf("criterias/criteria-%d.json", id64))
  102. if createErr != nil {
  103. fmt.Println(createErr)
  104. } else {
  105. encoder := json.NewEncoder(file)
  106. encoder.Encode(&crit)
  107. file.Close()
  108. }
  109. fmt.Printf("Created new Board ID:%d Name:'%s'\n", id, title)
  110. } else if id64 > 2 {
  111. fmt.Printf("Found existing Board ID:%d Name:'%s'\n", id, gameBoard.Name)
  112. if gameBoard.Name != title || gameBoard.Description != desc {
  113. gameBoard.Name = title
  114. gameBoard.Description = desc
  115. }
  116. gameBoard.Commit()
  117. }
  118. }
  119. }
  120. }
  121. func initArgs() {
  122. flag.BoolVar(&printVersionAndExit, "version", false, "Print version and exit")
  123. flag.StringVar(&sqlConnection, "conn", sqlConnectionDefault, "Database connection string")
  124. flag.StringVar(&sqlServer, "sql", sqlServerDefault, "SQL Database type")
  125. }
  126. func parseArgs() {
  127. flag.Parse()
  128. if printVersionAndExit {
  129. fmt.Println(Name + " v" + Version)
  130. os.Exit(0)
  131. }
  132. }