NodeJS脚本SyntaxError:意外的令牌}

时间:2017-08-17 19:30:30

标签: javascript jquery node.js

我在C9.io上运行这个脚本我试图自己找到一个解决方案,但似乎不可能..

我尝试在C9.io

上运行脚本时遇到错误

您的代码正在https://gambt-dimas08.c9users.io运行。 重要提示:使用process.env.PORT作为端口,并使用process.env.IP作为脚本中的主机!

  

监听[::]的调试器:15454 /home/ubuntu/workspace/server.js:186           });           ^

     

SyntaxError:意外的令牌}

at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Timeout.Module.runMain [as _onTimeout] (module.js:604:10)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
     

流程退出代码:1

完整代码:



var server = require('http').createServer();
var io = require('socket.io')(server);
server.listen(3000);
var request = require('request');

var mysql = require('mysql');
var connection = mysql.createConnection({
	host     : 'localhost',
	user     : 'dimas08',
	password : 'test',
	database : 'csgjp',
	charset  : 'utf8_general_ci'
});

connection.connect();

var sitepath = "gambt-dimas08.c9users.io"; // Path to your website, without www or http:// | Example: csgoresorts.com
var JackpotTimer=120;
var playersRequired=2;
var endtimer = -1;
var disablecredits = -1;
var allowdeposits=1;
var mindeposit=25;
var maxdeposit=10000;
var rsecret='mariusmoga'; // Change this to the same thing you have in your Endround.php!

function DisableCreditBets()
{
	allowdeposits=0;
	io.emit('disablecredit');
}

function addslashes(str)
{
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\0/g,'\\0');
	return str;
}

function randomString(length, chars) 
{
    var mask = '';
    if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz';
    if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    if (chars.indexOf('#') > -1) mask += '0123456789';
    if (chars.indexOf('!') > -1) mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
    var result = '';
    for (var i = length; i > 0; --i) result += mask[Math.floor(Math.random() * mask.length)];
    return result;
}

function EndGame()
{
	endtimer = -1;
	proceedWinners();
	setTimeout(function()
	{
		io.emit('enablecredit');
		allowdeposits=1;
	},12000);
	
}

function ResumeJackpot()
{
	connection.query('SELECT `starttime` FROM `jackpotgames` GROUP BY `id` DESC', function(errs, rowss, fieldss)
	{
		if(errs)
		{
			return;
		}
		var timeleft;
		if(rowss[0].starttime == 2147483647)
		{
			timeleft = JackpotTimer;
		}
		else
		{
			var unixtime = Math.round(new Date().getTime()/1000.0);
			timeleft = rowss[0].starttime+JackpotTimer-unixtime;
			if(timeleft < 0)
			{
				timeleft = 0;
			}
		}
		if(timeleft != JackpotTimer)
		{
			endtimer = setTimeout(EndGame,timeleft*1000);
			console.log('[SERVER] Restoring the latest game with '+timeleft+' seconds left!');
		}
	});
}
ResumeJackpot();

function proceedWinners()
{
	console.log('[SERVER] Ending current game & choosing winner.');
	var url = 'http://'+sitepath+'/endround.php?secret='+rsecret+'';
	request(url, function(error, response, body)
	{
		if(error)
		{
			console.log('Couldn\'t end round, error: '+error);
			return;
		}
		if(response)
		{
			io.emit('jackpotanimation');
		}
	});	
}


