Browse code

[mov] source/*.ts -> source/logic/*.ts

Christian FraƟ authored on 20/11/2021 15:04:11
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,65 @@
1
+/**
2
+ * gets the current UNIX timestamp
3
+ */
4
+function get_timestamp
5
+(
6
+): int
7
+{
8
+	return Math.floor(Date.now()/1000);
9
+}
10
+
11
+
12
+/**
13
+ * computes a floating point number in the interval [0,1[ out of a string
14
+ */
15
+function hash_string_to_unit
16
+(
17
+	x: string
18
+): float
19
+{
20
+	return (x.split("").reduce((x, y) => ((x + y.charCodeAt(0)) % 32), 0) / 32);
21
+}
22
+
23
+
24
+/**
25
+ * encodes a username as a CSS color
26
+ */
27
+function get_usercolor
28
+(
29
+	name: string
30
+): string
31
+{
32
+	const hue: float = hash_string_to_unit(name);
33
+	return `hsl(${(hue*360).toFixed(2)},50%,75%)`;
34
+}
35
+
36
+
37
+/**
38
+ * calls an API action of the backend
39
+ */
40
+async function backend_call
41
+(
42
+	conf: type_conf,
43
+	connection_id: (null | string),
44
+	action: string, data: any
45
+): Promise<any>
46
+{
47
+	const response: any = await fetch
48
+	(
49
+		`${conf.backend.scheme}://${conf.backend.host}:${conf.backend.port.toFixed(0)}/${conf.backend.path}`,
50
+		{
51
+			"method": "POST",
52
+			"body": JSON.stringify({"id": connection_id, "action": action, "data": data}),
53
+		}
54
+	);
55
+	if (response.ok)
56
+	{
57
+		return response.json();
58
+	}
59
+	else
60
+	{
61
+		console.error(response.text());
62
+		return Promise.reject<any>(new Error("backend call failed"));
63
+	}
64
+}
65
+