Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var socket = null; // The socket, globally accessable
- var url = 'ws://127.0.0.1:8000/tracker'; // Socket host
- var players; // All players
- function doSetupSocket() {
- // Get a WebSocket - browser dependent!
- socket = ("MozWebSocket" in window ? new MozWebSocket(url) : new WebSocket(url));
- socket.onclose = function(msg) {
- alert("Connection closed");
- }
- socket.onmessage = handleUpdate;
- }
- function login() {
- var command = new Object();
- command.action = "Login";
- nick = document.getElementById('name').value;
- command.data = nick;
- var stringversion = JSON.stringify(command);
- socket.send(stringversion);
- }
- function sendStart() {
- var command = new Object();
- command.action = "Start";
- command.data = "";
- var stringversion = JSON.stringify(command);
- socket.send(stringversion);
- }
- function updatePosition(x,y) {
- var command = new Object();
- command.action = "Move";
- command.data = new Array(x,y);
- var stringversion = JSON.stringify(command);
- socket.send(stringversion);
- }
- function fire() {
- var command = new Object();
- command.action = "Fire";
- command.data = new Array(x,y,player_direction);
- var stringversion = JSON.stringify(command);
- socket.send(stringversion);
- }
- function handleUpdate(msg)
- {
- var info = JSON.parse(msg.data);
- var infoaction = info['action'];
- var infodata = info['data'];
- if (infoaction=="Map") {
- init(); // Map becomes the confirmation of successful login
- mazedata = infodata;
- MAZE_WIDTH = infodata[0].length;
- MAZE_HEIGHT = infodata.length;
- } else if (infoaction=="LoginFail") {
- alert('Username already taken, please choose another');
- } else if (infoaction=="Position") {
- x = infodata['x'];
- y = infodata['y'];
- player_direction = infodata['direction'];
- colour = infodata['colour'];
- draw();
- }
- if (gamestarted==true) {
- if (infoaction=="ActivePlayersList") {
- players = infodata;
- document.getElementById('players').innerHTML = '<font face="verdana" color="#FFFFFF"><b>Players: </b></font>'; // clear players list
- for (var i=0; i<players.length; i++) {
- if (players[i]['x']!=null)
- document.getElementById('players').innerHTML += '<font face="verdana" color="#'+players[i]['colour']+'">'+players[i]['nick']+'</font>, ';
- if (players[i]['nick']==nick)
- updateHealth(players[i]['health']);
- }
- document.getElementById('players').innerHTML = document.getElementById('players').innerHTML.slice(0,-2); // slice off last ", "
- draw();
- } else if (infoaction=="Die") {
- if (infodata==null)
- alert('You have died due to running into a wall');
- else
- alert('You have died due to colliding with '+infodata);
- gamestarted = false;
- drawSplash();
- } else if (infoaction=="DieShot") {
- alert('You have died due to being shot by '+infodata);
- gamestarted = false;
- drawSplash();
- } else if (infoaction=="Fire") {
- draw();
- drawfire(infodata[0],infodata[1],infodata[2]);
- }
- }
- }
Add Comment
Please, Sign In to add comment