Avatar

@todepond

1 like9 public vals
Joined November 7, 2022
ribbit ribbit welcome to the pond

Example usage:

Create valimport { val } from "https://esm.town/v/todepond/val" await val("todepond.name") // "Luke" await val("todepond.add", 3, 2) // 5
Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
export const val = async (address, ...args) => {
const [handle, name] = address.split(".");
if (handle === undefined || name === undefined) {
throw new Error("Invalid address format. Expected 'handle.name'");
}
const body = args.length > 0 ? JSON.stringify({ args }) : null;
const response = await fetch(
`https://api.val.town/v1/run/${handle}.${name}`,
{
method: "POST",
body,
},
);
const text = await response.text();
try {
return JSON.parse(text);
} catch (e) {
return text;
}
};
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
import { blob } from "https://esm.town/v/std/blob";
import { decrypt } from "https://esm.town/v/todepond/decrypt";
import { Supporter } from "https://esm.town/v/todepond/Supporter";
import { getSupporters } from "https://esm.town/v/todepond/getSupporters";
import process from "node:process";
export async function getVotes() {
const supporters = await getSupporters(process.env.FAME_ADMIN_PASSWORD);
const votes = {
newFractal: 0,
snakesInSnakesInSnakes: 0,
thisIsATode: 0,
};
for (const supporter of supporters) {
if (supporter.votes === undefined) {
continue;
}
if (supporter.votes.newFractal) {
votes.newFractal += 1;
}
if (supporter.votes.snakesInSnakesInSnakes) {
votes.snakesInSnakesInSnakes += 1;
}
if (supporter.votes.thisIsATode) {
votes.thisIsATode += 1;
}
}
return votes;
}
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
import { blob } from "https://esm.town/v/std/blob";
import { email } from "https://esm.town/v/std/email";
import { getSupporters } from "https://esm.town/v/todepond/getSupporters";
import { setSupporters } from "https://esm.town/v/todepond/setSupporters";
import { loginSupporter } from "https://esm.town/v/todepond/loginSupporter";
import process from "node:process";
export async function setVotes(secret, votes) {
const id = await loginSupporter(secret);
if (id === null || typeof id !== "number") {
return { success: false, error: "Invalid secret code" };
}
const supporters = await getSupporters(process.env.FAME_ADMIN_PASSWORD);
const supporterIndex = supporters.findIndex((s) => s.id === id);
if (supporterIndex === -1) {
email({
to: "l2wilson94@gmail.com",
from: "todepond.com@valtown.email",
subject: "Vote error",
text: `Couldn't find supporter with id ${id}`,
});
return { success: false, error: "Invalid secret code" };
}
const supporter = supporters[supporterIndex];
const newSupporter = {
...supporter,
votes,
};
const newSupporters = [...supporters];
newSupporters[supporterIndex] = newSupporter;
const result = await setSupporters(
newSupporters,
supporters,
process.env.FAME_ADMIN_PASSWORD
);
if (result.success) {
const votedFor = [Object.keys(votes).filter((key) => votes[key] === true)];
email({
to: "l2wilson94@gmail.com",
from: "todepond.com@valtown.email",
subject: "New votes",
text: `Supporter ${
supporter.email
} has voted for the following: ${votedFor.join(", ")}`,
});
return { success: true };
} else {
return { success: false, error: "Failed saving votes. Please try again." };
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
import { blob } from "https://esm.town/v/std/blob";
import { getSupporters } from "https://esm.town/v/todepond/getSupporters";
import process from "node:process";
export async function loginAndGetSupporter(secret) {
const supporters = await getSupporters(process.env.FAME_ADMIN_PASSWORD);
const supporter = supporters.find((s) => s.secret === secret);
if (!supporter) {
return null;
}
return supporter;
}
1
export const name = "Luke";
1
2
3
export function add(a, b) {
return a + b;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { blob } from "https://esm.town/v/std/blob";
import { Hero } from "https://esm.town/v/todepond/Hero";
import process from "node:process";
export async function setHeroes(heroes: Hero[], originalHeroes: Hero[], password: string) {
if (password !== process.env.FAME_ADMIN_PASSWORD) {
return { success: false, error: "Wrong password" };
}
const actualOriginalHeroes = await blob.getJSON("heroes");
if (JSON.stringify(actualOriginalHeroes) !== JSON.stringify(originalHeroes)) {
return { success: false, error: "Conflict" };
}
await blob.setJSON("heroes", heroes);
return { success: true };
}
1
2
3
4
5
import { blob } from "https://esm.town/v/std/blob";
export async function getHeroes() {
return await blob.getJSON("heroes");
}
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
export type Hero = {
name: string;
tier: Tier;
flavour: Flavour;
supporter: number;
};
export type Tier =
| "froggy"
| "flappy"
| "beepy";
export type Flavour =
| "fire"
| "water"
| "air"
| "sand"
| "wood"
| "flower"
| "pink sand"
| "metal"
| "poison"
| "leaf"
| "void"
| "cloud";
Next