-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ccd45a
commit cc82005
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package countries | ||
|
||
const flagImageUrl string = "https://flagcdn.com/w320/" | ||
const flagImageFormat string = ".png" | ||
const maxVariantsCount = 6 | ||
const minVariantsCount = 2 | ||
|
||
// error constants | ||
const variantsCountToBig string = "the number of variants is too large" | ||
const variantsCountToLow string = "the number of variants is too small" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package countries | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"log" | ||
"path/filepath" | ||
) | ||
|
||
var countriesDB []CountryData | ||
|
||
func init() { | ||
countriesJson, err := ioutil.ReadFile(filepath.Join("data", "countries.json")) | ||
if err != nil { | ||
log.Printf("error opening file: %v \n", err) | ||
} | ||
|
||
err = json.Unmarshal((countriesJson), &countriesDB) | ||
if err != nil { | ||
log.Printf("error parsing JSON: %v \n", err) | ||
} | ||
} | ||
|
||
func GetCapitalQuiz(variants int) (CapitalQuiz, error) { | ||
if variants > maxVariantsCount { | ||
return CapitalQuiz{}, errors.New(variantsCountToBig) | ||
} | ||
|
||
if variants < minVariantsCount { | ||
return CapitalQuiz{}, errors.New(variantsCountToLow) | ||
} | ||
|
||
result := generateCapitalQuiz(variants) | ||
return result, nil | ||
} | ||
|
||
func GetCountryQuiz(variants int) (CountryQuiz, error) { | ||
if variants > maxVariantsCount { | ||
return CountryQuiz{}, errors.New(variantsCountToBig) | ||
} | ||
|
||
if variants < minVariantsCount { | ||
return CountryQuiz{}, errors.New(variantsCountToLow) | ||
} | ||
|
||
result := generateCountryQuiz(variants) | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package countries | ||
|
||
import ( | ||
"math/rand" | ||
"strings" | ||
|
||
"github.com/cheatsnake/shadify/pkg/assists" | ||
) | ||
|
||
func generateCapitalQuiz(variantsCount int) CapitalQuiz { | ||
variants := make([]string, variantsCount) | ||
randomCountryIdx := rand.Intn(len(countriesDB)) | ||
randomCountry := countriesDB[randomCountryIdx] | ||
flag := flagImageUrl + strings.ToLower(randomCountry.Code) + flagImageFormat | ||
variants[0] = randomCountry.Capital | ||
|
||
for i := 1; i < variantsCount; i += 0 { | ||
randomIdx := rand.Intn(len(countriesDB)) | ||
capital := countriesDB[randomIdx].Capital | ||
|
||
if assists.IndexOf(capital, variants) == -1 { | ||
variants[i] = countriesDB[randomIdx].Capital | ||
i++ | ||
} | ||
} | ||
|
||
rand.Shuffle(len(variants), func(i, j int) { | ||
variants[i], variants[j] = variants[j], variants[i] | ||
}) | ||
|
||
return CapitalQuiz{ | ||
Country: randomCountry.Country, | ||
Flag: flag, | ||
Variants: variants, | ||
Answer: randomCountry.Capital} | ||
} | ||
|
||
func generateCountryQuiz(variantsCount int) CountryQuiz { | ||
variants := make([]string, variantsCount) | ||
randomCountryIdx := rand.Intn(len(countriesDB)) | ||
randomCountry := countriesDB[randomCountryIdx] | ||
flag := flagImageUrl + strings.ToLower(randomCountry.Code) + flagImageFormat | ||
variants[0] = randomCountry.Country | ||
|
||
for i := 1; i < variantsCount; i += 0 { | ||
randomIdx := rand.Intn(len(countriesDB)) | ||
country := countriesDB[randomIdx].Country | ||
|
||
if assists.IndexOf(country, variants) == -1 { | ||
variants[i] = countriesDB[randomIdx].Country | ||
i++ | ||
} | ||
} | ||
|
||
rand.Shuffle(len(variants), func(i, j int) { | ||
variants[i], variants[j] = variants[j], variants[i] | ||
}) | ||
|
||
return CountryQuiz{ | ||
Flag: flag, | ||
Variants: variants, | ||
Answer: randomCountry.Country} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package countries | ||
|
||
type CountryData struct { | ||
Code string `json:"code"` | ||
Country string `json:"country"` | ||
Capital string `json:"capital"` | ||
} | ||
|
||
type CountryQuiz struct { | ||
Flag string `json:"flag"` | ||
Variants []string `json:"variants"` | ||
Answer string `json:"answer"` | ||
} | ||
|
||
type CapitalQuiz struct { | ||
Country string `json:"country"` | ||
Flag string `json:"flag"` | ||
Variants []string `json:"variants"` | ||
Answer string `json:"answer"` | ||
} |