Skip to content

Commit 60ff369

Browse files
committed
initClient in handshake
1 parent 234dcc6 commit 60ff369

File tree

2 files changed

+139
-144
lines changed

2 files changed

+139
-144
lines changed

src/client/scripts/index.coffee

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class Game
3232
@canvas=document.querySelector "#c"
3333
@pcanvas=document.querySelector "#c_player"
3434
@dimension=null
35-
@socket=io()
3635
if PRODUCTION
3736
console.log "Running in production mode"
3837
else
@@ -59,6 +58,11 @@ class Game
5958
@nick=RandomNick()
6059
document.location.href="\##{@nick}"
6160

61+
@socket=io {
62+
query:
63+
nick:@nick
64+
}
65+
6266
@stats=new Stats
6367
@drawcalls=@stats.addPanel new Stats.Panel( "calls", "#ff8", "#221" )
6468
@stats.showPanel 0

src/index.coffee

Lines changed: 134 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ module.exports=(mode)->
1313
convert = new Convert()
1414
helmet = require "helmet"
1515

16-
sf={}
17-
socketInfo={}
16+
playerSocket={}
1817
port=process.env.PORT or 8080
1918

2019
app.use helmet()
@@ -26,161 +25,153 @@ module.exports=(mode)->
2625
middleware = require "webpack-dev-middleware"
2726
devconfig=require "#{__dirname}/client/webpack.dev.coffee"
2827
compiler = webpack devconfig
29-
app.use middleware(compiler)
28+
app.use middleware compiler
3029

3130
server.listen port,()->
3231
opn "http://localhost:#{port}"
3332
console.log "Server is running on \x1b[34m*:#{port}\x1b[0m"
3433

3534
io.sockets.on "connection", (socket)->
36-
socketInfo[socket.id]={}
37-
bot=socketInfo[socket.id]
38-
socket.on "initClient",(data)->
39-
console.log "[\x1b[32m+\x1b[0m] #{data.nick}"
35+
query=socket.handshake.query
36+
console.log "[\x1b[32m+\x1b[0m] #{query.nick}"
4037

