Skip to content

Instantly share code, notes, and snippets.

@Xiaoy312
Created April 3, 2017 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xiaoy312/314e20599cb7bb36116305345cc7e6d9 to your computer and use it in GitHub Desktop.
Save Xiaoy312/314e20599cb7bb36116305345cc7e6d9 to your computer and use it in GitHub Desktop.
<Query Kind="Statements">
<Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>System.Drawing</Namespace>
<Namespace>System.Net.Http</Namespace>
</Query>
var source = "https://images.discordapp.net/.eJwNyFEOgyAMANC7cAA6LZbqbQgSJFFLaJd9LLv7fJ_v697jdJs7zLpuAHvTLGP3ajJSLb6K1LOk3tRnuSCZpXxc5TaFeY08IYeAExISU3iKMTIv9IpICz8Jn9Z9v6v7_QF_uiFA.o-3SRpCgiqN-u8fJNYzhi-0pRc4";
var client = new HttpClient();
var stream = await client.GetStreamAsync(source);
var image = (Bitmap)Image.FromStream(stream);
// $('.place-swatch').map(function(i,x) { return x.style['background-color'].replace(/rgb\((.+)\)/, '$1'); })
var colorIndexes = new[]
{
"255, 255, 255",
"228, 228, 228",
"136, 136, 136",
"34, 34, 34",
"255, 167, 209",
"229, 0, 0",
"229, 149, 0",
"160, 106, 66",
"229, 217, 0",
"148, 224, 68",
"2, 190, 1",
"0, 211, 221",
"0, 131, 199",
"0, 0, 234",
"207, 110, 228",
"130, 0, 128"
}
.Select(x => x.Split(',').Select(int.Parse).ToArray())
.Select((x, i) => new
{
Index = i,
Color = Color.FromArgb(255, x[0], x[1], x[2])
})
.ToDictionary(x => x.Color, x => x.Index);
var map = new int[image.Height, image.Width];
for (int x = 0; x < image.Width; x++)
for (int y = 0; y < image.Height; y++)
{
map[y, x] = colorIndexes[image.GetPixel(x, y)];
}
//var json = Newtonsoft.Json.JsonConvert.SerializeObject(map)
// .Replace("[[", "[\n\t[")
// .Replace("],", "],\n\t")
// .Replace("]]", "]\n]");
$@"
var image = [{string.Join(",", map.Cast<int>())}];
var target = {{
x: 725, y: 240,
width: {image.Width},
height: {image.Height},
}};
".Trim().Dump();
var image = [3,3,3,3,3,3,3,3,3,3,3,3,15,15,15,15,15,15,14,14,14,14,14,4,4,14,14,14,14,4,4,4,4,4,4,4,14,14,15,15,14,14,14,15,15,15,15,15,15,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,15,3,3,3,3,15,15,14,15,15,15,14,14,14,14,14,4,4,14,14,14,3,3,3,3,3,3,4,4,14,14,14,15,14,4,14,15,15,4,15,15,15,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,15,3,3,3,3,15,3,15,3,3,15,15,14,15,15,14,14,14,14,14,4,4,4,14,14,3,3,8,8,8,8,3,3,4,4,14,14,15,14,4,14,14,15,15,4,15,15,3,3,15,3,3,3,3,3,3,3,3,15,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,15,3,3,15,15,14,15,15,14,14,14,14,14,4,4,4,14,14,3,8,8,12,12,6,6,3,4,4,14,14,14,14,4,4,14,15,15,4,15,15,15,3,15,3,15,3,3,3,15,3,3,15,3,3,3,3,3,3,7,3,7,3,7,3,3,3,3,3,3,3,3,3,3,3,3,3,3,15,3,15,15,14,14,15,15,14,14,14,14,4,4,4,14,14,14,3,8,12,11,12,12,6,3,4,4,4,14,14,14,14,4,14,14,15,4,4,15,15,15,15,3,3,3,3,3,3,3,3,3,15,3,3,3,7,3,7,7,7,7,7,3,7,3,3,3,3,3,3,3,3,3,15,3,3,15,15,15,3,3,3,3,3,3,14,14,4,4,4,4,3,3,3,3,6,12,12,12,12,6,3,3,3,3,4,14,14,14,4,4,3,3,3,3,3,3,15,15,15,3,3,3,3,3,3,3,3,3,3,3,3,3,7,2,3,3,3,2,7,3,3,3,3,3,3,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8,3,3,8,3,8,3,3,3,14,4,4,15,15,3,3,3,6,3,7,7,7,7,7,3,6,3,3,3,3,3,15,15,15,4,3,7,7,8,8,7,8,7,8,7,8,7,7,8,7,7,8,7,8,7,8,7,7,8,7,8,7,8,7,8,8,7,7,7,8,7,7,8,7,8,7,7,8,7,7,8,8,8,7,7,3,14,4,4,15,15,3,3,3,3,3,6,3,6,3,6,3,3,3,3,3,3,3,15,15,15,4,3,3,3,8,3,3,8,8,8,3,8,3,3,8,3,3,8,3,8,3,8,8,3,8,3,3,3,8,3,8,3,8,3,3,8,3,3,8,8,8,3,3,8,3,3,8,3,8,3,3,3,14,4,15,15,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,15,15,15,14,3,7,8,7,7,8,7,8,7,8,8,7,8,8,7,8,8,8,7,8,7,7,8,8,8,8,8,7,8,7,8,7,8,8,8,7,8,7,8,7,7,8,7,7,8,7,8,7,3,14,14,4,15,15,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3];
var target = {
x: 725, y: 277,
width: 77,
height: 37,
};
// credit: https://gist.githubusercontent.com/Nemilar/
var colorsABGR = [];
var placed = 0;
// hooks
var client;
var canvasse;
var jQuery;
var test = 0;
r.placeModule("Flag", function(e){
client = e("client");
canvasse = e("canvasse");
jQuery = e("jQuery");
for(var i=0; i<client.palette.length; i++){
colorsABGR[i] = client.getPaletteColorABGR(i);
}
// Start
if(!test){
attempt();
} else {
testDraw();
}
});
function attempt(){
var incorrectPixels = [];
var toWait = client.getCooldownTimeRemaining();
if(toWait===0){
for(var i=0; i<image.length; i++){
if(image[i] === -1){
continue;
}
var targetPoint = getPoint(i);
var pixelColor = getPixel(targetPoint.x, targetPoint.y);
if(pixelColor !== colorsABGR[image[i]]){
incorrectPixels = incorrectPixels.concat(i);
console.log("Identified a wrong pixel, #" + i);
}
}
var pixToFix = incorrectPixels[Math.floor(Math.random() * incorrectPixels.length)];
console.log("Found " + incorrectPixels.length + " wrong tiles. Fixing one (# " + pixToFix + ")");
var targetPoint = getPoint(pixToFix);
client.setColor(image[pixToFix]);
client.drawTile(targetPoint.x, targetPoint.y);
console.log('Pixel Placed at: (' + targetPoint.x + ',' + targetPoint.y + ')');
}
setTimeout(attempt, toWait + Math.round(Math.random() * 1500));
}
function testDraw(){
for(var i=0; i<image.length; i++){
if(image[i] === -1){
continue;
}
var targetPoint = getPoint(i);
canvasse.drawTileAt(targetPoint.x, targetPoint.y, colorsABGR[image[i]]);
}
}
function getPoint(i){
var x = i % target.width;
return {
x: target.x + x,
y: target.y + (i - x) / target.width - target.height
};
}
function getPixel(x, y){
return canvasse.writeBuffer[canvasse.getIndexFromCoords(x, y)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment