Skip to content

Commit

Permalink
Support canvas expansion + future proofing
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahvdAa committed Jul 21, 2023
1 parent 6b2631e commit 1444ef2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
35 changes: 20 additions & 15 deletions src/CanvasPlacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ export class CanvasPlacer {
infoNotification(lang().TOAST_PLACING_PIXEL);
setHUDBody('');
try {
const canvases = await getCanvasURLS(client, [1, 4]);
const canvases = await getCanvasURLS(client, [1, 2, 4, 5]);
client.placeReference.clearRect(0, 0, client.placeReference.canvas.width, client.placeReference.canvas.height);

await loadURLToCanvas(client.placeReference, canvases[0], 0, -500);
await loadURLToCanvas(client.placeReference, canvases[1], 0, 500);
// todo: shove in array
await Promise.all([
loadURLToCanvas(client.placeReference, canvases[0], 1000, 0),
loadURLToCanvas(client.placeReference, canvases[1], 2000, 0),
loadURLToCanvas(client.placeReference, canvases[2], 1000, 1000),
loadURLToCanvas(client.placeReference, canvases[3], 2000, 1000)
]);

const wrongPixels = getIncorrectPixels(client);
if (wrongPixels.length !== 0) {
Expand All @@ -58,24 +63,24 @@ export class CanvasPlacer {
const pi = PALETTE.indexOf(hex.toUpperCase());
if (pi === -1) continue;

let displayX = pixel[0] + client.orderOffset.x;
let displayY = pixel[1] + client.orderOffset.y;
let canvasX = 0;
let canvasY = 0;
let canvasX = pixel[0];
let canvasY = pixel[1];
let canvas = 0;
canvasX = (displayX + 500) % 1000;
canvasY = (displayY + 1000) % 1000;
if (displayY < 0) {
if (displayX < -500) canvas = 0;
else if (displayX > 499) canvas = 2;
if (canvasY < 1000) {
if (canvasX < 1000) canvas = 0;
else if (canvasX >= 2000) canvas = 2;
else canvas = 1;
} else {
if (displayX < -500) canvas = 3;
else if (displayX > 499) canvas = 5;
if (canvasX < 1000) canvas = 3;
else if (canvasX >= 2000) canvas = 5;
else canvas = 4;
}

let displayX = canvasX - 1500;
let displayY = canvasY - 1000;
canvasX %= 1000;
canvasY %= 1000;
infoNotification(lang().TOAST_PLACING_PIXEL_AT.replace('{x}', displayX).replace('{y}', displayY));

let delay = await placePixel(client, canvasX, canvasY, pi, canvas);
if (typeof delay === 'number') {
this.cooldownEndsAt = delay;
Expand Down
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const client = {
orderReference: createCanvas('placenl-userscript-order-reference'),
orderPriority: createCanvas('placenl-userscript-order-priority'),
placeReference: createCanvas('placenl-userscript-place-reference'),
orderOffset: {x: 0, y: 0},
canvasPlacer: new CanvasPlacer()
};

Expand All @@ -29,8 +28,8 @@ client.ws.connect(client);

function createCanvas(id) {
const canvas = document.createElement('canvas');
canvas.width = 1000;
canvas.height = 1000;
canvas.width = 3000;
canvas.height = 2000;
const ctx = canvas.getContext('2d');
canvas.style.display = 'none';
canvas.id = id;
Expand Down
2 changes: 1 addition & 1 deletion src/util/orderUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function getIncorrectPixels(client) {

for (let y = 0; y < orderReference.height; y++) {
for (let x = 0; x < orderReference.width; x++) {
const i = ((y * orderReference.height) + x) * 4;
const i = ((y * orderReference.width) + x) * 4;
const a = orderReference.data[i + 3];
if (a === 0) continue;

Expand Down
7 changes: 1 addition & 6 deletions src/ws/handler/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ export async function handleOrder(client, payload) {
client.orderReference.clearRect(0, 0, client.orderReference.canvas.width, client.orderReference.canvas.height);
client.orderPriority.clearRect(0, 0, client.orderPriority.canvas.width, client.orderPriority.canvas.height);

// Rescale if necessary
client.orderReference.canvas.width = client.orderPriority.canvas.width = payload.size.width;
client.orderReference.canvas.height = client.orderPriority.canvas.height = payload.size.height;
client.orderOffset = payload.offset ?? {x: 0, y: 0};

await Promise.all([
loadURLToCanvas(client.orderReference, payload.images.order),
loadURLToCanvas(client.orderReference, 'https://chief.placenl.nl/orders/f2fca21c-7ab4-4b56-bcbc-b8601a809cf8.png' ?? payload.images.order, 1500 + payload.offset.x, 1000 + payload.offset.y),
payload.images.priority ? loadURLToCanvas(client.orderPriority, payload.images.priority) : Promise.resolve() // an empty promise - a lie
]);
}

0 comments on commit 1444ef2

Please sign in to comment.