-
Notifications
You must be signed in to change notification settings - Fork 6
/
scoreboard.ts
165 lines (146 loc) · 4.42 KB
/
scoreboard.ts
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
164
165
import { Data, toText } from "https://deno.land/x/[email protected]/mod.ts";
import { lucidTestnet } from "./common/offchain/setup_lucid.ts";
import {
SOLUTION_RECORD_ADDRESS,
SolutionRecordDatum,
} from "./common/offchain/test_utils.ts";
import {
bold,
brightYellow,
green,
} from "https://deno.land/[email protected]/fmt/colors.ts";
const RECORD_LIMIT = 11;
const TEAM_ADDRESSES = [
"addr_test1vr4wtjyyzuzhsxzfpzdmqj65pp3r7644wfl0u8lq6eh4jnqay43fc",
"addr_test1vzmegp355jwkyudds3rhl5hv82zuze2hphy5peghfqcwq3qd5daxy",
"addr_test1vznszvym4gy2ch8h5uk9hkt8ytkfly4zlk84qjsulpfjryg87qlg6",
];
type ParsedDatumType = {
problem_id: number;
timestamp: bigint;
address: string;
};
const taskNames = [
"00_hello_world",
"01_sell_nft",
"02_vesting",
"03_multisig_treasury",
"04_tipjar",
"05_purchase_offer",
"06_tipjar_v2",
"07_multisig_treasury_v2",
"08_lending",
"09_multisig_treasury_v3",
"10_king_of_cardano",
];
function showSolvedTasks(parsedDatums: ParsedDatumType[], ownAddress: string) {
const solvedTasks = parsedDatums
.filter((datum) => datum.address == ownAddress)
.sort((a, b) => a.problem_id - b.problem_id);
console.log(bold("Your solved tasks:"));
solvedTasks.forEach((task) => {
const solutionDate = new Date(Number(task.timestamp));
console.log(`\t${task.problem_id}\t at ${solutionDate.toLocaleString()}`);
});
}
function showTaskRecords(
parsedDatums: ParsedDatumType[],
taskId: number,
ownAddress: string,
showAll: boolean,
) {
const records = parsedDatums.filter((datum) => datum.problem_id == taskId);
console.log(
bold(
`Task ${green(taskNames[taskId])} was solved by ${
green(records.length.toString())
} unique addresses`,
),
);
const showRecords = records.filter(
(record, i) => {
const hideRow = !showAll && records.length > RECORD_LIMIT &&
i > RECORD_LIMIT / 2 && i < records.length - RECORD_LIMIT / 2 &&
record.address != ownAddress;
return !hideRow;
},
);
showRecords.forEach((record) => {
const solutionDate = new Date(Number(record.timestamp));
const row = `\t${solutionDate.toLocaleString()}\t${record.address}`;
const displayRow = record.address == ownAddress
? brightYellow(row + `\t (your solution)`)
: row;
console.log(displayRow);
});
const hidden = records.length - showRecords.length;
if (hidden > 0) console.log(`\t(... ${hidden} other records ...)`);
}
if (lucidTestnet == undefined) {
console.log(
"Testnet is not setup correctly, refer to README before proceeding",
);
Deno.exit(1);
}
const ownAddress = await lucidTestnet.wallet.address();
const allSolutionRecords = await lucidTestnet.utxosAt(SOLUTION_RECORD_ADDRESS);
const parsedDatums = allSolutionRecords.map((utxo) =>
Data.from(utxo.datum!, SolutionRecordDatum)
);
const datumsWithParsedAddresses: ParsedDatumType[] = parsedDatums
.map(
(datum) => ({
problem_id: Number(datum.problem_id),
timestamp: datum.timestamp,
address: toText(datum.solver_address),
}),
)
.sort((a, b) => a.timestamp < b.timestamp ? -1 : 1)
.filter((datum) =>
!TEAM_ADDRESSES.includes(datum.address) || datum.address == ownAddress
)
// Filter repeated solutions
.filter((value, index, self) =>
index ==
self.findIndex((value2) =>
value2.address == value.address && value2.problem_id == value.problem_id
)
);
console.log(bold(`Your address: ${ownAddress}`));
console.log("To show the scoreboard for all tasks, input a");
console.log(
"To view a complete scoreboard for a single task, input its task number (0..10)",
);
console.log("To show all tasks that you have solved, input s");
console.log("To exit, input e");
while (true) {
const choice = prompt("Please enter your choice:");
if (choice == null) {
console.log("Invalid choice");
} else if (choice == "a") {
for (let taskId = 0; taskId <= 10; taskId++) {
showTaskRecords(
datumsWithParsedAddresses,
taskId,
ownAddress,
false,
);
}
} else if (choice == "e") {
Deno.exit(0);
} else if (choice == "s") {
showSolvedTasks(datumsWithParsedAddresses, ownAddress);
} else {
const choiceNum = parseInt(choice, 10);
if (choiceNum < 0 || choiceNum > 10 || isNaN(choiceNum)) {
console.log("Invalid choice!");
continue;
}
showTaskRecords(
datumsWithParsedAddresses,
choiceNum,
ownAddress,
true,
);
}
}