Skip to content

Commit

Permalink
game is playable
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbosser committed Jan 13, 2015
1 parent c157cf5 commit 6913b02
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 13 deletions.
2 changes: 1 addition & 1 deletion game.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ var init = function(){

var start = null;
ball = new Ball(10, 10, width/2 - 5, height/2-5, canvas.width, canvas.height);
ball.k = initialBallK;
ball.k = 0;

platformR = new Platform(
100, // Height
Expand Down
21 changes: 10 additions & 11 deletions gamestarter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,35 @@ var checkIfPlayable = function(){
if(NUM_CONNECTED == 3){
startBtn.style.opacity = 1;
startBtn.style.cursor = "pointer";
startBtn.onclick = startGameFunction();
startBtn.onclick = startGameFunction;
}
}
startBtn.onclick=function(){
startGameFunction();
}

var ws = new WebSocket(WS_START + ip + ":" + port);
ws.addEventListener("open", function(event){
NUM_CONNECTED++
startBtn.style.opacity = 1;
checkIfPlayable();
littleConsole.innerHTML = "Connected to websocket!";
});

ws.addEventListener("message", function(event){
if(event.message == portP1 + " " + connectMessage){
console.log(event.data);
if(event.data == portP1 + " " + connectMsg){
p1Text.innerHTML = "P1 Connected!";
NUM_CONNECTED++;
checkIfPlayable();
}
else if(event.message == portP2 + " " + connectMessage){
else if(event.data == portP2 + " " + connectMsg){
p2Text.innerHTML = "P2 Connected!";
NUM_CONNECTED++;
checkIfPlayable();
}
else if(/\d{4} \d{0,}[.]\d{0,}/.test(event.message)){
else if((/\d{4} .\d*\.\d*/g).test(event.data)){
// First get first four digits
var inPort = /\d{4}/.match(event.message);
var angle = event.message.substring((inPort + " ").length);
var inPort = parseInt(event.data.substring(0,4));
var angle = parseFloat(event.data.substring((inPort + " ").length));

var speedRad = 20/(Math.PI/2);
var speedRad = 20/(90);

var speed = angle * speedRad;

Expand Down
70 changes: 70 additions & 0 deletions server/apiToInternal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function connectToUser(request){
var connection = request.accept();
var user = new ConnectedUser();
user.connection = connection;
//user.id = generateUUID();
console.log("A user connected");

user.connection.on("message", function (message){
handleMessage(message);
});
return user;
}
function ConnectedUser() {
this.connection;
this.id;

this.send = function (message){
this.connection.sendUTF(message);
}
}

function handleMessage(message){
if(message.type === "utf8"){
console.log("handling message: " + message.utf8Data);
var string = message.utf8Data;
var commands = string.split(";");
for (var a = 0; a < commands.length; a++) {
var inputs = commands[a].split(":");
var method = inputs[0];

//This is the actual api

if(method === "chat"){

console.log("chatting!");
var params = [];

for (var i = 1; i < inputs.length; i++) {
params.push(inputs[i]);
};

chat(params);
}

}
}
}

function chat(params){
var activeUsers = require("./backend").activeUsers;
var recipiants = params[0].split(",");
var message = params[1];

console.log(message);

for (var a = 0; a < recipiants.length; a++) {
console.log("in loop 1, recipiants: " + recipiants[a]);
for (var i = 0; i < activeUsers.length; i++) {
console.log("activeUser: " + activeUsers[i].id);
if(recipiants[a] == activeUsers[i].id){
console.log("sending message: " + message + ", to: " + activeUsers[i].id);
activeUsers[i].send(message);
}
}
}
}


exports.handleMessage = handleMessage;
exports.connectToUser = connectToUser;
73 changes: 73 additions & 0 deletions server/backend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
var WebSocketServer = require('websocket').server;
var APIInternal = require("./apiToInternal");
var httpServerFactory = require("./server");
var constants = require("./settings");
var httpServer1 = httpServerFactory.startAndGetServer(constants.PORT);
var httpServerL = httpServerFactory.startAndGetServer(constants.PORT_L);
var httpServerR = httpServerFactory.startAndGetServer(constants.PORT_R);

var wsServer1 = new WebSocketServer({
httpServer: httpServer1,
autoAcceptConnections: false
});
var wsServerL = new WebSocketServer({
httpServer: httpServerL,
autoAcceptConnections: false
});
var wsServerR = new WebSocketServer({
httpServer: httpServerR,
autoAcceptConnections: false
});

var client = null;
var left = null;
var right = null;

wsServer1.on("request", function(request){
if(!client) client = request.accept();

client.sendUTF("Hello! :) ");

if(left) client.sendUTF(constants.PORT_L + " connected")
if(right) client.sendUTF(constants.PORT_L + " connected")

client.on("close", function(reasonData, description) {
//Remove user from active users
client = null;
});
});

wsServerL.on("request", function(request){
if(!left) left = request.accept();

left.sendUTF("Hello! :) left");

left.on("message", function(message){
var newMsg = constants.PORT_L + " " + message.utf8Data;
if(client) client.sendUTF(newMsg);
console.log(newMsg);
});

left.on("close", function(reasonData, description) {
//Remove user from active users
left = null;
});
});

wsServerR.on("request", function(request){
if(!right) right = request.accept();

right.sendUTF("Hello! :) right");

right.on("message", function(message){
var newMsg = constants.PORT_R + " " + message.utf8Data;
if(client) client.sendUTF(newMsg);
console.log(newMsg);
});

right.on("close", function(reasonData, description) {
//Remove user from active users
right = null;
});
});

84 changes: 84 additions & 0 deletions server/backend2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
var WebSocketServer = require('websocket').server;
var APIInternal = require("./apiToInternal");
var httpServer = require("./server").startAndGetServer();

var wsServer = new WebSocketServer({
httpServer: httpServer,
autoAcceptConnections: false
});

var activeUsers = [];
var ids = [];
wsServer.on("request", function(request){
var user = APIInternal.connectToUser(request);
user.id = generateUUID();
activeUsers.push(user);
user.send("connection established. Currently " + activeUsers.length + " users connected");
user.send("Your id is: " + user.id);



user.connection.on("close", function(reasonData, description) {
//Remove user from active users
for (var i = 0; i < activeUsers.length; i++) {
var idOfActiveUser;
if(activeUsers[i].id === user.id) {
idOfActiveUser = activeUsers[i].id;
activeUsers.splice(i, 1);
}
var index = ids.indexOf(idOfActiveUser);
ids.splice(index, 1);
};
});
});



function generateUUID(){
ids.sort(function(a, b){return a-b});
if(activeUsers.length > ids.length){
//Take empty slot
ids = [];
console.log("There are more active users than ids. Taking empty slot");
for (var i = 0; i < activeUsers.length; i++) {
ids.push(activeUsers[i].id);
};
ids.sort(function(a, b){return a-b});

if (ids[0] >= 2) {
ids.push(1);
ids.sort(function(a, b){return a-b});
return 1;
}
else {
for (var i = 1; i < ids.length; i++) {
if (ids[i] - ids[i-1] >= 2){
ids.push(ids[i-1] + 1);
ids.sort(function(a, b){return a-b});
return ids[i-1] + 1;
}
};
console.log("Never more than one space between two ids. Giving top id");
ids.push(ids[ids.length - 1] + 1);
console.log("ids length: " + ids.length);
return ids[ids.length - 1];
}


}
else {

if(ids.length === 0){
ids.push(1);
return 1;
}
else {
var heighestId = ids[ids.length - 1];
ids.push(heighestId + 1);
return heighestId + 1;
}
}
}

exports.ids;
exports.activeUsers = activeUsers;
21 changes: 21 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var http = require("http");
var constants = require("./settings");
function startAndGetServer(port){

var httpServer = http.createServer(onRequest).listen(port, function(){
console.log((new Date()) + ' HTTP server is listening on port ' + port);
});

function onRequest(request, response){
console.log((new Date()) + " Recieved request for " + request.url)
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
console.log("Server kör");

return httpServer;
}
exports.startAndGetServer = startAndGetServer;


10 changes: 10 additions & 0 deletions server/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function define(name, value) {
Object.defineProperty(exports, name, {
value: value,
enumerable: true
});
}

define("PORT", 1337);
define("PORT_L", 1338);
define("PORT_R", 1339);
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ body{
}
.logo {
display: inline-block;
margin-top: 1em;
margin-top: 0.5em;
color:white;
font-family: "8bit";
font-size: 10em;
Expand Down

0 comments on commit 6913b02

Please sign in to comment.