2023-07-13 04:22:52 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
|
|
|
)
|
|
|
|
|
|
|
|
//counterfeiter:generate -o internal/mock/village_repository.gen.go . VillageRepository
|
|
|
|
type VillageRepository interface {
|
|
|
|
SaveTranslateCoordsParams(ctx context.Context, params domain.TranslateVillageCoordsParams) error
|
|
|
|
GetTranslateCoordsParams(ctx context.Context, sha256Hash string) (domain.TranslateVillageCoordsParams, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Village struct {
|
|
|
|
repo VillageRepository
|
|
|
|
twhelpSvc TWHelpService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVillage(repo VillageRepository, twhelpSvc TWHelpService) *Village {
|
|
|
|
return &Village{repo: repo, twhelpSvc: twhelpSvc}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Village) TranslateCoords(
|
|
|
|
ctx context.Context,
|
|
|
|
params domain.TranslateVillageCoordsParams,
|
|
|
|
page int32,
|
|
|
|
) (domain.TranslateVillageCoordsResult, error) {
|
2023-07-13 04:57:17 +00:00
|
|
|
if err := params.IsPageInRange(page); err != nil {
|
|
|
|
return domain.TranslateVillageCoordsResult{}, err
|
2023-07-13 04:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 04:43:06 +00:00
|
|
|
if _, err := v.twhelpSvc.GetServer(ctx, params.VersionCode(), params.ServerKey()); err != nil {
|
|
|
|
return domain.TranslateVillageCoordsResult{}, err
|
|
|
|
}
|
|
|
|
|
2023-07-13 04:22:52 +00:00
|
|
|
if err := v.repo.SaveTranslateCoordsParams(ctx, params); err != nil {
|
|
|
|
return domain.TranslateVillageCoordsResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
low := params.PerPage() * (page - 1)
|
|
|
|
high := params.PerPage() * page
|
|
|
|
if coordsLen := int32(len(params.Coords())); coordsLen < high {
|
|
|
|
high = coordsLen
|
|
|
|
}
|
|
|
|
|
|
|
|
coords := params.Coords()[low:high]
|
|
|
|
|
|
|
|
villages, err := v.twhelpSvc.ListVillagesByCoords(ctx, params.VersionCode(), params.ServerKey(), coords, 0, high-low)
|
|
|
|
if err != nil {
|
|
|
|
return domain.TranslateVillageCoordsResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
notFound := make([]string, 0, high)
|
|
|
|
for _, co := range coords {
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, v := range villages {
|
|
|
|
if co == v.Coords() {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
notFound = append(notFound, co)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return domain.TranslateVillageCoordsResult{
|
|
|
|
Villages: villages,
|
|
|
|
NotFound: notFound,
|
|
|
|
HasNext: params.MaxPage() > page,
|
|
|
|
HasPrev: page > 1,
|
|
|
|
Page: page,
|
|
|
|
MaxPage: params.MaxPage(),
|
|
|
|
ParamsSHA256: params.SHA256(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Village) TranslateCoordsFromHash(ctx context.Context, paramsSHA256Hash string, page int32) (domain.TranslateVillageCoordsResult, error) {
|
|
|
|
params, err := v.repo.GetTranslateCoordsParams(ctx, paramsSHA256Hash)
|
|
|
|
if err != nil {
|
|
|
|
return domain.TranslateVillageCoordsResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.TranslateCoords(ctx, params, page)
|
|
|
|
}
|