+var playCount = 0;
+
+/* state of pieces
+ 0: empty
+ 1: white
+ 2: black
+*/
+function getStone(i) {
+ switch (i) {
+ case 1:
+ return 'white';
+ case 2:
+ return 'black';
+ default:
+ return 'empty';
+ }
+}
+
+var session = 0;
+var state = [];
+for (var i = 0; i < boardSize; i++)
+{
+ state[i] = [];
+ for (var j = 0; j < boardSize; j++)
+ {
+ state[i][j] = 0;
+ }
+}
+
+// @connect
+// Connect to the websocket
+var socket;
+const connect = function() {
+ return new Promise((resolve, reject) => {
+ const socketProtocol = (window.location.protocol === 'https:' ? 'wss:' : 'ws:')
+ const port = 3000;
+ const socketUrl = `${socketProtocol}//${window.location.hostname}:${port}/ws/`
+
+ // Define socket
+ socket = new WebSocket(socketUrl);
+
+ socket.onopen = (e) => {
+ // Send a little test data, which we can use on the server if we want
+ // Resolve the promise - we are connected
+ resolve();
+ }
+
+ socket.onmessage = (msg) => {
+ // Any data from the server can be manipulated here.
+ let parsed = JSON.parse(msg.data);
+ switch (parsed.type) {
+ case "board":
+ console.log("Setting board state");
+ parsed.data.forEach( function (move, index) {
+ state[move.x][move.y] =
+ move.state === 'white' ? 1 :
+ move.state === 'black' ? 2 :
+ 0;
+ });
+ drawGrid();
+ break;
+ default:
+ console.log(msg);
+ }
+ }
+
+ socket.onerror = (e) => {
+ // Return an error if any occurs
+ console.log(e);
+ resolve();
+ // Try to connect again
+ connect();
+ }
+ });
+}
+
+// @isOpen
+// check if a websocket is open
+const isOpen = function(ws) {
+ console.log(ws.readyState);
+ return ws.readyState === ws.OPEN
+}