2020-06-25 11:24:06 +00:00
|
|
|
package cron
|
|
|
|
|
|
|
|
import "github.com/tribalwarshelp/shared/models"
|
|
|
|
|
2020-10-24 18:30:28 +00:00
|
|
|
type tribesSearchableByID struct {
|
|
|
|
tribes []*models.Tribe
|
2020-06-25 11:24:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:30:28 +00:00
|
|
|
func (searchable tribesSearchableByID) GetID(index int) int {
|
|
|
|
return searchable.tribes[index].ID
|
2020-06-25 11:24:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:30:28 +00:00
|
|
|
func (searchable tribesSearchableByID) Len() int {
|
|
|
|
return len(searchable.tribes)
|
2020-06-25 11:24:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:30:28 +00:00
|
|
|
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)
|
2020-06-25 11:24:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 11:01:07 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-06-25 11:24:06 +00:00
|
|
|
type searchableByID interface {
|
2020-10-24 18:30:28 +00:00
|
|
|
GetID(index int) int
|
|
|
|
Len() int
|
2020-06-25 11:24:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:30:28 +00:00
|
|
|
func makePlayersSearchable(players []*models.Player) searchableByID {
|
|
|
|
return playersSearchableByID{
|
|
|
|
players: players,
|
2020-06-25 11:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 18:30:28 +00:00
|
|
|
func makeTribesSearchable(tribes []*models.Tribe) searchableByID {
|
|
|
|
return tribesSearchableByID{
|
|
|
|
tribes: tribes,
|
2020-06-25 11:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 18:30:28 +00:00
|
|
|
func searchByID(haystack searchableByID, id int) int {
|
2020-06-25 11:24:06 +00:00
|
|
|
low := 0
|
2020-10-24 18:30:28 +00:00
|
|
|
high := haystack.Len() - 1
|
2020-06-25 11:24:06 +00:00
|
|
|
|
|
|
|
for low <= high {
|
|
|
|
median := (low + high) / 2
|
|
|
|
|
2020-10-24 18:30:28 +00:00
|
|
|
if haystack.GetID(median) < id {
|
2020-06-25 11:24:06 +00:00
|
|
|
low = median + 1
|
|
|
|
} else {
|
|
|
|
high = median - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 18:30:28 +00:00
|
|
|
if low == haystack.Len() || haystack.GetID(low) != id {
|
2020-10-31 11:01:07 +00:00
|
|
|
return -1
|
2020-06-25 11:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return low
|
|
|
|
}
|