-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.ts
111 lines (96 loc) · 2.34 KB
/
graph.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
import {PrismaClient} from "@prisma/client";
import {UndirectedGraph} from "graphology";
import {createPlayerIfDoesntExistAndReturnENS} from "./utils";
const prisma = new PrismaClient();
export async function updateNode(player: string, elo: number, g: UndirectedGraph) {
await prisma.player.update({
where: {
address: player,
},
data: {
elo: elo,
},
});
if (!g.hasNode(player)) {
g.addNode(player, {
elo,
ensName: null,
ensAvatar: null,
badges: [],
});
}
g.setNodeAttribute(player, 'elo', elo);
}
export async function loadGraph(g: UndirectedGraph) {
const allNodes = await prisma.player.findMany({
include: {
whiteListAttestations: true
}
});
for (const node of allNodes) {
g.mergeNode(node.address, {
elo: node.elo,
ensName: node.ensName,
ensAvatar: node.ensAvatar,
badges: node.whiteListAttestations.map(attestation => attestation.type),
});
}
const allEdges = await prisma.link.findMany({
select: {
player1: true,
player2: true
},
where: {
default: true
}
});
for (const edge of allEdges) {
g.mergeEdge(edge.player1, edge.player2);
}
}
export async function addLink(player1: string, player2: string, g: UndirectedGraph) {
const existingLink = await prisma.link.findUnique({
where: {
player1_player2: {
player1: player1,
player2: player2,
}
}
});
if (!existingLink) {
const ens1 = await createPlayerIfDoesntExistAndReturnENS(player1);
const ens2 = await createPlayerIfDoesntExistAndReturnENS(player2);
await prisma.link.createMany({
data: [
{
player1: player1,
player2: player2,
default: true,
},
{
player1: player2,
player2: player1,
default: false,
}
]
})
g.mergeEdge(player1, player2);
const [node1, node2] = [player1, player2].map(player => g.getNodeAttributes(player));
if (!node1.elo && node1.elo !== 0) {
g.mergeNode(player1, {
elo: 0,
ensName: ens1.name,
ensAvatar: ens1.avatar,
badges: [],
})
}
if (!node2.elo && node2.elo !== 0) {
g.mergeNode(player2, {
elo: 0,
ensName: ens2.name,
ensAvatar: ens2.avatar,
badges: [],
})
}
}
}