death-games
Version:
Un módulo creado principalmente para juegos en bots de discord.js
76 lines (47 loc) • 1.88 kB
JavaScript
class Roulette{
constructor(options){
let slots = 6
let elegido = 1
let jugadores;
if(!options || !options.jugadores) throw SyntaxError("Debes proporcionar la opción jugadores!")
if(!Array.isArray(options.jugadores)) throw TypeError("La opción jugadores debe ser un array")
if(options.jugadores.length < 1) throw SyntaxError("Debes proporcionar mínimo 1 ID en la opcion jugadores")
jugadores = options.jugadores
if(options.slots){
if(isNaN(options.slots)) throw TypeError("Debes proporcionar un número de slots! El default son 6!")
slots = options.slots
}
let random = Math.floor(Math.random() * slots)+1
this.elegir = function(num){
if(isNaN(num)) throw TypeError("Debes proporcionar un número en la función elegir!")
let numero = parseInt(Math.abs(+(num)))
if(!Number.isSafeInteger(numero)) throw RangeError("Debes proporcionar un número más pequeño!")
if(numero == 0) throw SyntaxError("No puedes girar 0 veces!!")
elegido = elegido+numero
this.game.posicion = elegido
if(elegido > slots){
let multiplicador = parseInt(elegido / slots)
let posicion = multiplicador * slots
elegido = elegido - posicion
if(elegido == 0) elegido++
this.game.posicion = elegido
}
this.game.turno = turno()
if(elegido == random) return true
return false
}
function turno(){
let turno = jugadores.shift()
jugadores.push(turno)
return turno
}
this.game = {
jugadores: jugadores,
turno: turno(),
slots: slots,
posicion: elegido,
bala: random
}
}
}
module.exports = Roulette