Skip to content

Commit

Permalink
Merge branch 'release/48.0.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Sep 15, 2022
2 parents cf67599 + 4083e1d commit 61da082
Show file tree
Hide file tree
Showing 9 changed files with 3,824 additions and 16 deletions.
7 changes: 4 additions & 3 deletions pkg/extractor/v6/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"bytes"
"encoding/json"
"errors"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/utils"
"math"
"net/url"
"regexp"
"strconv"
"strings"
"time"

"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/utils"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
lua "github.com/yuin/gopher-lua"
Expand Down Expand Up @@ -1509,7 +1510,7 @@ func extractUserInfos(pageHTML []byte, lang string) (ogame.UserInfos, error) {
res := ogame.UserInfos{}
res.PlayerID = int64(utils.ToInt(playerIDGroups[1]))
res.PlayerName = string(playerNameGroups[1])
html2 := subHTMLGroups[1]
html2 := []byte(strings.ReplaceAll(string(subHTMLGroups[1]), ",", "."))

infosRgx := regexp.MustCompile(`([\d\\.]+) \(Place ([\d.]+) of ([\d.]+)\)`)
switch lang {
Expand Down
5 changes: 3 additions & 2 deletions pkg/extractor/v874/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/utils"
"regexp"
"strings"
)

func extractBuffActivationFromDoc(doc *goquery.Document) (token string, items []ogame.Item, err error) {
Expand Down
12 changes: 11 additions & 1 deletion pkg/extractor/v9/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package v9

import (
"bytes"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
"github.com/alaingilbert/ogame/pkg/extractor/v874"
v874 "github.com/alaingilbert/ogame/pkg/extractor/v874"
"github.com/alaingilbert/ogame/pkg/ogame"
)

Expand Down Expand Up @@ -83,3 +84,12 @@ func (e *Extractor) ExtractResourcesDetailsFromFullPageFromDoc(doc *goquery.Docu
func (e *Extractor) ExtractConstructions(pageHTML []byte) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64) {
return ExtractConstructions(pageHTML, clockwork.NewRealClock())
}

func (e *Extractor) ExtractResourceSettings(pageHTML []byte) (ogame.ResourceSettings, string, error) {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(pageHTML))
return e.ExtractResourceSettingsFromDoc(doc)
}

func (e *Extractor) ExtractResourceSettingsFromDoc(doc *goquery.Document) (ogame.ResourceSettings, string, error) {
return extractResourceSettingsFromDoc(doc)
}
22 changes: 19 additions & 3 deletions pkg/extractor/v9/extractor_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package v9

import (
"github.com/alaingilbert/clockwork"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
"time"

"github.com/alaingilbert/clockwork"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/stretchr/testify/assert"
)

func TestExtractResourcesDetailsFromFullPage(t *testing.T) {
Expand Down Expand Up @@ -116,3 +117,18 @@ func TestGetConstructions(t *testing.T) {
assert.Equal(t, ogame.ComputerTechnologyID, researchID)
assert.Equal(t, int64(18355), researchCountdown)
}

func TestExtractResourceSettings(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("../../../samples/v9.0.4/en/resource_settings.html")
settings, _, _ := NewExtractor().ExtractResourceSettings(pageHTMLBytes)
assert.Equal(t, ogame.ResourceSettings{MetalMine: 100, CrystalMine: 100, DeuteriumSynthesizer: 100, SolarPlant: 100, FusionReactor: 100, SolarSatellite: 100, Crawler: 100, PlasmaTechnology: 100}, settings)
}

func TestExtractUserInfos(t *testing.T) {
pageHTMLBytes, _ := ioutil.ReadFile("../../../samples/v9.0.4/en/overview.html")
info, err := NewExtractor().ExtractUserInfos(pageHTMLBytes)
assert.NoError(t, err)
assert.Equal(t, int64(30478), info.Points)
assert.Equal(t, int64(1102), info.Rank)
assert.Equal(t, int64(2931), info.Total)
}
48 changes: 43 additions & 5 deletions pkg/extractor/v9/extracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package v9

import (
"errors"
"regexp"
"strings"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
"github.com/alaingilbert/ogame/pkg/extractor/v6"
v6 "github.com/alaingilbert/ogame/pkg/extractor/v6"
v7 "github.com/alaingilbert/ogame/pkg/extractor/v7"
"github.com/alaingilbert/ogame/pkg/extractor/v71"
v71 "github.com/alaingilbert/ogame/pkg/extractor/v71"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/utils"
"regexp"
"strings"
"time"
)

func ExtractConstructions(pageHTML []byte, clock clockwork.Clock) (buildingID ogame.ID, buildingCountdown int64, researchID ogame.ID, researchCountdown int64) {
Expand Down Expand Up @@ -532,3 +533,40 @@ func extractEspionageReportFromDoc(doc *goquery.Document, location *time.Locatio
}
return report, nil
}

func extractResourceSettingsFromDoc(doc *goquery.Document) (ogame.ResourceSettings, string, error) {
bodyID := v6.ExtractBodyIDFromDoc(doc)
if bodyID == "overview" {
return ogame.ResourceSettings{}, "", ogame.ErrInvalidPlanetID
}
vals := make([]int64, 0)
doc.Find("option").Each(func(i int, s *goquery.Selection) {
_, selectedExists := s.Attr("selected")
if selectedExists {
a, _ := s.Attr("value")
val := utils.DoParseI64(a)
vals = append(vals, val)
}
})

if len(vals) != 8 {
return ogame.ResourceSettings{}, "", errors.New("failed to find all resource settings")
}

res := ogame.ResourceSettings{}
res.MetalMine = vals[0]
res.CrystalMine = vals[1]
res.DeuteriumSynthesizer = vals[2]
res.SolarPlant = vals[3]
res.FusionReactor = vals[4]
res.SolarSatellite = vals[5]
res.Crawler = vals[6]
res.PlasmaTechnology = vals[7]

token, exists := doc.Find("form input[name=token]").Attr("value")
if !exists {
return ogame.ResourceSettings{}, "", errors.New("unable to find token")
}

return res, token, nil
}
4 changes: 3 additions & 1 deletion pkg/ogame/resourceSettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type ResourceSettings struct {
FusionReactor int64
SolarSatellite int64
Crawler int64
PlasmaTechnology int64
}

func (r ResourceSettings) String() string {
Expand All @@ -21,5 +22,6 @@ func (r ResourceSettings) String() string {
" Solar Plant: " + utils.FI64(r.SolarPlant) + "\n" +
" Fusion Reactor: " + utils.FI64(r.FusionReactor) + "\n" +
" Solar Satellite: " + utils.FI64(r.SolarSatellite) + "\n" +
" Crawler: " + utils.FI64(r.Crawler)
" Crawler: " + utils.FI64(r.Crawler) + "\n" +
" Plasma Technology: " + utils.FI64(r.PlasmaTechnology)
}
4 changes: 3 additions & 1 deletion pkg/ogame/resourceSettings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestResourceSettings_String(t *testing.T) {
FusionReactor: 5,
SolarSatellite: 6,
Crawler: 7,
PlasmaTechnology: 8,
}
expected := "\n" +
" Metal Mine: 1\n" +
Expand All @@ -23,6 +24,7 @@ func TestResourceSettings_String(t *testing.T) {
" Solar Plant: 4\n" +
" Fusion Reactor: 5\n" +
" Solar Satellite: 6\n" +
" Crawler: 7"
" Crawler: 7\n" +
" Plasma Technology: 8"
assert.Equal(t, expected, r.String())
}
Loading

0 comments on commit 61da082

Please sign in to comment.