-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
135 lines (118 loc) · 3.68 KB
/
Copy pathmain.js
File metadata and controls
135 lines (118 loc) · 3.68 KB
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
const SCRIPT_URL = 'https://script.google.com/macros/s/AKfycbxGVeugBr0qE0KiY-CiOm7ipvMvxCRZthib6S5QZth6vcTfmkuyYaH6qx3zU2Yqxu1_bA/exec';
const CORRECT = {
1: 3_991,
2: 577,
3: 234_000_000,
4: 5_149_900.8,
5: 500_000,
6: 11_388,
7: 52_721,
8: 12,
9: 1_696_346,
10: 145_000_000,
11: 40,
12: 123,
13: 26_500_000_000_000_000_000
}
const TEAM_NAMES = {};
// Read cell using plain text output
async function readCell(row, col) {
try {
const res = await fetch(SCRIPT_URL, {
method: 'POST',
headers: { 'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({ action: 'read', row, col })
});
const text = await res.text();
return JSON.parse(text);
} catch (err) {
console.error("Read failed:", err);
return null;
}
}
// Write cell using plain text output
async function writeCell(row, col, value) {
try {
const res = await fetch(SCRIPT_URL, {
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
body: JSON.stringify({ action: 'write', row, col, value })
});
const text = await res.text();
const success = JSON.parse(text).success;
return success;
} catch (err) {
console.error("Write failed:", err);
return false;
}
}
async function updateTeamNames() {
for (let row = 4; row < 30; row++) {
readCell(row, 1).then(response => {
let teamName = response.value;
if (teamName) {
TEAM_NAMES[teamName] = row;
}
});
}
}
function decrypt(value) {
return value;
}
updateTeamNames();
document.getElementById('submitBtn').addEventListener('click', function () {
const problem = document.getElementById('problem').value.trim();
const team = document.getElementById('team').value.trim();
const low = document.getElementById('low').value.trim();
const high = document.getElementById('high').value.trim();
const statusEl = document.getElementById('status');
statusEl.textContent = '';
statusEl.style.color = 'red';
if (!problem || !team || !low || !high) {
statusEl.textContent = 'All the input boxes must be filled in.';
return;
}
const problemNum = parseInt(problem, 10);
const lowNum = parseFloat(low);
const highNum = parseFloat(high);
if (isNaN(problemNum) || isNaN(lowNum) || isNaN(highNum)) {
statusEl.textContent = 'Problem, Low, and High must be numbers.';
return;
}
if (problemNum < 1 || problemNum > 13) {
statusEl.textContent = 'Problem must be between 1 and 13.';
return;
}
if (lowNum > highNum) {
statusEl.textContent = 'Low must be less than or equal to High.';
return;
}
if (!TEAM_NAMES.hasOwnProperty(team)) {
statusEl.textContent = `Team ${team} does not exist.`;
return;
}
let row = TEAM_NAMES[team];
let col = problemNum + 1;
let correctAnswer = decrypt(CORRECT[problem]);
readCell(row, col).then(response => {
let currentValue = String(response.value);
let newValue = null;
if (lowNum <= correctAnswer && correctAnswer <= highNum) {
// Easy to make it best guess instead
newValue = Math.floor(highNum/lowNum);
} else {
// This would be nicer if it was initially blank instead of hyphens
// Currently overrides correct answers
newValue = !(currentValue.includes('X')) ? 'X' : currentValue + 'X';
}
writeCell(row, col, newValue).then(success => {
if (success) {
statusEl.style.color = 'green';
statusEl.textContent = `Successfully updated Row ${row} Column ${col} with value of ${newValue}!`;
} else {
statusEl.style.color = 'yellow';
statusEl.textContent = 'Error occurred in updating scores. This isn\'t supposed to happen idk why it did.';
}
});
})
});