-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathawards.ts
140 lines (129 loc) · 3.75 KB
/
awards.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
import logger from "./logger";
logger.info("Awards script started");
import * as db from "./db";
import { date, now } from "./timestamp";
import { Award } from "./types";
export const awards = async () => {
switch (process.argv[2]) {
case "monthly":
await monthly();
break;
case "weekly":
await weekly();
break;
case "early_adopters":
await earlyAdopters();
break;
default:
throw new Error("unknown award type");
}
process.exit(0);
};
const monthly = async () => {
const client = await db.retry();
logger.info("connected to postgres.");
const leaderboard = await db.leaderBoardTop(30);
const timestamp = date(now());
try {
await client.query("BEGIN");
await Promise.all(
leaderboard.map(async entry => {
const user = await db.getUser(entry.id);
const text = `UPDATE users SET awards = $1 WHERE id = $2`;
const award: Award = {
type: "monthly_rank",
position: entry.rank,
timestamp,
};
logger.info(`User ${user.id} got award ${JSON.stringify(award)}`);
return await client.query(text, [
JSON.stringify(user.awards.concat([award])),
entry.id,
]);
})
);
await client.query("UPDATE users SET points = 0");
await client.query("COMMIT");
} catch (e) {
logger.error(e);
await client.query("ROLLBACK");
throw e;
}
};
const weekly = async () => {
const client = await db.retry();
logger.info("connected to postgres.");
await Promise.all(
["Planeta", "Europa"].map(async tag => {
const topScores: { id: string; score: number }[] = (
await db.topScores(tag)
).slice(0, 3);
const topRanks = topScores.map((scored, i) => ({
id: scored.id,
rank: i + 1,
}));
const timestamp = date(now());
try {
await client.query("BEGIN");
await Promise.all(
topRanks.map(async entry => {
const user = await db.getUser(entry.id);
const text = `UPDATE users SET awards = $1 WHERE id = $2`;
const award: Award = {
type: "weekly_rank",
table: tag,
position: entry.rank,
timestamp,
};
logger.info(`User ${user.id} got award ${JSON.stringify(award)}`);
return await client.query(text, [
JSON.stringify(user.awards.concat([award])),
entry.id,
]);
})
);
await client.query("COMMIT");
} catch (e) {
logger.error(e);
await client.query("ROLLBACK");
throw e;
}
})
);
};
const earlyAdopters = async () => {
const client = await db.retry();
logger.info("connected to postgres.");
const users = await client.query("SELECT id, name FROM users");
const timestamp = date(now());
logger.info(`adding early_adopter award to ${users.rows.length} users`);
try {
client.query("BEGIN");
await Promise.all(
users.rows.map(async entry => {
const user = await db.getUser(entry.id);
const text = `UPDATE users SET awards = $1 WHERE id = $2 RETURNING id,name,awards`;
const award: Award = {
type: "early_adopter",
position: 0,
timestamp,
};
if (entry.id == "404") {
logger.debug(text);
logger.debug([user.awards.concat([award]), entry.id]);
}
const result = await client.query(text, [
JSON.stringify(user.awards.concat([award])),
user.id,
]);
logger.debug("result", result.rows);
})
);
await client.query("COMMIT");
logger.info("COMMITted");
} catch (e) {
logger.error(e);
await client.query("ROLLBACK");
throw e;
}
};