1 |
|
2 |
|
3 |
|
4 | let httpProxy = require('http-proxy');
|
5 | let express = require('express');
|
6 | let EventEmitter = require('events').EventEmitter;
|
7 | EventEmitter.defaultMaxListeners = 0;
|
8 |
|
9 |
|
10 | require('./util/mixin/base');
|
11 |
|
12 |
|
13 | process.on('uncaughtException', function(err) {
|
14 | console.error(' Caught exceptio n: ' + err.stack);
|
15 | });
|
16 |
|
17 | let commonFunc = require('./util/commonFunc');
|
18 | let getAsnyc = require('./util/async')
|
19 | let cache = require('./util/cache')
|
20 | let formula = require('./util/formula')
|
21 | let Indicator = require('./util/Indicator')
|
22 | let updateMgr = require('./util/updateMgr')
|
23 | let Collection = require('./util/Collection')
|
24 | let config = require('./util/configInterface')
|
25 | let {applyMixins, extendObj, clone} = require('./util/mixin/comm')
|
26 | let filelist = require('./util/filelist')
|
27 | let iniFile = require(`${process.cwd()}/gameconfig`);
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | class Facade
|
33 | {
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | static startProxy(options) {
|
41 |
|
42 | var proxy = httpProxy.createProxyServer({});
|
43 | proxy.on('error', function (err, req, res) {
|
44 | res.writeHead(500, {'Content-Type': 'text/plain'});
|
45 | res.end('Something went wrong.');
|
46 | });
|
47 |
|
48 | var server = (require(options.protocol||'http')).createServer(function(req, res) {
|
49 | var host = req.headers.host, ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
50 | if(options.router[host]) {
|
51 | proxy.web(req, res, { target: options.router[host].target });
|
52 | } else {
|
53 | res.writeHead(200, {'Content-Type': 'text/plain'});
|
54 | res.end('Welcome! visit [wallet.vallnet.cn] for wallet service and [crm.vallnet.cn] for crm service.');
|
55 | }
|
56 | });
|
57 | server.listen(options.port);
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | static addWebSite(protocol, host, port, route) {
|
70 | let app = express();
|
71 | if(Array.isArray(route)) {
|
72 | route.map(rt => {
|
73 | if(typeof rt.dir == 'string') {
|
74 | app.use(rt.path, express.static(rt.dir));
|
75 | } else if(typeof rt.dir == 'function') {
|
76 | let router = express.Router();
|
77 | router.request(path, async (req, res) => {
|
78 | try {
|
79 | res.send(await rt.dir(req.query));
|
80 | } catch(e) {
|
81 | res.end();
|
82 | console.error(e);
|
83 | }
|
84 | });
|
85 | app.use("/", router);
|
86 | }
|
87 | });
|
88 | }
|
89 |
|
90 | let httpObj = require(protocol);
|
91 | let hrv = httpObj.createServer(app);
|
92 | hrv.listen(port, host, () => {
|
93 | console.log(`静态网站服务在 ${protocol}://${host}:${port} 上准备就绪`);
|
94 | });
|
95 | }
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 | static async boot(options, startup) {
|
102 | this.serverType = {};
|
103 | this.serverTypeMapping = {};
|
104 |
|
105 |
|
106 | let corelist = filelist.mapPackagePath(`${__dirname}/./core`);
|
107 | if(this.$addition) {
|
108 | corelist = corelist.concat(filelist.mapPath('app/core'));
|
109 | }
|
110 | corelist.map(srv => {
|
111 | let srvObj = require(srv.path);
|
112 | this.serverType[srv.name.split('.')[0]] = srvObj;
|
113 | srvObj.mapping.map(key => {
|
114 | this.serverTypeMapping[key] = srvObj;
|
115 | });
|
116 | });
|
117 |
|
118 |
|
119 | let core = this.FactoryOfCore(!!options?options.env:{});
|
120 | extendObj(core.options, options);
|
121 |
|
122 | if(this.$addition) {
|
123 | await core.loadModel();
|
124 | }
|
125 |
|
126 |
|
127 | if(options.loading) {
|
128 | options.loading.map(table=>{
|
129 | core.addLoadingModel(table);
|
130 | });
|
131 | }
|
132 |
|
133 | let app = express();
|
134 |
|
135 | app.all('*',function (req, res, next) {
|
136 |
|
137 | res.header('Access-Control-Allow-Origin', '*');
|
138 | res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
|
139 | res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
|
140 |
|
141 | if (req.method == 'OPTIONS') {
|
142 |
|
143 | res.send(200);
|
144 | } else {
|
145 | next();
|
146 | }
|
147 | });
|
148 |
|
149 |
|
150 | let bodyParser = require('body-parser');
|
151 | app.use(bodyParser.urlencoded({extended: true}))
|
152 | app.use(bodyParser.json());
|
153 |
|
154 |
|
155 |
|
156 | await core.Start(app);
|
157 |
|
158 | if(options.static) {
|
159 | for(let [route, path] of options.static) {
|
160 | if(typeof path == 'string') {
|
161 | core.static(route, path);
|
162 | } else if(typeof path == 'function') {
|
163 | core.addRouter(route, path);
|
164 | }
|
165 | }
|
166 | }
|
167 |
|
168 |
|
169 | app.use(function(req, res, next) {
|
170 | res.status(404).send('Sorry cant find the path!');
|
171 | });
|
172 |
|
173 |
|
174 | app.use(function(err, req, res, next) {
|
175 | console.error(err.stack);
|
176 | res.status(500).send('Something broke!');
|
177 | });
|
178 |
|
179 | if(typeof startup == 'function') {
|
180 | await startup(core);
|
181 | }
|
182 |
|
183 | return core;
|
184 | }
|
185 |
|
186 | |
187 |
|
188 |
|
189 |
|
190 |
|
191 | static FactoryOfCore(env) {
|
192 | let ret = null;
|
193 | if(!!this.serverTypeMapping[env.serverType] && !!this.ini.servers[env.serverType] && !!this.ini.servers[env.serverType][env["serverId"]]) {
|
194 | ret = new this.serverTypeMapping[env.serverType](this.tools.extend(
|
195 | {serverType: env.serverType, serverId: env.serverId},
|
196 | this.ini.servers["Index"][1],
|
197 | this.ini.servers[env.serverType][env["serverId"]]
|
198 | ));
|
199 | } else {
|
200 | throw new Error(`无法识别的服务器类型和编号 ${env.serverType}.${env.serverId}`);
|
201 | }
|
202 | return ret;
|
203 | }
|
204 |
|
205 | |
206 |
|
207 |
|
208 | static get addition() {
|
209 | return this.$addition || false;
|
210 | }
|
211 | |
212 |
|
213 |
|
214 | static set addition(val) {
|
215 | this.$addition = val;
|
216 | return this;
|
217 | }
|
218 |
|
219 | |
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | static registerServer(pair) {
|
226 | if(!!pair) {
|
227 | this.serverTypeMapping[pair[0]] = pair[1];
|
228 | }
|
229 | }
|
230 |
|
231 | |
232 |
|
233 |
|
234 | static get current(){
|
235 | return this.$current;
|
236 | }
|
237 | |
238 |
|
239 |
|
240 | static set current(val){
|
241 | this.$current = val;
|
242 | return this.$current;
|
243 | }
|
244 |
|
245 |
|
246 | static get CoreOfBase() {
|
247 | return require('./core/CoreOfBase');
|
248 | }
|
249 | static get CoreOfIndex() {
|
250 | return require('./core/CoreOfIndex');
|
251 | }
|
252 | static get CoreOfLogic() {
|
253 | return require('./core/CoreOfLogic');
|
254 | }
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | |
260 |
|
261 |
|
262 | static get Collection(){
|
263 | return Collection;
|
264 | }
|
265 |
|
266 | |
267 |
|
268 |
|
269 | static get Control(){
|
270 | return require('./util/baseCtl');
|
271 | }
|
272 | |
273 |
|
274 |
|
275 | static get Service(){
|
276 | return require('./util/baseService');
|
277 | }
|
278 | |
279 |
|
280 |
|
281 | static get Assistant(){
|
282 | return require('./model/baseAssistant');
|
283 | }
|
284 |
|
285 | |
286 |
|
287 |
|
288 | static get Assistants() {
|
289 | return {
|
290 | Pocket: require('./model/assistant/item'),
|
291 | }
|
292 | }
|
293 |
|
294 | |
295 |
|
296 |
|
297 | static get BaseEntity(){
|
298 | return require('./model/BaseEntity');
|
299 | }
|
300 |
|
301 | |
302 |
|
303 |
|
304 | static get BaseUserEntity() {
|
305 | return require('./model/entity/BaseUserEntity');
|
306 | }
|
307 |
|
308 | |
309 |
|
310 |
|
311 | static get BaseAllyObject() {
|
312 | return require('./model/entity/BaseAllyObject');
|
313 | }
|
314 |
|
315 | |
316 |
|
317 |
|
318 | static get BaseLogEntity() {
|
319 | return require('./model/entity/BuyLogEntity');
|
320 | }
|
321 |
|
322 |
|
323 |
|
324 | |
325 |
|
326 |
|
327 | static get assistants() {
|
328 | if(!this.$assistants){
|
329 | this.$assistants = {};
|
330 | filelist.mapPackagePath(`${__dirname}/./model/assistant`).map(mod=>{
|
331 | let mid = mod.name.split('.')[0];
|
332 | this.$assistants[mid] = require(mod.path);
|
333 | });
|
334 | if(this.$addition) {
|
335 | filelist.mapPath('app/model/assistant').map(mod=>{
|
336 | let mid = mod.name.split('.')[0];
|
337 | this.$assistants[mid] = require(mod.path);
|
338 | });
|
339 | }
|
340 | }
|
341 | return this.$assistants;
|
342 | }
|
343 | |
344 |
|
345 |
|
346 | static get models() {
|
347 | if(!this.$models){
|
348 |
|
349 | this.$models = {};
|
350 | filelist.mapPackagePath(`${__dirname}/./model/table`).map(mod=>{
|
351 | let mid = mod.name.split('.')[0];
|
352 | this.$models[mid] = require(mod.path)[mid];
|
353 | });
|
354 | if(this.$addition) {
|
355 | filelist.mapPath('app/model/table').map(mod=>{
|
356 | let mid = mod.name.split('.')[0];
|
357 | this.$models[mid] = require(mod.path)[mid];
|
358 | });
|
359 | }
|
360 | }
|
361 | return this.$models;
|
362 | }
|
363 | |
364 |
|
365 |
|
366 | static get entities(){
|
367 | if(!this.$entities) {
|
368 | this.$entities = {};
|
369 |
|
370 |
|
371 | filelist.mapPackagePath(`${__dirname}/./model/entity`).map(mod=>{
|
372 | let mid = mod.name.split('.')[0];
|
373 | this.$entities[mid] = require(mod.path);
|
374 | });
|
375 |
|
376 | this.$entities.UserEntity = require('./model/entity/BaseUserEntity');
|
377 | this.$entities.AllyObject = require('./model/entity/BaseAllyObject');
|
378 |
|
379 | if(this.$addition) {
|
380 |
|
381 | filelist.mapPath('app/model/entity').map(mod=>{
|
382 | let mid = mod.name.split('.')[0];
|
383 | this.$entities[mid] = require(mod.path);
|
384 | });
|
385 | }
|
386 | }
|
387 | return this.$entities;
|
388 | }
|
389 |
|
390 | |
391 |
|
392 |
|
393 | static get tools() {
|
394 | return {
|
395 | mixin: applyMixins,
|
396 | extend: extendObj,
|
397 | clone: clone,
|
398 | Sequelize: require('sequelize'),
|
399 | seqconn: require('./util/sequel'),
|
400 | maintain: require('./util/maintain'),
|
401 | formula: formula,
|
402 | cache: cache,
|
403 | Indicator: Indicator,
|
404 | updateMgr: updateMgr,
|
405 | getAsnyc: getAsnyc,
|
406 | Lock: require('./util/Lock')
|
407 | };
|
408 | }
|
409 |
|
410 | |
411 |
|
412 |
|
413 | static get autoExec() {
|
414 | if(!this.$autoExec){
|
415 | this.$autoExec = {};
|
416 | filelist.mapPackagePath(`${__dirname}/./util/autoExec`).map(mod=>{
|
417 | let mid = mod.name.split('.')[0];
|
418 | this.$autoExec[mid] = require(mod.path);
|
419 | });
|
420 | if(this.$addition) {
|
421 | filelist.mapPath('app/util/autoExec').map(mod=>{
|
422 | let mid = mod.name.split('.')[0];
|
423 | this.$autoExec[mid] = require(mod.path);
|
424 | });
|
425 | }
|
426 | }
|
427 | return this.$autoExec;
|
428 | }
|
429 |
|
430 | |
431 |
|
432 |
|
433 | static get ini(){
|
434 | return iniFile;
|
435 | }
|
436 | |
437 |
|
438 |
|
439 | static get config(){
|
440 | return config;
|
441 | }
|
442 | |
443 |
|
444 |
|
445 | static get util(){
|
446 | return commonFunc;
|
447 | }
|
448 | |
449 |
|
450 |
|
451 | static get const(){
|
452 | if(!this.$constList) {
|
453 |
|
454 | this.$constList = require( './define/comm');
|
455 |
|
456 | for(let fl of filelist.mapPackagePath(`${__dirname}/./define`)){
|
457 | let id = fl.name.split('.')[0];
|
458 | if(id != 'comm'){
|
459 | let n = require(fl.path);
|
460 | extendObj(this.$constList, n);
|
461 | }
|
462 | }
|
463 |
|
464 | if(this.$addition) {
|
465 |
|
466 | for(let fl of filelist.mapPath('app/define')){
|
467 | let n = require(fl.path);
|
468 | extendObj(this.$constList, n);
|
469 | }
|
470 | }
|
471 |
|
472 | this.$constList.AddConditionType = (name, val) => {
|
473 | this.$constList.em_Condition_Type[name] = val;
|
474 | };
|
475 | this.$constList.AddResType = (name, val) => {
|
476 | this.$constList.ResType[name] = val;
|
477 | };
|
478 | }
|
479 |
|
480 | return this.$constList;
|
481 | }
|
482 | }
|
483 |
|
484 | class Util
|
485 | {
|
486 | static get BonusObject() {
|
487 | return require('./util/comm/BonusObject');
|
488 | }
|
489 |
|
490 | static get EffectManager() {
|
491 | return require('./util/comm/EffectManager');
|
492 | }
|
493 |
|
494 | static get EffectObject() {
|
495 | return require('./util/comm/EffectObject');
|
496 | }
|
497 |
|
498 | static get TollgateObject() {
|
499 | return require('./util/tollgate/TollgateObject');
|
500 | }
|
501 |
|
502 | static get OperationInfo() {
|
503 | return require('./util/tollgate/OperationInfo');
|
504 | }
|
505 |
|
506 | static get PotentialClientItem() {
|
507 | return require('./util/potential/PetClientItem');
|
508 | }
|
509 |
|
510 | static get BattleManager() {
|
511 | return require('./util/battle/BattleManager');
|
512 | }
|
513 |
|
514 | static get BaseBattleParam() {
|
515 | let {BaseBattleParam} = require('./util/battle/util');
|
516 | return BaseBattleParam;
|
517 | }
|
518 |
|
519 | static get BattleHero() {
|
520 | let {BattleHero} = require('./util/battle/hero');
|
521 | return BattleHero;
|
522 | }
|
523 |
|
524 | static get LargeNumberCalculator() {
|
525 | return require('./util/comm/LargeNumberCalculator');
|
526 | }
|
527 |
|
528 | static get EventData() {
|
529 | let {EventData} = require('./util/comm/EventData');
|
530 | return EventData;
|
531 | }
|
532 | }
|
533 |
|
534 | Facade.Util = Util;
|
535 |
|
536 | exports = module.exports = Facade;
|