1
window.addEventListener('load', () => {
3
$(document).on('change', '.lobby_change_mode', (event) => {
4
let mode = event.target.value;
7
url: "/games/change_game_mode/",
17
$(document).on('click', '.header_friend_invite', (event) => {
18
let friend_id = event.target.id.replace('invite_friend_', '');
19
let friend_name = event.target.innerHTML;
22
friendSocket = new WebSocket (
23
'ws://' + window.location.host + '/ws/user/' + friend_id
27
friendSocket.onopen = (e) => {
31
JSON.stringify({'message': {'action': 'invitation', 'sender': {'pk': user_id, 'nickname': $('.lobby_the_player>span:first-child').html()}}})
36
friendSocket.onmessage = (e) => {
38
const data = JSON.parse(e.data)['message'];
39
let action = data['action'];
40
if(action == 'reject') {
41
$('.lobby_invitation_rejected').css('display', 'inline');
42
window.setTimeout(() => {
43
$('.lobby_invitation_rejected').css('display', 'none');
46
} else if(action == 'accept') {
52
friendSocket.onclose = (e) => {
58
friendSocket.onerror = (e) => {
65
$(document).on('click', '.lobby_start_game_button', (event) => {
67
event.target.innerHTML = 'Отменить поиск';
68
event.target.classList.add('lobby_cancel_queue_button');
69
event.target.classList.remove('lobby_start_game_button');
81
queue_id = data.queue_id;
84
create_queue_socket(queue_id);
87
if(data.result == 'start') {
88
is_last_player = true;
98
$(document).on('click', '.lobby_accept_request_button', () => {
100
data = {'action': 'accept_count'};
101
if($('.lobby_theme').length) {
102
data['theme'] = $('.lobby_theme').val();
106
JSON.stringify({'message': data})
111
$(document).on('click', '.lobby_cancel_queue_button', () => {
116
window.addEventListener('beforeunload', () => {
120
$('.lobby_chat_close').on('click', () => {
121
$('.lobby_chat_block').css('display', '');
124
$('.lobby_chat_open').on('click', () => {
125
$('.lobby_chat_block').css('display', 'flex');
128
url: "/chat/load_messages/",
129
data: {type: 'lobby'},
131
let messages = data['messages'];
132
if (messages.length > 0) {
133
let date = messages[0].created_at.slice(0, 10);
134
let html_string = '';
135
for (let message of messages) {
136
let message_date = message.created_at.slice(0, 10);
137
if(date != message_date) {
138
html_string += `<span class='chat_date'>${date}</span>`;
141
let time = message.created_at.slice(11, 16);
142
let message_sender = data['players'][message.sender_id];
143
if (message.sender_id == parseInt(user_id)) {
144
html_string += `<div class='chat_sent'><span class='chat_message_sender'>${message_sender}</span>
145
<span class='chat_message_text'>${message.text}<span class='chat_message_time'>${time}</span></span></div>`;
147
html_string += `<div class='chat_received'><span class='chat_message_sender'>${message_sender}</span>
148
<span class='chat_message_text'>${message.text}<span class='chat_message_time'>${time}</span></span></div>`;
151
html_string += `<span class='chat_date'>${date}</span>`;
152
$('.lobby_chat_messages').html(html_string);
160
$('.lobby_chat_textarea').on('keydown', (event) => {
162
if (event.keyCode == 13) {
163
event.preventDefault();
164
let chat_message = event.target.value;
165
event.target.value = '';
168
url: "/chat/create_messages/",
169
data: {message: chat_message, type: 'lobby'},