2021-04-25 16:21:34 +00:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
2021-04-25 18:24:26 +00:00
|
|
|
"github.com/tribalwarshelp/shared/tw/dataloader"
|
2021-04-25 17:20:38 +00:00
|
|
|
"net/http"
|
2021-04-25 16:21:34 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tribalwarshelp/shared/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func countPlayerVillages(villages []*models.Village) int {
|
|
|
|
count := 0
|
|
|
|
for _, village := range villages {
|
|
|
|
if village.PlayerID != 0 {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDateDifferenceInDays(t1, t2 time.Time) int {
|
|
|
|
diff := t1.Sub(t2)
|
|
|
|
return int(diff.Hours() / 24)
|
|
|
|
}
|
|
|
|
|
|
|
|
func calcPlayerDailyGrowth(diffInDays, points int) int {
|
|
|
|
if diffInDays > 0 {
|
|
|
|
return points / diffInDays
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-04-25 17:20:38 +00:00
|
|
|
func newHTTPClient() *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 18:24:26 +00:00
|
|
|
func newDataloader(url string) dataloader.DataLoader {
|
|
|
|
return dataloader.New(&dataloader.Config{
|
|
|
|
BaseURL: url,
|
|
|
|
Client: newHTTPClient(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-25 16:21:34 +00:00
|
|
|
type tribesSearchableByID struct {
|
|
|
|
tribes []*models.Tribe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (searchable tribesSearchableByID) GetID(index int) int {
|
|
|
|
return searchable.tribes[index].ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (searchable tribesSearchableByID) Len() int {
|
|
|
|
return len(searchable.tribes)
|
|
|
|
}
|
|
|
|
|
|
|
|
type playersSearchableByID struct {
|
|
|
|
players []*models.Player
|
|
|
|
}
|
|
|
|
|
|
|
|
func (searchable playersSearchableByID) GetID(index int) int {
|
|
|
|
return searchable.players[index].ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (searchable playersSearchableByID) Len() int {
|
|
|
|
return len(searchable.players)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ennoblementsSearchableByNewOwnerID struct {
|
|
|
|
ennoblements []*models.Ennoblement
|
|
|
|
}
|
|
|
|
|
|
|
|
func (searchable ennoblementsSearchableByNewOwnerID) GetID(index int) int {
|
|
|
|
return searchable.ennoblements[index].NewOwnerID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (searchable ennoblementsSearchableByNewOwnerID) Len() int {
|
|
|
|
return len(searchable.ennoblements)
|
|
|
|
}
|
|
|
|
|
|
|
|
type searchableByID interface {
|
|
|
|
GetID(index int) int
|
|
|
|
Len() int
|
|
|
|
}
|
|
|
|
|
|
|
|
func makePlayersSearchable(players []*models.Player) searchableByID {
|
|
|
|
return playersSearchableByID{
|
|
|
|
players: players,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTribesSearchable(tribes []*models.Tribe) searchableByID {
|
|
|
|
return tribesSearchableByID{
|
|
|
|
tribes: tribes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchByID(haystack searchableByID, id int) int {
|
|
|
|
low := 0
|
|
|
|
high := haystack.Len() - 1
|
|
|
|
|
|
|
|
for low <= high {
|
|
|
|
median := (low + high) / 2
|
|
|
|
|
|
|
|
if haystack.GetID(median) < id {
|
|
|
|
low = median + 1
|
|
|
|
} else {
|
|
|
|
high = median - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if low == haystack.Len() || haystack.GetID(low) != id {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return low
|
|
|
|
}
|