-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (177 loc) Β· 6.34 KB
/
index.js
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
/*
In your terminal, install packages and run the code:
yarn install
node index.js
*/
require('dotenv').config()
const fetch = require('node-fetch')
const storage = require('node-persist')
// Helper functions
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function callEndpointAfterCD(endpoint, method, data) {
try {
let res = await fetch(
`https://lambda-treasure-hunt.herokuapp.com/api/${endpoint}/`,
{
method: `${method}`,
headers: { Authorization: `Token ${process.env.TOKEN}` },
body: JSON.stringify(data)
}
)
res = await res.json()
await sleep(res.cooldown * 1000)
console.log(res)
return res
} catch (err) {
console.error(err)
}
}
function the_other_side(direction_traveled) {
if (direction_traveled === 'n') {
return 's'
} else if (direction_traveled === 's') {
return 'n'
} else if (direction_traveled === 'e') {
return 'w'
} else if (direction_traveled === 'w') {
return 'e'
}
}
// Main game
async function main() {
await storage.init()
if (!(await storage.getItem(`${process.env.NAME}'s-traveled`))) {
await storage.setItem(`${process.env.NAME}'s-traveled`, [])
}
let traveled = await storage.getItem(`${process.env.NAME}'s-traveled`)
if (!(await storage.getItem(`${process.env.NAME}'s-map`))) {
await storage.setItem(`${process.env.NAME}'s-map`, {})
}
let visited = await storage.getItem(`${process.env.NAME}'s-map`)
while (Object.keys(visited).length <= 500) {
console.log(`π Visited length: ${Object.keys(visited).length} \n`)
const player = await callEndpointAfterCD('adv/status', 'post')
let current_room = await callEndpointAfterCD('adv/init', 'get')
// take all room treasures, if available and player not at max capacity
if (
current_room.items.length &&
player.encumbrance < player.strength &&
parseInt(player.gold) <= 1000
) {
for (let item of current_room.items) {
await callEndpointAfterCD('adv/take', 'post', { name: item })
console.log(`πΈ Treasure collected \n`)
}
}
let this_room_id = current_room.room_id
if (current_room.title.includes('Pirate Ry')) {
await storage.setItem(`Pirate-Room-ID`, this_room_id)
console.log(`π Pirate Ry room #: ${this_room_id}`)
// purchase new name, if player is at Pirate Ry's and has at least 1000 gold
if (parseInt(player.gold) >= 1000) {
await callEndpointAfterCD('adv/change_name', 'post', {
name: process.env.NAME,
confirm: 'aye'
})
console.log(`π¨ Changed name \n`)
}
}
if (current_room.title.includes('Shop')) {
await storage.setItem(`Shop-Room-ID`, this_room_id)
console.log(`π Shop room #: ${this_room_id}`)
// sell all treasures, if player is at the shop
for (let i = 0; i < parseInt(player.encumbrance); i++) {
await callEndpointAfterCD('adv/sell', 'post', {
name: 'treasure',
confirm: 'yes'
})
console.log(`π¨ Sold treasure \n`)
}
}
if (!(this_room_id in visited)) {
visited[this_room_id] = {}
for (let i = 0; i < current_room.exits.length; i++) {
visited[this_room_id][current_room.exits[i]] = '?'
}
}
/*
- If each exit directions in the current room is '?' (i.e. unexplored), add it to unexplored array
- Then, randomly select one in the next step
*/
let unexplored = []
let current_exits = visited[this_room_id]
await storage.setItem(`${process.env.NAME}'s-map`, visited)
// console.log(`πͺ Current room exits: ${JSON.stringify(current_exits)} \n`)
for (x in visited[this_room_id]) {
if (visited[this_room_id][x] === '?') {
unexplored.push(x)
}
}
// console.log(`π Unexplored exits: ${unexplored} \n`)
if (unexplored.length > 0) {
let direction = unexplored[Math.floor(Math.random() * unexplored.length)]
// travel in a random direction
let new_current_room = await callEndpointAfterCD('adv/move', 'post', {
direction: direction
})
if (traveled[traveled.length - 1] != this_room_id) {
traveled.push(this_room_id)
}
await storage.setItem(`${process.env.NAME}'s-traveled`, traveled)
// console.log(`π§ Working on route: ${traveled} \n`)
let new_room_id = new_current_room.room_id
// console.log(`π New room ID: ${new_room_id} \n`)
visited[this_room_id][direction] = new_room_id
if (!(new_room_id in visited)) {
visited[new_room_id] = {}
for (let i = 0; i < new_current_room.exits.length; i++) {
visited[new_room_id][new_current_room.exits[i]] = '?'
}
}
let op_dir = the_other_side(direction)
visited[new_room_id][op_dir] = this_room_id
await storage.setItem(`${process.env.NAME}'s-map`, visited)
if (
current_room.items.length &&
player.encumbrance < player.strength &&
parseInt(player.gold) <= 1000
) {
for (let item of current_room.items) {
await callEndpointAfterCD('adv/take', 'post', { name: item })
console.log(`πΈ Treasure collected \n`)
}
}
} else {
/*
- Generate a list of directions to get to the nearest unexplored room using BFS
- Loop through and send the player in those directions in order
*/
let backwards_movement = traveled.pop()
for (x in visited[this_room_id]) {
if (visited[this_room_id][x] === backwards_movement) {
await callEndpointAfterCD('adv/move', 'post', {
direction: x,
next_room_id: JSON.stringify(backwards_movement)
})
// console.log(`π§ Wise traveler \n`)
// take all room treasures, if available and player not at max capacity
if (
current_room.items.length &&
player.encumbrance < player.strength &&
parseInt(player.gold) <= 1000
) {
for (let item of current_room.items) {
await callEndpointAfterCD('adv/take', 'post', { name: item })
console.log(`πΈ Treasure collected \n`)
}
}
}
}
}
await storage.setItem(`${process.env.NAME}'s-map`, visited)
await storage.setItem(`${process.env.NAME}'s-traveled`, traveled)
}
}
main()