· 9 years ago · Oct 01, 2016, 12:18 AM
1// account login information
2var config = require('./config.js');
3var requestify, io;
4
5module.exports.startGame = function(requestifyCore, ioCore) {
6 requestify = requestifyCore;
7 io = ioCore;
8 setInterval(function() {
9 checkJackpot();
10 }, 5000);
11};
12
13var started = false;
14
15function checkJackpot() {
16 if (started == false) {
17 playJackpot();
18 }
19}
20
21function playJackpot() {
22 requestify.post('http://' + config.domain + '/bot-api/jackpot/getGame', {
23 secretKey: config.secretKey
24 }).then(function (response) {
25 var result = JSON.parse(response.body);
26
27 if (result.success == true) {
28 started = true;
29 var timer = 90;
30
31 var interval = setInterval(function() {
32 logMsg(" Game finish in " + timer + " seconds");
33 io.emit('jackpot-timer', {
34 jackpot_id: result.jackpot_id,
35 timer: timer
36 });
37 timer--;
38
39 if (timer == 0) {
40 clearInterval(interval);
41 finishJackpot();
42 }
43 }, 1000);
44 }
45 }, function(response) {
46 logMsg("[Error] Cannot play Jackpot");
47 });
48}
49
50function finishJackpot() {
51 requestify.post('http://' + config.domain + '/bot-api/jackpot/finishGame', {
52 secretKey: config.secretKey
53 }).then(function (response) {
54 var result = JSON.parse(response.body);
55
56 logMsg(" Game finished, starting newGame timer");
57 newGameTimer();
58 io.emit('jackpot-finished', result);
59 started = false;
60 }, function(response) {
61 logMsg("[Error] Cannot finish Jackpot");
62 finishJackpot();
63 });
64}
65
66function newGameTimer() {
67 var timer = 15;
68
69 var interval = setInterval(function() {
70 logMsg(" Game starts in " + timer + " seconds");
71 io.emit('newgame-timer', {
72 timer: timer
73 });
74 timer--;
75
76 if (timer == 0) {
77 clearInterval(interval);
78 playJackpot();
79 }
80 }, 1000);
81}
82
83function logMsg(log){
84 console.log("[JackpotGame]" + log);
85}