-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.go
More file actions
131 lines (116 loc) · 2.7 KB
/
repl.go
File metadata and controls
131 lines (116 loc) · 2.7 KB
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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/TwiN/go-color"
"github.com/fadhilradh/pokedex-cli/config"
)
func StartCLI(config *config.Config) {
scanner := bufio.NewScanner(os.Stdin)
fmt.Println(color.InGreen(`
Welcome to Pokedex CLI game !
In this game you can :
> explore locations in the Pokemon world
> encounter pokemon in each location
> catch pokemon and inspect them
Type 'help' to see all available commands in this game.
Catch 'em all !
--- made by fadhilradh with Go ---
`))
GetInput(scanner, "Pokedex > ", mainCommands, true)
}
func GetInput(scanner *bufio.Scanner, msg string, commandList func() map[string]cliCommand, isWelcome bool) {
for {
if isWelcome {
fmt.Print(color.Red + msg + color.Reset)
} else {
fmt.Print(msg)
}
scanner.Scan()
err := scanner.Err()
if err != nil {
log.Fatal(err)
}
input := scanner.Text()
words := strings.Fields(input)
if len(input) == 0 {
continue
}
command, exists := commandList()[words[0]]
if exists {
params := []string{}
if len(words) > 1 {
params = words[1:]
}
err := command.callback(&cfg, params...)
if err != nil {
log.Fatal(err)
}
continue
} else {
fmt.Println("Unknown command")
continue
}
}
}
func mainCommands() map[string]cliCommand {
return map[string]cliCommand{
"map": {
name: "map",
description: "List next 10 locations on the map",
callback: commandMap,
},
"mapb": {
name: "mapb",
description: "List previous 10 locations on the map",
callback: commandMapBack,
},
"explore": {
name: "explore [location name]",
description: "Explore a location and find Pokemons",
callback: commandExplore,
},
"catch": {
name: "catch [pokemon name]",
description: "Attempt to catch a Pokemon. Success chance is based on Pokemon's level and rarity",
callback: commandCatch,
},
"inspect": {
name: "inspect [pokemon name]",
description: "Inspect a caught pokemon",
callback: commandInspect,
},
"pokedex": {
name: "pokedex",
description: "List all caught pokemon",
callback: commandPokedex,
},
"help": {
name: "help",
description: "Displays a help message",
callback: commandHelp,
},
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: commandExit,
},
}
}
func EncounterCommands() map[string]cliCommand {
return map[string]cliCommand{
"battle": {
name: "battle",
description: "Battle Pokemon with chance to capture it",
callback: commandBattle,
},
"run": {
name: "run",
description: "Run away with your life !",
callback: commandRun,
},
}
}