/
drops.ts
204 lines (173 loc) · 5.47 KB
/
drops.ts
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
import * as alt from 'alt-server';
import Database from '@stuyk/ezmongodb';
import * as Athena from '@AthenaServer/api';
import { ItemDrop, StoredItem } from '@AthenaShared/interfaces/item';
import { deepCloneObject } from '@AthenaShared/utility/deepCopy';
type UnpushedItemDrop = Omit<ItemDrop, '_id'>;
const DEFAULT_EXPIRATION = 60000 * 5; // 5 Minutes
const drops: Array<ItemDrop> = [];
const markAsTaken: { [key: string]: boolean } = {};
/**
* Initialize all item drops stored in the database.
*
*/
async function init() {
await Database.createCollection(Athena.database.collections.Drops);
const results = await Database.fetchAllData<ItemDrop>(Athena.database.collections.Drops);
for (let i = 0; i < results.length; i++) {
results[i]._id = String(results[i]._id);
drops.push(results[i]);
Athena.controllers.itemDrops.append(results[i]);
markAsTaken[String(results[i]._id)] = false;
}
}
/**
* Adds a new item drop to the database.
*
* @param {StoredItem} storedItem
* @return {Promise<ItemDrop>}
*/
async function addToDatabase(storedItem: UnpushedItemDrop): Promise<ItemDrop> {
storedItem = deepCloneObject<UnpushedItemDrop>(storedItem);
const document = await Database.insertData<UnpushedItemDrop>(storedItem, Athena.database.collections.Drops, true);
const convertedDoc = <ItemDrop>document;
convertedDoc._id = String(convertedDoc._id);
markAsTaken[String(convertedDoc._id)] = false;
drops.push(convertedDoc);
return convertedDoc;
}
/**
* Simply tries to remove an entry from the database.
*
* @param {string} id
*/
async function removeFromDatabase(id: string) {
await Database.deleteById(id, Athena.database.collections.Drops);
}
/**
* Add a dropped item.
*
* @param {StoredItem} item
* @param {alt.IVector3} pos A position in the world.
* @return {Promise<string>}
*/
export async function add(item: StoredItem, pos: alt.IVector3, player: alt.Player = undefined): Promise<string> {
if (Overrides.add) {
return await Overrides.add(item, pos, player);
}
const baseItem = Athena.systems.inventory.factory.getBaseItem(item.dbName);
if (typeof baseItem === 'undefined') {
return undefined;
}
if (baseItem.behavior && !baseItem.behavior.canDrop) {
return undefined;
}
const expiration =
typeof baseItem.msTimeout === 'number' ? Date.now() + baseItem.msTimeout : Date.now() + DEFAULT_EXPIRATION;
item.isEquipped = false;
const document = await addToDatabase({
...item,
name: baseItem.name,
pos,
expiration,
model: baseItem.model,
});
Athena.controllers.itemDrops.append(document);
if (typeof player !== 'undefined') {
Athena.player.events.trigger('drop-item', player, item);
Athena.systems.inventory.events.invoke(player, 'onDrop', item.dbName, item.slot);
}
return document._id as string;
}
/**
* Get the current item drop.
*
* @param {string} id
* @return {(ItemDrop | undefined)}
*/
export function get(id: string): ItemDrop | undefined {
if (Overrides.get) {
return Overrides.get(id);
}
return drops.find((x) => x._id === id);
}
/**
* Remove the dropped item based on identifier.
*
* @param {string} id
* @return {(Promise<StoredItem | undefined>)}
*/
export async function sub(id: string): Promise<StoredItem | undefined> {
if (Overrides.sub) {
return Overrides.sub(id);
}
let itemClone: StoredItem = undefined;
for (let i = drops.length - 1; i >= 0; i--) {
if (Date.now() > drops[i].expiration && drops[i]._id !== id) {
delete markAsTaken[id];
drops.splice(i, 1);
continue;
}
if (drops[i]._id !== id) {
continue;
}
delete markAsTaken[id];
Athena.controllers.itemDrops.remove(id);
const newItem = deepCloneObject<ItemDrop>(drops.splice(i, 1));
await removeFromDatabase(id);
delete newItem._id;
delete newItem.pos;
delete newItem.expiration;
delete newItem.pos;
delete newItem.name;
itemClone = newItem;
}
return itemClone;
}
/**
* Check if an item is available by database id
*
*
* @param {string} _id
* @return {void}
*/
export function isItemAvailable(_id: string) {
if (Overrides.isItemAvailable) {
return Overrides.isItemAvailable(_id);
}
return typeof markAsTaken[_id] !== 'undefined' && markAsTaken[_id] === false;
}
/**
* Mark an item as being taken
*
*
* @param {string} _id
* @param {boolean} value
*/
export function markForTaken(_id: string, value: boolean) {
markAsTaken[_id] = value;
}
interface DropFuncs {
add: typeof add;
get: typeof get;
sub: typeof sub;
isItemAvailable: typeof isItemAvailable;
markForTaken: typeof markForTaken;
}
const Overrides: Partial<DropFuncs> = {};
export function override(functionName: 'add', callback: typeof add);
export function override(functionName: 'get', callback: typeof get);
export function override(functionName: 'sub', callback: typeof sub);
export function override(functionName: 'isItemAvailable', callback: typeof isItemAvailable);
export function override(functionName: 'markForTaken', callback: typeof markForTaken);
/**
* Used to override inventory drop item functionality
*
*
* @param {keyof DropFuncs} functionName
* @param {*} callback
*/
export function override(functionName: keyof DropFuncs, callback: any): void {
Overrides[functionName] = callback;
}
init();