1 decade ago by ShawnSwander
I need to control my non player entities from the server but my server script doesn't have impact embedded and I tried just putting it in as it is in main.js and it just crashes my script...
Here's the original server script though there isn't much reason to look at it
Another route I see is not adding any impact functions to my server and just send a socket to everyone to update based on a host client's recommended move (this gets around random values not being the same for everyone then broadcast that information.
Not sure what other options would be best...
Here's the original server script though there isn't much reason to look at it
Another route I see is not adding any impact functions to my server and just send a socket to everyone to update based on a host client's recommended move (this gets around random values not being the same for everyone then broadcast that information.
var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs = require('fs') app.listen(8080); var playerlocation = 0; var playerlist = []; function handler (req, res) { fs.readFile(__dirname + '/index.html', function (err, data) { if(err){ res.writeHead(500); return res.end('Error loading index.html'); } res.writeHead(200); res.end(data); }); } io.sockets.on('connection', function (socket) { socket.on('attack', function (playername, mobname, ocv, dcv, evade, dmg, pd){ var dmgroll = 0; hitroll = Math.floor(((Math.random()*6)+1)+Math.floor((Math.random()*6)+1)+Math.floor((Math.random()*6)+1)); if (hitroll+ocv-evade <11){ evade=true; } if (hitroll+ocv-evade-dcv>=11){ hit=true; for(i=0;i<dmg;i++){ dmgroll = dmgroll + ((Math.random()*6)+1); } if (i-dmg != 0){//assess half die rolls dmgroll = (dmgroll + ((Math.random()*6)+1))*(i-dmg); } dmgroll = dmgroll - pd; }else{ hit=false; } socket.emit('attackresult', playername,mobname,evade,hit,dmgroll); }); socket.on('recievedata', function (positionx,positiony,currentanimation,gamename) { socket.broadcast.emit('playermove', positionx,positiony,currentanimation,gamename);//trigger playmore to everyone function on index.html }); socket.on('initializeplayer', function (newplayername,posx,posy) { socket.clientname = newplayername; playerlist.push(newplayername); io.sockets.emit('addplayer',playerlist,newplayername,posx,posy); }); socket.on('disconnect', function(){ delete playerlist[socket.clientname]; for(var i in playerlist){ if(playerlist[i] == socket.clientname){ playerlist.splice(i, 1); } } if(playerlist[i] == socket.clientname){ playerlist.splice(i, 1); } socket.broadcast.emit('message',socket.clientname); socket.broadcast.emit('netreplayer',playerlist); }); });
Not sure what other options would be best...