Skip to content

Commit dd23c16

Browse files
committed
🚿 clean up
1 parent 7377068 commit dd23c16

File tree

11 files changed

+102
-114
lines changed

11 files changed

+102
-114
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# snk
22

3+
![type definitions](https://img.shields.io/npm/types/typescript?style=flat-square)
4+
![code style](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)
5+
36
![](https://raw.githubusercontent.com/Platane/snk/output/github-contribution-grid-snake.gif)
47

58
Generates a snake game from a github user contributions grid and output a screen capture as gif
69

7-
---
10+
- [demo](https://platane.github.io/snk/index.html)
811

9-
[demo](https://platane.github.io/snk/index.html)
12+
- [github action](https://github.com/marketplace/actions/generate-snake-game-from-github-contribution-grid)

packages/compute/generateGrid.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { Grid, Color } from "./grid";
33
const rand = (a: number, b: number) => Math.floor(Math.random() * (b - a)) + a;
44

55
export const generateEmptyGrid = (width: number, height: number) =>
6-
generateGrid(width, height, { colors: [], emptyP: 1 });
6+
generateRandomGrid(width, height, { colors: [], emptyP: 1 });
77

8-
export const generateGrid = (
8+
export const generateRandomGrid = (
99
width: number,
1010
height: number,
1111
options: { colors: Color[]; emptyP: number } = {

packages/compute/snake.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Point } from "./point";
22

3+
export type Snake = Point[];
4+
35
export const snakeSelfCollideNext = (
4-
snake: Point[],
6+
snake: Snake,
57
direction: Point,
68
options: { maxSnakeLength: number }
79
) => {
@@ -14,11 +16,11 @@ export const snakeSelfCollideNext = (
1416
return false;
1517
};
1618

17-
export const snakeSelfCollide = (snake: Point[]) => {
19+
export const snakeSelfCollide = (snake: Snake) => {
1820
for (let i = 1; i < snake.length; i++)
1921
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) return true;
2022

2123
return false;
2224
};
2325

24-
export const copySnake = (x: Point[]) => x.map((p) => ({ ...p }));
26+
export const copySnake = (x: Snake) => x.map((p) => ({ ...p }));

packages/demo/index.ts

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,89 @@
1-
// import { generateGrid } from "@snk/compute/generateGrid";
2-
3-
import { generateGrid } from "@snk/compute/generateGrid";
1+
import { generateRandomGrid } from "@snk/compute/generateGrid";
42
import { Color, copyGrid } from "@snk/compute/grid";
53
import { computeBestRun } from "@snk/compute";
64
import { step } from "@snk/compute/step";
75
import { drawWorld } from "@snk/draw/drawWorld";
8-
import { Point } from "@snk/compute/point";
9-
10-
const copySnake = (x: Point[]) => x.map((p) => ({ ...p }));
11-
12-
export const run = async () => {
13-
const drawOptions = {
14-
sizeBorderRadius: 2,
15-
sizeCell: 16,
16-
sizeDot: 12,
17-
colorBorder: "#1b1f230a",
18-
colorDots: { 1: "#9be9a8", 2: "#40c463", 3: "#30a14e", 4: "#216e39" },
19-
colorEmpty: "#ebedf0",
20-
colorSnake: "purple",
21-
};
22-
23-
const gameOptions = { maxSnakeLength: 5 };
6+
import { copySnake } from "@snk/compute/snake";
7+
8+
const drawOptions = {
9+
sizeBorderRadius: 2,
10+
sizeCell: 16,
11+
sizeDot: 12,
12+
colorBorder: "#1b1f230a",
13+
colorDots: { 1: "#9be9a8", 2: "#40c463", 3: "#30a14e", 4: "#216e39" },
14+
colorEmpty: "#ebedf0",
15+
colorSnake: "purple",
16+
};
2417

25-
const grid0 = generateGrid(42, 7, { colors: [1, 2, 3, 4], emptyP: 3 });
18+
const gameOptions = { maxSnakeLength: 5 };
2619

27-
const snake0 = [
28-
{ x: 4, y: -1 },
29-
{ x: 3, y: -1 },
30-
{ x: 2, y: -1 },
31-
{ x: 1, y: -1 },
32-
{ x: 0, y: -1 },
33-
];
34-
const stack0: Color[] = [];
20+
const grid0 = generateRandomGrid(42, 7, { colors: [1, 2, 3, 4], emptyP: 3 });
3521

36-
const chain = computeBestRun(grid0, snake0, gameOptions);
22+
const snake0 = [
23+
{ x: 4, y: -1 },
24+
{ x: 3, y: -1 },
25+
{ x: 2, y: -1 },
26+
{ x: 1, y: -1 },
27+
{ x: 0, y: -1 },
28+
];
29+
const stack0: Color[] = [];
3730

38-
const canvas = document.createElement("canvas");
39-
canvas.width = drawOptions.sizeCell * (grid0.width + 4);
40-
canvas.height = drawOptions.sizeCell * (grid0.height + 4) + 100;
41-
document.body.appendChild(canvas);
42-
const ctx = canvas.getContext("2d")!;
31+
const chain = computeBestRun(grid0, snake0, gameOptions);
4332

44-
const update = (n: number) => {
45-
const snake = copySnake(snake0);
46-
const stack = stack0.slice();
47-
const grid = copyGrid(grid0);
33+
//
34+
// draw
4835

49-
for (let i = 0; i < n; i++) step(grid, snake, stack, chain[i], gameOptions);
36+
const canvas = document.createElement("canvas");
37+
canvas.width = drawOptions.sizeCell * (grid0.width + 4);
38+
canvas.height = drawOptions.sizeCell * (grid0.height + 4) + 100;
39+
document.body.appendChild(canvas);
40+
const ctx = canvas.getContext("2d")!;
5041

51-
ctx.clearRect(0, 0, 9999, 9999);
52-
drawWorld(ctx, grid, snake, stack, drawOptions);
53-
};
42+
const update = (n: number) => {
43+
const snake = copySnake(snake0);
44+
const stack = stack0.slice();
45+
const grid = copyGrid(grid0);
5446

55-
const input: any = document.createElement("input");
56-
input.type = "range";
57-
input.style.width = "100%";
58-
input.min = 0;
59-
input.max = chain.length;
60-
input.step = 1;
61-
input.value = 0;
62-
input.addEventListener("input", () => update(+input.value));
63-
document.addEventListener("click", () => input.focus());
47+
for (let i = 0; i < n; i++) step(grid, snake, stack, chain[i], gameOptions);
6448

65-
document.body.appendChild(input);
49+
ctx.clearRect(0, 0, 9999, 9999);
50+
drawWorld(ctx, grid, snake, stack, drawOptions);
51+
};
6652

53+
//
54+
// controls
55+
56+
const input: any = document.createElement("input");
57+
input.type = "range";
58+
input.style.width = "100%";
59+
input.min = 0;
60+
input.max = chain.length;
61+
input.step = 1;
62+
input.value = 0;
63+
input.addEventListener("input", () => {
64+
setAutoPlay(false);
6765
update(+input.value);
66+
});
67+
document.addEventListener("click", () => input.focus());
6868

69-
// while (chain.length) {
70-
// await wait(100);
71-
72-
// step(grid, snake, stack, chain.shift()!, gameOptions);
73-
74-
// ctx.clearRect(0, 0, 9999, 9999);
75-
// drawWorld(ctx, grid, snake, stack, options);
76-
// }
69+
document.body.appendChild(input);
7770

78-
// const wait = (delay = 0) => new Promise((r) => setTimeout(r, delay));
71+
const autoplayButton = document.createElement("button");
72+
let cancel: any;
73+
const loop = () => {
74+
input.value = (1 + input.value) % +input.max;
75+
update(+input.value);
76+
cancelAnimationFrame(cancel);
77+
cancel = requestAnimationFrame(loop);
78+
};
79+
const setAutoPlay = (a: boolean) => {
80+
autoplayButton.innerHTML = a ? "pause ⏸" : "play ▶";
81+
if (a) loop();
82+
else cancelAnimationFrame(cancel);
7983
};
84+
autoplayButton.addEventListener("click", () =>
85+
setAutoPlay(autoplayButton.innerHTML === "pause ⏸")
86+
);
8087

81-
run();
88+
setAutoPlay(true);
89+
update(+input.value);

packages/demo/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "@snk/demo",
33
"version": "1.0.0",
44
"dependencies": {
5-
"@snk/compute": "1.0.0"
5+
"@snk/compute": "1.0.0",
6+
"@snk/draw": "1.0.0"
67
},
78
"devDependencies": {
89
"webpack": "4.43.0",

packages/demo/webpack.config.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const config: Configuration = {
2727
],
2828
},
2929
plugins: [
30-
// game
3130
new HtmlWebpackPlugin({
3231
title: "demo",
3332
filename: "index.html",
@@ -39,9 +38,6 @@ const config: Configuration = {
3938

4039
devtool: false,
4140
stats: "errors-only",
42-
43-
// @ts-ignore
44-
devServer: {},
4541
};
4642

4743
export default config;

packages/gif-creator/__tests__/createGif.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createGif } from "..";
2-
import { generateGrid } from "@snk/compute/generateGrid";
2+
import { generateRandomGrid } from "@snk/compute/generateGrid";
33
import { computeBestRun } from "@snk/compute";
44

55
const drawOptions = {
@@ -17,7 +17,7 @@ const gameOptions = { maxSnakeLength: 5 };
1717
const gifOptions = { delay: 200 };
1818

1919
it("should generate gif", async () => {
20-
const grid = generateGrid(14, 7, { colors: [1, 2, 3, 4], emptyP: 3 });
20+
const grid = generateRandomGrid(14, 7, { colors: [1, 2, 3, 4], emptyP: 3 });
2121

2222
const snake = [
2323
{ x: 4, y: -1 },

packages/gif-creator/__tests__/dev.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createGif } from "..";
2-
import { generateGrid } from "@snk/compute/generateGrid";
2+
import { generateRandomGrid } from "@snk/compute/generateGrid";
33
import { computeBestRun } from "@snk/compute";
44

55
const drawOptions = {
@@ -16,7 +16,7 @@ const gameOptions = { maxSnakeLength: 5 };
1616

1717
const gifOptions = { delay: 20 };
1818

19-
const grid = generateGrid(42, 7, { colors: [1, 2, 3, 4], emptyP: 3 });
19+
const grid = generateRandomGrid(42, 7, { colors: [1, 2, 3, 4], emptyP: 3 });
2020

2121
const snake = [
2222
{ x: 4, y: -1 },

packages/gif-creator/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { copySnake } from "@snk/compute/snake";
77
import { drawWorld } from "@snk/draw/drawWorld";
88
import { step } from "@snk/compute/step";
99
import * as tmp from "tmp";
10-
// @ts-ignore
1110
import * as execa from "execa";
1211

1312
export const createGif = async (

packages/github-user-contribution/__tests__/getGithubUserContribution.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getGithubUserContribution } from "..";
33
it("should get user contribution", async () => {
44
const { cells, colorScheme } = await getGithubUserContribution("platane");
55

6-
expect(cells).toBeDefined();
6+
expect(cells.length).toBeGreaterThan(300);
77
expect(colorScheme).toEqual([
88
"#ebedf0",
99
"#9be9a8",

0 commit comments

Comments
 (0)