fetch server config/unit_config/building_config
This commit is contained in:
parent
80baa079e2
commit
fc6d815404
|
@ -179,16 +179,18 @@ func (h *handler) updateData() {
|
||||||
sh := &updateServerDataHandler{
|
sh := &updateServerDataHandler{
|
||||||
db: h.db.WithParam("SERVER", pg.Safe(server.Key)),
|
db: h.db.WithParam("SERVER", pg.Safe(server.Key)),
|
||||||
baseURL: url,
|
baseURL: url,
|
||||||
|
server: server,
|
||||||
}
|
}
|
||||||
count++
|
count++
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(server *models.Server, sh *updateServerDataHandler) {
|
go func(server *models.Server, sh *updateServerDataHandler) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
log.Printf("%s: Updating", server.Key)
|
log.Printf("%s: updating data", server.Key)
|
||||||
if err := sh.update(); err != nil {
|
if err := sh.update(); err != nil {
|
||||||
log.Println(errors.Wrap(err, server.Key))
|
log.Println(errors.Wrap(err, server.Key))
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
log.Printf("%s: updated", server.Key)
|
log.Printf("%s: data updated", server.Key)
|
||||||
}
|
}
|
||||||
}(server, sh)
|
}(server, sh)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,9 @@ package cron
|
||||||
import (
|
import (
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
|
"encoding/xml"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,3 +29,17 @@ func getCSVData(url string, compressed bool) ([][]string, error) {
|
||||||
}
|
}
|
||||||
return uncompressAndGetCsvLines(resp.Body)
|
return uncompressAndGetCsvLines(resp.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getXML(url string, decode interface{}) error {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
bytes, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return xml.Unmarshal(bytes, decode)
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/tribalwarshelp/shared/models"
|
"github.com/tribalwarshelp/shared/models"
|
||||||
|
|
||||||
|
@ -13,21 +14,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
endpointPlayers = "/map/player.txt"
|
endpointConfig = "/interface.php?func=get_config"
|
||||||
endpointTribe = "/map/ally.txt"
|
endpointUnitConfig = "/interface.php?func=get_unit_info"
|
||||||
endpointVillage = "/map/village.txt"
|
endpointBuildingConfig = "/interface.php?func=get_building_info"
|
||||||
endpointKillAtt = "/map/kill_att.txt"
|
endpointPlayers = "/map/player.txt"
|
||||||
endpointKillDef = "/map/kill_def.txt"
|
endpointTribe = "/map/ally.txt"
|
||||||
endpointKillSup = "/map/kill_sup.txt"
|
endpointVillage = "/map/village.txt"
|
||||||
endpointKillAll = "/map/kill_all.txt"
|
endpointKillAtt = "/map/kill_att.txt"
|
||||||
endpointKillAttTribe = "/map/kill_att_tribe.txt"
|
endpointKillDef = "/map/kill_def.txt"
|
||||||
endpointKillDefTribe = "/map/kill_def_tribe.txt"
|
endpointKillSup = "/map/kill_sup.txt"
|
||||||
endpointKillAllTribe = "/map/kill_all_tribe.txt"
|
endpointKillAll = "/map/kill_all.txt"
|
||||||
|
endpointKillAttTribe = "/map/kill_att_tribe.txt"
|
||||||
|
endpointKillDefTribe = "/map/kill_def_tribe.txt"
|
||||||
|
endpointKillAllTribe = "/map/kill_all_tribe.txt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type updateServerDataHandler struct {
|
type updateServerDataHandler struct {
|
||||||
baseURL string
|
baseURL string
|
||||||
db *pg.DB
|
db *pg.DB
|
||||||
|
server *models.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
type parsedODLine struct {
|
type parsedODLine struct {
|
||||||
|
@ -290,6 +295,36 @@ func (h *updateServerDataHandler) getVillages() ([]*models.Village, error) {
|
||||||
return villages, nil
|
return villages, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *updateServerDataHandler) getConfig() (*models.Config, error) {
|
||||||
|
url := h.baseURL + endpointConfig
|
||||||
|
cfg := &models.Config{}
|
||||||
|
err := getXML(url, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "getConfig")
|
||||||
|
}
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *updateServerDataHandler) getBuildingConfig() (*models.BuildingConfig, error) {
|
||||||
|
url := h.baseURL + endpointBuildingConfig
|
||||||
|
cfg := &models.BuildingConfig{}
|
||||||
|
err := getXML(url, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "getBuildingConfig")
|
||||||
|
}
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *updateServerDataHandler) getUnitConfig() (*models.UnitConfig, error) {
|
||||||
|
url := h.baseURL + endpointUnitConfig
|
||||||
|
cfg := &models.UnitConfig{}
|
||||||
|
err := getXML(url, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "getUnitConfig")
|
||||||
|
}
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (h *updateServerDataHandler) update() error {
|
func (h *updateServerDataHandler) update() error {
|
||||||
pod, err := h.getOD(false)
|
pod, err := h.getOD(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -311,6 +346,18 @@ func (h *updateServerDataHandler) update() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
cfg, err := h.getConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
buildingCfg, err := h.getBuildingConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
unitCfg, err := h.getUnitConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
tx, err := h.db.Begin()
|
tx, err := h.db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -394,6 +441,14 @@ func (h *updateServerDataHandler) update() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.server.Config = *cfg
|
||||||
|
h.server.UnitConfig = *unitCfg
|
||||||
|
h.server.BuildingConfig = *buildingCfg
|
||||||
|
h.server.DataUpdatedAt = time.Now()
|
||||||
|
if err := tx.Update(h.server); err != nil {
|
||||||
|
return errors.Wrap(err, "cannot update server")
|
||||||
|
}
|
||||||
|
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -8,6 +8,6 @@ require (
|
||||||
github.com/joho/godotenv v1.3.0
|
github.com/joho/godotenv v1.3.0
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
github.com/tribalwarshelp/shared v0.0.0-20200618150949-896cd6c98d06
|
github.com/tribalwarshelp/shared v0.0.0-20200619133428-b2cab5452bdc
|
||||||
golang.org/x/net v0.0.0-20200528225125-3c3fba18258b // indirect
|
golang.org/x/net v0.0.0-20200528225125-3c3fba18258b // indirect
|
||||||
)
|
)
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -88,8 +88,8 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
|
||||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
|
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
|
||||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
|
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
|
||||||
github.com/tribalwarshelp/shared v0.0.0-20200618150949-896cd6c98d06 h1:T87o4fice0XTUdAG/tctFCDiuyUGb10bY1hdWvwlrro=
|
github.com/tribalwarshelp/shared v0.0.0-20200619133428-b2cab5452bdc h1:Rx6Mi32rNCp0YZ2KfVDBttPCZ4UMC3WWjyHGMhAbJ/4=
|
||||||
github.com/tribalwarshelp/shared v0.0.0-20200618150949-896cd6c98d06/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
|
github.com/tribalwarshelp/shared v0.0.0-20200619133428-b2cab5452bdc/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
|
||||||
github.com/vmihailenco/bufpool v0.1.5/go.mod h1:fL9i/PRTuS7AELqAHwSU1Zf1c70xhkhGe/cD5ud9pJk=
|
github.com/vmihailenco/bufpool v0.1.5/go.mod h1:fL9i/PRTuS7AELqAHwSU1Zf1c70xhkhGe/cD5ud9pJk=
|
||||||
github.com/vmihailenco/bufpool v0.1.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6czKAd94=
|
github.com/vmihailenco/bufpool v0.1.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6czKAd94=
|
||||||
github.com/vmihailenco/bufpool v0.1.11/go.mod h1:AFf/MOy3l2CFTKbxwt0mp2MwnqjNEs5H/UxrkA5jxTQ=
|
github.com/vmihailenco/bufpool v0.1.11/go.mod h1:AFf/MOy3l2CFTKbxwt0mp2MwnqjNEs5H/UxrkA5jxTQ=
|
||||||
|
|
Reference in New Issue
Block a user