-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
okay, some basic functions appear to exist
- Loading branch information
Showing
6 changed files
with
217 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
|
||
const name = 'blue'; | ||
const secret = 'Secret'; | ||
|
||
//based on https://gist.github.com/tedmiston/5935757 | ||
|
||
const net = require('net'); | ||
|
||
const client = new net.Socket(); | ||
client.connect(15466, '127.0.0.1', function() { | ||
console.log('connected'); | ||
//send handshake: | ||
const data = `g${secret}${name}`; | ||
client.write('H'); | ||
client.write(Buffer.from([data.length])); | ||
client.write(data); | ||
}); | ||
|
||
let recv = Buffer.from([]); | ||
|
||
client.on('data', function(data) { | ||
recv = Buffer.concat([recv, data]); | ||
//console.log('recv is: ' + recv); | ||
while (recv.length >= 2) { | ||
const type = recv.toString('utf8',0,1); | ||
const length = recv[1]; | ||
|
||
//wait for more data: | ||
if (recv.length < 2 + length) break; | ||
|
||
const data = recv.subarray(2, 2+length); | ||
|
||
if (type == 'T') { | ||
const message = data.toString(); | ||
console.log(`Server said: '${message}'`); | ||
} else if (type == 'V') { | ||
const view = data.toString(); | ||
console.assert(view.length === 9); | ||
console.log(`${view[0]}${view[1]}${view[2]}\n${view[3]}${view[4]}${view[5]}\n${view[6]}${view[7]}${view[8]}`); | ||
//random movement: | ||
let moves = []; | ||
if (view[1] == ' ') moves.push('N'); | ||
if (view[3] == ' ') moves.push('W'); | ||
if (view[5] == ' ') moves.push('E'); | ||
if (view[7] == ' ') moves.push('S'); | ||
|
||
const move = moves[Math.floor(Math.random() * moves.length)]; | ||
|
||
console.log(`moving ${move}`); | ||
|
||
client.write('M'); | ||
client.write(Buffer.from([1])); | ||
client.write(move); | ||
} else { | ||
console.log(`Ignoring '${type}' of length ${length}`); | ||
} | ||
|
||
//trim the gotten data: | ||
recv = Buffer.from(recv.subarray(2 + length)); | ||
} | ||
}); | ||
|
||
client.on('close', function() { | ||
console.log('server closed connection'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
# ###### #### # | ||
# # # # # | ||
# # ########### # | ||
# # # # | ||
# # # | ||
# ### #### # | ||
# ### ##### # # | ||
# # | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
red secret R #ff0000 | ||
green secret G #00ff00 | ||
blue secret B #0000ff | ||
green SECRET G #00ff00 | ||
blue Secret B #0000ff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters