-
Notifications
You must be signed in to change notification settings - Fork 76
/
ygopro.coffee
123 lines (108 loc) · 3.55 KB
/
ygopro.coffee
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
_ = require 'underscore'
_.str = require 'underscore.string'
_.mixin(_.str.exports())
Struct = require('./struct.js').Struct
loadJSON = require('load-json-file').sync
@i18ns = loadJSON './data/i18n.json'
@i18nR = {}
@reloadI18nR = () ->
for lang, data of @i18ns
@i18nR[lang]={}
for key, text of data
@i18nR[lang][key]={
regex: new RegExp("\\$\\{"+key+"\\}",'g'),
text: text
}
@reloadI18nR()
YGOProMessagesHelper = require("./YGOProMessages.js").YGOProMessagesHelper # 为 SRVPro2 准备的库,这里拿这个库只用来测试,SRVPro1 对异步支持不是特别完善,因此不会有很多异步优化
@helper = new YGOProMessagesHelper(9000)
@structs = @helper.structs
@structs_declaration = @helper.structs_declaration
@typedefs = @helper.typedefs
@proto_structs = @helper.proto_structs
@constants = @helper.constants
translateHandler = (handler) ->
return (buffer, info, datas, params)->
await return await handler(buffer, info, params.client, params.server, datas)
@stoc_follow = (proto, synchronous, callback)->
@helper.addHandler("STOC_#{proto}", translateHandler(callback), synchronous, 1)
return
@stoc_follow_before = (proto, synchronous, callback)->
@helper.addHandler("STOC_#{proto}", translateHandler(callback), synchronous, 0)
return
@stoc_follow_after = (proto, synchronous, callback)->
@helper.addHandler("STOC_#{proto}", translateHandler(callback), synchronous, 2)
return
@ctos_follow = (proto, synchronous, callback)->
@helper.addHandler("CTOS_#{proto}", translateHandler(callback), synchronous, 1)
return
@ctos_follow_before = (proto, synchronous, callback)->
@helper.addHandler("CTOS_#{proto}", translateHandler(callback), synchronous, 0)
return
@ctos_follow_after = (proto, synchronous, callback)->
@helper.addHandler("CTOS_#{proto}", translateHandler(callback), synchronous, 2)
return
#消息发送函数,至少要把俩合起来....
@stoc_send = (socket, proto, info)->
return @helper.sendMessage(socket, "STOC_#{proto}", info)
@ctos_send = (socket, proto, info)->
return @helper.sendMessage(socket, "CTOS_#{proto}", info)
#util
@split_chat_lines = (msg, player, lang) ->
lines = []
for line in _.lines(msg)
if player>=10
line="[Server]: "+line
for o,r of @i18nR[lang]
line=line.replace(r.regex, r.text)
lines.push(line)
return lines
@stoc_send_chat = (client, msg, player = 8)->
if !client
console.log "err stoc_send_chat"
return
for line in @split_chat_lines(msg, player, client.lang)
await @stoc_send client, 'CHAT', {
player: player
msg: line
}
return
@stoc_send_chat_to_room = (room, msg, player = 8)->
if !room
console.log "err stoc_send_chat_to_room"
return
for client in room.players
@stoc_send_chat(client, msg, player) if client
for client in room.watchers
@stoc_send_chat(client, msg, player) if client
room.recordChatMessage(msg, player)
return
@stoc_send_hint_card_to_room = (room, card)->
if !room
console.log "err stoc_send_hint_card_to_room"
return
for client in room.players
@stoc_send client, 'GAME_MSG', {
curmsg: 2,
type: 10,
player: 0,
data: card
} if client
for client in room.watchers
@stoc_send client, 'GAME_MSG', {
curmsg: 2,
type: 10,
player: 0,
data: card
} if client
return
@stoc_die = (client, msg)->
await @stoc_send_chat(client, msg, @constants.COLORS.RED)
await @stoc_send client, 'ERROR_MSG', {
msg: 1
code: 9
} if client
if client
client.system_kicked = true
client.destroy()
return '_cancel'