-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (54 loc) · 2.04 KB
/
server.js
File metadata and controls
66 lines (54 loc) · 2.04 KB
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
require('dotenv').config();
const http = require('http');
const createApp = require('./src/api/expressApp');
const Colonist = require('./src/core/Colonist');
const Colony = require('./src/core/Colony');
const Engine = require('./src/core/Engine');
const dbPool = require('./src/database/dbConnect');
const GameMultiplayer = require('./src/services/socket');
const PORT = process.env.PORT || 3001;
async function startServer() {
try {
console.log('Connecting to database...');
const connection = await dbPool.getConnection();
connection.release();
console.log('Database connected');
console.log('Initializing game...');
const station = new Colony("Alpha");
try {
const commander = new Colonist("John", "commander");
const engineer = new Colonist("Jane", "engineer");
const scientist = new Colonist("Bob", "scientist");
const medic = new Colonist("Alice", "medic");
station.addMember(commander);
station.addMember(engineer);
station.addMember(scientist);
station.addMember(medic);
console.log('Colonists created');
} catch (error) {
console.error("Error creating colonists:", error.message);
process.exit(1);
}
const game = new Engine(station);
const app = createApp(game);
const server = http.createServer(app);
const gameMultiplayer = new GameMultiplayer(server);
console.log('Socket.io initialized');
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
game.start();
process.on('SIGINT', () => {
console.log('\n\nShutting down...');
game.stop();
server.close(() => {
console.log('Server closed');
process.exit(0);
});
});
} catch (error) {
console.error('Fatal error:', error.message);
process.exit(1);
}
}
startServer();