Skip to content

Commit

Permalink
Add API endpoints for language and pantheons
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Overmyer committed Apr 25, 2019
1 parent 9c10341 commit 363dda1
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 58 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ The API supports the following routes:

**Heraldry, specific seed**: `/heraldry/123`

**Language, random**: `/language/`

**Language, specific seed**: `/language/123`

**Organization, random**: `/organization/`

**Organization, specific seed**: `/organization/123`

**Pantheon, random**: `/pantheon/`

**Pantheon, specific seed**: `/pantheon/123`

**Region, random**: `/region/`

**Region, specific seed**: `/region/123`
Expand Down
56 changes: 56 additions & 0 deletions cmd/worldapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
"github.com/ironarachne/world/pkg/country"
"github.com/ironarachne/world/pkg/culture"
"github.com/ironarachne/world/pkg/heraldry"
"github.com/ironarachne/world/pkg/language"
"github.com/ironarachne/world/pkg/organization"
"github.com/ironarachne/world/pkg/pantheon"
"github.com/ironarachne/world/pkg/random"
"github.com/ironarachne/world/pkg/region"
"github.com/ironarachne/world/pkg/town"
Expand Down Expand Up @@ -131,6 +133,28 @@ func getHeraldryRandom(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(o)
}

func getLanguage(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")

var o language.Language

random.SeedFromString(id)

o = language.Generate()

json.NewEncoder(w).Encode(o)
}

func getLanguageRandom(w http.ResponseWriter, r *http.Request) {
var o language.Language

rand.Seed(time.Now().UnixNano())

o = language.Generate()

json.NewEncoder(w).Encode(o)
}

func getOrganization(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")

Expand All @@ -153,6 +177,32 @@ func getOrganizationRandom(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(o)
}

func getPantheon(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")

var o pantheon.Pantheon
var l language.Language

random.SeedFromString(id)

l = language.Generate()
o = pantheon.Generate(15, l)

json.NewEncoder(w).Encode(o)
}

func getPantheonRandom(w http.ResponseWriter, r *http.Request) {
var o pantheon.Pantheon
var l language.Language

rand.Seed(time.Now().UnixNano())

l = language.Generate()
o = pantheon.Generate(15, l)

json.NewEncoder(w).Encode(o)
}

func getRegion(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")

Expand Down Expand Up @@ -230,9 +280,15 @@ func main() {
r.Get("/heraldry", getHeraldryRandom)
r.Get("/heraldry/{id}", getHeraldry)

r.Get("/language", getLanguageRandom)
r.Get("/language/{id}", getLanguage)

r.Get("/organization", getOrganizationRandom)
r.Get("/organization/{id}", getOrganization)

r.Get("/pantheon", getPantheonRandom)
r.Get("/pantheon/{id}", getPantheon)

r.Get("/region", getRegionRandom)
r.Get("/region/{id}", getRegion)

Expand Down
65 changes: 65 additions & 0 deletions pkg/pantheon/deities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package pantheon

import (
"math/rand"

"github.com/ironarachne/world/pkg/random"
)

// Deity is a fictional god or goddess
type Deity struct {
Name string
Domains []Domain
Appearance string
Gender string
PersonalityTraits []string
}

// GenerateDeity generates a random deity
func (pantheon Pantheon) GenerateDeity() Deity {
var deity Deity
var domain Domain
var allDomains []Domain

domains := getAllDomains()

numberOfDomains := rand.Intn(3) + 1

for _, deity := range pantheon.Deities {
for _, d := range deity.Domains {
allDomains = append(allDomains, d)
}
}

for i := 0; i < numberOfDomains; i++ {
domain = getRandomDomain(domains)

// Only add domain if it isn't already in Domains slice
if !isDomainInSlice(domain, deity.Domains) && !isDomainInSlice(domain, allDomains) {
deity.Domains = append(deity.Domains, domain)
allDomains = append(allDomains, domain)
}
}

appearances := getRandomGeneralAppearances(3)
appearances = append(appearances, getAllAppearancesForDomains(deity.Domains)...)

deity.Appearance = random.String(appearances)
deity.Gender = getRandomGender()

deity.PersonalityTraits = deity.getRandomTraits()

deity.Name = pantheon.Language.RandomName()

return deity
}

func getRandomGender() string {
genders := map[string]int{
"female": 6,
"male": 5,
"none": 1,
}

return random.StringFromThresholdMap(genders)
}
58 changes: 0 additions & 58 deletions pkg/pantheon/pantheon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ import (
"github.com/ironarachne/world/pkg/random"
)

// Deity is a fictional god or goddess
type Deity struct {
Name string
Domains []Domain
Appearance string
Gender string
PersonalityTraits []string
}

// Pantheon is a nonhierarchical group of deities
type Pantheon struct {
Language language.Language
Expand All @@ -30,45 +21,6 @@ type Relationship struct {
Descriptor string
}

// GenerateDeity generates a random deity
func (pantheon Pantheon) GenerateDeity() Deity {
var deity Deity
var domain Domain
var allDomains []Domain

domains := getAllDomains()

numberOfDomains := rand.Intn(3) + 1

for _, deity := range pantheon.Deities {
for _, d := range deity.Domains {
allDomains = append(allDomains, d)
}
}

for i := 0; i < numberOfDomains; i++ {
domain = getRandomDomain(domains)

// Only add domain if it isn't already in Domains slice
if !isDomainInSlice(domain, deity.Domains) && !isDomainInSlice(domain, allDomains) {
deity.Domains = append(deity.Domains, domain)
allDomains = append(allDomains, domain)
}
}

appearances := getRandomGeneralAppearances(3)
appearances = append(appearances, getAllAppearancesForDomains(deity.Domains)...)

deity.Appearance = random.String(appearances)
deity.Gender = getRandomGender()

deity.PersonalityTraits = deity.getRandomTraits()

deity.Name = pantheon.Language.RandomName()

return deity
}

// Generate creates a random pantheon of deities
func Generate(maxSize int, lang language.Language) Pantheon {
var deity Deity
Expand Down Expand Up @@ -119,13 +71,3 @@ func (pantheon Pantheon) GenerateRelationships() []Relationship {

return relationships
}

func getRandomGender() string {
genders := []string{
"female",
"male",
"none",
}

return random.String(genders)
}

0 comments on commit 363dda1

Please sign in to comment.