1
+ import { Client } from "https://deno.land/x/mysql/mod.ts" ;
2
+
3
+ enum LastActionEnum {
4
+ None ,
5
+ GetRank ,
6
+ UpdateRank
7
+ }
8
+
9
+ export default class ConnectionManager {
10
+ public client : Client
11
+ public isConnected : boolean = false
12
+ private lastAction : LastActionEnum = LastActionEnum . None
13
+ private currentRankData : { id :string , name :string , level :number } [ ] = [ ]
14
+ private reconnectAttempts : number = 0
15
+ private hostname : string = ''
16
+ private username : string = ''
17
+ private dbname : string = ''
18
+ private password : string = ''
19
+
20
+ constructor ( ) {
21
+ this . client = new Client ( )
22
+ this . parseConnectionString ( )
23
+ this . connect ( )
24
+ }
25
+
26
+ public async getRank ( ) : Promise < { id :string , name :string , level :number } [ ] > {
27
+ try {
28
+ const players = await this . client . query ( `select * from tinyrank` ) ;
29
+ let result = [ ]
30
+ for ( const player of players ) {
31
+ result . push ( { id :player . PlayerId , name :player . PlayerName , level :player . PlayerLevel } )
32
+ }
33
+
34
+ return result
35
+ } catch ( e ) {
36
+ this . client . close ( )
37
+ this . isConnected = false
38
+ let result = [ ]
39
+ result . push ( { id :'' , name :'' , level :0 } )
40
+ result . push ( { id :'' , name :'' , level :0 } )
41
+ result . push ( { id :'' , name :'' , level :0 } )
42
+
43
+ console . log ( `DB error:` )
44
+ console . log ( e )
45
+ this . lastAction = LastActionEnum . GetRank
46
+ this . reconnect ( )
47
+ return result
48
+ }
49
+ }
50
+
51
+ public async updateRank ( topPlayers : { id :string , name :string , level :number } [ ] ) {
52
+ try {
53
+ this . currentRankData = topPlayers
54
+
55
+ await this . client . execute (
56
+ `update tinyrank set ?? = ?, ?? = ?, ?? = ? where Id = 0` ,
57
+ [ "PlayerId" , topPlayers [ 0 ] . id ,
58
+ "PlayerName" , topPlayers [ 0 ] . name ,
59
+ "PlayerLevel" , topPlayers [ 0 ] . level ] ) ;
60
+ await this . client . execute (
61
+ `update tinyrank set ?? = ?, ?? = ?, ?? = ? where Id = 1` ,
62
+ [ "PlayerId" , topPlayers [ 1 ] . id ,
63
+ "PlayerName" , topPlayers [ 1 ] . name ,
64
+ "PlayerLevel" , topPlayers [ 1 ] . level ] ) ;
65
+ await this . client . execute (
66
+ `update tinyrank set ?? = ?, ?? = ?, ?? = ? where Id = 2` ,
67
+ [ "PlayerId" , topPlayers [ 2 ] . id ,
68
+ "PlayerName" , topPlayers [ 2 ] . name ,
69
+ "PlayerLevel" , topPlayers [ 2 ] . level ] ) ;
70
+ } catch ( e ) {
71
+ this . client . close ( )
72
+ this . isConnected = false
73
+ console . log ( `DB error:` )
74
+ console . log ( e )
75
+ this . lastAction = LastActionEnum . UpdateRank
76
+ this . reconnect ( )
77
+ }
78
+ }
79
+
80
+ public async connect ( ) {
81
+ try {
82
+ await this . client . connect ( {
83
+ hostname : this . hostname ,
84
+ username : this . username ,
85
+ db : this . dbname ,
86
+ password : this . password ,
87
+ } ) ;
88
+ this . isConnected = true
89
+ } catch ( e ) {
90
+ this . isConnected = false
91
+ console . log ( e )
92
+ }
93
+ }
94
+
95
+ private parseConnectionString ( ) {
96
+ const connectionString = Deno . env . get ( 'tinyconnectionstring' )
97
+ if ( connectionString ) {
98
+ const variables = connectionString . split ( ';' )
99
+
100
+ this . hostname = variables [ 0 ]
101
+ this . username = variables [ 1 ]
102
+ this . dbname = variables [ 2 ]
103
+ this . password = variables [ 3 ]
104
+ } else {
105
+ this . isConnected = false
106
+ console . log ( `error getting env: ${ Deno . env . get ( 'tinyconnectionstring' ) } ` )
107
+ }
108
+ }
109
+
110
+ private async reconnect ( ) {
111
+ console . log ( `retrying connection: ${ this . reconnectAttempts } ` )
112
+ setTimeout ( async ( ) => {
113
+ await this . connect ( )
114
+ if ( ! this . isConnected ) {
115
+ this . reconnectAttempts += 1
116
+ this . reconnect ( )
117
+ } else {
118
+ this . reconnectAttempts = 0
119
+ console . log ( 'reconnected to db!' )
120
+ if ( this . lastAction == LastActionEnum . UpdateRank ) {
121
+ this . updateRank ( this . currentRankData )
122
+ }
123
+ }
124
+ } , 5000 )
125
+ }
126
+ }
0 commit comments