From 0a6086949bbfe0fcb0815b89cc1b4315db0d3383 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 10 Aug 2016 16:50:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20=D0=BE=D0=BD?= =?UTF-8?q?=D0=BB=D0=B0=D0=B9=D0=BD=20=D0=B8=D0=B3=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/game/pipes-online/pipes_online.own | 176 ++++++++++++++++ examples/game/pipes-online/server/main.js | 188 ++++++++++++++++++ .../game/pipes-online/server/package.json | 17 ++ 3 files changed, 381 insertions(+) create mode 100644 examples/game/pipes-online/pipes_online.own create mode 100644 examples/game/pipes-online/server/main.js create mode 100644 examples/game/pipes-online/server/package.json diff --git a/examples/game/pipes-online/pipes_online.own b/examples/game/pipes-online/pipes_online.own new file mode 100644 index 0000000..fc5548a --- /dev/null +++ b/examples/game/pipes-online/pipes_online.own @@ -0,0 +1,176 @@ +use "std" +use "canvas" +use "socket" + +/// --- PIPES CELL --- +CELL_START = 0 +HORIZONTAL = 0 +VERTICAL = 1 +LEFT_TO_DOWN = 2 +LEFT_TO_UP = 3 +RIGHT_TO_UP = 4 +RIGHT_TO_DOWN = 5 +CROSS = 6 +CELL_LAST = 6 + +Cells = [ + {"index": HORIZONTAL, "next": VERTICAL}, + {"index": VERTICAL, "next": HORIZONTAL}, + {"index": LEFT_TO_DOWN, "next": LEFT_TO_UP}, + {"index": LEFT_TO_UP, "next": RIGHT_TO_UP}, + {"index": RIGHT_TO_UP, "next": RIGHT_TO_DOWN}, + {"index": RIGHT_TO_DOWN, "next": LEFT_TO_DOWN}, + {"index": CROSS, "next": CROSS} +]; + + +def draw(v, cellSize) { + c2 = cellSize / 2 + match v { + case HORIZONTAL : fillRect(0, c2 - 2, cellSize, 4) + case VERTICAL : fillRect(c2 - 2, 0, 4, cellSize) + case LEFT_TO_DOWN : { + fillRect(0, c2 - 2, c2, 4) + fillRect(c2 - 2, c2 - 2, 4, c2 + 2) + } + case LEFT_TO_UP : { + fillRect(0, c2 - 2, c2, 4) + fillRect(c2 - 2, 0, 4, c2 + 2) + } + case RIGHT_TO_UP : { + fillRect(c2 - 2, c2 - 2, c2 + 2, 4) + fillRect(c2 - 2, 0, 4, c2 + 2) + } + case RIGHT_TO_DOWN : { + fillRect(c2 - 2, c2 - 2, c2 + 2, 4) + fillRect(c2 - 2, c2 - 2, 4, c2 + 2) + } + case CROSS : { + fillRect(c2 - 2, 0, 4, cellSize) + fillRect(0, c2 - 2, cellSize, 4) + } + } +} + + +/// --- PIPES BOARD --- +SIZE = 10 + +// Creating game board +board = newarray(SIZE, SIZE) +boardGhost = newarray(SIZE, SIZE) + +def switchCell(x, y) { + board[x][y] = Cells[board[x][y]].next +} +def setGhostCell(x, y) { + boardGhost[x][y] = Cells[boardGhost[x][y]].next +} + + +/// --- PIPES MAIN --- +translateX = 0 translateY = 0 +isGameFinished = false +isWin = false + +/* frect with translate ability */ +def fillRect(x,y,w,h) { + frect(translateX+x, translateY+y, w, h) +} + +WIDTH = 320 HEIGHT = 320 +WINDOW_WIDTH = WIDTH * 2 +window("Pipes Online", WINDOW_WIDTH, HEIGHT) +cellSize = WIDTH / SIZE + +// cursor +curX = 0 curY = 0 +curGhostX = 0 curGhostY = 0 + +// Initialize client +socket = newSocket("http://localhost:6469") +socket.on("gameStart", def(data) { + data = data[0] + for i=0, i 0) { + curX-- + socket.emit("updateCursor", {"x": curX, "y": curY}) + } else if (key == VK_RIGHT && curX < SIZE - 1) { + curX++ + socket.emit("updateCursor", {"x": curX, "y": curY}) + } else if (key == VK_UP && curY > 0) { + curY-- + socket.emit("updateCursor", {"x": curX, "y": curY}) + } else if (key == VK_DOWN && curY < SIZE - 1) { + curY++ + socket.emit("updateCursor", {"x": curX, "y": curY}) + } else if (key == VK_FIRE) { + switchCell(curX, curY) + socket.emit("switchCell", {"x": curX, "y": curY}) + } + else if (key == 48) run = 0 + } + + // background + color(isGameFinished ? (isWin ? #66FF66 : #FF6666) : #FFFFFF) + frect(0, 0, WIDTH, HEIGHT) + color(isGameFinished ? (!isWin ? #66FF66 : #FF6666) : #DDDDDD) + frect(WIDTH, 0, WIDTH, HEIGHT) + // cursor + color(#4444FF) + frect(curX*cellSize, curY*cellSize, cellSize, cellSize) + color(#4040DD) + frect(WIDTH + curGhostX*cellSize, curGhostY*cellSize, cellSize, cellSize) + for (i=0, i 0) && (supportRight(board[curX - 1][curY])) ) { + if (isConnected(board, curX - 1, curY, visited)) return true; + } + if ( supportRight(current) && (curX < SIZE - 1) && (supportLeft(board[curX + 1][curY])) ) { + if (isConnected(board, curX + 1, curY, visited)) return true; + } + if ( supportUp(current) && (curY > 0) && (supportDown(board[curX][curY - 1])) ) { + if (isConnected(board, curX, curY - 1, visited)) return true; + } + if ( supportDown(current) && (curY < SIZE - 1) && (supportUp(board[curX][curY + 1])) ) { + if (isConnected(board, curX, curY + 1, visited)) return true; + } + return false; + }; + + var isFinished = function(board) { + // Start pipe must have left touchpoint + if (!supportLeft(board[0][0])) return false; + // Finish pipe - right touchpoint + if (!supportRight(board[SIZE - 1][SIZE - 1])) return false; + + var visited = new Array(SIZE); + for (var i = 0; i < SIZE; i++) { + visited[i] = new Array(SIZE); + for (var j = 0; j < SIZE; j++) visited[i][j] = false; + } + // Recursive traversal from left upper pipe + return isConnected(board, 0, 0, visited); + }; + + self.isGameFinished = function (isGhost) { + return isFinished(isGhost ? self.board2 : self.board1); + }; + + return self; +}; + +var board = {}; +var players = []; + +io.on('connection', function (socket) { + if (players.length >= 2) { + console.log('Sorry, server is full.'); + return; + } + + socket.playerId = players.length + 1; + players.push({socket: socket}); + console.log('Player ' + socket.playerId + ' Connected!'); + + socket.on('switchCell', function (data) { + var isGhost = socket.playerId === 2; + var current = isGhost ? 1 : 0; + var opposite = isGhost ? 0 : 1; + board.switchCell(data.x, data.y, isGhost); + players[opposite].socket.emit('updateGhostCell', data); + if (board.isGameFinished(isGhost)) { + players[current].socket.emit('gameFinished', true); + players[opposite].socket.emit('gameFinished', false); + } else if (board.isGameFinished(!isGhost)) { + players[opposite].socket.emit('gameFinished', true); + players[current].socket.emit('gameFinished', false); + } + }); + socket.on('updateCursor', function (data) { + var isGhost = socket.playerId === 2; + var opposite = isGhost ? 0 : 1; + players[opposite].socket.emit('updateGhostCursor', data); + }); + socket.on('connect_timeout', function (exception) { + console.log('SOCKET TIMEOUT ' + exception); + socket.destroy(); + }); + socket.on('disconnect', function () { + console.log('disconnect Player ' + socket.playerId); + players.splice(socket.playerId - 1, 1); + }); + + // start game + if (players.length === 2) { + board = Board(SIZE); + board.create(); + + players[0].socket.emit('gameStart', board.board1); + players[1].socket.emit('gameStart', board.board2); + } +}); + +/*io.on('connection', function(socket) { + console.log('New connection'); + socket.on('greetings', function(data) { + console.log('Got greetings from client'); + socket.emit('pong', "Hello from server"); + }); + socket.on('complex_object', function(data) { + console.log('Got object: ' + data); + socket.emit('complex_object', {key1: data.key2, key2: data.key1, arr: [0,1,2,"34"]}); + }); + });*/ +/*io.on('connection', function(socket) { + console.log('New connection'); + socket.emit('pong', "Hello"); + + socket.on('ping', function(data) { + console.log('Got ping from client'); + socket.emit('pong', "Hello from server, " + data); + }); + socket.on('pong', function(data) { + console.log('Got pong from client'); + console.log('pong'); + }); + });*/ \ No newline at end of file diff --git a/examples/game/pipes-online/server/package.json b/examples/game/pipes-online/server/package.json new file mode 100644 index 0000000..7d5139d --- /dev/null +++ b/examples/game/pipes-online/server/package.json @@ -0,0 +1,17 @@ +{ + "name": "TestSocketIOServer", + "version": "1.0.0", + "keywords": [ + "util", + "functional", + "server", + "client", + "browser" + ], + "author": "aNNiMON", + "contributors": [], + "dependencies": { + "express": "^4.13.4", + "socket.io": "^1.4.5" + } +}