Skip to content

Commit

Permalink
Improved commenting.
Browse files Browse the repository at this point in the history
  • Loading branch information
L05 committed Sep 3, 2019
1 parent f13729c commit 1b09e69
Showing 1 changed file with 77 additions and 81 deletions.
158 changes: 77 additions & 81 deletions public/lib/p5.multiplayer.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
////////////
// COMMON

// Initialize Network related variables
let socket;
let roomId = null;
let waiting = true;
let connected = false;
let id = null;
let hostConnected = false;

// Process URL
// Used to process the room ID. In order to specify a room ID,
// include ?=uniqueName, where uniqueName is replaced with the
// desired unique room ID.
function _processUrl() {
const parameters = location.search.substring(1).split("&");

////////////
// SETUP

function setupClient() {
_processUrl();

// Socket.io - open a connection to the web server on specified port
let addr = serverIp;
if (local) { addr = serverIp + ':' + serverPort; }
socket = io.connect(addr);

socket.emit('join', {name: 'client', roomId: roomId});
const temp = parameters[0].split("=");
roomId = unescape(temp[1]);

socket.on('id', function(data) {
id = data;
console.log("id: " + id);
});
console.log("id: " + roomId);
}

socket.on('found', function(data) {
connected = data.status;
waiting = false;
console.log("connected: " + connected);
})
// Send data from client to host via server
function sendData(datatype, data) {
data.type = datatype;
data.roomId = roomId;

socket.emit('clientConnect', {
roomId: roomId
});
socket.emit('sendData', data);
}

socket.on('receiveData', onReceiveData);
// Displays a message while attempting connection
function _displayWaiting() {
push();
fill(200);
textAlign(CENTER, CENTER);
textSize(20);
text("Attempting connection...", width/2, height/2-10);
pop();
}

////////////
// HOST

// Initialize Network related variables
let hostConnected = false;

function setupHost() {
_processUrl();

Expand All @@ -58,6 +63,14 @@ function setupHost() {
socket.on('receiveData', onReceiveData);
}

function isHostConnected(display=false) {
if (!hostConnected) {
if (display) { _displayWaiting(); }
return false;
}
return true;
}

function onHostConnect (data) {
console.log("Host connected to server.");
hostConnected = true;
Expand All @@ -67,35 +80,49 @@ function onHostConnect (data) {
}
}

// Process URL
// Used to process the room ID. In order to specify a room ID,
// include ?=uniqueName, where uniqueName is replaced with the
// desired unique room ID.
function _processUrl() {
const parameters = location.search.substring(1).split("&");
// Displays server address in lower left of screen
function displayAddress() {
push();
fill(255);
textSize(50);
text(serverIp+"/?="+roomId, 10, height-50);
pop();
}

const temp = parameters[0].split("=");
roomId = unescape(temp[1]);
////////////
// CLIENT

console.log("id: " + roomId);
}
// Initialize Network related variables
let waiting = true;
let connected = false;

function setupClient() {
_processUrl();

////////////
// MESSAGING
// Socket.io - open a connection to the web server on specified port
let addr = serverIp;
if (local) { addr = serverIp + ':' + serverPort; }
socket = io.connect(addr);

// Send data from client to host via server
function sendData(datatype, data) {
data.type = datatype;
data.roomId = roomId;

// Send rotation data to server
socket.emit('sendData', data);
}
socket.emit('join', {name: 'client', roomId: roomId});

socket.on('id', function(data) {
id = data;
console.log("id: " + id);
});

////////////
// FLOW CONTROL
socket.on('found', function(data) {
connected = data.status;
waiting = false;
console.log("connected: " + connected);
})

socket.emit('clientConnect', {
roomId: roomId
});

socket.on('receiveData', onReceiveData);
}

function isClientConnected(display=false) {
if (waiting) {
Expand All @@ -110,28 +137,6 @@ function isClientConnected(display=false) {
return true;
}

function isHostConnected(display=false) {
if (!hostConnected) {
if (display) { _displayWaiting(); }
return false;
}
return true;
}


////////////
// ONSCREEN DISPLAY

// Displays a message while attempting connection
function _displayWaiting() {
push();
fill(200);
textAlign(CENTER, CENTER);
textSize(20);
text("Attempting connection...", width/2, height/2-10);
pop();
}

// Displays a message instructing player to look at host screen
// for correct link.
function _displayInstructions() {
Expand All @@ -143,12 +148,3 @@ function _displayInstructions() {
text("bottom of the host screen.", width/2, height/2+10);
pop();
}

// Displays server address in lower left of screen
function displayAddress() {
push();
fill(255);
textSize(50);
text(serverIp+"/?="+roomId, 10, height-50);
pop();
}

0 comments on commit 1b09e69

Please sign in to comment.