-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.gs
117 lines (104 loc) · 4.04 KB
/
Code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const patternDescriptions = { 0: 'Fluctuating', 1: 'Large spike', 2: 'Decreasing', 3: 'Small spike', 4: 'All patterns' }
function findInColumn (array, col, target) {
for (var i = 0; i < array.length; i++) {
if (array[i][col] === target) {
return i
}
}
return -1
}
function roundPrecision (value, precision) {
var multiplier = Math.pow(10, precision || 0)
return Math.round(value * multiplier) / multiplier
}
function analyzePossibilities (inTurnipPrices, inPreviousPatternInt) {
const predictor = new Predictor(inTurnipPrices, false, inPreviousPatternInt) // eslint-disable-line no-undef
return predictor.analyze_possibilities()
}
function predictTurnips (daisyPrice, nookPrices, previousPattern) { // eslint-disable-line no-unused-vars
// The prices array is formatted like:
// [price, price, M-AM, M-PM, T-AM, T-PM, W-AM, W-PM, Th-AM, Th-PM, F-AM, F-PM, S-AM, S-PM, Su-AM, Su-PM]
var turnipPrices = [daisyPrice, daisyPrice, ...nookPrices]
// Parse previous pattern
var previousPatternInt
switch (previousPattern) {
case 'Fluctuating':
previousPatternInt = 0
break
case 'Large spike':
previousPatternInt = 1
break
case 'Decreasing':
previousPatternInt = 2
break
case 'Small spike':
previousPatternInt = 3
break
default:
previousPatternInt = -1
}
// Parse the incoming price array for integers
for (let i = 0; i < turnipPrices.length; i++) {
turnipPrices[i] = parseInt(turnipPrices[i])
}
// NaN-pad length if needed
for (let i = turnipPrices.length; i < 14; i++) {
turnipPrices.push(NaN)
}
// Obtain all possible patterns and probabilities. Obtain the min max values from the 'All patterns' pattern.
var pricePrediction = []
var possiblePatterns = []
for (const poss of analyzePossibilities(turnipPrices, previousPatternInt)) { // eslint-disable-line no-undef
// Obtain min max values from the 'All patterns' pattern.
if (poss.pattern_number === 4) {
for (const day of poss.prices.slice(2)) {
if (day.min > day.max) {
pricePrediction.push('')
} else if (day.min !== day.max) {
pricePrediction.push(day.min + ' to ' + day.max)
} else {
pricePrediction.push(day.min + '')
}
}
// Collect possible patterns and aggregate the probabilities.
} else {
if (findInColumn(possiblePatterns, 0, poss.pattern_number) === -1) {
possiblePatterns.push([poss.pattern_number, poss.category_total_probability])
}
}
}
// Generate the 'Possible patterns' strings
pricePrediction.push('Possible patterns')
// Parse the possiblePatterns array.
var possiblePatternsString = ''
if (possiblePatterns.length === 0) {
possiblePatternsString += 'Unknown'
} else {
for (const patt of possiblePatterns) {
// Delimiter if not the first entry.
if (possiblePatternsString.length !== 0) {
possiblePatternsString += ' | '
}
// Omit probabilities if they are not available
if (isNaN(patt[1])) {
possiblePatternsString += patternDescriptions[patt[0]]
} else {
// Convert probabilities to percentages rounded to one decimal place
possiblePatternsString += patternDescriptions[patt[0]] + ' (' + roundPrecision(patt[1] * 100, 1) + '%)'
}
}
}
possiblePatternsString = possiblePatternsString.replace('(100%) ', '(≥ 99.5%) ')
possiblePatternsString = possiblePatternsString.replace('(0%)', '(< 0.05%)')
pricePrediction.push(possiblePatternsString)
return pricePrediction
}
function testAnalyzePossibilities () { // eslint-disable-line no-unused-vars
const predictor = new Predictor([103, 103, 91, 138, 161, 504, , 143, , , , , , ], false, 0) // eslint-disable-line no-undef, no-sparse-arrays, array-bracket-spacing
const prediction = predictor.analyze_possibilities()
return prediction
}
function testPredictTurnips (daisyPrice, nookPrices, previousPattern) { // eslint-disable-line no-unused-vars
const prediction = predictTurnips(103, [91, 138, 161], 'Fluctuating')
return prediction
}