forked from empiricaly/meteor-empirica-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
128 lines (106 loc) · 3.14 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
import "./startup/server/index.js";
import SimpleSchema from "simpl-schema";
SimpleSchema.debug = true;
import { playerIdForConn } from "./startup/server/connections.js";
import { callOnChange } from "./api/server/onchange.js";
import { callOnSubmit } from "./api/server/onsubmit.js";
import shared from "./shared";
import log from "./lib/log";
import { getFunctionParameters } from "./lib/utils";
const safeCallback = function(name, func) {
try {
switch (name) {
case "onGameStart":
case "onRoundStart":
case "onStageStart":
case "onStageEnd":
case "onRoundEnd":
case "onGameEnd":
handleCallbackFuncParameters(func);
break;
default:
break;
}
return func.apply(this, arguments);
} catch (err) {
console.error(`Fatal error encounter calling Empirica.${name}:`);
console.error(err);
}
};
const handleCallbackFuncParameters = func => {
const parameters = getFunctionParameters(func);
const handler = {
getOwnPropertyDescriptor(target, keyIndex) {
const key = keyIndex.split("__-__")[0];
const index = parseInt(keyIndex.split("__-__")[1]);
if (
(key === "game" && index === 0) ||
(key === "round" && index === 1) ||
(key === "stage" && index === 2)
) {
return;
} else if (key === "players") {
throw new Error(
`the "players" argument has been deprecated, use "game.players" instead`
);
} else {
throw new Error(`"${key}" property is not allowed on this callback`);
}
}
};
const proxy = new Proxy({}, handler);
parameters.forEach((key, index) => {
const keyIndex = key + "__-__" + index;
Object.getOwnPropertyDescriptor(proxy, keyIndex);
});
};
// Maybe could do better...
const config = { bots: {} };
const Empirica = {
// New name for init: gameInit
gameInit(func) {
config.gameInit = func;
},
bot(name, func) {
if (config.bots[name]) {
throw `Bot "${name}" was declared twice!`;
}
config.bots[name] = () => safeCallback("bot", func);
},
onGameStart(func) {
config.onGameStart = () => safeCallback("onGameStart", func);
},
onRoundStart(func) {
config.onRoundStart = () => safeCallback("onRoundStart", func);
},
onStageStart(func) {
config.onStageStart = () => safeCallback("onStageStart", func);
},
onStageEnd(func) {
config.onStageEnd = () => safeCallback("onStageEnd", func);
},
onRoundEnd(func) {
config.onRoundEnd = () => safeCallback("onRoundEnd", func);
},
onGameEnd(func) {
config.onGameEnd = () => safeCallback("onGameEnd", func);
},
onSet(func) {
config.onSet = () => safeCallback("onSet", func);
},
onAppend(func) {
config.onAppend = () => safeCallback("onAppend", func);
},
onChange(func) {
config.onChange = () => safeCallback("onChange", func);
},
onSubmit(func) {
config.onSubmit = () => safeCallback("onSubmit", func);
}
};
export { config };
export default Empirica;
// Help access to server only modules from shared modules
shared.playerIdForConn = playerIdForConn;
shared.callOnChange = callOnChange;
shared.callOnSubmit = callOnSubmit;