Skip to content

Instantly share code, notes, and snippets.

@shaunidiot
Created November 8, 2017 03:13
Show Gist options
  • Save shaunidiot/96410e35ee694c11ec74fe7fd47933a3 to your computer and use it in GitHub Desktop.
Save shaunidiot/96410e35ee694c11ec74fe7fd47933a3 to your computer and use it in GitHub Desktop.
let Crypto = require('crypto');
let Random = require('random-js');
Random = new Random(Random.engines.browserCrypto);
let Functions = require('../config/functions.js');
module.exports = class MinesManager {
constructor(SocketID, BombCount, DepositAmount, Rank = 0) {
this._SocketID = SocketID;
this._BombCount = BombCount;
this._DepositAmount = DepositAmount;
this._MinDeposit = 30;
this._MaxDeposit = 100000; // MinesManager.Limits()[Rank].MaxBet
this._ExpiryTime = 1.8e6;
this._Expiry = Date.now() + this._ExpiryTime;
this._Secret = Crypto.randomBytes(12).toString('hex');
this._Hash = Crypto.randomBytes(12).toString('hex');
this._Payouts = {
1: [0.028, 0.032, 0.038, 0.041, 0.044, 0.048, 0.054, 0.062, 0.074, 0.086, 0.096, 0.109, 0.133, 0.155, 0.192, 0.233, 0.293, 0.376, 0.502, 0.704, 1.055, 1.745, 3.466, 10.28],
3: [0.111, 0.134, 0.159, 0.198, 0.238, 0.296, 0.37, 0.474, 0.606, 0.785, 1.042, 1.421, 1.968, 2.818, 4.194, 6.534, 10.762, 19.117, 37.672, 86.413, 125, 200],
5: [0.216, 0.287, 0.38, 0.51, 0.701, 0.973, 1.381, 2.006, 2.981, 4.554, 7.204, 11.849, 20.442, 37.399, 73.556, 158.898, 194.647, 220, 250, 300],
24: [20]
}
this._GameEnums = Object.freeze({
GameEnd: {
GAME_TIME_OUT: 0,
BOMB_FIND: 1,
NO_TILE_LEFT: 2,
CASHED_OUT: 12
},
GameError: {
INVALID_TILE: 6,
TILE_CLICKED: 7,
MIN_DEPOSIT: 8,
MAX_DEPOSIT: 9,
BOMB_COUNT: 10,
GAME_COMPLETE: 11
},
GameStatus: {
PENDING: 3, // Waiting for click
CLICKED: 4, // User has clicked
COMPLETE: 5 // Game has ended
}
});
this._Response = this._GameResponse(this._GameEnums.GameStatus.PENDING, {});
this._GameStatus = this._GameEnums.GameStatus.PENDING;
if(isNaN(parseInt(DepositAmount)) || DepositAmount < this._MinDeposit)
return this._GameResponse(this._GameEnums.GameError.MIN_DEPOSIT, {MinDeposit: this._MinDeposit});
if(DepositAmount > this._MaxDeposit)
return this._GameResponse(this._GameEnums.GameError.MAX_DEPOSIT, {MaxDeposit: this._MaxDeposit});
if(BombCount != 1 && BombCount != 3 && BombCount != 5 && BombCount != 24)
return this._GameResponse(this._GameEnums.GameError.BOMB_COUNT, {Bombs: [1, 3, 5, 24]});
this._VisualGrid = [
['□', '□', '□', '□', '□'],
['□', '□', '□', '□', '□'],
['□', '□', '□', '□', '□'],
['□', '□', '□', '□', '□'],
['□', '□', '□', '□', '□']
];
if(this._GameStatus == this._GameEnums.GameStatus.PENDING)
return this._StartGame();
}
_StartGame() {
this._Payout = this._DepositAmount;
this._ClientPayouts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
this._NextPayout = Math.floor(this._Payouts[this._BombCount][0] * this._DepositAmount);
this._BombPositions = [];
this._SelectedTiles = [];
for(let i = 0; i < this._BombCount; i++) this._BombPositions.push(this._GenerateUniqueNumber(1, 25, this._BombPositions));
this._Hash = this._Hash + "-" + this._BombPositions.join("-");
this._GameHash = Crypto.createHmac('sha256', this._Secret).update(this._Hash).digest('hex');
for(let i = 0; i < this._BombCount; i++) {
let BombPosition = this._BombPositions[i];
let Row = Math.ceil(BombPosition / 5) - 1;
let Column = BombPosition % 5 > 0 ? BombPosition % 4 : 4;
this._VisualGrid[Row][Column] = '■';
}
return this._GameResponse(this._GameEnums.GameStatus.PENDING, this);
}
_Cashout() {
if(this._GameStatus != this._GameEnums.GameStatus.PENDING && this._GameStatus != this._GameEnums.GameStatus.CLICKED)
return this._GameResponse(this._GameEnums.GameError.GAME_COMPLETE, {});
this._GameStatus = this._GameEnums.GameStatus.COMPLETE;
return this._GameResponse(this._GameEnums.GameEnd.CASHED_OUT, {Payout: this._Payout});
}
_TileClick(Tile) {
if(this._GameStatus == this._GameEnums.GameStatus.COMPLETE)
return this._GameResponse(this._GameEnums.GameStatus.COMPLETE, {});
if(isNaN(parseInt(Tile)) || Tile < 1 || Tile > 25)
return this._GameResponse(this._GameEnums.GameError.INVALID_TILE, {});
if(this._SelectedTiles.includes(Tile))
return this._GameResponse(this._GameEnums.GameError.TILE_CLICKED, {});
Tile = parseInt(Tile);
this._SelectedTiles.push(Tile);
if(this._BombPositions.includes(Tile)) {
this._GameStatus = this._GameEnums.GameStatus.COMPLETE;
return this._GameResponse(this._GameEnums.GameEnd.BOMB_FIND, {});
}
this._ClientPayouts[Tile-1] = this._NextPayout;
let NextTile = this._SelectedTiles.length == this._Payouts[this._BombCount].length ? -1 : this._SelectedTiles.length;
this._Payout += Math.floor(this._Payouts[this._BombCount][this._SelectedTiles.length-1] * this._DepositAmount);
this._NextPayout = NextTile > -1 ? Math.floor(this._Payouts[this._BombCount][NextTile] * this._DepositAmount) : -1;
if(this._Payouts[this._BombCount].length == this._SelectedTiles.length) {
this._GameStatus = this._GameEnums.GameStatus.COMPLETE;
return this._GameResponse(this._GameEnums.GameEnd.NO_TILE_LEFT, {});
}
return this._GameResponse(this._GameEnums.GameStatus.CLICKED, {Payout: this._Payout, NextPayout: this._NextPayout});
}
_GenerateVisualGrid() {
for(let i = 0; i < 5; i++) {
console.log(this._VisualGrid[i].join(' '));
}
}
_GenerateUniqueNumber(Minimum, Maximum, Used) {
let Number = this._GenerateNumber(Minimum, Maximum);
if(Used.includes(Number)) return this._GenerateUniqueNumber(Minimum, Maximum, Used);
return Number;
}
_GenerateNumber(Minimum, Maximum) {
return Random.integer(Minimum, Maximum);
}
_GameResponse(Status, Data) {
return ({
Status: Status,
Data: Data
});
}
_GameBrief() {
return this._GameResponse(this._GameStatus, {
GameHash: this._GameHash,
BombCount: this._BombCount,
Expiry: this._Expiry,
DepositAmount: this._DepositAmount,
NextPayout: this._NextPayout,
SelectedTiles: this._SelectedTiles,
Payout: this._Payout,
ClientPayouts: this._ClientPayouts
});
}
_GameAllData() {
return {
SocketID: this._SocketID,
BombCount: this._BombCount,
DepositAmount: this._DepositAmount,
MinDeposit: this._MinDeposit,
MaxDeposit: this._MaxDeposit,
ExpiryTime: this._ExpiryTime,
Expiry: this._Expiry,
Secret: this._Secret,
Hash: this._Hash,
GameHash: this._GameHash,
BombPositions: this._BombPositions,
SelectedTiles: this._SelectedTiles,
Payout: this._Payout,
NextPayout: this._NextPayout
}
}
static Limits() {
return [
{
MaxGames: 3,
MaxBet: 100000
},
{
MaxGames: 9,
MaxBet: 150000
},
{
MaxGames: 15,
MaxBet: 200000
}
]
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment