Первая рабочая версия

This commit is contained in:
2023-03-27 21:17:55 +04:00
parent 79c2afbb7e
commit dc6e613392
16 changed files with 357 additions and 144 deletions
+55 -4
View File
@@ -25,17 +25,51 @@ export class AprilFoolsJokeClass {
this.jokes[joke.id] = joke;
}
addCustom(id: string, handler: Function, settings: JokeSettings|null = null) {
const joke = new class extends Joke {
id = id;
settings: JokeSettings = { chance: 10 };
start() {
handler.call(this, this.settings);
}
};
Object.assign(joke.settings, settings ?? {});
this.add(joke);
}
core_settings = new CoreSettings;
get settings(): SettingsMap {
const result: SettingsMap = {};
result['core'] = this.core_settings;
for (const entries of Object.entries(this.jokes)) {
const id = entries[0];
const joke = entries[1];
result[id] = joke.settings;
}
return result;
}
/**
* Запись новых настроек
*
* @param settings
*/
setSettings(settings: SettingsMap): void {
set settings(settings: SettingsMap) {
for (const entry of Object.entries(settings)) {
const id = entry[0];
const jokeSettings = entry[1];
const targetSettings = (id === 'core') ? this.core_settings : this.jokes[id].settings;
this.jokes[id].settings = { ...this.jokes[id].settings, ...jokeSettings };
Object.assign(targetSettings, jokeSettings);
}
}
@@ -61,9 +95,19 @@ export class AprilFoolsJokeClass {
/**
* Запуск всех шуток
*/
startAll() {
startAll(forced: boolean = false): void {
for (const key of Object.keys(this.jokes)) {
this.start(key);
this.start(key, forced);
}
}
regularStart(): void {
const today = new Date();
const month = today.getMonth() + 1; // Note that getMonth() returns 0-indexed month, so we need to add 1
const day = today.getDate();
if (month === 4 && day === 1 && AprilFoolsJokeClass.check(this.core_settings.chance)) {
this.startAll();
}
}
@@ -85,4 +129,11 @@ interface JokeMap {
*/
interface SettingsMap {
[key: string]: JokeSettings;
}
/**
*
*/
class CoreSettings implements JokeSettings {
chance = 100;
}
+3 -8
View File
@@ -3,7 +3,7 @@
*/
export interface JokeSettings {
/**
* Вероятность срабатывания в процентах (0 для отключения)
* Вероятность срабатывания в процентах
*/
chance: number;
}
@@ -15,7 +15,7 @@ export abstract class Joke {
/**
* Идентификатор шутки
*/
id: string;
abstract id: string;
/**
* Настройки шутки
@@ -25,12 +25,7 @@ export abstract class Joke {
/**
* Флаг, указывающий, должна ли шутка быть запущена
*/
enabled: boolean;
/**
* Флаг, указывающий, может ли шутка быть остановлена
*/
is_stopable: boolean;
enabled: boolean = true;
/**
* Запуск шутки
+6 -4
View File
@@ -3,15 +3,17 @@ import $ from "jquery";
import { AprilFoolsJokeClass } from "../handler";
/**
* **Зеркало**
* **Карнавал**
*
* Элементы форума отражаются (по крайней мере, пытаются)
* Аватарки приобретают интересные цвета
*
* @author Kozhilya
*/
export class CarnivalJoke extends Joke {
settings: CarnivalJokeSettings;
id = 'carnival'
settings = new CarnivalJokeSettings;
start(): void {
$(this.settings.selector).each((_, elem) => {
const filters: string[] = [];
@@ -25,7 +27,7 @@ export class CarnivalJoke extends Joke {
}
if (filters.length > 0) {
$(elem).css('filter', filters.join(' '));
$(elem).css({'filter': filters.join(' ')});
}
});
}
+3 -1
View File
@@ -9,7 +9,9 @@ import $ from "jquery";
* @author Kozhilya
*/
export class CrazyLettersJoke extends Joke {
settings: CrazyLettersJokeSettings;
id = 'crazy_letters';
settings = new CrazyLettersJokeSettings;
start(): void {
$(this.settings.selector).each((_, elem) => {
+3 -1
View File
@@ -9,7 +9,9 @@ import $ from "jquery";
* @author Kozhilya
*/
export class CrazyPunctuationJoke extends Joke {
settings: CrazyPunctuationJokeSettings;
id = 'crazy_punctuation';
settings = new CrazyPunctuationJokeSettings;
private transform(text: string): string {
let newText = "";
+3 -1
View File
@@ -9,7 +9,9 @@ import $ from "jquery";
* @author Kozhilya
*/
export class HumanitarianHellJoke extends Joke {
settings: HumanitarianHellJokeSettings;
id = 'humanitarian_hell';
settings = new HumanitarianHellJokeSettings;
start(): void {
$(this.settings.selector).each(function () {
+3 -1
View File
@@ -9,7 +9,9 @@ import $ from "jquery";
* @author Kozhilya
*/
export class MirrorJoke extends Joke {
settings: MirrorJokeSettings;
id = 'mirror';
settings = new MirrorJokeSettings;
start(): void {
$('.post, .post h3, .post-links, .post-author, .post-content')
+2 -1
View File
@@ -9,8 +9,9 @@ import $ from "jquery";
* @author Kozhilya
*/
export class ShakeJoke extends StopableJoke {
id = 'shake';
settings: ShakeJokeSettings;
settings = new ShakeJokeSettings;
private interval_id: NodeJS.Timer;
+2
View File
@@ -14,6 +14,8 @@ import { CrazyPunctuationJoke } from "./jokes/crazy_punctuation";
cl.add(new joke());
});
cl.regularStart();
return cl;
})([
ShakeJoke,