-
Notifications
You must be signed in to change notification settings - Fork 209
/
chat-manager.js
147 lines (132 loc) Β· 3.85 KB
/
chat-manager.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
import {makeId, makePromise} from './util.js';
import metaversefileApi from 'metaversefile';
const _getEmotion = text => {
let match;
if (match = text.match(/(π|π|π|π|π|(?:^|\s)lol(?:$|\s))/)) {
match.emotion = 'joy';
return match;
} else if (match = text.match(/(π|π|π|π|π|π|π|β€οΈ|π|π|π|π|π½)/)) {
match.emotion = 'fun';
return match;
} else if (match = text.match(/(π|π|π|π±|π¨|π°|π«)/)) {
match.emotion = 'sorrow';
return match;
} else if (match = text.match(/(π |π‘|πΏ|π₯|π’)/)) {
match.emotion = 'angry';
return match;
} else if (match = text.match(/(π|π²|πΆ)/)) {
match.emotion = 'neutral';
return match;
} else {
return null;
}
};
class ChatManager extends EventTarget {
constructor() {
super();
this.voiceRunning = false;
this.voiceQueue = [];
}
addPlayerMessage(player, m, {timeout = 3000} = {}) {
const match = _getEmotion(m.message);
const emotion = match ? match.emotion : null;
const value = emotion ? 1 : 0;
player.addAction(m);
const _addFacePose = () => {
if (emotion) {
player.addAction({
type: 'facepose',
emotion,
value: 1,
});
}
};
_addFacePose();
const _removeFacePose = () => {
if (emotion) {
const facePoseActionIndex = player.findActionIndex(action => action.type === 'facepose' && action.value === value);
if (facePoseActionIndex !== -1) {
player.removeActionIndex(facePoseActionIndex);
}
}
};
this.dispatchEvent(new MessageEvent('messageadd', {
data: {
player,
message: m,
},
}));
const localTimeout = setTimeout(() => {
this.removePlayerMessage(player, m);
_removeFacePose();
}, timeout);
m.cleanup = () => {
clearTimeout(localTimeout);
};
return m;
}
addMessage(message, opts) {
const chatId = makeId(5);
const localPlayer = metaversefileApi.useLocalPlayer();
const m = {
type: 'chat',
chatId,
playerId: localPlayer.playerId,
playerName: localPlayer.name,
message,
};
return this.addPlayerMessage(localPlayer, m, opts);
}
removePlayerMessage(player, m) {
m.cleanup();
const actionIndex = player.findActionIndex(action => action.chatId === m.chatId);
if (actionIndex !== -1) {
player.removeActionIndex(actionIndex);
} else {
console.warn('remove unknown message action 2', m);
}
this.dispatchEvent(new MessageEvent('messageremove', {
data: {
player,
message: m,
},
}));
}
removeMessage(m) {
const localPlayer = metaversefileApi.useLocalPlayer();
this.removePlayerMessage(localPlayer, m);
}
async waitForVoiceTurn(fn) {
// console.log('wait for voice queue', this.voiceRunning, this.voiceQueue.length);
if (!this.voiceRunning) {
this.voiceRunning = true;
// console.log('wait 0');
const p = fn();
// console.log('wait 1');
const result = await p;
// console.log('wait 2');
this.voiceRunning = false;
if (this.voiceQueue.length > 0) {
const fn2 = this.voiceQueue.shift();
this.waitForVoiceTurn(fn2);
}
return result;
} else {
const p = makePromise();
this.voiceQueue.push(async () => {
const p2 = fn();
// console.log('wait 3');
const result = await p2;
// console.log('wait 4');
p.accept(result);
return result;
});
const result = await p;
return result;
}
}
}
const chatManager = new ChatManager();
export {
chatManager,
};