Created
December 16, 2016 21:34
-
-
Save popey456963/6037511e658dab6d173289ce3ead0518 to your computer and use it in GitHub Desktop.
A JS Bot
This file contains hidden or 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
var socket = require('socket.io-client')('http://localhost:8080', { transports: ['websocket'] }) | |
var chalk = require('chalk') | |
var listen_events = ['queue_update', 'game_update'] | |
socket.on('connect', function() { | |
console.log('Connected Successfully') | |
socket.emit('play', 'Beep Boop', 'Sy5tdf37g') | |
socket.emit('set_force_start', null, true) | |
}) | |
var past_players = 0 | |
socket.on('queue_update', function(data) { | |
if (data != past_players) { | |
console.log('We have ' + data + ' people in our current queue.') | |
past_players = data | |
} | |
}) | |
var player_index = 0 | |
socket.on('game_start', function(data) { | |
console.log("===== A game has started! =====") | |
console.log("Player Index: " + data.playerIndex) | |
console.log("Replay ID: " + data.replay_id) | |
console.log("Players: " + data.usernames.join(", ")) | |
player_index = data.playerIndex | |
}) | |
var c = { | |
"E": chalk.gray, | |
"M": chalk.white, | |
"?": chalk.yellow, | |
"F": chalk.green, | |
"Bad": chalk.red, | |
"FG": chalk.blue, | |
"EG": chalk.magenta, | |
"V": chalk.cyan | |
} | |
var width, height, general | |
socket.on('game_update', function(data) { | |
console.log("===== Game Turn " + data.turn + " =====") | |
width = parseInt(data.map[0]) | |
height = parseInt(data.map[1]) | |
var map_data = data.map.slice(2, 2 + width * height) | |
var map = [] | |
var mountain_data = data.map.slice(2 + width * height, 3 + 2 * width * height) | |
var counter = 0 | |
for (var j = 0; j < height; j++) { | |
var line = [] | |
for (var i = 0; i < width; i++) { | |
line.push([map_data[counter]]) | |
counter++ | |
} | |
map.push(line) | |
} | |
var counter = 0 | |
var y = 0 | |
for (var j = 0; j < height; j++) { | |
var x = 0 | |
for (var i = 0; i < width; i++) { | |
if (mountain_data[counter] == -1) { | |
map[y][x].push("E") | |
} else if (mountain_data[counter] == -2 || mountain_data[counter] == -4) { | |
map[y][x].push("M") | |
} else if (mountain_data[counter] == -3) { | |
map[y][x].push("?") | |
} else if (mountain_data[counter] == player_index) { | |
map[y][x].push("F") | |
} else { | |
map[y][x].push("Bad") | |
} | |
counter++ | |
x++ | |
} | |
y++ | |
} | |
map[Math.floor(data.generals[player_index] / width)][data.generals[player_index] % width][1] = "FG" | |
general = [Math.floor(data.generals[player_index] / width), data.generals[player_index] % width] | |
for (var i = 0; i < data.generals.length; i++) { | |
if (data.generals[i] != -1 && i != player_index) { | |
map[Math.floor(data.generals[i] / width)][data.generals[i] % width][1] = "EG" | |
} | |
} | |
for (var i = 0; i < data.cities.length; i++) { | |
if (data.cities[i] != -1) { | |
map[Math.floor(data.cities[i] / width)][data.cities[i] % width][1] = "V" | |
} | |
} | |
makeMove(map) | |
printMap(map) | |
}) | |
var attackIndex = 0 | |
function attack(from, to, is50) { | |
socket.emit('attack', from, to, is50, attackIndex) | |
attackIndex++ | |
} | |
var moves = [[0, 1], [0, -1], [-1, 0], [1, 0]] | |
var expand = ["E", "Bad"] | |
var reinforce = ["F"] | |
function makeMove(map) { | |
for (var y = 0; y < map.length; y++) { | |
for (var x = 0; x < map[y].length; x++) { | |
if (map[y][x][1] == "F" || map[y][x][1] == "FG") { | |
// console.log("Found friendly at " + x + ", " + y) | |
for (var i = 0; i < moves.length; i++) { | |
if (y + moves[i][0] >= height || y + moves[i][0] < 0) { | |
// console.log("Move Y out of bounds") | |
continue | |
} | |
if (x + moves[i][1] >= width || x + moves[i][1] < 0) { | |
// console.log("Move X out of bounds") | |
continue | |
} | |
if (expand.indexOf(map[y + moves[i][0]][x + moves[i][1]][1]) != -1) { | |
// console.log("Right type to reinforce") | |
if (map[y][x][0] - 1 > map[y + moves[i][0]][x + moves[i][1]][0]) { | |
var point = xyToPoint(x, y) | |
var attackPoint = xyToPoint(x + moves[i][1], y + moves[i][0]) | |
console.log("Attacking " + attackPoint + " from " + point) | |
attack(point, attackPoint, false) | |
// ATTACK IT! | |
return | |
} | |
} | |
} | |
moves = shuffle(moves) | |
} | |
} | |
} | |
reinforceInstead(map) | |
} | |
function shuffle(array) { | |
var currentIndex = array.length, temporaryValue, randomIndex; | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
// And swap it with the current element. | |
temporaryValue = array[currentIndex]; | |
array[currentIndex] = array[randomIndex]; | |
array[randomIndex] = temporaryValue; | |
} | |
return array; | |
} | |
function reinforceInstead(map) { | |
var friendlies = [] | |
for (var y = 0; y < map.length; y++) { | |
for (var x = 0; x < map[y].length; x++) { | |
if (map[y][x][1] == "F" || map[y][x][1] == "FG") { | |
friendlies.push([x, y, map[y][x][0]]) | |
// console.log("Found friendly at " + x + ", " + y) | |
} | |
} | |
} | |
friendlies.sort(function(a, b) { | |
return b[2] - a[2] | |
}) | |
for (var p = 0; p < friendlies.length; p++) { | |
console.log("Testing Frindly: ", friendlies[p]) | |
x = friendlies[p][0] | |
y = friendlies[p][1] | |
for (var i = 0; i < moves.length; i++) { | |
if (y + moves[i][0] >= height || y + moves[i][0] < 0) { | |
// console.log("Move Y out of bounds") | |
continue | |
} | |
if (x + moves[i][1] >= width || x + moves[i][1] < 0) { | |
// console.log("Move X out of bounds") | |
continue | |
} | |
if (reinforce.indexOf(map[y + moves[i][0]][x + moves[i][1]][1]) != -1) { | |
// console.log("Right type to reinforce") | |
var distanceFirst = Math.sqrt(Math.pow(x - general[0], 2) + Math.pow(y - general[1], 2)) | |
var distanceSecond = Math.sqrt(Math.pow(x + moves[i][1] - general[0], 2) + Math.pow(y + moves[i][0] - general[1], 2)) | |
if (distanceFirst < distanceSecond) { | |
// console.log("Right distance!") | |
var point = xyToPoint(x, y) | |
var attackPoint = xyToPoint(x + moves[i][1], y + moves[i][0]) | |
console.log("Reinforcing units out to " + attackPoint + " from " + point) | |
attack(point, attackPoint, false) | |
// REINFORCE IT! | |
return | |
} | |
} | |
} | |
moves = shuffle(moves) | |
console.log(moves) | |
} | |
} | |
function xyToPoint(x, y) { | |
return y * width + x | |
} | |
function printMap(map) { | |
for (var i = 0; i < map.length; i++) { | |
for (var j = 0; j < map[i].length; j++) { | |
if (map[i][j][0] != 0) { | |
map[i][j] = c[map[i][j][1]]((String(map[i][j][0]) + " ").substring(0, 4)) | |
} | |
else { | |
map[i][j] = c[map[i][j][1]]((String(map[i][j][1]) + " ").substring(0, 4)) | |
} | |
} | |
console.log(map[i].join("")) | |
} | |
} | |
socket.on('disconnect', function(){ | |
console.log('Disconnected.') | |
}) | |
var onevent = socket.onevent | |
socket.onevent = function (packet) { | |
var args = packet.data || [] | |
onevent.call (this, packet) // original call | |
packet.data = ["*"].concat(args) | |
onevent.call(this, packet) // additional call to catch-all | |
} | |
socket.on("*", function(event, data) { | |
if (listen_events.indexOf(event) == -1) { | |
console.log("We got an event of type: " + event) | |
console.log("It sent us the following data: " + JSON.stringify(data)) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment