Skip to content

Commit

Permalink
Merge branch 'release/39.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed May 26, 2020
2 parents 7402b57 + 1e1ed91 commit f879cf7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 2 additions & 0 deletions extracts_v7.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ func extractExpeditionMessagesFromDocV7(doc *goquery.Document, location *time.Lo

func extractMarketplaceMessagesFromDocV7(doc *goquery.Document, location *time.Location) ([]MarketplaceMessage, int64, error) {
msgs := make([]MarketplaceMessage, 0)
tab, _ := strconv.ParseInt(doc.Find("ul.pagination li").Last().AttrOr("data-tab", ""), 10, 64)
nbPage, _ := strconv.ParseInt(doc.Find("ul.pagination li").Last().AttrOr("data-page", "1"), 10, 64)
doc.Find("li.msg").Each(func(i int, s *goquery.Selection) {
if idStr, exists := s.Attr("data-msg-id"); exists {
Expand All @@ -719,6 +720,7 @@ func extractMarketplaceMessagesFromDocV7(doc *goquery.Document, location *time.L
marketTransactionID, _ = strconv.ParseInt(marketTransactionIDStr, 10, 64)
}
msg := MarketplaceMessage{ID: id}
msg.Type = tab
msg.CreatedAt, _ = time.ParseInLocation("02.01.2006 15:04:05", s.Find(".msg_date").Text(), location)
msg.Token = token
msg.MarketTransactionID = marketTransactionID
Expand Down
35 changes: 29 additions & 6 deletions ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -3765,6 +3765,7 @@ type ExpeditionMessage struct {
// MarketplaceMessage ...
type MarketplaceMessage struct {
ID int64
Type int64 // 26: purchases, 27: sales
CreatedAt time.Time
Token string
MarketTransactionID int64
Expand Down Expand Up @@ -3832,9 +3833,11 @@ func (b *OGame) collectAllMarketplaceMessages() error {
msgs := make([]MarketplaceMessage, 0)
msgs = append(msgs, purchases...)
msgs = append(msgs, sales...)
newToken := ""
var err error
for _, msg := range msgs {
if msg.MarketTransactionID != 0 {
err := b.collectMarketplaceMessage(msg)
newToken, err = b.collectMarketplaceMessage(msg, newToken)
if err != nil {
return err
}
Expand All @@ -3843,17 +3846,37 @@ func (b *OGame) collectAllMarketplaceMessages() error {
return nil
}

func (b *OGame) collectMarketplaceMessage(msg MarketplaceMessage) error {
payload := url.Values{
type collectMarketplaceResponse struct {
MarketTransactionID int `json:"marketTransactionId"`
Status string `json:"status"`
Message string `json:"message"`
StatusMessage string `json:"statusMessage"`
NewToken string `json:"newToken"`
Components []interface{} `json:"components"`
}

func (b *OGame) collectMarketplaceMessage(msg MarketplaceMessage, newToken string) (string, error) {
params := url.Values{
"page": {"componentOnly"},
"component": {"marketplace"},
"action": {"collectPrice"},
"marketTransactionId": {strconv.FormatInt(msg.MarketTransactionID, 10)},
"token": {msg.Token},
"asJson": {"1"},
}
_, err := b.postPageContent(url.Values{"page": {"messages"}}, payload)
return err
if msg.Type == 26 { // purchase
params.Set("action", "collectItem")
} else if msg.Type == 27 { // sale
params.Set("action", "collectPrice")
}
payload := url.Values{
"newToken": {newToken},
}
by, err := b.postPageContent(params, payload)
var res collectMarketplaceResponse
if err := json.Unmarshal(by, &res); err != nil {
return "", errors.New("failed to unmarshal json response: " + err.Error())
}
return res.NewToken, err
}

func (b *OGame) getMarketplacePurchasesMessages() ([]MarketplaceMessage, error) {
Expand Down
1 change: 1 addition & 0 deletions ogame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ func TestExtractMarketplaceMessages(t *testing.T) {
msgs, _, _ := NewExtractorV7().ExtractMarketplaceMessages(pageHTMLBytes, time.FixedZone("OGT", 3600))
assert.Equal(t, 9, len(msgs))
assert.Equal(t, int64(12912161), msgs[3].ID)
assert.Equal(t, int64(27), msgs[3].Type)
assert.Equal(t, int64(1379), msgs[3].MarketTransactionID)
assert.Equal(t, "164ba9f6e5cbfdaa03c061730767d779", msgs[3].Token)
}
Expand Down
3 changes: 2 additions & 1 deletion prioritize.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@ func (b *Prioritize) CollectAllMarketplaceMessages() error {
func (b *Prioritize) CollectMarketplaceMessage(msg MarketplaceMessage) error {
b.begin("CollectMarketplaceMessage")
defer b.done()
return b.bot.collectMarketplaceMessage(msg)
_, err := b.bot.collectMarketplaceMessage(msg, "")
return err
}

// GetExpeditionMessages gets the expedition messages
Expand Down

0 comments on commit f879cf7

Please sign in to comment.