41-
socketInfo[socket.id]=data
42-
socketInfo[socket.id].bot=mineflayer.createBot {
38+
playerSocket[socket.id]=
39+
bot:mineflayer.createBot {
4340
host: config.ip
4441
port: config.port
45-
username: socketInfo[socket.id].nick
42+
username: query.nick
4643
version: config.version
4744
}
4845

49-
bot=()->
50-
if socketInfo[socket.id] isnt undefined
51-
return socketInfo[socket.id].bot
52-
else
53-
return null
46+
bot=()->
47+
if playerSocket[socket.id] isnt undefined
48+
return playerSocket[socket.id].bot
49+
else
50+
return null
5451

55-
emit=(array)->
56-
io.to(socket.id).emit array...
52+
emit=(array)->
53+
io.to(socket.id).emit array...
5754

58-
bot()._client.on "map_chunk",(packet)->
59-
cell=new Chunk()
60-
cell.load packet.chunkData,packet.bitMap,true,true
61-
# for i in [0..255]
62-
# light=cell.getBlockLight 0, i, 0
63-
# if light isnt 0
64-
# console.log light
65-
# break
66-
emit ["mapChunk", cell.sections,packet.x,packet.z,packet.biomes]
67-
return
68-
bot()._client.on "respawn",(packet)->
69-
emit ["dimension",packet.dimension.value.effects.value]
70-
return
71-
botEventMap=
72-
"heldItemChanged":(item)->
73-
socketInfo[socket.id].held=item
74-
return
75-
"login":()->
76-
emit ["dimension",bot().game.dimension]
77-
return
78-
"move":()->
79-
emit ["move",bot().entity.position]
80-
return
81-
"health":()->
82-
emit ["hp",bot().health]
83-
emit ["food",bot().food]
84-
return
85-
"spawn":()->
86-
emit ["spawn",bot().entity.yaw,bot().entity.pitch]
87-
return
88-
"kicked":(reason,loggedIn)->
89-
emit ["kicked",reason]
90-
return
91-
"message":(msg)->
92-
emit ["msg",convert.toHtml(msg.toAnsi())]
93-
return
94-
"experience":()->
95-
emit ["xp",bot().experience]
96-
return
97-
"blockUpdate":(oldb,newb)->
98-
emit ["blockUpdate",[newb.position.x,newb.position.y,newb.position.z,newb.stateId]]
99-
return
100-
"diggingCompleted":(block)->
101-
emit ["diggingCompleted",block]
102-
return
103-
"diggingAborted":(block)->
104-
emit ["diggingAborted",block]
105-
return
106-
for i of botEventMap
107-
((i)->
108-
socketInfo[socket.id].bot.on i, ()->
109-
if bot() isnt null
110-
botEventMap[i] arguments...
111-
return
112-
)(i)
113-
inv=""
114-
socketInfo[socket.id].int=setInterval ()->
115-
inv_new=JSON.stringify(bot().inventory.slots)
116-
if inv isnt inv_new
117-
inv=inv_new
118-
emit ["inventory",bot().inventory.slots]
119-
entities=[]
120-
for k,v of bot().entities
121-
if v.type is "mob"
122-
entities.push [v.position.x,v.position.y,v.position.z]
123-
emit ["entities",entities]
124-
return
125-
,10
126-
socketEventMap=
127-
"blockPlace":(pos,vec)->
128-
block=bot().blockAt(new vec3(pos...))
129-
vecx=[
130-
[1,0,0]
131-
[-1,0,0]
132-
[0,1,0]
133-
[0,-1,0]
134-
]
135-
if socketInfo[socket.id].held isnt undefined and socketInfo[socket.id].held isnt null
136-
console.log socketInfo[socket.id].held
137-
bot().placeBlock block,new vec3(vec...),(r)->
138-
console.log r
139-
return
140-
return
141-
"invc":(num)->
142-
item=bot().inventory.slots[num+36]
143-
if item isnt null and item isnt undefined
144-
bot().equip item,"hand"
145-
else if socketInfo[socket.id].held isnt undefined
146-
bot().unequip "hand"
147-
return
148-
"move":(state,toggle)->
149-
bot().setControlState(state,toggle)
150-
return
151-
"command":(com)->
152-
bot().chat(com)
153-
return
154-
"rotate":(data)->
155-
bot().look data...
156-
return
157-
"disconnect":()->
158-
try
159-
clearInterval socketInfo[socket.id].int
160-
console.log "[\x1b[31m-\x1b[0m] #{socketInfo[socket.id].nick}"
161-
socketInfo[socket.id].bot.end()
162-
delete socketInfo[socket.id]
163-
return
164-
"dig":(pos)->
165-
block=bot().blockAt(vec3(pos[0],pos[1]-16,pos[2]))
166-
if block isnt null
167-
digTime=bot().digTime(block)
168-
if bot().targetDigBlock isnt null
169-
console.log "Already digging..."
170-
bot().stopDigging()
171-
emit ["digTime",digTime,block]
172-
console.log "Start"
173-
bot().dig block,false,(xd)->
174-
if xd is undefined
175-
console.log "SUCCESS"
176-
else
177-
console.log "FAIL"
178-
return
179-
"stopDigging":(callback)->
180-
bot().stopDigging()
181-
return
182-
for i of socketEventMap
183-
socket.on i,socketEventMap[i]
55+
bot()._client.on "map_chunk",(packet)->
56+
cell=new Chunk()
57+
cell.load packet.chunkData,packet.bitMap,true,true
58+
emit ["mapChunk", cell.sections,packet.x,packet.z,packet.biomes]
59+
return
60+
bot()._client.on "respawn",(packet)->
61+
emit ["dimension",packet.dimension.value.effects.value]
18462
return
63+
botEventMap=
64+
"heldItemChanged":(item)->
65+
playerSocket[socket.id].held=item
66+
return
67+
"login":()->
68+
emit ["dimension",bot().game.dimension]
69+
return
70+
"move":()->
71+
emit ["move",bot().entity.position]
72+
return
73+
"health":()->
74+
emit ["hp",bot().health]
75+
emit ["food",bot().food]
76+
return
77+
"spawn":()->
78+
emit ["spawn",bot().entity.yaw,bot().entity.pitch]
79+
return
80+
"kicked":(reason,loggedIn)->
81+
emit ["kicked",reason]
82+
return
83+
"message":(msg)->
84+
emit ["msg",convert.toHtml(msg.toAnsi())]
85+
return
86+
"experience":()->
87+
emit ["xp",bot().experience]
88+
return
89+
"blockUpdate":(oldb,newb)->
90+
emit ["blockUpdate",[newb.position.x,newb.position.y,newb.position.z,newb.stateId]]
91+
return
92+
"diggingCompleted":(block)->
93+
emit ["diggingCompleted",block]
94+
return
95+
"diggingAborted":(block)->
96+
emit ["diggingAborted",block]
97+
return
98+
for i of botEventMap
99+
((i)->
100+
playerSocket[socket.id].bot.on i, ()->
101+
if bot() isnt null
102+
botEventMap[i] arguments...
103+
return
104+
)(i)
105+
inv=""
106+
playerSocket[socket.id].int=setInterval ()->
107+
inv_new=JSON.stringify(bot().inventory.slots)
108+
if inv isnt inv_new
109+
inv=inv_new
110+
emit ["inventory",bot().inventory.slots]
111+
entities=[]
112+
for k,v of bot().entities
113+
if v.type is "mob"
114+
entities.push [v.position.x,v.position.y,v.position.z]
115+
emit ["entities",entities]
116+
return
117+
,10
118+
socketEventMap=
119+
"blockPlace":(pos,vec)->
120+
block=bot().blockAt(new vec3(pos...))
121+
vecx=[
122+
[1,0,0]
123+
[-1,0,0]
124+
[0,1,0]
125+
[0,-1,0]
126+
]
127+
if playerSocket[socket.id].held isnt undefined and playerSocket[socket.id].held isnt null
128+
console.log playerSocket[socket.id].held
129+
bot().placeBlock block,new vec3(vec...),(r)->
130+
console.log r
131+
return
132+
return
133+
"invc":(num)->
134+
item=bot().inventory.slots[num+36]
135+
if item isnt null and item isnt undefined
136+
bot().equip item,"hand"
137+
else if playerSocket[socket.id].held isnt undefined
138+
bot().unequip "hand"
139+
return
140+
"move":(state,toggle)->
141+
bot().setControlState(state,toggle)
142+
return
143+
"command":(com)->
144+
bot().chat(com)
145+
return
146+
"rotate":(data)->
147+
bot().look data...
148+
return
149+
"disconnect":()->
150+
try
151+
clearInterval playerSocket[socket.id].int
152+
console.log "[\x1b[31m-\x1b[0m] #{query.nick}"
153+
playerSocket[socket.id].bot.end()
154+
delete playerSocket[socket.id]
155+
return
156+
"dig":(pos)->
157+
block=bot().blockAt(vec3(pos[0],pos[1]-16,pos[2]))
158+
if block isnt null
159+
digTime=bot().digTime(block)
160+
if bot().targetDigBlock isnt null
161+
console.log "Already digging..."
162+
bot().stopDigging()
163+
emit ["digTime",digTime,block]
164+
console.log "Start"
165+
bot().dig block,false,(xd)->
166+
if xd is undefined
167+
console.log "SUCCESS"
168+
else
169+
console.log "FAIL"
170+
return
171+
"stopDigging":(callback)->
172+
bot().stopDigging()
173+
return
174+
for i of socketEventMap
175+
socket.on i,socketEventMap[i]
185176
return
186177
return

0 commit comments

Comments
 (0)