Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const WebSocket = require('ws');
- const wss = new WebSocket.Server({ port: 3004 });
- var RandomOrg = require('random-org');
- var mysql = require('mysql');
- var con = mysql.createConnection({
- host: "185.14.30.21",
- user: "root",
- password: "VuTbmyPkjaPM4BCp",
- database: "wheelbet",
- });
- const _time = 20;
- var clients = [];
- var players = [];
- var game_started = false;
- var timer_started = false;
- var round_id = 0;
- con.query("SELECT count(id) as cnt FROM matches", function (err, res, fields) {
- global.endGames = res[0].cnt;
- });
- con.query("SELECT max(sum) as val FROM matches WHERE DATE(date) = DATE(NOW())", function (err, res, fields) {
- global.maxWinDay = res[0].val;
- });
- con.query("SELECT max(sum) as val FROM matches", function (err, res, fields) {
- global.maxMoney = res[0].val;
- });
- function sendRoundInfo(ws) {
- con.query("SELECT id FROM matches where type_id = 1 order by id desc limit 0,1", function (err, last_match, fields) {
- if (!game_started)
- round_id = last_match[0].id + 1;
- roundInfo = ['roundInfo', {
- 'id': round_id,
- 'players': players,
- 'jackpot': players.reduce(function(p, c) {
- return p + parseInt(c.status.bet);
- }, 0),
- 'time': 20,
- }];
- ws.send(JSON.stringify(roundInfo))
- });
- }
- function sendStatToClients() {
- ////console.log('send stat game.js ' + endGames);
- clients.forEach(function(client) {
- client.send(JSON.stringify({'stats': {
- 'online': wss.clients.size,
- 'endGames': {val:global.endGames},
- 'maxWinDay': {val:global.maxWinDay},
- 'maxMoney': {val:global.maxMoney}}
- }));
- });
- }
- wss.on('connection', function connection(ws) {
- clients.push(ws);
- sendRoundInfo(ws);
- clients.forEach(function(client) {
- client.send(JSON.stringify({'stats': {
- 'online': wss.clients.size,
- 'endGames': '',
- 'maxWinDay': '',
- 'maxMoney': ''}
- }));
- });
- ws.send(JSON.stringify({'stats': {
- 'online': wss.clients.size,
- 'endGames': {val:global.endGames},
- 'maxWinDay': {val:global.maxWinDay},
- 'maxMoney': {val:global.maxMoney}}
- }));
- ws.on('message', function (msg) {
- var msg = JSON.parse(msg);
- switch(msg.event)
- {
- case 'bet':
- if (game_started) break;
- con.query("SELECT * FROM users where remember_token = ? limit 0,1", [msg.token], function (err, new_player, fields)
- {
- if (err) throw err;
- if (new_player[0].balance < parseInt(msg.bet)) {
- throw err;
- }
- var new_balance = new_player[0].balance - parseInt(msg.bet);
- con.query("UPDATE users SET balance = ? where id = ?", [new_balance, new_player[0].id], function (err, result){});
- var round_bets_sum = msg.bet;
- players.forEach(function(player, index) {
- round_bets_sum += parseInt(player.status.bet);
- if (player.id == new_player[0].id) {
- players[index].status.bet += parseInt(msg.bet);
- }
- });
- var new_player_tickets_from = 0;
- var new_player_tickets_to = 0;
- // calc raises
- if (players.length > 0) {
- var last_player_ticket_to = players[players.length - 1].status.tickets[0][1];
- new_player_tickets_from = parseInt(last_player_ticket_to) + 1;
- new_player_tickets_to = parseInt(last_player_ticket_to) + (msg.bet * 100);
- } else {
- new_player_tickets_from = 1;
- new_player_tickets_to = msg.bet*100
- }
- var new_player_obj = {
- "id": new_player[0].id,
- "gid": round_id,
- "name": new_player[0].fullname,
- "avatar": new_player[0].avatar,
- "team": false,
- "raise": msg.bet / round_bets_sum,
- "status": {
- "n": parseInt(players.length),
- "bet": parseInt(msg.bet),
- "tickets": [
- [
- new_player_tickets_from,
- new_player_tickets_to
- ]
- ]
- },
- "_lastDate": Date.now()
- }
- var response_player;
- var player_exists = false;
- // check if is exists user
- players.forEach(function(player, index) {
- if (player.id == new_player_obj.id) {
- player_exists = true;
- response_player = players[index];
- }
- });
- // update raises after new bet when timer is started
- players.forEach(function(player, index) {
- if (index == 0) {
- players[index].status.tickets[0][0] = 1;
- players[index].status.tickets[0][1] = players[index].status.bet*100
- } else {
- var last_player_ticket_to = players[index - 1].status.tickets[0][1];
- players[index].status.tickets[0][0] = parseInt(last_player_ticket_to) + 1;
- players[index].status.tickets[0][1] = parseInt(last_player_ticket_to) + (players[index].status.bet * 100);
- }
- });
- if (!player_exists) {
- players.push(new_player_obj);
- response_player = new_player_obj;
- }
- var bet = ['bet', {
- "date": Date.now(),
- "round": {
- "id": round_id,
- "time": _time,
- "timer": (players.length > 1) ? true : false
- },
- "player": response_player,
- }];
- clients.forEach(function(client) {
- client.send(JSON.stringify(bet));
- });
- //console.log(players.length)
- if (players.length > 1 && !timer_started) {
- timer_started = true;
- setTimeout(function() {
- game_started = true;
- var random = new RandomOrg({
- apiKey: '0f25fbe9-279e-4ac2-a1ff-6e5b7f2bf885',
- hashedApiKey: 'RIsHTjoCLnlNF6lXi9hXjyB+ABn/xp4kv+D4WkGV1WQFgDsgZuVoWazVgPgwvN8PaI1l0mlymS+5hIMKmSBaJg=='
- });
- random
- .generateSignedIntegers({ min: 1, max: players[players.length - 1].status.tickets[0][1], n: 1 })
- .then(function(result) {
- var winner_index = 0;
- var win_ticket = result.random.data[0];
- players.forEach(function(player, index) {
- if (win_ticket >= player.status.tickets[0][0] && win_ticket <= player.status.tickets[0][1])
- {
- winner_index = index;
- }
- });
- var jackpot = players.reduce(function(p, c) {
- return p + parseInt(c.status.bet);
- }, 0);
- var gid = round_id;
- // add row to db
- var sql = "INSERT INTO matches (type_id, sum, win_user_id, win_ticket, date) VALUES ?";
- var date = new Date().toISOString().slice(0, 19).replace('T', ' ');
- var values = [
- ['1', jackpot, players[winner_index].id, win_ticket, date],
- ];
- con.query(sql, [values], function (err,result){
- gid = result.insertId;
- });
- global.endGames = gid;
- gid = gid + 1;
- var winner = ['winner', {
- "random": {
- "num": win_ticket,
- "random": result.random,
- "signature": result.signature
- },
- "round": {
- "id": round_id,
- "gid": gid
- },
- "player": {
- "n": winner_index,
- "id": players[winner_index].id,
- "avatar": players[winner_index].avatar,
- "name": players[winner_index].name,
- "money": players[winner_index].status.bet,
- "jackpot": jackpot,
- }
- }];
- setTimeout(function() {
- con.query("UPDATE users SET balance = balance + ? where id = ?", [jackpot, players[winner_index].id], function (){});
- clients.forEach(function(client) {
- client.send(JSON.stringify(winner));
- });
- players = []
- game_started = false
- timer_started = false
- sendStatToClients()
- }, (_time-5)*1000);
- });
- },(_time-5)*1000);
- }
- });
- default:
- ////console.log ('unknown event:', msg.event)
- break;
- }
- });
- ws.on('close', function() {
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement