forked from ably-labs/multiplayer-flappy-bird
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
137 lines (127 loc) · 3.67 KB
/
server.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const envConfig = require("dotenv").config();
const express = require("express");
const Ably = require("ably");
const gameChannelName = "flappy-game";
let gameChannel;
let birdCount = 0;
let gameTicker;
let isGameTickerOn = false;
let gameStateObj;
let birds = {};
let highScore = 0;
let highScoreNickname = "anonymous panda";
let birdChannels = {};
let obstacleTimer = 0;
let topScoreChannel;
let topScoreChannelName = "flappy-top-score";
const app = express();
app.use(express.static("public"));
const realtime = new Ably.Realtime({
key: process.env.ABLY_API_KEY,
});
const uniqueId = function () {
return "id-" + Math.random().toString(36).substr(2, 16);
};
app.get("/", (request, response) => {
response.sendFile(__dirname + "/index.html");
});
app.get("/auth", function (req, res) {
var tokenParams = {
clientId: uniqueId(),
};
realtime.auth.createTokenRequest(tokenParams, function (err, tokenRequest) {
if (err) {
res.status(500).send("Error requesting token: " + JSON.stringify(err));
} else {
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(tokenRequest));
}
});
});
const listener = app.listen(process.env.PORT, () => {
console.log("App is listening on port " + listener.address().port);
});
realtime.connection.once("connected", () => {
topScoreChannel = realtime.channels.get(topScoreChannelName, {
params: { rewind: 1 },
});
topScoreChannel.subscribe("score", (msg) => {
highScore = msg.data.score;
highScoreNickname = msg.data.nickname;
topScoreChannel.unsubscribe();
});
gameChannel = realtime.channels.get(gameChannelName);
gameChannel.presence.subscribe("enter", (msg) => {
birdCount++;
console.log("JOINED Bird Count: " + birdCount);
if (birdCount === 1 && !isGameTickerOn) {
console.log("STARTING GAME TICK");
gameTicker = setInterval(startGameTick, 100);
isGameTickerOn = true;
}
birds[msg.clientId] = {
id: msg.clientId,
left: 220,
bottom: 350,
isDead: false,
nickname: msg.data.nickname,
score: 0,
};
subscribeToPlayerInput(msg.clientId);
});
gameChannel.presence.subscribe("leave", (msg) => {
if (birds[msg.clientId] != undefined) {
birdCount--;
console.log("LEFT Bird count " + birdCount + " " + msg.clientId);
birds[msg.clientId].isDead = true;
setTimeout(() => {
delete birds[msg.clientId];
}, 250);
if (birdCount === 0) {
console.log("STOPPING GAME TICK");
isGameTickerOn = false;
clearInterval(gameTicker);
}
}
});
});
function subscribeToPlayerInput(id) {
birdChannels[id] = realtime.channels.get("bird-position-" + id);
birdChannels[id].subscribe("pos", (msg) => {
if (birds[id]) {
birds[id].bottom = msg.data.bottom;
birds[id].nickname = msg.data.nickname;
birds[id].score = msg.data.score;
if (msg.data.score > highScore) {
highScore = msg.data.score;
highScoreNickname = msg.data.nickname;
topScoreChannel.publish("score", {
score: highScore,
nickname: highScoreNickname,
});
}
}
});
}
function startGameTick() {
if (obstacleTimer === 0 || obstacleTimer === 3000) {
obstacleTimer = 0;
gameStateObj = {
birds: birds,
highScore: highScore,
highScoreNickname: highScoreNickname,
launchObstacle: true,
obstacleHeight: Math.random() * 60,
};
} else {
gameStateObj = {
birds: birds,
highScore: highScore,
highScoreNickname: highScoreNickname,
launchObstacle: false,
obstacleHeight: "",
};
}
obstacleTimer += 100;
gameChannel.publish("game-state", gameStateObj);
}