-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.user.js
144 lines (127 loc) · 4.65 KB
/
snake.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// ==UserScript==
// @name GitHub Contributions Snake
// @namespace http://tampermonkey.net/
// @version 2024-07-19
// @description try to take over the world!
// @author GV3Dev
// @match https://github.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant none
// ==/UserScript==
class SnakeElem {
constructor() {
this.body = [{x: 0, y: 0}];
this.length = 1;
this.foodEaten = 0;
this.direction = "right";
}
createSnake(cells, columns) {
for (let i = 0; i < this.length; i++) {
const pos = this.body[i];
makeGreen(cells[pos.y * columns + pos.x], 4);
}
}
move(direction, cells, columns, rows, food) {
const head = { ...this.body[0] };
switch (direction) {
case "left":
if (this.direction !== "right") this.direction = "left";
break;
case "right":
if (this.direction !== "left") this.direction = "right";
break;
case "down":
if (this.direction !== "up") this.direction = "down";
break;
case "up":
if (this.direction !== "down") this.direction = "up";
break;
}
switch (this.direction) {
case "left":
head.x = (head.x - 1 + columns) % columns;
break;
case "right":
head.x = (head.x + 1) % columns;
break;
case "down":
head.y = (head.y + 1) % rows;
break;
case "up":
head.y = (head.y - 1 + rows) % rows;
break;
}
this.body.unshift(head);
if (head.x === food.position.x && head.y === food.position.y) {
this.foodEaten++;
this.length += 2;
food.getEaten(cells, this, columns, rows);
} else {
const tail = this.body.pop();
cells[tail.y * columns + tail.x].setAttribute("data-level", "0");
}
this.createSnake(cells, columns);
}
}
class FoodElem {
constructor() {
this.position = null;
}
getEaten(cells, snake, columns, rows) {
cells[this.position.y * columns + this.position.x].style.backgroundColor = "#161b22";
this.createFood(cells, columns, rows, snake);
}
createFood(cells, columns, rows, snake) {
let randCell;
let x, y;
do {
randCell = Math.floor(Math.random() * cells.length);
y = Math.floor(randCell / columns);
x = randCell % columns;
} while (cells[randCell].getAttribute("data-level") !== "0" || snake.body.some(part => part.x === x && part.y === y));
cells[y * columns + x].style.backgroundColor = "red";
this.position = {x, y};
}
}
const main = () => {
const keepCheck = setInterval(() => {
const contribBoard = document.querySelector(".js-yearly-contributions");
if (contribBoard !== null) {
clearInterval(keepCheck);
let cells = contribBoard.querySelectorAll(`.ContributionCalendar-day`);
let columns = contribBoard.querySelector(`.js-calendar-graph-table tbody`).firstElementChild.children.length;
let rows = Math.floor(cells.length / columns+1);
cleanGrid();
const snake = new SnakeElem();
const food = new FoodElem();
food.createFood(cells, columns, rows, snake);
snake.createSnake(cells, columns);
appendControls(snake, cells, columns, rows, food);
}
}, 100);
}
const appendControls = (snake, cells, columns, rows, food) => {
window.addEventListener("keydown", (evt) => {
if (["ArrowUp", "ArrowRight", "ArrowLeft", "ArrowDown"].includes(evt.code)) {
evt.preventDefault();
}
if (evt.code === "ArrowUp") {
snake.move("up", cells, columns, rows, food);
} else if (evt.code === "ArrowRight") {
snake.move("right", cells, columns, rows, food);
} else if (evt.code === "ArrowLeft") {
snake.move("left", cells, columns, rows, food);
} else if (evt.code === "ArrowDown") {
snake.move("down", cells, columns, rows, food);
}
});
}
function makeGreen(elem, level) {
elem.setAttribute("data-level", `${level}`);
}
function cleanGrid() {
const contribBoard = document.querySelector(".js-yearly-contributions");
let cells = contribBoard.querySelectorAll(`.ContributionCalendar-day`);
cells.forEach((cell) => { cell.setAttribute("data-level", "0"); });
}
main();