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{
|
||||
db: h.db.WithParam("SERVER", pg.Safe(server.Key)),
|
||||
baseURL: url,
|
||||
server: server,
|
||||
}
|
||||
count++
|
||||
wg.Add(1)
|
||||
go func(server *models.Server, sh *updateServerDataHandler) {
|
||||
defer wg.Done()
|
||||
log.Printf("%s: Updating", server.Key)
|
||||
log.Printf("%s: updating data", server.Key)
|
||||
if err := sh.update(); err != nil {
|
||||
log.Println(errors.Wrap(err, server.Key))
|
||||
return
|
||||
} else {
|
||||
log.Printf("%s: updated", server.Key)
|
||||
log.Printf("%s: data updated", server.Key)
|
||||
}
|
||||
}(server, sh)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,9 @@ package cron
|
|||
import (
|
||||
"compress/gzip"
|
||||
"encoding/csv"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
@ -27,3 +29,17 @@ func getCSVData(url string, compressed bool) ([][]string, error) {
|
|||
}
|
||||
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"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/tribalwarshelp/shared/models"
|
||||
|
||||
|
@ -13,21 +14,25 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
endpointPlayers = "/map/player.txt"
|
||||
endpointTribe = "/map/ally.txt"
|
||||
endpointVillage = "/map/village.txt"
|
||||
endpointKillAtt = "/map/kill_att.txt"
|
||||
endpointKillDef = "/map/kill_def.txt"
|
||||
endpointKillSup = "/map/kill_sup.txt"
|
||||
endpointKillAll = "/map/kill_all.txt"
|
||||
endpointKillAttTribe = "/map/kill_att_tribe.txt"
|
||||
endpointKillDefTribe = "/map/kill_def_tribe.txt"
|
||||
endpointKillAllTribe = "/map/kill_all_tribe.txt"
|
||||
endpointConfig = "/interface.php?func=get_config"
|
||||
endpointUnitConfig = "/interface.php?func=get_unit_info"
|
||||
endpointBuildingConfig = "/interface.php?func=get_building_info"
|
||||
endpointPlayers = "/map/player.txt"
|
||||
endpointTribe = "/map/ally.txt"
|
||||
endpointVillage = "/map/village.txt"
|
||||
endpointKillAtt = "/map/kill_att.txt"
|
||||
endpointKillDef = "/map/kill_def.txt"
|
||||
endpointKillSup = "/map/kill_sup.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 {
|
||||
baseURL string
|
||||
db *pg.DB
|
||||
server *models.Server
|
||||
}
|
||||
|
||||
type parsedODLine struct {
|
||||
|
@ -290,6 +295,36 @@ func (h *updateServerDataHandler) getVillages() ([]*models.Village, error) {
|
|||
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 {
|
||||
pod, err := h.getOD(false)
|
||||
if err != nil {
|
||||
|
@ -311,6 +346,18 @@ func (h *updateServerDataHandler) update() error {
|
|||
if err != nil {
|
||||
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()
|
||||
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()
|
||||
}
|
||||
|
||||
|
|
2
go.mod
2
go.mod
|
@ -8,6 +8,6 @@ require (
|
|||
github.com/joho/godotenv v1.3.0
|
||||
github.com/pkg/errors v0.9.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
|
||||
)
|
||||
|
|
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/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/tribalwarshelp/shared v0.0.0-20200618150949-896cd6c98d06 h1:T87o4fice0XTUdAG/tctFCDiuyUGb10bY1hdWvwlrro=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200618150949-896cd6c98d06/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200619133428-b2cab5452bdc h1:Rx6Mi32rNCp0YZ2KfVDBttPCZ4UMC3WWjyHGMhAbJ/4=
|
||||
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.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6czKAd94=
|
||||
github.com/vmihailenco/bufpool v0.1.11/go.mod h1:AFf/MOy3l2CFTKbxwt0mp2MwnqjNEs5H/UxrkA5jxTQ=
|
||||
|
|
Reference in New Issue