Skip to content

Commit 8eeef7a

Browse files
authored
Merge pull request #99 from tiny-devs/dev
Merge with Master
2 parents eae9bee + b55a024 commit 8eeef7a

File tree

4 files changed

+141
-8
lines changed

4 files changed

+141
-8
lines changed

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: deno run --allow-net=:${PORT} --cached-only --allow-read main.ts --port=${PORT}
1+
web: deno run --allow-net --cached-only --allow-read --allow-env main.ts --port=${PORT}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Play now (TODO: Deploy)
77
Install Deno: [installation guide](https://deno.land/manual/getting_started/installation)
88

99
Run the game server (you'll need 2 terminals)
10-
Server: (From the root folder)
11-
`deno run --allow-net=:3000 --allow-read main.ts --port=3000`
12-
Client: (From client's folder)
10+
Server: (From the root folder)
11+
`deno run --allow-net=:3000 --allow-read main.ts --port=3000`
12+
Client: (From client's folder)
1313
`yarn install`
1414
`yarn start`
1515

server/clientHandler.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@ import Room from './map/rooms/room.ts'
66
import Map from './map/map.ts'
77
import { Npc } from './entities/npc.ts'
88
import { PveData } from './pve/pveData.ts'
9+
import ConnectionManager from "./db/main.ts"
910

1011
export class ClientHandler {
1112
public boardColumns: number = 16
1213
public boardRows: number = 16
1314
public playerNames: string[] = []
1415
public map: Map
1516
private topPlayers: {id:string,name:string,level:number}[]
17+
private db: ConnectionManager
1618

1719
constructor(serverConfigs: any) {
1820
this.boardRows = serverConfigs.boardRows
1921
this.boardColumns = serverConfigs.boardColumns
2022

2123
this.map = new Map(this)
2224

25+
this.db = new ConnectionManager()
26+
2327
this.topPlayers = []
24-
this.topPlayers.push({id:'',name:'',level:0})
25-
this.topPlayers.push({id:'',name:'',level:0})
26-
this.topPlayers.push({id:'',name:'',level:0})
28+
29+
this.db.getRank().then(result => {
30+
this.topPlayers = result
31+
})
2732
}
2833

2934
private broadcastRank(): void {
@@ -350,7 +355,9 @@ export class ClientHandler {
350355
players.splice(0, players.length)
351356
if (updated) {
352357
this.topPlayers.splice(3)
353-
this.broadcastRank()
358+
this.db.updateRank(this.topPlayers).then(() => {
359+
this.broadcastRank()
360+
})
354361
}
355362

356363
return updated

server/db/main.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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

Comments
 (0)