-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.js
172 lines (145 loc) · 5.86 KB
/
profile.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
const fs = require("fs");
const path = require("path");
const axios = require("axios");
// @author armisto#2174
module.exports = {
/**
* Adds an item to the profile JSON.
*
* @param {any} profile the profile object, loaded from a file
* @param {string} itemId the item ID of the item to add
* @param {any} item the item object to add
* @param {any[]} profileChangesArr (optional) if a change is made, adds a profile change entry to the profileChanges array
* @returns {boolean} if the a change is made
*/
addItem(profile, itemId, item, profileChangesArr) {
if (profile.items[itemId]) {
return false;
}
profile.items[itemId] = item;
if (profileChangesArr) {
profileChangesArr.push({ "changeType": "itemAdded", "itemId": itemId, "item": item });
}
return true;
},
/**
* Removes an item from the profile JSON.
*
* @param {any} profile the profile object, loaded from a file
* @param {string} itemId the item ID of the item to remove
* @param {any[]} profileChangesArr (optional) if a change is made, adds a profile change entry to the profileChanges array
* @returns {boolean} if the a change is made
*/
removeItem(profile, itemId, profileChangesArr) {
if (!profile.items[itemId]) {
return false;
}
//commented it to prevent removing gift item from the profile
//delete profile.items[itemId];
delete profile.items[itemId];
if (profileChangesArr) {
profileChangesArr.push({ "changeType": "itemRemoved", "itemId": itemId });
}
return true;
},
/**
* Modifies the quantity of a specified item in the profile JSON.
*
* @param {any} profile the profile object, loaded from a file
* @param {string} itemId the item ID
* @param {number} quantity the new quantity
* @param {any[]} profileChangesArr (optional) if a change is made, adds a profile change entry to the profileChanges array
* @returns {boolean} if the a change is made
*/
changeItemQuantity(profile, itemId, newQuantity, profileChangesArr) {
if (!profile.items[itemId] || profile.items[itemId].quantity == newQuantity) {
return false;
}
profile.items[itemId].quantity = newQuantity;
if (profileChangesArr != null) {
profileChangesArr.push({ "changeType": "itemQuantityChanged", "itemId": itemId, "quantity": newQuantity });
}
return true;
},
/**
* Modifies an attribute value of a specified item in the profile JSON.
*
* @param {any} profile the profile object, loaded from a file
* @param {string} itemId the item ID of the item to have one of its attributes' value changed
* @param {string} attributeName the item attribute property name
* @param {any} attributeValue the new attribute value
* @param {any[]} profileChangesArr (optional) if a change is made, adds a profile change entry to the profileChanges array
* @returns {boolean} if the a change is made
*/
changeItemAttribute(profile, itemId, attributeName, attributeValue, profileChangesArr) {
var item = profile.items[itemId];
if (!item) {
return false;
}
if (!item.attributes) {
item.attributes = {};
} /*else if (item.attributes[attributeName] == attributeValue) {
return false;
}*/
item.attributes[attributeName] = attributeValue;
if (profileChangesArr != null) {
profileChangesArr.push({ "changeType": "itemAttrChanged", "itemId": itemId, "attributeName": attributeName, "attributeValue": attributeValue });
}
return true;
},
/**
* Modifies the a stat value of the profile JSON.
*
* @param {any} profile the profile object, loaded from a file
* @param {string} statName the stat property name
* @param {any} statValue the stat value to modify to
* @param {any[]} profileChangesArr (optional) if a change is made, adds a profile change entry to the profileChanges array
* @returns {boolean} if the a change is made
*/
modifyStat(profile, statName, statValue, profileChangesArr) {
if (!statName || statValue == null) {
return false;
}
if (!profile.stats) {
profile.stats = {
attributes: {
}
}
}
profile.stats.attributes[statName] = statValue;
if (profileChangesArr != null) {
profileChangesArr.push({ "changeType": "statModified", "name": statName, "value": statValue });
}
return true;
},
/**
* Changes the updated date of the profile and its revision so the client can detect if it has been changed.
*
* @param {any} profile the profile object, loaded from a file
* @returns {any} the supplied profile object
*/
bumpRvn(profile) {
profile.rvn += 1;
profile.updated = new Date().toISOString();
profile.commandRevision += 1;
return profile;
},
readProfile(accountId, profileId) {
try {
return JSON.parse(fs.readFileSync(path.join(__dirname, `/config/${accountId}/profiles/profile_${profileId}.json`), "utf8"));
} catch (e) {
return null;
}
},
readProfileTemplate(profileId) {
// console.log(`/config_template/profiles/profile_${profileId}.json`);
try {
return JSON.parse(fs.readFileSync(path.join(__dirname, `/config_template/profiles/profile_${profileId}.json`), "utf8"));
} catch (e) {
return null;
}
},
saveProfile(accountId, profileId, data) {
fs.writeFileSync(path.join(__dirname, `/config/${accountId}/profiles/profile_${profileId}.json`), JSON.stringify(data, null, 2));
}
};