Last active
June 30, 2021 01:09
-
-
Save zundra/53fee1235d62de4bec0c6797525dd767 to your computer and use it in GitHub Desktop.
Avg_Std_Med.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = {}; | |
var stateVars = { | |
roundResults: [], | |
historyLoaded: false, | |
}; | |
if (engine.gameState === "GAME_STARTING") { | |
log("starting script..."); | |
} | |
engine.on("GAME_STARTING", onGameStarted); | |
engine.on("GAME_ENDED", onGameEnded); | |
function onGameStarted() { | |
if (!stateVars.historyLoaded) | |
{ | |
log("Loading history..."); | |
loadHistory(); | |
stateVars.historyLoaded = true; | |
} | |
} | |
function onGameEnded() | |
{ | |
if (!stateVars.historyLoaded) | |
{ | |
return; | |
} | |
var lastGame = engine.history.first(); | |
stateVars.roundResults.push(lastGame.bust); | |
if (stateVars.roundResults.length > 50) { | |
var throwAway = stateVars.roundResults.shift(); | |
log("Throwing away last value ", throwAway); | |
} | |
var avg = calculateAverage(); | |
var std = calculateStandardDeviation(); | |
var med = calculateMedian(); | |
log("AVERAGE = ", avg, "OVER", stateVars.roundResults.length, "GAMES"); | |
log("STANDARD DEV = " + std); | |
log("DIFF = " + (avg - std)); | |
log("MEDIAN = " + med); | |
} | |
function loadHistory() { | |
var history = engine.history.toArray().reverse(); | |
for (var i = 0; i < history.length; i++) { | |
var game = history[i]; | |
stateVars.roundResults.push(game.bust); | |
} | |
} | |
// Helper functions | |
function calculateStandardDeviation() { | |
var array = stateVars.roundResults; | |
const n = array.length | |
const mean = array.reduce((a, b) => a + b) / n | |
return Math.sqrt(array.map(x => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n) | |
} | |
function calculateMedian() { | |
var array = stateVars.roundResults.slice(); | |
if (array.length === 0) return 0; | |
array.sort(function(a, b) { | |
return a - b; | |
}); | |
var half = Math.floor(array.length / 2); | |
if (array.length % 2) | |
return array[half]; | |
return (array[half - 1] + array[half]) / 2.0; | |
} | |
function calculateAverage() { | |
var runningTotal = 0; | |
if (stateVars.roundResults.length == 0) { | |
return 0; | |
} | |
for (var i = 0; i < stateVars.roundResults.length; i++) { | |
runningTotal += stateVars.roundResults[i]; | |
} | |
return runningTotal / stateVars.roundResults.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment