Commit 0a29c950 authored by marioromera's avatar marioromera
Browse files

all shit fixed

parent 1dc8205c
This diff is collapsed.
......@@ -48,10 +48,7 @@ export default function NodePool(el, network) {
const { context } = this;
context.beginPath();
context.moveTo(
Math.max(this.nodeSize, Math.min(this.width - this.nodeSize, d.x)),
Math.max(this.nodeSize, Math.min(this.width - this.nodeSize, d.y))
);
context.moveTo(this.width / 2, this.height / 2);
context.arc(d.x, d.y, this.nodeSize, 0, 2 * Math.PI);
context.fillStyle = this.color(index);
context.fill();
......@@ -63,6 +60,11 @@ export default function NodePool(el, network) {
};
this.addNode = function (source_id, id, text) {
this.simulation
.force('link', null)
.force('charge', null)
.force('center', null);
console.log('Adding node', source_id, id, text);
const newNode = new Node(id, text);
......@@ -71,6 +73,7 @@ export default function NodePool(el, network) {
source: source_id,
target: id,
});
this.start();
};
......
......@@ -42,18 +42,23 @@ state.dataObserver.subscribe((update) => {
});
}
if (update.sessions) {
console.log('Sessions updated', state.data.sessions);
draw.drawSessions(state.data.sessions);
if (!state.data.overallConnected.includes(state.id)) {
updateGossipData('overallConnected', [
...state.data.overallConnected,
state.id,
]);
}
const sessionsLength = state.data.sessions.length;
const sessionsLength = state.data.sessions.length;
const peersNumber = state.data.overallConnected.length;
const myIndex = state.data.overallConnected.indexOf(state.id);
const myTurn = sessionsLength % peersNumber === myIndex;
const myTurn =
state.connectedPeers.length &&
(!sessionsLength ||
state.data.sessions[sessionsLength - 1].from !== state.id);
draw.$myTurn.next(myTurn);
draw.$myTurn.next(myTurn);
if (update.sessions) {
console.log('Sessions updated', state.data.sessions);
draw.drawSessions(state.data.sessions);
}
});
......
......@@ -139,8 +139,8 @@ body {
#drawing-svg {
display: inline-block;
border: 1px black solid;
border: 1px white dashed;
border-radius: 5px;
pointer-events: all;
}
......
......@@ -4,7 +4,6 @@ import { initScuttlebutt, updateGossipData } from './scuttlebutt';
import { drawOffer, drawAnswer, clearRendererCredentials } from '.';
import _ from 'lodash';
let connectionInterval;
// We start the peer Library by creating an instance for each ID
export function createPeer({ id, initiator }) {
if (Peer.WEBRTC_SUPPORT) {
......@@ -78,8 +77,6 @@ export function addConnected(id) {
// Concatenate the id of the connected peer
state.connectedPeers = [...state.connectedPeers, id];
state.data.overallConnected.push(state.id);
state.gossipViz.addNode(state.id, id, state.connectedPeers.indexOf(id));
}
......@@ -133,11 +130,18 @@ export function addListenerToPeerEvents(id) {
// Start the scuttlebutt protocol
initScuttlebutt();
updateGossipData('overallConnected', [
...state.data.overallConnected,
state.id,
]);
});
peer.on('error', function (e) {
clearInterval(connectionInterval);
console.error(e);
removePeer(peer.id);
});
peer.on('end', function (e) {
removePeer(peer.id);
});
}
......@@ -7,10 +7,6 @@ import { state, saveData } from './state';
export function initScuttlebutt() {
setGossipPeers();
listenGossipEvents();
Object.keys(state.data).forEach((k) => {
updateGossipData(k, state.data[k]);
});
}
/**
......@@ -20,9 +16,7 @@ export function initScuttlebutt() {
*/
export function updateGossipData(key, value) {
const { gossip } = state;
if (gossip) {
console.log('Updating gossip data', key, value);
gossip.set(key, value);
}
}
......@@ -46,7 +40,8 @@ export function setGossipPeers() {
});
}
function updateHandler([key, value]) {
function updateHandler(update) {
const [key, value] = update;
// Set state data
saveData({ key, value });
}
......
......@@ -37,7 +37,7 @@ export let state = {
};
export function saveData({ key, value }) {
state.data = { ...state.data, [key]: value };
state.data[key] = value;
state.dataObserver.next({ [key]: value });
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment