This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranvier
165 lines (143 loc) · 5.53 KB
/
ranvier
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
#!/usr/bin/env node
'use strict';
/**
* Main file, use this to run the server:
* node ranvier [options]
*
* Options:
* -v Verbose loggin
* --port Port to listen on
* --locale Locale to act as the default
* --save Minutes between autosave
* --respawn Minutes between respawn
*/
/* NPM Modules */
const semver = require('semver');
const net = require('net');
const commander = require('commander');
const wrap = require('wrap-ansi');
const argv = require('optimist').argv;
// Package.json for versioning
const pkg = require('./package.json');
if (!semver.satisfies(process.version, pkg.engines.node)) {
throw new Error(
`Ranvier's core engine requires Node version ${pkg.engines.node},
you are currently running Node ${process.version}.`
);
}
// State managers and factories
const AccountManager = require('./src/AccountManager');
const AreaManager = require('./src/AreaManager');
const BehaviorManager = require('./src/BehaviorManager');
const ChannelManager = require('./src/ChannelManager');
const ClassManager = require('./src/ClassManager');
const CommandManager = require('./src/CommandManager');
const EffectFactory = require('./src/EffectFactory');
const EventManager = require('./src/EventManager');
const GameServer = require('./src/GameServer');
const HelpManager = require('./src/HelpManager');
const ItemFactory = require('./src/ItemFactory');
const ItemManager = require('./src/ItemManager');
const MobFactory = require('./src/MobFactory');
const MobManager = require('./src/MobManager');
const PartyManager = require('./src/PartyManager');
const PlayerManager = require('./src/PlayerManager');
const QuestFactory = require('./src/QuestFactory');
const QuestGoalManager = require('./src/QuestGoalManager');
const QuestRewardManager = require('./src/QuestRewardManager');
const RoomManager = require('./src/RoomManager');
const SkillManager = require('./src/SkillManager');
// Wrapper for ranvier.json
const Config = require('./src/Config');
// cmdline options
commander
.version(pkg.version)
.option('-s, --save [time]', 'Number of seconds between auto-save ticks [10]', 10)
.option('-r, --respawn [time]', 'Number of minutes between respawn ticks [120]', 120)
.option('-p, --port [portNumber]', 'Port to host the server [23]', Config.get('port', 23))
.option('-v, --verbose', 'Verbose console logging.', true)
.option('-e, --prettyErrors', 'Pretty-print formatting for error stack traces.', false)
.parse(process.argv);
// Set debug variable and encoding.
// 'net' by default to help find possible server errors.
process.env.NODE_DEBUG = 'net';
process.stdin.setEncoding('utf8');
const Logger = require('./src/Logger');
const logfile = Config.get('logfile');
if (logfile) {
Logger.setFileLogging(logfile);
}
if (commander.prettyErrors) {
Logger.enablePrettyErrors();
}
// Set logging level based on CLI option or environment variable.
const logLevel = commander.verbose ?
'verbose' :
process.env.LOG_LEVEL || Config.get('logLevel') || 'debug';
Logger.setLevel(logLevel);
// Global state object, server instance and configurable intervals.
let GameState = {};
let saveInterval, tickInterval, playerTickInterval;
/**
* Do the dirty work
*/
function init(restartServer) {
Logger.log("START - Loading entities");
restartServer = typeof restartServer === 'undefined' ? true : restartServer;
GameState = {
AccountManager: new AccountManager(),
AreaManager: new AreaManager(),
ChannelManager: new ChannelManager(),
ClassManager: new ClassManager(), // player class manager
CommandManager: new CommandManager(),
Config, // All global server settings like default respawn time, save interval, port, what bundles to load, etc.
EffectFactory: new EffectFactory(),
HelpManager: new HelpManager(),
InputEventManager: new EventManager(),
ItemBehaviorManager: new BehaviorManager(),
ItemFactory: new ItemFactory(),
ItemManager: new ItemManager(),
MobBehaviorManager: new BehaviorManager(),
MobFactory: new MobFactory(),
MobManager: new MobManager(),
PartyManager: new PartyManager(),
PlayerManager: new PlayerManager(),
QuestFactory: new QuestFactory(),
QuestGoalManager: new QuestGoalManager(),
QuestRewardManager: new QuestRewardManager(),
RoomBehaviorManager: new BehaviorManager(),
RoomManager: new RoomManager(),
SkillManager: new SkillManager(),
SpellManager: new SkillManager(),
ServerEventManager: new EventManager(),
GameServer: new GameServer(),
};
// Setup bundlemanager
const BundleManager = new (require('./src/BundleManager'))(GameState);
GameState.BundleManager = BundleManager;
BundleManager.loadBundles();
GameState.ServerEventManager.attach(GameState.GameServer);
if (restartServer) {
Logger.log("START - Starting server");
GameState.GameServer.startup(commander);
// Save every 10 minutes by default.
Logger.log(`Setting autosave to ${commander.save} seconds.`);
clearInterval(saveInterval);
saveInterval = setInterval(() => {
GameState.PlayerManager.saveAll();
}, commander.save * 1000);
// Ticks for effect processing and combat happen every half second
clearInterval(tickInterval);
tickInterval = setInterval(() => {
GameState.AreaManager.tickAll(GameState);
GameState.ItemManager.tickAll();
}, Config.get('entityTickFrequency', 100));
clearInterval(playerTickInterval);
playerTickInterval = setInterval(() => {
GameState.PlayerManager.emit('updateTick');
}, Config.get('playerTickFrequency', 100));
}
}
// START IT UP!
init();
// vim: set syn=javascript :