(function ($) { this.RoomsRoom = Room.extend({ minWidth: 320, maxWidth: 1024, type: 'rooms', title: 'Rooms', events: { 'change select[name=sections]': 'refresh' }, isSideRoom: true, initialize: function () { this.focusedSection = ''; this.$el.addClass('ps-room-light').addClass('scrollable'); var buf = '
'; buf += '

'; buf += '

Loading...

'; buf += '

'; buf += '

'; this.$el.html(buf); app.on('response:rooms', this.update, this); var settings = Dex.prefs('serversettings'); if (settings) app.send('/updatesettings ' + JSON.stringify(settings)); app.send('/cmd rooms'); app.user.on('change:named', this.updateUser, this); this.update(); this.chatroomInterval = setInterval(function () { if (app.curSideRoom && app.curSideRoom.id === 'rooms') { app.send('/cmd rooms'); } }, 20 * 1000); }, initSectionSelection: function () { var buf = ['']; var sectionTitles = app.roomsData.sectionTitles; if (!sectionTitles) { this.$('select[name=sections]').parent().hide(); return; } for (var i = 0; i < sectionTitles.length; i++) { var sectionName = BattleLog.escapeHTML(sectionTitles[i]); buf.push(''); } this.$('select[name=sections]').html(buf.join('')); }, updateUser: function () { this.update(); }, focus: function () { if (new Date().getTime() - this.lastUpdate > 20 * 1000) { app.send('/cmd rooms'); this.lastUpdate = new Date().getTime(); } var prevPos = this.$el.scrollTop(); if (!this.$('select:focus').length) { this.$('button[name=joinRoomPopup]').focus(); } this.$el.scrollTop(prevPos); }, joinRoomPopup: function () { app.addPopupPrompt("Room name:", "Join room", function (room) { var routeLength = (Config.routes.client + '/').length; if (room.substr(0, 7) === 'http://') room = room.slice(7); if (room.substr(0, 8) === 'https://') room = room.slice(8); if (room.substr(0, routeLength) === Config.routes.client + '/') room = room.slice(routeLength); if (room.substr(0, 8) === 'psim.us/') room = room.slice(8); if (room.substr(0, document.location.hostname.length + 1) === document.location.hostname + '/') room = room.slice(document.location.hostname.length + 1); room = toRoomid(room); if (!room) return; app.tryJoinRoom(room); }); }, toggleMoreRooms: function () { this.showMoreRooms = !this.showMoreRooms; this.updateRoomList(); this.$el.find('button[name=toggleMoreRooms]').text( this.showMoreRooms ? 'Hide more rooms' : 'Show more rooms' ); }, update: function (rooms) { if (rooms) { this.lastUpdate = new Date().getTime(); app.roomsData = rooms; } if (!app.roomsData) return; this.initSectionSelection(); this.updateRoomList(); if (!app.roomsFirstOpen && window.location.host !== 'demo.psim.us') { if (Config.roomsFirstOpenScript) { Config.roomsFirstOpenScript(); } app.roomsFirstOpen = 2; } }, renderRoomBtn: function (roomData) { var id = toID(roomData.title); var buf = '
(' + Number(roomData.userCount) + ' users) ' + BattleLog.escapeHTML(roomData.title) + '
' + BattleLog.escapeHTML(roomData.desc || '') + '
'; if (roomData.subRooms && roomData.subRooms.length) { buf += '
Subrooms:'; for (var i = 0; i < roomData.subRooms.length; i++) { buf += ' ' + BattleLog.escapeHTML(roomData.subRooms[i]) + ''; } buf += '
'; } buf += '
'; return buf; }, compareRooms: function (roomA, roomB) { return roomB.userCount - roomA.userCount; }, updateRoomList: function () { var rooms = app.roomsData; if (rooms.userCount) { var userCount = Number(rooms.userCount); var battleCount = Number(rooms.battleCount); var leftSide = ' '; var rightSide = ''; this.$('.roomlisttop').html('
' + leftSide + '' + rightSide + '
'); } if (rooms.pspl) { for (var i = 0; i < rooms.pspl.length; i++) { rooms.pspl[i].spotlight = "Spotlight rooms"; } rooms.chat = rooms.pspl.concat(rooms.chat); rooms.pspl = null; } if (rooms.official) { for (var i = 0; i < rooms.official.length; i++) { rooms.official[i].section = "Official"; } rooms.chat = rooms.official.concat(rooms.chat); rooms.official = null; } var allRooms = rooms.chat; if (this.focusedSection) { var sectionFilter = this.focusedSection; allRooms = allRooms.filter(function (roomData) { return (roomData.section || 'Other') === sectionFilter; }); } var spotlightLabel = ''; var spotlightRooms = []; var officialRooms = []; var otherRooms = []; var hiddenRooms = []; for (var i = 0; i < allRooms.length; i++) { var roomData = allRooms[i]; if (roomData.spotlight) { spotlightRooms.push(roomData); spotlightLabel = roomData.spotlight; } else if (roomData.section === 'Official') { officialRooms.push(roomData); } else if (roomData.privacy === 'hidden') { hiddenRooms.push(roomData); } else { otherRooms.push(roomData); } } this.$('.roomlist').first().html( (officialRooms.length ? '

Official chat rooms

' + officialRooms.sort(this.compareRooms).map(this.renderRoomBtn).join("") : '' ) + (spotlightRooms.length ? '

' + BattleLog.escapeHTML(spotlightLabel) + '

' + spotlightRooms.sort(this.compareRooms).map(this.renderRoomBtn).join("") : '' ) ); this.$('.roomlist').last().html( (otherRooms.length ? '

Chat rooms

' + otherRooms.sort(this.compareRooms).map(this.renderRoomBtn).join("") : '' ) + (hiddenRooms.length && this.showMoreRooms ? '

Hidden rooms

' + hiddenRooms.sort(this.compareRooms).map(this.renderRoomBtn).join("") : '' ) ); }, roomlist: function () { app.joinRoom('battles'); }, closeHide: function () { app.sideRoom = app.curSideRoom = null; clearInterval(this.chatroomInterval); this.chatroomInterval = null; this.close(); }, finduser: function () { if (app.isDisconnected) { app.addPopupMessage("You are offline."); return; } app.addPopupPrompt("Username", "Open", function (target) { if (!target) return; if (toID(target) === 'zarel') { app.addPopup(Popup, { htmlMessage: "Zarel is very busy; please don't contact him this way. If you're looking for help, try joining the Help room?" }); return; } app.addPopup(UserPopup, { name: target }); }); }, refresh: function () { var section = this.$('select[name=sections]').val(); this.focusedSection = section; this.updateRoomList(); } }); this.BattlesRoom = Room.extend({ minWidth: 320, maxWidth: 1024, type: 'battles', title: 'Battles', isSideRoom: true, events: { 'change select[name=elofilter]': 'refresh', 'submit .search': 'submitSearch' }, initialize: function () { this.$el.addClass('ps-room-light').addClass('scrollable'); var buf = '

'; buf += '

'; buf += ''; buf += '

'; buf += '

Loading...

'; buf += '
'; this.$el.html(buf); this.$list = this.$('.list'); this.$refreshButton = this.$('button[name=refresh]'); this.format = ''; app.on('response:roomlist', this.update, this); app.send('/cmd roomlist'); this.update(); }, selectFormat: function (format, button) { if (!window.BattleFormats) { return; } var self = this; app.addPopup(FormatPopup, { format: format, sourceEl: button, selectType: 'watch', onselect: function (newFormat) { self.changeFormat(newFormat); } }); }, changeFormat: function (format) { this.format = format; this.data = null; this.update(); this.refresh(); }, focus: function (e) { if (e && $(e.target).is('input')) return; if (e && $(e.target).closest('select, a').length) return; if (new Date().getTime() - this.lastUpdate > 60 * 1000) { this.refresh(); } var prevPos = this.$el.scrollTop(); this.$('button[name=refresh]').focus(); this.$el.scrollTop(prevPos); }, rejoin: function () { this.refresh(); }, renderRoomBtn: function (id, roomData, matches) { var format = (matches[1] || ''); var formatBuf = ''; if (roomData.minElo) { formatBuf += '(' + (typeof roomData.minElo === 'number' ? 'rated: ' : '') + BattleLog.escapeHTML('' + roomData.minElo) + ')'; } formatBuf += (format ? '[' + BattleLog.escapeFormat(format) + ']
' : ''); var roomDesc = formatBuf + '' + BattleLog.escapeHTML(roomData.p1) + ' vs. ' + BattleLog.escapeHTML(roomData.p2) + ''; if (!roomData.p1) { matches = id.match(/[^0-9]([0-9]*)$/); roomDesc = formatBuf + 'empty room ' + matches[1]; } else if (!roomData.p2) { roomDesc = formatBuf + '' + BattleLog.escapeHTML(roomData.p1) + ''; } return '
' + roomDesc + '
'; }, submitSearch: function (e) { e.preventDefault(); this.refresh(); }, update: function (data) { if (!data && !this.data) { if (app.isDisconnected) { this.$list.html('

You are offline.

'); } else { this.$list.html('

Loading...

'); } return; } this.$('button[name=refresh]')[0].disabled = false; // Synchronize stored room data with incoming data if (data) this.data = data; var rooms = this.data.rooms; var buf = []; for (var id in rooms) { var roomData = rooms[id]; var matches = ChatRoom.parseBattleID(id); // bogus room ID could be used to inject JavaScript if (!matches || this.format && matches[1] !== this.format) { continue; } buf.push(this.renderRoomBtn(id, roomData, matches)); } if (!buf.length) return this.$list.html('

No ' + BattleLog.escapeFormat(this.format) + ' battles are going on right now.

'); return this.$list.html('

' + buf.length + (buf.length === 100 ? '+' : '') + ' ' + BattleLog.escapeFormat(this.format) + ' ' + (buf.length === 1 ? 'battle' : 'battles') + '

' + buf.join("")); }, refresh: function () { var usernamePrefix = this.$('input[name=prefixsearch]').val(); var elofilter = this.$('select[name=elofilter]').val(); var searchParams = [this.format, elofilter, toID(usernamePrefix)]; app.send('/cmd roomlist ' + searchParams.join(',')); this.lastUpdate = new Date().getTime(); // Prevent further refreshes until we get a response. this.$refreshButton[0].disabled = true; } }); }).call(this, jQuery);