forked from mycard/srvpro
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ygopro.js
203 lines (172 loc) · 5.41 KB
/
ygopro.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Generated by CoffeeScript 2.6.1
(function() {
var Struct, YGOProMessagesHelper, _, loadJSON, translateHandler;
_ = require('underscore');
_.str = require('underscore.string');
_.mixin(_.str.exports());
Struct = require('./struct.js').Struct;
loadJSON = require('load-json-file').sync;
this.i18ns = loadJSON('./data/i18n.json');
this.i18nR = {};
this.reloadI18nR = function() {
var data, key, lang, ref, results, text;
ref = this.i18ns;
results = [];
for (lang in ref) {
data = ref[lang];
this.i18nR[lang] = {};
results.push((function() {
var results1;
results1 = [];
for (key in data) {
text = data[key];
results1.push(this.i18nR[lang][key] = {
regex: new RegExp("\\$\\{" + key + "\\}", 'g'),
text: text
});
}
return results1;
}).call(this));
}
return results;
};
this.reloadI18nR();
YGOProMessagesHelper = require("./YGOProMessages.js").YGOProMessagesHelper; // 为 SRVPro2 准备的库,这里拿这个库只用来测试,SRVPro1 对异步支持不是特别完善,因此不会有很多异步优化
this.helper = new YGOProMessagesHelper(9000);
this.structs = this.helper.structs;
this.structs_declaration = this.helper.structs_declaration;
this.typedefs = this.helper.typedefs;
this.proto_structs = this.helper.proto_structs;
this.constants = this.helper.constants;
translateHandler = function(handler) {
return async function(buffer, info, datas, params) {
return (await handler(buffer, info, params.client, params.server, datas));
};
};
this.stoc_follow = function(proto, synchronous, callback) {
this.helper.addHandler(`STOC_${proto}`, translateHandler(callback), synchronous, 1);
};
this.stoc_follow_before = function(proto, synchronous, callback) {
this.helper.addHandler(`STOC_${proto}`, translateHandler(callback), synchronous, 0);
};
this.stoc_follow_after = function(proto, synchronous, callback) {
this.helper.addHandler(`STOC_${proto}`, translateHandler(callback), synchronous, 2);
};
this.ctos_follow = function(proto, synchronous, callback) {
this.helper.addHandler(`CTOS_${proto}`, translateHandler(callback), synchronous, 1);
};
this.ctos_follow_before = function(proto, synchronous, callback) {
this.helper.addHandler(`CTOS_${proto}`, translateHandler(callback), synchronous, 0);
};
this.ctos_follow_after = function(proto, synchronous, callback) {
this.helper.addHandler(`CTOS_${proto}`, translateHandler(callback), synchronous, 2);
};
//消息发送函数,至少要把俩合起来....
this.stoc_send = function(socket, proto, info) {
return this.helper.sendMessage(socket, `STOC_${proto}`, info);
};
this.ctos_send = function(socket, proto, info) {
return this.helper.sendMessage(socket, `CTOS_${proto}`, info);
};
//util
this.split_chat_lines = function(msg, player, lang) {
var i, len, line, lines, o, r, ref, ref1;
lines = [];
ref = _.lines(msg);
for (i = 0, len = ref.length; i < len; i++) {
line = ref[i];
if (player >= 10) {
line = "[Server]: " + line;
}
ref1 = this.i18nR[lang];
for (o in ref1) {
r = ref1[o];
line = line.replace(r.regex, r.text);
}
lines.push(line);
}
return lines;
};
this.stoc_send_chat = async function(client, msg, player = 8) {
var i, len, line, ref;
if (!client) {
console.log("err stoc_send_chat");
return;
}
ref = this.split_chat_lines(msg, player, client.lang);
for (i = 0, len = ref.length; i < len; i++) {
line = ref[i];
await this.stoc_send(client, 'CHAT', {
player: player,
msg: line
});
}
};
this.stoc_send_chat_to_room = function(room, msg, player = 8) {
var client, i, j, len, len1, ref, ref1;
if (!room) {
console.log("err stoc_send_chat_to_room");
return;
}
ref = room.players;
for (i = 0, len = ref.length; i < len; i++) {
client = ref[i];
if (client) {
this.stoc_send_chat(client, msg, player);
}
}
ref1 = room.watchers;
for (j = 0, len1 = ref1.length; j < len1; j++) {
client = ref1[j];
if (client) {
this.stoc_send_chat(client, msg, player);
}
}
room.recordChatMessage(msg, player);
};
this.stoc_send_hint_card_to_room = function(room, card) {
var client, i, j, len, len1, ref, ref1;
if (!room) {
console.log("err stoc_send_hint_card_to_room");
return;
}
ref = room.players;
for (i = 0, len = ref.length; i < len; i++) {
client = ref[i];
if (client) {
this.stoc_send(client, 'GAME_MSG', {
curmsg: 2,
type: 10,
player: 0,
data: card
});
}
}
ref1 = room.watchers;
for (j = 0, len1 = ref1.length; j < len1; j++) {
client = ref1[j];
if (client) {
this.stoc_send(client, 'GAME_MSG', {
curmsg: 2,
type: 10,
player: 0,
data: card
});
}
}
};
this.stoc_die = async function(client, msg) {
await this.stoc_send_chat(client, msg, this.constants.COLORS.RED);
if (client) {
await this.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 9
});
}
if (client) {
client.system_kicked = true;
client.destroy();
}
return '_cancel';
};
}).call(this);