Skip to content

Commit

Permalink
Extra EspionageReportSummary parsing alaingilbert#2
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashover committed Mar 26, 2020
1 parent bc55fe1 commit eaee8c9
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 134 deletions.
74 changes: 22 additions & 52 deletions extracts_v6.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ func parseRes(input string) (res int64) {
// Deuterium: 4,8M
// Deuterium: 140.000

log.Print("Original input: ", input)
//log.Print("Original input: ", input)

if strings.Contains(input, ":") {
input = strings.Split(input, ":")[1]
Expand All @@ -789,78 +789,48 @@ func parseRes(input string) (res int64) {
input = strings.Replace(input, ",", ".", 1)
}

if strings.Contains(input, "Miljard") {
log.Print(2.1 * 1e9)
log.Print(input)
if strings.Contains(input, "Miljard") { // Todo: Billion in English? Or just return the value as string instead of int64
//log.Print(2.1 * 1e9)
//log.Print(input)

input = strings.Split(input, "M")[0] // regex : \d+\.\d+
log.Print(input, reflect.TypeOf(input))
//log.Print(input, reflect.TypeOf(input))

input = strings.TrimSpace(input)
log.Print(input, reflect.TypeOf(input))
//log.Print(input, reflect.TypeOf(input))

input, err := strconv.ParseFloat(input, 64)
log.Print(input, reflect.TypeOf(input), err)
if err != nil {
log.Print(input, reflect.TypeOf(input), err)
}

inputres := input * 1e9
log.Print(input)

/*
2M ==> 2 => < 10 => * 1000000
2,1M ==> 21 => < 100 => * 100000
2,13M ==> 213 => < 1000 => * 10000
2,123M ==> 2123 => < 10000 => * 1000
*/
// if deuterium < 10 {
// deuteriumres = deuterium + 1000000
// } else if deuterium == 2 {
// deuteriumres = deuterium + 100000
// } else if deuterium == 3 {
// deuteriumres = deuterium + 10000
// } else if deuterium == 4 {
// deuteriumres = deuterium + 1000
// }

log.Print(inputres, reflect.TypeOf(inputres))
//log.Print(input, inputres, reflect.TypeOf(inputres))

inputres2 := int64(inputres)
log.Print(inputres2, reflect.TypeOf(inputres2))
//log.Print(inputres2, reflect.TypeOf(inputres2))

return inputres2
} else if strings.Contains(input, "M") {
log.Print("Sample: ", 2.1 * 1e6)
log.Print(input)
//log.Print("Sample: ", 2.1 * 1e6)
//log.Print(input)

input = strings.Split(input, "M")[0] // regex : \d+\.\d+
log.Print(input, reflect.TypeOf(input))
//log.Print(input, reflect.TypeOf(input))

input = strings.TrimSpace(input)
log.Print(input, reflect.TypeOf(input))
//log.Print(input, reflect.TypeOf(input))

input, err := strconv.ParseFloat(input, 64)
log.Print(input, reflect.TypeOf(input), err)
if err != nil {
log.Print(input, reflect.TypeOf(input), err)
}

inputres := input * 1e6
log.Print(input)

/*
2M ==> 2 => < 10 => * 1000000
2,1M ==> 21 => < 100 => * 100000
2,13M ==> 213 => < 1000 => * 10000
2,123M ==> 2123 => < 10000 => * 1000
*/
// if deuterium < 10 {
// deuteriumres = deuterium + 1000000
// } else if deuterium == 2 {
// deuteriumres = deuterium + 100000
// } else if deuterium == 3 {
// deuteriumres = deuterium + 10000
// } else if deuterium == 4 {
// deuteriumres = deuterium + 1000
// }

log.Print(inputres, reflect.TypeOf(inputres))
//log.Print(input, inputres, reflect.TypeOf(inputres))

inputres2 := int64(inputres)
log.Print(inputres2, reflect.TypeOf(inputres2))
//log.Print(inputres2, reflect.TypeOf(inputres2))

return inputres2
} else {
Expand Down
29 changes: 13 additions & 16 deletions extracts_v71.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"log"

"github.com/PuerkitoBio/goquery"
"github.com/alaingilbert/clockwork"
"golang.org/x/net/html"
Expand Down Expand Up @@ -823,6 +825,7 @@ func extractBuffActivationFromDocV71(doc *goquery.Document) (token string, items
return
}

// This function was initially created to extract MessageIDs from EspionageReportSummary messages. It has since been extended to include all fields, but kept it original name.
func extractEspionageReportMessageIDsFromDocV71(doc *goquery.Document) ([]EspionageReportSummary, int64) {
msgs := make([]EspionageReportSummary, 0)
nbPage, _ := strconv.ParseInt(doc.Find("ul.pagination li").Last().AttrOr("data-page", "1"), 10, 64)
Expand All @@ -836,12 +839,17 @@ func extractEspionageReportMessageIDsFromDocV71(doc *goquery.Document) ([]Espion
report := EspionageReportSummary{ID: id, Type: messageType}
report.From = s.Find("span.msg_sender").Text()

// Added extra logging information (timestamp + linenumbers)
log.SetFlags(log.LstdFlags | log.Lshortfile)

// Username
report.Username = strings.TrimSpace(s.Find("div.compacting").First().Find("span").Eq(1).Text())

// Find PlayerClass
// playerClass := s.Find("div.compacting").First().Find("span").Eq(1).Text()
// report.PlayerClass = strings.TrimSpace(strings.Split(playerClass, ":")[1])
playerClass := s.Find("div.compacting").Eq(1).Text()
if len(playerClass) > 0 && strings.Contains(playerClass, ":") {
report.PlayerClass = strings.TrimSpace(strings.Split(playerClass, ":")[1])
}

// Find target coordinate
spanLink := s.Find("span.msg_title a")
Expand All @@ -863,14 +871,10 @@ func extractEspionageReportMessageIDsFromDocV71(doc *goquery.Document) ([]Espion
}

// LastActivity
log.SetFlags(log.LstdFlags | log.Lshortfile)
report.LastActivity = 0

activity := s.Find("span.msg_content").First().Find("span.ctn.ctn4.fright").First().Text()
activity = strings.TrimSpace(activity)

m := regexp.MustCompile(`(\d{2})`).FindStringSubmatch(activity)

if len(m) > 0 {
minutes := ParseInt(m[1])

Expand All @@ -882,8 +886,6 @@ func extractEspionageReportMessageIDsFromDocV71(doc *goquery.Document) ([]Espion
// CounterEspionage
ceTxt := s.Find("span.msg_content div.compacting").Eq(3).Find("span.fright").Text()
ceTxtRegexp := regexp.MustCompile(`: (\d+)%`).FindStringSubmatch(ceTxt)
log.Print(ceTxt)
log.Print(ceTxtRegexp)
if len(ceTxtRegexp) == 2 {
report.CounterEspionage, _ = strconv.ParseInt(ceTxtRegexp[1], 10, 64)
}
Expand Down Expand Up @@ -940,36 +942,31 @@ log.Print(ceTxtRegexp)
}

// Metal, Crystal, Deuterium
log.SetFlags(log.LstdFlags | log.Lshortfile)

// res := s.Find("div.compacting").Eq(2).Find("span")
res := s.Find("span.resspan").Parent()

metal := res.Find("span.resspan").Eq(0).Text()
crystal := res.Find("span.resspan").Eq(1).Text()
deuterium := res.Find("span.resspan").Eq(2).Text()

log.Print("preParseres = ", metal, crystal, deuterium)
//log.Print("preParseres = ", metal, crystal, deuterium)

report.Metal = parseRes(metal)
report.Crystal = parseRes(crystal)
report.Deuterium = parseRes(deuterium)

log.Print(report.Metal, report.Crystal, report.Deuterium)
//log.Print(report.Metal, report.Crystal, report.Deuterium)

// APIKey
apikey, _ := s.Find("span.icon_apikey").Attr("title")
blaat, _ := goquery.NewDocumentFromReader(strings.NewReader(apikey))
snarfzonk, _ := blaat.Find("input").First().Attr("value")

snarfzonk := blaat.Find("input").First().AttrOr("value", "")
report.APIKey = snarfzonk

// CreatedAt
// <span class="msg_date fright">09.02.2019 23:25:56</span>
msgDate, _ := time.Parse("02.01.2006 15:04:05", s.Find("span.msg_date").Text())
report.CreatedAt = msgDate

//log.Fatal("--end--")
msgs = append(msgs, report)
}
}
Expand Down
2 changes: 0 additions & 2 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"strconv"
"strings"

"log"

"github.com/labstack/echo"
)

Expand Down
2 changes: 1 addition & 1 deletion ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -3347,7 +3347,7 @@ func (b *OGame) sendFleetV6(celestialID CelestialID, ships []Quantifiable, speed
}

// EspionageReportType type of espionage report (action or report)
type EspionageReportType int
type EspionageReportType int64

// Action message received when an enemy is seen naer your planet
const Action EspionageReportType = 0
Expand Down
Loading

0 comments on commit eaee8c9

Please sign in to comment.