Skip to content

Commit

Permalink
Support moons in LoathingDate
Browse files Browse the repository at this point in the history
  • Loading branch information
gausie committed Nov 16, 2023
1 parent 72ab28e commit 0f9e461
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/clients/LoathingDate.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dedent } from "ts-dedent";
import { describe, expect, test } from "vitest";

import { LoathingDate } from "./LoathingDate.js";
Expand Down Expand Up @@ -37,3 +38,29 @@ describe("Hamburglar", () => {
expect(d.getHamburglarPhase()).toBe(0);
});
});

describe("SVG", () => {
test("One example of SVG", () => {
const d = new LoathingDate(new Date(Date.UTC(2023, 10, 16, 12, 0, 0)));
expect(d.getMoonsAsSvg()).toBe(dedent`
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="110" height="30" style="dominant-baseline: hanging;">
<text x="10" y="2" font-size="30">🌘</text>
<text x="70" y="2" font-size="30">πŸŒ•</text>
<text x="87" y="11" font-size="10">πŸŒ‘</text>
</svg>
`);
});

test("Another example of SVG", () => {
const d = new LoathingDate(new Date(Date.UTC(2023, 10, 15, 12, 0, 0)));
expect(d.getMoonsAsSvg()).toBe(dedent`
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="110" height="30" style="dominant-baseline: hanging;">
<text x="10" y="2" font-size="30">πŸŒ‘</text>
<text x="70" y="2" font-size="30">πŸŒ•</text>
<text x="50" y="11" font-size="10">πŸŒ•</text>
</svg>
`);
});
});
68 changes: 68 additions & 0 deletions src/clients/LoathingDate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { dedent } from "ts-dedent";

export class LoathingDate {
static EPOCH = new Date(Date.UTC(2003, 1, 1, 3, 30));
static COLLISION = 1218;
Expand Down Expand Up @@ -80,6 +82,72 @@ export class LoathingDate {
return (cycle * 2) % 11;
}

getHamburglarLight() {
const g = this.getGrimacePhase();
const r = this.getRonaldPhase();
switch (this.getHamburglarPhase()) {
case 0:
return g > 0 && g < 5 ? -1 : 1;
case 1:
return g < 4 ? 1 : -1;
case 2:
return g > 3 ? 1 : 0;
case 4:
return g > 0 && g < 5 ? 1 : 0;
case 5:
return r > 3 ? 1 : 0;
case 7:
return r > 0 && r < 5 ? 1 : 0;
case 8:
return r > 0 && r < 5 ? -1 : 1;
case 9:
return r < 4 ? 1 : -1;
case 10:
return (r > 3 ? 1 : 0) + (g > 0 && g < 5 ? 1 : 0);
default:
return 0;
}
}

getMoonsAsSvg() {
const moonIcons = ["πŸŒ‘", "🌘", "πŸŒ—", "πŸŒ–", "πŸŒ•", "πŸŒ”", "πŸŒ“", "πŸŒ’"];

const hamburglarPositions = [
73,
87,
100,
null,
60,
40,
null,
0,
13,
27,
50,
];
const hamburglarPhase = this.getHamburglarPhase();
const hamburglarX = hamburglarPhase
? hamburglarPositions[hamburglarPhase]
: null;
const hamburglarIcon = this.getHamburglarLight() > 0 ? "πŸŒ•" : "πŸŒ‘";

return dedent`
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="110" height="30" style="dominant-baseline: hanging;">
<text x="10" y="2" font-size="30">${
moonIcons[this.getRonaldPhase()]
}</text>
<text x="70" y="2" font-size="30">${
moonIcons[this.getGrimacePhase()]
}</text>
${
hamburglarX !== null &&
`<text x="${hamburglarX}" y="11" font-size="10">${hamburglarIcon}</text>`
}
</svg>
`;
}

toString() {
return `${this.getMonthName()} ${this.getDate()} Year ${this.getYear()}`;
}
Expand Down

0 comments on commit 0f9e461

Please sign in to comment.