-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
218 lines (199 loc) · 6.26 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"time"
Pokemon "tpSpace/PokemonGo-lang/entity"
User "tpSpace/PokemonGo-lang/entity"
"golang.org/x/crypto/bcrypt"
)
var logined bool = false
var gUser User.User
func main() {
// print out "Welcome to PokemonGo-land" in terminal ascii art style
banner()
fmt.Println("Let's catch some Pokemons!")
fmt.Println("1. Login")
fmt.Println("2. Register")
fmt.Println("3. Exit")
fmt.Println("Please enter your choice: ")
var choice int
fmt.Scanln(&choice)
switch choice {
case 1:
var username, password string
fmt.Print("Enter username: ")
fmt.Scanln(&username)
fmt.Print("Enter password: ")
fmt.Scanln(&password)
if login(username, password) {
fmt.Println("Login successful-----------")
logined = true
break
} else {
logined = false
return // exit the program
}
case 2:
var username, password string
fmt.Print("Enter username: ")
fmt.Scanln(&username)
fmt.Print("Enter password: ")
fmt.Scanln(&password)
register(username, password)
//
case 3:
fmt.Println("Goodbye!")
return // exit the program
default:
fmt.Println("Invalid choice")
// exit the program
return
}
// if login successful, start the game
for {
if logined {
// game()
// clear screen
clearScreen()
fmt.Println("Welcome back to PokemonGo-land!")
fmt.Println("1. PokeBat")
fmt.Println("2. Inventory")
fmt.Println("3. PokeCat")
fmt.Println("4. Exit")
choice := 0
fmt.Print("Please enter your choice: ")
fmt.Scanln(&choice)
switch choice {
case 1:
Connection()
case 2:
Inventory()
case 3:
// CatchPokemon()
break
case 4:
fmt.Println("Goodbye!")
return
default:
fmt.Println("Invalid choice")
fmt.Println("Going back to main menu")
time.Sleep(2 * time.Second)
}
// Connection()
// break
} else {
break
}
}
// Crawler(611,649)
}
func login(username string, password string) bool {
// Read data from the JSON file
file, err := os.Open("user.json")
if err != nil {
fmt.Println("Error opening file:", err)
return false
}
defer file.Close()
byteValue, err := io.ReadAll(file)
if err != nil {
fmt.Println("Error reading file:", err)
return false
}
var users []User.User
json.Unmarshal(byteValue, &users)
// Check if the username and password are correct
for _, user := range users {
if user.Username == username {
// Compare the hashed password with the provided password
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
if err == nil {
fmt.Println("Login successful")
return true
} else {
fmt.Println("Login failed: Incorrect password")
return false
}
}
}
// load the user data to global variable
for _, user := range users {
if user.Username == username {
gUser = user
}
}
fmt.Println("Login failed: Username not found")
return false
}
func register(username, password string) {
// Read data from the JSON file
file, err := os.Open("user.json")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
byteValue, err := io.ReadAll(file)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
var users []User.User
json.Unmarshal(byteValue, &users)
// Check if the username already exists
for _, user := range users {
if user.Username == username {
fmt.Println("Username already exists")
return
}
}
// Hash the password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
fmt.Println("Error hashing password:", err)
return
}
// Add the new user
newUser := User.User{Username: username, Password: string(hashedPassword), Inventory: []Pokemon.Pokemon{}}
users = append(users, newUser)
// Write the updated user list back to the JSON file
newUserData, err := json.Marshal(users)
if err != nil {
fmt.Println("Error marshalling new user data:", err)
return
}
err = os.WriteFile("user.json", newUserData, 0644)
if err != nil {
fmt.Println("Error writing to file:", err)
return
}
fmt.Println("User registered successfully")
}
func banner() {
fmt.Println(` __ __ _ _ `)
fmt.Println(`\ \ / /__| | ___ ___ _ __ ___ ___ | |_ ___ `)
fmt.Println(` \ \ /\ / / _ \ |/ __/ _ \| '_ \` + "`" + ` _ \ / _ \ | __/ _ \ `)
fmt.Println(` \ V V / __/ | (_| (_) | | | | | | __/ | || (_) | `)
fmt.Println(` __\_/\_/ \___|_|\___\___/|_| |_| |_|\___| \__\___/ _ _ `)
fmt.Println(`| _ \ ___ | | _____ _ __ ___ ___ _ __ / ___| ___ | | __ _ _ __ __| |`)
fmt.Println(`| |_) / _ \| |/ / _ \ '_ \` + "`" + `_ \ / _ \| ' _ \ | | _/ _ \ _____| | / _\` + "`" + ` | '_ \ / _\` + "`" + `|`)
fmt.Println(`| __/ (_) | < __/ | | | | | (_) | | | | |_| | (_) |_____| |__| (_| | | | | (_| |`)
fmt.Println(`|_| \___/|_|\_\___|_| |_| |_|\___/|_| |_|\____|\___/ |_____\__,_|_| |_|\__,_|`)
}
func clearScreen() {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
}
func Inventory() {
// print out the user's inventory
fmt.Println("Inventory")
fmt.Println("---------")
for i, pokemon := range gUser.Inventory {
fmt.Printf("%d. %s\n", i+1, pokemon.General.Name)
}
}