Using goroutines and channels between the different information functions

This commit is contained in:
Thomas Schwery 2017-02-23 12:01:37 +01:00
parent bb7be9b62a
commit 9244802e7e

69
main.go
View file

@ -120,13 +120,34 @@ func main() {
transport := httptransport.NewWithClient("esi.tech.ccp.is", "/latest", []string{"https"}, client)
swaggerclient := ESI.New(transport, strfmt.Default)
printCharacterInformation(swaggerclient, m)
charInfoChan := make(chan string)
cloneInfoChan := make(chan string)
planetInfoChan := make(chan string)
skillsInfoChan := make(chan string)
go printCharacterInformation(charInfoChan, swaggerclient, m)
go printClonesInformation(cloneInfoChan, swaggerclient, m)
go printCharacterPlanets(planetInfoChan, swaggerclient, m)
go printCharacterSkillQueue(skillsInfoChan, swaggerclient, m)
for msg := range charInfoChan {
fmt.Print(msg)
}
fmt.Printf("\n\nClones\n")
for msg := range cloneInfoChan {
fmt.Print(msg)
}
fmt.Printf("\n\nPlanetary interaction\n")
printCharacterPlanets(swaggerclient, m)
for msg := range planetInfoChan {
fmt.Print(msg)
}
fmt.Printf("\n\nSkill queue\n")
printCharacterSkillQueue(swaggerclient, m)
for msg := range skillsInfoChan {
fmt.Print(msg)
}
}
func readConfigurationFile() error {
@ -173,7 +194,7 @@ func getCharacterInfo(client *http.Client) (*Character, error) {
return &m, nil
}
func printCharacterSkillQueue(swaggerclient *ESI.App, m *Character) {
func printCharacterSkillQueue(outChan chan string, swaggerclient *ESI.App, m *Character) {
callParam := ESISkills.NewGetCharactersCharacterIDSkillqueueParams()
callParam.WithCharacterID(m.CharacterID)
@ -216,7 +237,7 @@ func printCharacterSkillQueue(swaggerclient *ESI.App, m *Character) {
// The queue is only updated when the user logs in with the client
// we thus need to do the computations and filtering ourselves
if finishDate.Before(time.Now()) {
fmt.Printf("✔ %"+maxSkillFormat+"s %d\n",
outChan <- fmt.Sprintf("✔ %"+maxSkillFormat+"s %d\n",
name,
*skill.FinishedLevel,
)
@ -224,7 +245,7 @@ func printCharacterSkillQueue(swaggerclient *ESI.App, m *Character) {
}
if startDate.Before(time.Now()) {
fmt.Printf("%s %"+maxSkillFormat+"s %d, %s - ends on %s (%s)\n",
outChan <- fmt.Sprintf("%s %"+maxSkillFormat+"s %d, %s - ends on %s (%s)\n",
aurora.Green("➠").Bold(),
name,
*skill.FinishedLevel,
@ -235,7 +256,7 @@ func printCharacterSkillQueue(swaggerclient *ESI.App, m *Character) {
continue
}
fmt.Printf(" %"+maxSkillFormat+"s %d, %s - ends on %s (%s)\n",
outChan <- fmt.Sprintf(" %"+maxSkillFormat+"s %d, %s - ends on %s (%s)\n",
name,
*skill.FinishedLevel,
formatDuration(skillDuration, 2),
@ -243,6 +264,8 @@ func printCharacterSkillQueue(swaggerclient *ESI.App, m *Character) {
formatDuration(finishDuration, 3),
)
}
close(outChan)
}
func formatDuration(duration time.Duration, daysLength int32) string {
@ -253,7 +276,7 @@ func formatDuration(duration time.Duration, daysLength int32) string {
return fmt.Sprintf("%"+fmt.Sprintf("%d", daysLength)+"dd %02d:%02d", days, hours, minutes)
}
func printCharacterInformation(swaggerclient *ESI.App, m *Character) {
func printCharacterInformation(outChan chan string, swaggerclient *ESI.App, m *Character) {
callParam := ESIWallet.NewGetCharactersCharacterIDWalletsParams()
callParam.WithCharacterID(m.CharacterID)
@ -267,11 +290,11 @@ func printCharacterInformation(swaggerclient *ESI.App, m *Character) {
ac := accounting.Accounting{Symbol: "ISK ", Precision: 0, Thousand: "'"}
fmt.Printf("Name: %s\n", m.CharacterName)
outChan <- fmt.Sprintf("Name: %s\n", m.CharacterName)
for _, wallet := range wallets {
if wallet.Balance > 0 {
fmt.Printf("Wallet: %s\n", ac.FormatMoney(wallet.Balance/100))
outChan <- fmt.Sprintf("Wallet: %s\n", ac.FormatMoney(wallet.Balance/100))
}
}
@ -299,11 +322,15 @@ func printCharacterInformation(swaggerclient *ESI.App, m *Character) {
stationName = universeNames[*position.StationID]
}
fmt.Printf("Currently in %s - %s\n",
outChan <- fmt.Sprintf("Currently in %s - %s\n",
universeNames[*position.SolarSystemID],
stationName,
)
close(outChan)
}
func printClonesInformation(outChan chan string, swaggerclient *ESI.App, m *Character) {
cloCallParams := ESIClones.NewGetCharactersCharacterIDClonesParams()
cloCallParams.WithCharacterID(m.CharacterID)
@ -315,11 +342,10 @@ func printCharacterInformation(swaggerclient *ESI.App, m *Character) {
clones := cloresponse.Payload.JumpClones
fmt.Printf("\nClones: \n")
for _, clone := range clones {
sInfo, _ := getStructureStationInfo(swaggerclient, clone.LocationType, clone.LocationID)
fmt.Printf(" %s, %s, %s\n ",
outChan <- fmt.Sprintf(" %s, %s, %s\n ",
sInfo.Name,
sInfo.System.ConstellationName,
sInfo.System.RegionName,
@ -328,10 +354,13 @@ func printCharacterInformation(swaggerclient *ESI.App, m *Character) {
implantNames := getUniverseNames(swaggerclient, &clone.Implants)
for _, implant := range clone.Implants {
implantName := implantNames[implant]
fmt.Printf(" %s, ", implantName)
outChan <- fmt.Sprintf(" %s, ", implantName)
}
outChan <- "\n"
}
close(outChan)
}
type structureInfo struct {
@ -410,7 +439,7 @@ func (s byPIPinType) Less(i, j int) bool {
return *a.PinID < *b.PinID
}
func printCharacterPlanets(swaggerclient *ESI.App, m *Character) {
func printCharacterPlanets(outChan chan string, swaggerclient *ESI.App, m *Character) {
callParam := ESIPlanetaryInteraction.NewGetCharactersCharacterIDPlanetsParams()
callParam.WithCharacterID(m.CharacterID)
@ -433,7 +462,7 @@ func printCharacterPlanets(swaggerclient *ESI.App, m *Character) {
planetName := getPlanetInformation(swaggerclient, *planet.PlanetID)
fmt.Printf("Planet %s, %s, %s - Type %s, Lvl %d - Updated %s\n",
outChan <- fmt.Sprintf("Planet %s, %s, %s - Type %s, Lvl %d - Updated %s\n",
planetName.PlanetName,
planetSystemInfo.ConstellationName,
planetSystemInfo.RegionName,
@ -471,7 +500,7 @@ func printCharacterPlanets(swaggerclient *ESI.App, m *Character) {
duration = time.Time(pin.ExpiryTime).Sub(now)
}
fmt.Printf(" %s Extractor %4ds cycle, %s, %d per cycle, %s %s (%s)\n",
outChan <- fmt.Sprintf(" %s Extractor %4ds cycle, %s, %d per cycle, %s %s (%s)\n",
status,
*pin.ExtractorDetails.CycleTime,
pinNames[*pin.ExtractorDetails.ProductTypeID],
@ -486,9 +515,9 @@ func printCharacterPlanets(swaggerclient *ESI.App, m *Character) {
schematicInfo, serr := getSchematicsInformation(swaggerclient, pin.SchematicID)
if serr != nil {
log.Printf("Error on getSchematicsInformation: %T, %s\n", serr, serr)
fmt.Printf(" ✔ Factory ????? cycle, ?????\n")
outChan <- fmt.Sprintf(" ✔ Factory ????? cycle, ?????\n")
} else {
fmt.Printf(" ✔ Factory %4ds cycle, %s\n",
outChan <- fmt.Sprintf(" ✔ Factory %4ds cycle, %s\n",
schematicInfo.CycleTime,
schematicInfo.SchematicName,
)
@ -496,6 +525,8 @@ func printCharacterPlanets(swaggerclient *ESI.App, m *Character) {
}
}
}
close(outChan)
}
func getUniverseNames(swaggerclient *ESI.App, itemIds *[]int32) map[int32]string {