javascript 检查由用户模式定义的数组,并在找到时停止
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 检查由用户模式定义的数组,并在找到时停止相关的知识,希望对你有一定的参考价值。
var Patterns_array = [ `RGRGRR`, `RRRRRG`, `GRGRGR` ] //G = Green, R = Red
var PatternToFindTarget = 2
var Target_Analyzer = 3
//*************************** */
var continue_play_after_catch = false
var base_bet = 1
var bet_multiplier = 2
var target = 2
var max_turns = 8
var max_loss = 2000
//*************************** */
var engine = this
class FindPattern {
constructor (Patterns = [], toFind, analyzer) {
this.PatternToFind = toFind
this.skip_bet_amount = 100
this.skip_target_size = 1.01
this.debug = true
this.Patterns = []
this.history = []
this.match = 0
this.rolls = 0
this.running = true
this.target_analyzer = analyzer
for (let i = 0; i < Patterns.length; i++) {
this.Patterns.push(Patterns[ i ])
}
}
Match(check_history, pattern) {
if (check_history.length < pattern.length) {
return
}
let convert_pattern = this.Convert(pattern)
for (let i = 0; i < check_history.length; i++) {
if (check_history[ i ] == convert_pattern[ i ]) {
this.match++
} else {
this.match = 0
}
}
this.UpdateLog()
if (this.match < convert_pattern.length) {
return false
} else {
return true
}
}
Convert(pattern) {
let converted = []
for (let i = 0; i < pattern.length; i++) {
if (pattern[ i ] == "G") {
converted.push(1)
}
if (pattern[ i ] == "R") {
converted.push(0)
}
}
return converted
}
Update(multiplier) {
if (multiplier >= this.PatternToFind && multiplier <= this.PatternToFind * this.target_analyzer) {
this.history.push(1)
} else {
this.history.push(0)
}
if (this.history.length > this.LongestLength) {
this.history.shift()
}
}
set Skip_bet_amount(value) {
this.skip_bet_amount = value
}
set Skip_target_size(value) {
this.skip_target_size = value
}
get LongestLength() {
for (let i = 0; i < this.Patterns.length; i++) {
if (this.Patterns[ i ].length >= this._longest) {
this._longest = this.Patterns[ i ].length
}
}
return this._longest
}
get History() {
return this.history
}
UpdateLog() {
engine.clearLog()
for (let i = 0; i < this.Patterns.length; i++) {
engine.log(`#${i + 1} Pattern ${this.Patterns[ i ]}`)
engine.log(this.Convert(this.Patterns[ i ]))
engine.log(this.history.slice(-this.Patterns[ i ].length))
if (this.debug) engine.log(`Matches ${this.match}/${this.Patterns[ i ].length}`)
engine.log(`---------------------------`)
}
engine.log(`Roll ${this.rolls}`)
}
async Found(pattern = undefined) {
if (pattern != undefined) {
engine.log(`${pattern} is Match!`)
} else {
engine.log(`Match found!`)
}
this.sound_loop(5)
await this.Terminate()
//this.running = false
this.UpdateLog()
}
async Terminate(continues = continue_play_after_catch) {
if (!continues) {
engine.stop()
}
}
async IteratePatterns() {
for (let i = 0; i < this.Patterns.length; i++) {
if (this.Match(this.history.slice(this.history.length - this.Patterns[ i ].length, this.history.length), this.Patterns[ i ]) == true) {
this.Found(this.Patterns[ i ])
}
}
}
async sleep(ms = 2000) {
if (ms != 0) {
return new Promise(resolve => setTimeout(resolve, ms))
}
}
async sound_loop(sound_number, name) {
let sound_track = [ `cl_hihat`, `claves`, `conga1`, `cowbell`, `hi_conga`, `hightom`, `rimshot`, `snare`, `tom1` ]
let url = `https://raw.githubusercontent.com/ruzli/misc/master/`
let format = `.wav`
let full_string = url + name + format
if (name != undefined) {
full_string = url + name + format
} else if (sound_number != undefined) {
full_string = url + sound_track[ sound_number ] + format
} else {
engine.log(`Sound not found.`)
}
const audio = new Audio(full_string)
await audio.play()
return new Promise(resolve => audio.onended = resolve)
}
async StartFind() {
while (this.running) {
this.rolls++
const { multiplier } = await engine.bet(this.skip_bet_amount, this.skip_target_size)
this.Update(multiplier)
this.UpdateLog()
this.IteratePatterns()
}
}
}
async function startMartingale(base_bet = base_bet, bet_multiplier = bet_multiplier, target_size = target, turns = max_turns, max_loss = max_loss) {
let turn = 0
let bet_size = base_bet
let current_loss = 0
//patternStart.running = false
while (true) {
if (turn >= turns || current_loss >= max_loss) {
engine.log(current_loss)
await engine.stop()
}
const { multiplier } = await engine.bet(bet_size * 100, target_size)
if (multiplier >= target_size) {
turn = 0
currnet_loss = current_loss - (bet_size * (target_size - 1))
bet_size = base_bet
} else {
turn++
bet_size *= bet_multiplier
current_loss = current_loss + bet_size
}
}
}
var patternStart = new FindPattern(Patterns_array, PatternToFindTarget, Target_Analyzer)
if (continue_play_after_catch) patternStart.Terminate = async function () { await startMartingale(base_bet, bet_multiplier, target, max_turns, max_loss)}
await patternStart.StartFind()
以上是关于javascript 检查由用户模式定义的数组,并在找到时停止的主要内容,如果未能解决你的问题,请参考以下文章
如何将c#中的List转换为javascript中的数组并检查数组项