forked from mikebryant/ac-nh-turnip-prices
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCode.gs
163 lines (140 loc) · 4.67 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* A Google App Script to manage Animal Crossing New Horizon's Stalk Market predictions
*
* @name google-sheets-stalk-market-calculator
* @version 1.1.0
*
* Derived from drfuzzyness's Google Sheets code
* <https://github.com/drfuzzyness/google-sheets-stalk-market-calculator>
* <file: google-sheets-stalk-market-calculator/Code.gs>
*
* Engine in Engine.gs lifted wholesale from Mike Bryant's excellent webapp translation of Treeki's reverse engineering of the Animal Crossing source code
* <https://mikebryant/ac-nh-turnip-prices>
* <file: ac-nh-turnip-prices/js/predictions.js>
*/
/**
*
* @param {Array<object>} predictions_array
* @param {Sheet} sheet The sheet of the spreadsheet we're targeting
* @param {number} startRow
*/
function writeForecastToSheet(predictions_array, sheet, startRow)
{
// Create 2D array
let table_grid = [];
for(const prediction of predictions_array)
{
if(prediction !== undefined)
{
let row = [];
row.push( PATTERN_HUMAN[ prediction.pattern_number ] );
row.push(prediction.probability);
for(let i = 2; i < prediction.prices.length; i++)
{
const price = prediction.prices[i];
row.push(price.min);
row.push(price.max);
}
table_grid.push(row);
}
}
// Write 2D array if there are entries to write
if(table_grid.length > 0 && table_grid[0].length > 0)
{
const numRows = table_grid.length;
sheet.getRange(startRow, 1, numRows, NUM_OF_COLUMNS).setValues(table_grid);
if(MAX_NUM_OF_ENTRIES - numRows > 0) {
sheet.hideRows(START_ROW_OF_RESULTS_TABLE + numRows, MAX_NUM_OF_ENTRIES - numRows);
}
}
}
function calculateOutput( data, first_buy, last_pattern )
{
let predictor = new Predictor( data, first_buy, last_pattern );
let possibilities = predictor.analyze_possibilities();
return possibilities;
}
function updateSheet(sheet)
{
try
{
clearProbabilities(sheet);
// Get first buy
const first_buy = ( sheet.getRange( USER_FIRST_BUY ).getValue() == 'Yes' ) ? true: false;
// Get the user's last pattern
const last_pattern = PATTERN[ sheet.getRange( USER_PREVIOUS_PATTERN ).getValue().toString().toUpperCase().split( ' ' ).join( '_' ) ];
// Get the user's buy price
const buy_price = sheet.getRange( USER_BUY_PRICE ).getValue();
// Get the user's sell prices
const sell_prices = sheet.getRange( USER_SELL_PRICE_RANGE ).getValues();
const sell_prices_sanitized = sell_prices[0].map( function ( el ) { return ( el != '' ) ? el : NaN } );
const sell_prices_captured = sell_prices[0].filter( function ( el ) { return el != ''; } );
// Only continue if there are sell prices
if( sell_prices_captured.length > 0 )
{
const prices = [ buy_price, buy_price, ...sell_prices_sanitized ];
forecast = calculateOutput( prices, first_buy, last_pattern );
if( forecast[0].weekMax == '-Infinity' )
{
throw new Error("Your prices do not match any known pattern.");
}
else
{
writeForecastToSheet( forecast, sheet, START_ROW_OF_RESULTS_TABLE );
}
}
}
catch (error)
{
writeError(error, sheet);
return;
}
}
function onOpen( open )
{
const sheet = open.range.getSheet();
const sheetName = sheet.getName();
for( let i = 0; i < WHITELISTED_SHEET_PREFIX.length; i++ )
{
if( sheetName.startsWith( WHITELISTED_SHEET_PREFIX[i] ) )
{
updateSheet( sheet );
}
}
}
function onEdit( edit )
{
const sheet = edit.range.getSheet();
const sheetName = sheet.getName();
for( let i = 0; i < WHITELISTED_SHEET_PREFIX.length; i++ )
{
if( sheetName.startsWith( WHITELISTED_SHEET_PREFIX[i] ) )
{
updateSheet( sheet );
}
}
}
function clearProbabilities( sheet )
{
// Clear previous error
sheet.getRange( USER_ERROR_RANGE )
.setBackground( USER_ERROR_DEFAULT_STYLE[ 'bgColor' ] )
.setFontColor( USER_ERROR_DEFAULT_STYLE[ 'color' ] )
.setFontWeight( USER_ERROR_DEFAULT_STYLE[ 'fontWeight' ] )
.setHorizontalAlignment( USER_ERROR_DEFAULT_STYLE[ 'textAlign' ] )
.setValue( USER_ERROR_DEFAULT_TEXT );
// Clear previous contents
sheet.showRows( START_ROW_OF_RESULTS_TABLE, MAX_NUM_OF_ENTRIES );
sheet.getRange( START_ROW_OF_RESULTS_TABLE, 1, MAX_NUM_OF_ENTRIES, NUM_OF_COLUMNS )
.clear( {contentsOnly: true} );
}
function writeError( errorDescription, sheet )
{
clearProbabilities( sheet );
sheet.getRange( USER_ERROR_RANGE )
.setBackground( USER_ERROR_ACTIVE_STYLE[ 'bgColor' ] )
.setFontColor( USER_ERROR_ACTIVE_STYLE[ 'color' ] )
.setFontWeight( USER_ERROR_ACTIVE_STYLE[ 'fontWeight' ] )
.setHorizontalAlignment( USER_ERROR_ACTIVE_STYLE[ 'textAlign' ] )
.setValue( errorDescription );
}