Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move all chat-controlled settings into chat-client module #423

Merged
merged 2 commits into from
May 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 6 additions & 108 deletions src/client/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function startGame(type) {
animloop();
socket.emit('respawn');
window.chat.socket = socket;
window.chat.registerFunctions();
window.canvas.socket = socket;
global.socket = socket;
}
Expand Down Expand Up @@ -125,24 +126,23 @@ var target = {x: player.x, y: player.y};
global.target = target;

window.canvas = new Canvas();
window.chat = new ChatClient();

var visibleBorderSetting = document.getElementById('visBord');
visibleBorderSetting.onchange = toggleBorder;
visibleBorderSetting.onchange = settings.toggleBorder;

var showMassSetting = document.getElementById('showMass');
showMassSetting.onchange = toggleMass;
showMassSetting.onchange = settings.toggleMass;

var continuitySetting = document.getElementById('continuity');
continuitySetting.onchange = toggleContinuity;
continuitySetting.onchange = settings.toggleContinuity;

var continuitySetting = document.getElementById('roundFood');
continuitySetting.onchange = toggleRoundFood;
continuitySetting.onchange = settings.toggleRoundFood;

var c = window.canvas.cv;
var graph = c.getContext('2d');

window.chat = new ChatClient();

$( "#feed" ).click(function() {
socket.emit('1');
window.canvas.reenviar = false;
Expand All @@ -153,108 +153,6 @@ $( "#split" ).click(function() {
window.canvas.reenviar = false;
});

function checkLatency() {
// Ping.
global.startPingTime = Date.now();
socket.emit('ping');
}

function toggleDarkMode() {
var LIGHT = '#f2fbff',
DARK = '#181818';
var LINELIGHT = '#000000',
LINEDARK = '#ffffff';

if (global.backgroundColor === LIGHT) {
global.backgroundColor = DARK;
global.lineColor = LINEDARK;
window.chat.addSystemLine('Dark mode enabled.');
} else {
global.backgroundColor = LIGHT;
global.lineColor = LINELIGHT;
window.chat.addSystemLine('Dark mode disabled.');
}
}

function toggleBorder() {
if (!global.borderDraw) {
global.borderDraw = true;
window.chat.addSystemLine('Showing border.');
} else {
global.borderDraw = false;
window.chat.addSystemLine('Hiding border.');
}
}

function toggleMass() {
if (global.toggleMassState === 0) {
global.toggleMassState = 1;
window.chat.addSystemLine('Viewing mass enabled.');
} else {
global.toggleMassState = 0;
window.chat.addSystemLine('Viewing mass disabled.');
}
}

function toggleContinuity() {
if (!global.continuity) {
global.continuity = true;
window.chat.addSystemLine('Continuity enabled.');
} else {
global.continuity = false;
window.chat.addSystemLine('Continuity disabled.');
}
}

function toggleRoundFood(args) {
if (args || global.foodSides < 10) {
global.foodSides = (args && !isNaN(args[0]) && +args[0] >= 3) ? +args[0] : 10;
window.chat.addSystemLine('Food is now rounded!');
} else {
global.foodSides = 5;
window.chat.addSystemLine('Food is no longer rounded!');
}
}

// TODO: Break out many of these GameControls into separate classes.

window.chat.registerCommand('ping', 'Check your latency.', function () {
checkLatency();
});

window.chat.registerCommand('dark', 'Toggle dark mode.', function () {
toggleDarkMode();
});

window.chat.registerCommand('border', 'Toggle visibility of border.', function () {
toggleBorder();
});

window.chat.registerCommand('mass', 'Toggle visibility of mass.', function () {
toggleMass();
});

window.chat.registerCommand('continuity', 'Toggle continuity.', function () {
toggleContinuity();
});

window.chat.registerCommand('roundfood', 'Toggle food drawing.', function (args) {
toggleRoundFood(args);
});

window.chat.registerCommand('help', 'Information about the chat commands.', function () {
chat.printHelp();
});

window.chat.registerCommand('login', 'Login as an admin.', function (args) {
socket.emit('pass', args);
});

window.chat.registerCommand('kick', 'Kick a player, for admins only.', function (args) {
socket.emit('kick', args);
});


// socket stuff.
function setupSocket(socket) {
// Handle ping.
Expand Down
105 changes: 105 additions & 0 deletions src/client/js/chat-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,48 @@ class ChatClient {
global.chatClient = this;
}

// TODO: Break out many of these GameControls into separate classes.

registerFunctions() {
var self = this;
this.registerCommand('ping', 'Check your latency.', function () {
self.checkLatency();
});

this.registerCommand('dark', 'Toggle dark mode.', function () {
self.toggleDarkMode();
});

this.registerCommand('border', 'Toggle visibility of border.', function () {
self.toggleBorder();
});

this.registerCommand('mass', 'Toggle visibility of mass.', function () {
self.toggleMass();
});

this.registerCommand('continuity', 'Toggle continuity.', function () {
self.toggleContinuity();
});

this.registerCommand('roundfood', 'Toggle food drawing.', function (args) {
self.toggleRoundFood(args);
});

this.registerCommand('help', 'Information about the chat commands.', function () {
self.printHelp();
});

this.registerCommand('login', 'Login as an admin.', function (args) {
self.socket.emit('pass', args);
});

this.registerCommand('kick', 'Kick a player, for admins only.', function (args) {
self.socket.emit('kick', args);
});
global.chatClient = this;
}

// Chat box implementation for the users.
addChatLine(name, message, me) {
if (this.mobile) {
Expand Down Expand Up @@ -112,6 +154,69 @@ class ChatClient {
}
}
}

checkLatency() {
// Ping.
global.startPingTime = Date.now();
socket.emit('ping');
}

toggleDarkMode() {
var LIGHT = '#f2fbff',
DARK = '#181818';
var LINELIGHT = '#000000',
LINEDARK = '#ffffff';

if (global.backgroundColor === LIGHT) {
global.backgroundColor = DARK;
global.lineColor = LINEDARK;
this.addSystemLine('Dark mode enabled.');
} else {
global.backgroundColor = LIGHT;
global.lineColor = LINELIGHT;
this.addSystemLine('Dark mode disabled.');
}
}

toggleBorder() {
if (!global.borderDraw) {
global.borderDraw = true;
this.addSystemLine('Showing border.');
} else {
global.borderDraw = false;
this.addSystemLine('Hiding border.');
}
}

toggleMass() {
if (global.toggleMassState === 0) {
global.toggleMassState = 1;
this.addSystemLine('Viewing mass enabled.');
} else {
global.toggleMassState = 0;
this.addSystemLine('Viewing mass disabled.');
}
}

toggleContinuity() {
if (!global.continuity) {
global.continuity = true;
this.addSystemLine('Continuity enabled.');
} else {
global.continuity = false;
this.addSystemLine('Continuity disabled.');
}
}

toggleRoundFood(args) {
if (args || global.foodSides < 10) {
global.foodSides = (args && !isNaN(args[0]) && +args[0] >= 3) ? +args[0] : 10;
this.addSystemLine('Food is now rounded!');
} else {
global.foodSides = 5;
this.addSystemLine('Food is no longer rounded!');
}
}
}

export default ChatClient;