-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (102 loc) · 2.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/joho/godotenv"
)
var client ApiClient
var reader *bufio.Reader
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal(err)
}
apiKey := os.Getenv("TMDB_API_RA_TOKEN")
client = ApiClient{}.New(apiKey)
reader = bufio.NewReader(os.Stdin)
}
func main() {
var currentActor Actor
var endActor Actor
var currentMovie Movie
// 1. Get random start and end actors
currentActor, endActor, err := client.GetActors()
check(err)
for {
for {
// 1a. Display start and end
fmt.Printf("Actor : %s\n", currentActor.Name)
fmt.Printf("Goal : %s\n", endActor.Name)
// 2. Ask for movie from user
movieName, err := input("\nGuess movie:")
check(err)
// 3. Search for movies with the name provided
movies, err := client.SearchMovies(movieName)
check(err)
// 4. For each currentMovie from search, get credits from id and
// see whether the actor is credited or not.
var success bool = false
for _, m := range movies {
success = client.MovieHasCast(m.Id, currentActor.Id)
if success {
currentMovie = m
break
}
}
// 5. Report success/fail
if success {
fmt.Print("\nCorrect!\n\n")
break
} else {
fmt.Print("\nIncorrect. Try again.\n\n")
}
}
for {
// 6. Print new current movie
fmt.Printf("Movie : %s (%s)\n", currentMovie.Title, strings.Split(currentMovie.ReleaseDate, "-")[0])
fmt.Printf("Goal : %s\n", endActor.Name)
// 7. Ask to guess actor in new movie
actorName, err := input("\nGuess actor:")
check(err)
// 8. Search for actors with name provided
actors, err := client.SearchActors(actorName)
check(err)
// 9. For each actor, see if they're in the movie
var success bool = false
for _, a := range actors {
success = client.MovieHasCast(currentMovie.Id, a.Id)
if success {
currentActor = a
break
}
}
if success {
fmt.Print("\nCorrect!\n\n")
break
} else {
fmt.Print("\nIncorrect. Try again.\n\n")
}
}
if currentActor.Id == endActor.Id {
fmt.Println("You won!")
os.Exit(0)
}
}
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func input(question string) (string, error) {
fmt.Printf("%s\n> ", question)
resp, err := reader.ReadString('\n')
if err != nil {
return "", err
}
resp = resp[:len(resp)-1] // Remove the newline char
return resp, nil
}