|
4 | 4 | angular.module('tf2stadium.services')
|
5 | 5 | .factory('ChatService', ChatService);
|
6 | 6 |
|
7 |
| - // Persistent map of room id -> messages list |
8 |
| - var chatRoomLogs = Object.create(null); |
9 |
| - function getChatRoom(id) { |
10 |
| - if (angular.isUndefined(chatRoomLogs[id])) { |
11 |
| - chatRoomLogs[id] = []; |
12 |
| - } |
13 |
| - return chatRoomLogs[id]; |
14 |
| - } |
| 7 | + /** @ngInject */ |
| 8 | + function ChatService(Websocket, $rootScope, LobbyService) { |
| 9 | + var factory = {}; |
15 | 10 |
|
16 |
| - function ChatRoom(id) { |
17 |
| - this.changeRoom(angular.isDefined(id)? id : -1); |
18 |
| - } |
| 11 | + // Persistent map of room id -> messages list |
| 12 | + var chatRoomLogs = Object.create(null); |
| 13 | + function getChatRoom(id) { |
| 14 | + if (angular.isUndefined(chatRoomLogs[id])) { |
| 15 | + chatRoomLogs[id] = []; |
| 16 | + } |
| 17 | + return chatRoomLogs[id]; |
| 18 | + } |
19 | 19 |
|
20 |
| - ChatRoom.prototype.changeRoom = function chageRoom(id) { |
21 |
| - if (id !== this.id) { |
22 |
| - this.id = id; |
23 |
| - this.messages = getChatRoom(id); |
| 20 | + function ChatRoom(id) { |
| 21 | + this.changeRoom(angular.isDefined(id)? id : -1); |
24 | 22 | }
|
25 |
| - }; |
26 | 23 |
|
27 |
| - ChatRoom.prototype.leave = function leave() { |
28 |
| - this.changeRoom(-1); |
29 |
| - }; |
| 24 | + ChatRoom.prototype.changeRoom = function chageRoom(id) { |
| 25 | + if (id !== this.id) { |
| 26 | + this.id = id; |
| 27 | + this.messages = getChatRoom(id); |
| 28 | + } |
| 29 | + }; |
30 | 30 |
|
31 |
| - /** @ngInject */ |
32 |
| - function ChatService(Websocket, $rootScope, LobbyService) { |
33 |
| - var factory = {}; |
| 31 | + ChatRoom.prototype.leave = function leave() { |
| 32 | + this.changeRoom(-1); |
| 33 | + }; |
34 | 34 |
|
35 | 35 | var globalChatRoom = new ChatRoom(0);
|
36 | 36 |
|
|
67 | 67 | });
|
68 | 68 |
|
69 | 69 | Websocket.onJSON('chatReceive', function (message) {
|
70 |
| - getChatRoom(message.room).push(message); |
| 70 | + message.timestamp = new Date(message.timestamp * 1000); |
| 71 | + |
| 72 | + var log = getChatRoom(message.room); |
| 73 | + |
| 74 | + // Insert messages in sorted order (sorted by message id) |
| 75 | + if (log.length === 0 || log[log.length - 1].id < message.id) { |
| 76 | + log.push(message); |
| 77 | + } else { |
| 78 | + // performance likely isn't an issue, but since the log is |
| 79 | + // sorted by id, it would be better to use a binary search |
| 80 | + // here (also, use ES6 findIndex when available). |
| 81 | + var insertIdx = 0; |
| 82 | + while (log[insertIdx].id < message.id) { |
| 83 | + insertIdx++; |
| 84 | + } |
| 85 | + if (log[insertIdx].id === message.id) { |
| 86 | + // Same message id? Overwrite the logged message |
| 87 | + log[insertIdx] = message; |
| 88 | + } else { |
| 89 | + // else insert it into the array (yeah, splice is far from |
| 90 | + // efficient, but this should be very rare). |
| 91 | + log.splice(insertIdx, 0, message); |
| 92 | + } |
| 93 | + } |
| 94 | + |
71 | 95 | $rootScope.$emit('chat-message', message);
|
72 | 96 | });
|
73 | 97 |
|
74 |
| - |
75 | 98 | Websocket.onJSON('chatHistoryClear', function (data) {
|
76 | 99 | // Note: ChatRooms may have pointers to the arrays in
|
77 | 100 | // chatRoomLogs, so we have to mutate the actual logs rather
|
|
0 commit comments