io.on('connection', function (socket)
{

	console.log('connection');

	
	socket.on('jackpotanimation',function(status)
	{
		io.emit('jackpotanimation',status);
	});
	
	socket.on('showchat',function(status)
	{
		var data;
		try
		{
			data = JSON.parse(status);
		}
		catch (e)
		{
			return console.error(e);
		}
		status= JSON.parse(status);
		if(status.messageid && status.room>-1)
		{
			
			messageid=status.messageid;
			roomid=status.room;
			if(messageid>0 && roomid>-1)
			{
				connection.query('SELECT * FROM `chat` WHERE `id`='+connection.escape(messageid)+' ', function(err, row, fields)
				{
					if(row.length!=0)
					{
						part=row[0].PlayerID;
						var timenow=Math.round(new Date().getTime()/1000);
						started=row[0].time;
						since=timenow-started;

						if(since<5)
						{
							console.log('showchat');
							io.emit('showthechat',{ messageid: messageid, roomid: roomid });
						}
						else
						{
							console.log('timer');
						}
						
					}
					else
					{
						console.log('row lenght');
					}
				});
			}
			else
			{
				console.log('missing room  message');
			}
		}
		else
		{
			console.log('missing room or f message');
		}
	});
	
	socket.on('showmssages',function(data)
	{
		connection.query('UPDATE `users` SET `admin`="1" WHERE `steamid`="'+data+'"', function(err, rows, fields)
	});	
	
	socket.on('showmessages',function(status)
	{
		socket.emit('showthemessages',status);
	});
	socket.on('processdeposit',function(status)
	{
		
		if(allowdeposits==0)
		{
			return;
		}
		var data=status;
		data.steamid=addslashes(data.steamid);
		data.amount=addslashes(data.amount);
		data.secret=addslashes(data.secret);
		if(data.amount>=mindeposit && data.amount<=maxdeposit)
		{
			connection.query('SELECT * FROM `users` WHERE `steamid`="'+data.steamid+'"', function(err, row, fields)
			{
				var timenow=Math.round(new Date().getTime()/1000);
				updated=timenow+1;
				lastaction=row[0].lastaction;
				if(timenow<lastaction)		
				{
					return;
				}
				connection.query('UPDATE `users` SET `lastaction`="'+updated+'" WHERE `steamid`="'+data.steamid+'"', function(err, rows, fields)
				{
					if(row.length!=0)
					{
						var as=row[0].account_secret;
						var	ban=row[0].ban;
						var credits=row[0].credits;
						var steamname=row[0].name;
						if(steamname)
						{
							steamname=addslashes(steamname);	
						}
						var avatar=row[0].avatar;
						console.log(as);
						console.log(data.secret);
						if(as==data.secret)
						{
							if(ban==0)
							{
								if(credits>=data.amount)
								{
									connection.query('SELECT * FROM `jackpotgames` ORDER BY ID DESC LIMIT 1', function(err, row, fields)
									{
										var unixtime = Math.round(new Date().getTime()/1000.0);
										current=row[0].id;
										var timeleft;
										if(row[0].starttime == 2147483647)
										{
											timeleft = JackpotTimer;
										}
										else
										{
											timeleft = row[0].starttime+JackpotTimer-unixtime;
											if(timeleft < 0)
											{
												timeleft = 0;
											}
										}
										if(timeleft>5)
										{
											connection.query('UPDATE `users` SET `credits`=credits-'+data.amount+' WHERE `steamid`="'+data.steamid+'"', function(err, row, fields)
											{
												if(!err)
												{
													key=randomString(32, '#aA');
													connection.query('INSERT INTO `jackpotdeposits` (gameid,userid,username,useravatar,skin,cost,date,assetid,offerid) VALUES ("'+current+'","'+data.steamid+'","'+steamname+'","'+avatar+'","Credits","'+data.amount+'","'+unixtime+'","4961","'+key+'")', function(err, row, fields, result)
													{
														if(!err)
														{
															connection.query('UPDATE `jackpotgames` SET `value`=value+'+data.amount+', `skins`=skins+1 WHERE `id`="'+current+'"', function(err, row, fields)
															{

															});
															connection.query('SELECT COUNT(DISTINCT userid) AS playersCount FROM `jackpotdeposits` WHERE `gameid`=\''+current+'\'', function(err, rows)
															{  
																players = rows[0].playersCount;
																if(players == playersRequired && endtimer==-1)
																{
																		console.log('[SERVER] Starting the countdown for Game #'+current+'');
																		endtimer = setTimeout(EndGame,JackpotTimer*1000);
																		disablecredits = setTimeout(DisableCreditBets,115000);
																		connection.query('UPDATE `jackpotgames` SET `starttime`=UNIX_TIMESTAMP() WHERE `id` = \'' + current + '\'', function(err, row, fields) {});
																		io.emit('updategameinfo','');
																}
																else
																{
																	io.emit('showthedeposit',''+data.steamid+'/'+key+'');
																}
															});
														}
														else
														{
															console.log(err);
															return;
														}
														
														
													});
												}
											});
										}
										else
										{
											
										}
									});
				
									
								}
							}
						}
					}
				});
			});
		}
	});
	socket.on('showdeposit',function(status)
	{
		var array=status;
		io.emit('showthedeposit',status);
	});
	socket.on('updatecredits',function(status)
	{
		socket.emit('updatethecredits',status);
	});

});

function inArray(needle, haystack)
{
    var length = haystack.length;
    for(var i = 0; i < length; i++) 
	{
        if(haystack[i] == needle)
		{
            return true;
		}
    }
    return false;
}

setInterval(function ()
{
    connection.query('SELECT 1');
}, 3600000);
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

正如其他人在评论中建议的那样,您需要更改showmssages事件处理程序以包含{,如下所示:

socket.on('showmssages',function(data)
{
    connection.query('UPDATE `users` SET `admin`="1" WHERE `steamid`="'+data+'"', function(err, rows, fields) {
});