-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.js
More file actions
126 lines (106 loc) · 4.14 KB
/
chat.js
File metadata and controls
126 lines (106 loc) · 4.14 KB
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
class ChatSystem {
constructor(auth) {
this.auth = auth;
this.currentTab = 'all';
this.init();
}
init() {
this.initializeListeners();
this.setupChatUI();
}
initializeListeners() {
const wishesRef = db.ref('wishes');
wishesRef.on('child_added', (snapshot) => {
const message = snapshot.val();
if (this.shouldShowMessage(message)) {
this.displayMessage(message);
}
});
}
shouldShowMessage(message) {
if (this.currentTab === 'all') return true;
if (this.currentTab === 'friends' && this.auth.currentUser) {
return this.auth.currentUser.friends &&
(this.auth.currentUser.friends[message.playerId] ||
message.playerId === this.auth.currentUser.id);
}
return false;
}
async sendMessage(text) {
if (!text.trim()) return;
try {
const messageData = {
playerId: this.auth.currentUser ? this.auth.currentUser.id : 'guest',
playerName: this.auth.currentUser ? this.auth.currentUser.name : 'Guest',
text: text,
timestamp: firebase.database.ServerValue.TIMESTAMP
};
await db.ref('wishes').push(messageData);
} catch (error) {
console.error('Error sending message:', error);
}
}
displayMessage(message) {
const chatMessages = document.getElementById('chatMessages');
const messageElement = document.createElement('div');
const isCurrentUser = this.auth.currentUser &&
message.playerId === this.auth.currentUser.id;
messageElement.className = `chat-message ${isCurrentUser ? 'sent' : 'received'}`;
messageElement.innerHTML = `
<div class="message-header">
<span class="message-username">${message.playerName}</span>
<span class="message-time">${this.formatTime(message.timestamp)}</span>
</div>
<div class="message-text">${message.text}</div>
`;
chatMessages.appendChild(messageElement);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
setupChatUI() {
const chatToggle = document.querySelector('.chat-toggle');
const chatSystem = document.querySelector('.chat-system');
const chatTabs = document.querySelectorAll('.chat-tab');
const chatInput = document.getElementById('chatInput');
const sendButton = document.getElementById('sendMessage');
chatToggle.addEventListener('click', () => {
chatSystem.classList.toggle('collapsed');
chatToggle.textContent = chatSystem.classList.contains('collapsed') ? '▼' : '▲';
});
chatTabs.forEach(tab => {
tab.addEventListener('click', () => {
chatTabs.forEach(t => t.classList.remove('active'));
tab.classList.add('active');
this.currentTab = tab.dataset.chat;
this.refreshMessages();
});
});
sendButton.addEventListener('click', () => {
this.sendMessage(chatInput.value);
chatInput.value = '';
});
chatInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.sendMessage(chatInput.value);
chatInput.value = '';
}
});
}
formatTime(timestamp) {
const date = new Date(timestamp);
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
}
async refreshMessages() {
const chatMessages = document.getElementById('chatMessages');
chatMessages.innerHTML = '';
const messages = await db.ref('wishes')
.orderByChild('timestamp')
.limitToLast(50)
.once('value');
messages.forEach(snapshot => {
const message = snapshot.val();
if (this.shouldShowMessage(message)) {
this.displayMessage(message);
}
});
}
}