-
Notifications
You must be signed in to change notification settings - Fork 0
/
populatedb.js
165 lines (152 loc) · 4.76 KB
/
populatedb.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
#! /usr/bin/env node
console.log(
'This script populates some items to your database. Specified database as argument - e.g.: node populatedb "mongodb+srv://cooluser:[email protected]/inventory?retryWrites=true&w=majority"'
);
// Get arguments passed on command line
const userArgs = process.argv.slice(2);
const mongoose = require('mongoose');
const Item = require('./models/item');
const Category = require('./models/category');
const categories = [];
const items = [];
mongoose.set('strictQuery', false);
const mongoDB = userArgs[0];
main().catch((err) => console.log(err));
async function main() {
console.log('Debug: About to connect');
await mongoose.connect(mongoDB);
console.log('Debug: Should be connected?');
await createCategories();
await createItems();
console.log('Debug: Closing mongoose');
mongoose.connection.close();
console.log('Debug: Connection closed');
process.exit(0); // Explicitly exit the Node.js process
}
// We pass the index to the ...Create functions so that, for example,
// category[0] will always be the Weapon Category, regardless of the order
// in which the elements of promise.all's argument complete.
async function categoryCreate(index, name) {
const category = new Category({ name, default: true });
await category.save();
categories[index] = category;
console.log(`Added category: ${name}`);
}
async function itemCreate(
index,
name,
description,
category,
value,
rarity,
equippable,
privateItem
) {
const itemdetail = {
name,
description,
value,
rarity,
equippable,
private: privateItem,
};
if (category !== false) itemdetail.category = category;
const item = new Item(itemdetail);
await item.save();
items[index] = item;
console.log(`Added item: ${name}`);
}
async function createCategories() {
console.log('Adding categories');
await Promise.all([
categoryCreate(0, 'Weapon'),
categoryCreate(1, 'Potion'),
categoryCreate(2, 'Armor'),
categoryCreate(3, 'Magic'),
]);
}
async function createItems() {
console.log('Adding Items');
await Promise.all([
itemCreate(
0,
'Adamantine Armor',
"This suit of armor is reinforced with adamantine, one of the hardest substances in existence. While you're wearing it, any critical hit against you becomes a normal hit.",
[categories[2], categories[3]],
500,
'Uncommon',
true,
false
),
itemCreate(
1,
'Potion of Healing (Greater)',
"You regain 4d4 + 4 hit points when you drink this potion. The potion's red liquid glimmers when agitated.",
[categories[1]],
100,
'Uncommon',
false,
false
),
itemCreate(
2,
'Potion of Water Breathing',
'You can breathe underwater for 1 hour after drinking this potion. Its cloudy green fluid smells of the sea and has a jellyfish-like bubble floating in it.',
[categories[1]],
200,
'Uncommon',
false,
false
),
itemCreate(
3,
'Battleaxe',
'1d8 slashing. Proficiency with a battleaxe allows you to add your proficiency bonus to the attack roll for any attack you make with it.',
[categories[0]],
10,
'Common',
true,
false
),
itemCreate(
4,
'Dagger',
'1d4 piercing. Proficiency with a dagger allows you to add your proficiency bonus to the attack roll for any attack you make with it.',
[categories[0]],
2,
'Common',
true,
false
),
itemCreate(
5,
'Spiked Armor',
'14 + Dex modifier (max 2) AC. Stealth: Disadvantage. Spiked armor is a rare type of medium armor made by dwarves. It consists of a leather coat and leggings covered with spikes that are usually made of metal.',
[categories[2]],
75,
'Rare',
true,
false
),
itemCreate(
6,
'Ring of Invisibility',
'While wearing this ring, you can turn invisible as an action. Anything you are wearing or carrying is invisible with you. You remain invisible until the ring is removed, until you attack or cast a spell, or until you use a bonus action to become visible again.',
[categories[3]],
1000,
'Legendary',
true,
false
),
itemCreate(
7,
'Arrows',
'Arrows are used with a weapon that has the ammunition property to make a ranged attack. Each time you attack with the weapon, you expend one piece of ammunition. Drawing the ammunition from a quiver, case, or other container is part of the attack (you need a free hand to load a one-handed weapon). At the end of the battle, you can recover half your expended ammunition by taking a minute to search the battlefield.',
[categories[0]],
1,
'Common',
false,
false
),
]);
}