forked from AHSFNU/syzoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrating_calculation.ts
52 lines (45 loc) · 1.2 KB
/
rating_calculation.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
import * as TypeORM from "typeorm";
import Model from "./common";
declare var syzoj: any;
import Contest from "./contest";
import RatingHistory from "./rating_history";
@TypeORM.Entity()
export default class RatingCalculation extends Model {
@TypeORM.PrimaryGeneratedColumn()
id: number;
@TypeORM.Index({})
@TypeORM.Column({ nullable: true, type: "integer" })
contest_id: number;
contest?: Contest;
async loadRelationships() {
this.contest = await Contest.findById(this.contest_id);
}
async delete() {
const histories = await RatingHistory.find({
where: {
rating_calculation_id: this.id
}
});
for (const history of histories) {
await history.loadRelationships();
const user = history.user;
await history.destroy();
const ratingItem = (await RatingHistory.findOne({
where: {
user_id: user.id
},
order: {
rating_calculation_id: 'DESC'
}
}));
if (ratingItem) {
user.rating = ratingItem.rating_after;
} else {
user.rating = syzoj.config.default.user.rating;
user.is_rated = false;
}
await user.save();
}
await this.destroy();
}
}