-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbackEnd.js
168 lines (157 loc) · 4.4 KB
/
backEnd.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @file Sets up an Express backend server for examples, serving static files,
* replacing HTML strings, and enabling a game socket service.
* The behavior adapts based on the environment mode (NODE_ENV) inject in process.env with cross-env package.
* See {@link https://nodejs.org/api/process.html#processenv-env}, {@link https://www.npmjs.com/package/cross-env}
*
* requires {@link https://www.npmjs.com/package/@ud-viz/game_node}
* requires {@link https://www.npmjs.com/package/@ud-viz/utils_shared}
* requires {@link https://www.npmjs.com/package/reload}
* requires {@link https://www.npmjs.com/package/string-replace-middleware}
*/
const udvizVersion = require('../package.json').version;
const path = require('path');
const { MESSAGE, RELOAD_PORT, DEFAULT_PORT } = require('./constant');
const reload = require('reload');
const { stringReplace } = require('string-replace-middleware');
const express = require('express');
const { SocketService } = require('@ud-viz/game_node');
const {
NoteManager,
DomElement3DCubeManager,
} = require('@ud-viz/game_node_template');
const { Object3D } = require('@ud-viz/game_shared');
const {
NativeCommandManager,
constant,
} = require('@ud-viz/game_shared_template');
/**
* The environment mode.
*
* @type {string}
*/
const NODE_ENV = process.env.NODE_ENV || 'development';
console.log('Back-end starting in', NODE_ENV, 'mode');
/**
* Express application instance.
*
* @type {object}
*/
const app = new express();
// Apply string replacements for different values in HTML responses
app.use(
stringReplace(
{
RUN_MODE: NODE_ENV,
},
{
contentTypeFilterRegexp: /text\/html/,
}
)
);
app.use(
stringReplace(
{
SCRIPT_TAG_RELOAD:
NODE_ENV == 'development'
? '<script src="/reload/reload.js"></script>'
: '',
},
{
contentTypeFilterRegexp: /text\/html/,
}
)
);
app.use(
stringReplace(
{
UDVIZ_VERSION: udvizVersion,
},
{
contentTypeFilterRegexp: /text\/html/,
}
)
);
// Serve static files
app.use(express.static(path.resolve(__dirname, '../')));
/**
* @type {number}
*/
const PORT = process.env.PORT || DEFAULT_PORT;
/**
* The HTTP server instance.
*
* @type {object}
*/
const httpServer = app.listen(PORT, (err) => {
if (err) {
console.error('Server could not start');
return;
}
console.log('Http server listening on port', PORT);
});
// Initialize examples game socket service
const gameSocketService = new SocketService(httpServer);
gameSocketService
.loadGameThreads(
[
// Define the game thread
new Object3D({
name: 'Note game',
static: true,
matrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1844753.5, 5174961, 0, 1],
components: {
GameScript: {
scriptParams: [
{ id: NoteManager.ID_SCRIPT },
{ id: NativeCommandManager.ID_SCRIPT },
],
},
ExternalScript: {
scriptParams: [
{ id: constant.ID_SCRIPT.NOTE_UI },
{ id: constant.ID_SCRIPT.CAMERA_MANAGER },
],
},
},
}),
new Object3D({
uuid: 'dom_element_3d_cube_game_uuid',
name: 'DomElement 3D cube game',
static: true,
matrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1844753.5, 5174961, 0, 1],
components: {
GameScript: {
scriptParams: [
{ id: DomElement3DCubeManager.ID_SCRIPT },
{ id: NativeCommandManager.ID_SCRIPT },
],
variables: {
idRenderData: 'cube',
domElement3D: {
position: { x: 0, y: 0.52, z: 0.5 },
rotation: { x: Math.PI * 0.5, y: Math.PI, z: 0 },
scale: { x: 1, y: 1, z: 1 },
},
defaultSpeedTranslate: 0.5,
defaultSpeedRotate: 0.001,
},
},
ExternalScript: {
scriptParams: [
{ id: constant.ID_SCRIPT.CONTROLLER_NATIVE_COMMAND_MANAGER },
],
},
},
}),
],
'./bin/gameThreadChild.js'
)
.then(() => {
console.log('Game SocketService initialized');
// Notify parent process if possible
if (process.send) {
process.send(MESSAGE.READY);
}
if (NODE_ENV == 'development') reload(app, { port: RELOAD_PORT }); // client should have script tag reload
});