-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
169 lines (141 loc) · 3.77 KB
/
repl.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
)
type Pokemon struct {
Name string `json:"name"`
ID int `json:"id"`
Types []struct {
Type struct {
Name string `json:"name"`
} `json:"type"`
} `json:"types"`
Moves []struct {
Move struct {
Name string `json:"name"`
} `json:"move"`
} `json:"moves"`
}
func getPokemonDetails(name string) (*Pokemon, error) {
url := fmt.Sprintf("https://pokeapi.co/api/v2/pokemon/%s", name)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var pokemon Pokemon
err = json.Unmarshal(body, &pokemon)
if err != nil {
return nil, err
}
return &pokemon, nil
}
func startingRepl() {
fmt.Println("> Welcome to Pokedex, type help to know more.")
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("pokedex> ")
scanner.Scan()
text := scanner.Text()
switch text {
case "help":
fmt.Println("Welcome to the Pokedex!")
fmt.Println("Usage: ")
fmt.Println()
fmt.Println("help: Displays a help message")
fmt.Println("type <pokemon name> to get details about it")
fmt.Println("type <explore> to see areas where pokemon are available")
fmt.Println("type <explore area-name> to see the pokemons available")
fmt.Println("type catch to catch the pokemon")
fmt.Println("exit: Exit the Pokedex")
fmt.Println("Note: always use lower case for pokemon names")
case "exit":
os.Exit(0)
case "explore":
random_pokemons()
default:
pokemonDetails, err := getPokemonDetails(text)
if err != nil {
fmt.Println("Error fetching Pokémon details:", err)
duration := 3 * time.Second
// Create a ticker that ticks every second
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
// Countdown loop
for remaining := duration; remaining >= 0; remaining -= time.Second {
// Convert the remaining duration to integer seconds
secondsLeft := int64(remaining / time.Second)
// Use \r to move the cursor to the beginning of the line
// Display the time in seconds
fmt.Printf("\rQuitting pokedex in: %d seconds", secondsLeft)
time.Sleep(1 * time.Second)
}
fmt.Println()
return
}
fmt.Printf("Name: %s, ID: %d\n", pokemonDetails.Name, pokemonDetails.ID)
// Displaying types
fmt.Print("Type: ")
for _, t := range pokemonDetails.Types {
fmt.Printf("%s ", t.Type.Name)
}
fmt.Println()
// Displaying moves (let's limit to first 5 moves for brevity)
fmt.Print("Moves: ")
for i, m := range pokemonDetails.Moves {
if i >= 5 {
break
}
fmt.Printf("%s, ", m.Move.Name)
}
fmt.Println()
}
}
}
// if text == "help" {
// fmt.Println("Welcome to the Pokedex!")
// fmt.Println("Usage: ")
// fmt.Println()
// fmt.Println("help: Displays a help message")
// fmt.Println("type pokemon name to get details about it")
// fmt.Println("exit or quit: Exit the Pokedex")
// } else if text == "exit" || text == "quit" {
// os.Exit(0)
// } else {
// pokemonDetails, err := getPokemonDetails(text)
// if err != nil {
// fmt.Println("Error fetching Pokémon details:", err)
// return
// }
// fmt.Printf("Name: %s, ID: %d\n", pokemonDetails.Name, pokemonDetails.ID)
// // Displaying types
// fmt.Print("Type: ")
// for _, t := range pokemonDetails.Types {
// fmt.Printf("%s ", t.Type.Name)
// }
// fmt.Println()
// // Displaying moves (let's limit to first 5 moves for brevity)
// fmt.Print("Moves: ")
// for i, m := range pokemonDetails.Moves {
// if i >= 5 {
// break
// }
// fmt.Printf("%s, ", m.Move.Name)
// }
// fmt.Println()
func cleaned(str string) []string {
lower := strings.ToLower(str)
words := strings.Fields(lower)
return words
}