forked from RanvierMUD/bundle-example-bugreport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player-events.js
70 lines (62 loc) · 2.28 KB
/
player-events.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
'use strict';
module.exports = (srcPath) => {
const Broadcast = require(srcPath + 'Broadcast');
const Logger = require(srcPath + 'Logger');
const Config = require(srcPath + 'Config');
const PlayerRoles = require(srcPath + 'PlayerRoles');
const RoleAudience = require(srcPath + 'ChannelAudience/RoleAudience');
function getReportMethod(type) {
switch (type) {
case 'bug':
return Logger.error;
case 'typo':
return Logger.warn;
case 'suggestion':
default:
return Logger.verbose;
}
}
function getFormattedReport(type, description) {
const header = getReportHeader.call(this, type, description);
const specialized = getSpecializedReport.call(this, type, description);
return `${header}${specialized}`;
}
function getReportHeader(type, description) {
const now = (new Date()).toISOString();
return `REPORT\nType: ${type}\nReported By: ${this.name}\nRoom: ${this.room.title}\nTime: ${now}\nDescription: ${description}\n`;
}
function getSpecializedReport(type, description) {
const room = this.room;
const serializeRoom = room => JSON.stringify({
name: room.name,
desc: room.description,
entities: [...room.items, ...room.players, ...room.npcs].map(ent => ({name: ent.name, id: ent.id, desc: ent.description || '' }))
});
switch (type) {
case 'bug':
return `PlayerData: ${JSON.stringify(this.serialize())} RoomData: ${serializeRoom(room)}`;
case 'typo':
return `PlayerInv: ${JSON.stringify(this.inventory.serialize())} RoomData: ${serializeRoom(room)}`;
case 'suggestion':
default:
return '';
}
}
return {
listeners: {
bugReport: state => function (report) {
const { description, type } = report;
const reportMethod = getReportMethod(type);
const formattedReport = getFormattedReport.call(this, type, description);
reportMethod(formattedReport);
if (Config.get('reportToAdmins')) {
const message = `Report from ${this.name}: ${description}. See the server logs for more details.`;
const minRole = type === 'bug'
? PlayerRoles.ADMIN
: PlayerRoles.BUILDER;
Broadcast.sayAt(new RoleAudience({ minRole }), message);
}
}
}
};
};