Skip to content

Commit

Permalink
feat: Widget multi events support (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 authored Feb 20, 2021
1 parent 7346786 commit c56cec8
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 127 deletions.
12 changes: 12 additions & 0 deletions app/server/db/Models/StoreMigration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { DataTypes } = require("sequelize");
const sequelize = require("../index");

const StoreMigration = sequelize.define("StoreMigration", {
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
});

module.exports = StoreMigration;
31 changes: 31 additions & 0 deletions app/server/db/migrations/20210220100805-store-migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";

const { Sequelize } = require("sequelize");

const storeMigrations = {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
createdAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
updatedAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
};

module.exports = {
up: async ({ context }) => {
await context.createTable("StoreMigrations", storeMigrations);
},
down: async () => {},
};
2 changes: 2 additions & 0 deletions app/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const loggers = require("./libs/loggers");
const socket = require("./libs/socket.io");
const settings = require("./libs/settings");
const { getServerURL } = require("./utils");
const migrateStore = require("../stores/migrate");
const { getSystemFonts } = require("./libs/files");
const errorHandler = require("./libs/errorHandler");
const { init: i18next } = require("./libs/i18next");
Expand Down Expand Up @@ -71,6 +72,7 @@ async function start() {
await umzug.up();
await i18next();
await twitchInit();
await migrateStore.up();

let port = await settings.get("server.port");

Expand Down
17 changes: 12 additions & 5 deletions app/server/libs/panels.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ function createWidget(widget = {}) {
id: uuid(),
component: null,
trigger: "immediat",
eventName: "none",
commandName: "none",
rewardId: "none",
events: [],
label: null,
labelSize: 16,
labelPadding: 8,
Expand Down Expand Up @@ -314,10 +312,19 @@ function importArchive(panel, archive) {
function getShortcuts() {
const shortcuts = [];

const pushEvent = (event) => {
if (event.shortcutName && !shortcuts.includes(event.shortcutName)) {
shortcuts.push(event.shortcutName);
}
};

panels.forEach(({ widgets }) => {
widgets.forEach((widget) => {
if (widget.shortcutName && !shortcuts.includes(widget.shortcutName)) {
shortcuts.push(widget.shortcutName);
pushEvent(widget);
if (widget.events) {
widget.events.forEach((event) => {
pushEvent(event);
});
}
});
});
Expand Down
34 changes: 23 additions & 11 deletions app/server/libs/twitch/pushActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@ const types = {
AnimeTimeline: "anime",
};

function isInvalidShortcut(widget, eventProps) {
function isInvalidShortcut(event, eventProps) {
return (
eventProps.accelerator && widget.shortcutName !== eventProps.accelerator
eventProps.accelerator && event.shortcutName !== eventProps.accelerator
);
}

function isInvalidCommand(widget, eventProps) {
return eventProps.command && widget.commandName !== eventProps.command.name;
function isInvalidCommand(event, eventProps) {
return eventProps.command && event.commandName !== eventProps.command.name;
}

function isInvalidReward(widget, eventProps) {
return eventProps.reward && widget.rewardId !== eventProps.reward.id;
function isInvalidReward(event, eventProps) {
return eventProps.reward && event.rewardId !== eventProps.reward.id;
}

function getValidEvents(widget, eventName) {
let events = widget.events.filter((event) => event.eventName === eventName);

if (widget.eventName === eventName) {
events.push(widget);
}

return events;
}

module.exports = function pushActions(eventName, eventProps) {
Expand All @@ -35,12 +45,14 @@ module.exports = function pushActions(eventName, eventProps) {
const type = types[widget.component.name];

if (!["anime", "obs"].includes(type)) return;
if (widget.eventName !== eventName) return;
if (isInvalidReward(widget, eventProps)) return;
if (isInvalidCommand(widget, eventProps)) return;
if (isInvalidShortcut(widget, eventProps)) return;

push({ type, widget, eventProps });
getValidEvents(widget, eventName).forEach((event) => {
if (isInvalidReward(event, eventProps)) return;
if (isInvalidCommand(event, eventProps)) return;
if (isInvalidShortcut(event, eventProps)) return;

push({ type, widget, event, eventProps });
});
});
});
};
3 changes: 2 additions & 1 deletion app/static/locales/en/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"ask-purge": "Delete {{count}} files not in use?",
"ask-remove-asset": "Remove \"{{filename}}\" from the timeline?",
"global-shortcut-reserved": "This shortcut is reserved by the system!",
"type-shortcut-here": "Type shortcut here..."
"type-shortcut-here": "Type shortcut here...",
"add-event": "Add event"
},
"words": {
"settings": "settings",
Expand Down
3 changes: 2 additions & 1 deletion app/static/locales/es/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"ask-purge": "¿Borrar {{count}} archivos no utilizados?",
"ask-remove-asset": "¿Eliminar \"{{filename}}\" de la línea de tiempo?",
"global-shortcut-reserved": "Este acceso directo está reservado por el sistema!",
"type-shortcut-here": "Escriba el acceso directo aquí..."
"type-shortcut-here": "Escriba el acceso directo aquí...",
"add-event": "Añadir evento"
},
"words": {
"settings": "opciones",
Expand Down
3 changes: 2 additions & 1 deletion app/static/locales/fr/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"ask-purge": "Supprimer {{count}} fichiers non utiliser ?",
"ask-remove-asset": "Supprimer \"{{filename}}\" de la timeline ?",
"global-shortcut-reserved": "Ce raccourci est réservé par le système!",
"type-shortcut-here": "Tapez le raccourci ici..."
"type-shortcut-here": "Tapez le raccourci ici...",
"add-event": "Ajouter un évènement"
},
"words": {
"settings": "options",
Expand Down
34 changes: 34 additions & 0 deletions app/stores/migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const StoreMigration = require("../server/db/Models/StoreMigration");
const loggers = require("../server/libs/loggers");
const path = require("path");
const fs = require("fs");

const migrationsPath = path.join(__dirname, "migrations");
const logger = loggers.get("server");

async function up() {
const files = fs.readdirSync(migrationsPath).sort((a, b) => a - b);
const migrations = await StoreMigration.findAll({
attributes: ["name"],
raw: true,
});

for (let i = 0, l = files.length; i < l; i++) {
const file = files[i];
if (!migrations.find((m) => m.name === file)) {
logger.info(`[store] migration from ${file}`);
const { up } = require(path.join(migrationsPath, file));
await up();
StoreMigration.create({ name: file });
}
}
}

async function down() {
logger.warn("[store] up method not implemented !!!");
}

module.exports = {
up,
down,
};
46 changes: 46 additions & 0 deletions app/stores/migrations/20210220101638-multi-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { panels: store } = require("../index");

function clean(widget) {
delete widget.eventName;
delete widget.commandName;
delete widget.rewardId;
delete widget.shortcutName;
}

module.exports = {
up: async () => {
const panels = store.get("panels");

panels.forEach((panel) => {
panel.widgets.forEach((widget) => {
widget.events = widget.events || [];

const event = {};

if (!widget.eventName || widget.eventName === "none") {
clean(widget);
return;
}

event.eventName = widget.eventName;

if (event.eventName === "onCommand") {
event.commandName = widget.commandName;
} else if (event.eventName === "onRedemption") {
event.rewardId = widget.rewardId;
} else if (event.eventName === "onShortcut") {
event.shortcutName = widget.shortcutName;
}

if (Object.keys(event).length) {
widget.events.push(event);
}

clean(widget);
});
});

store.set("panels", panels);
},
down: async () => {},
};
Loading

0 comments on commit c56cec8

Please sign in to comment.