Skip to content

Commit

Permalink
Merge pull request #1 from crisvdn/feature/implementation
Browse files Browse the repository at this point in the history
Feature/implementation
  • Loading branch information
crisvdn committed Jan 26, 2024
2 parents cd1ac81 + 2b30a9c commit 0019dcd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 51 deletions.
81 changes: 42 additions & 39 deletions MMM-MTG.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,52 @@ const random = '/cards/random';
const commander = '/cards/random?q=is%3Acommander';

Module.register("MMM-MTG", {
default: {
commander: true,
interval: 0,
},

getDom: function () {
var imgElement = document.createElement('img');
if(this.commander){
imgElement.innerHTML = this.GetCard(commander);
}else{
imgElement.innerHTML = this.GetCard(random);
}
return imgElement;
defaults: {
text: "MTG",
sizePx: '450px',
showCommandersOnly: true,
interval: 1,
},
imageUri: "",
requiresVersion: "2.25.0",

getData: function () {
this.sendSocketNotification("GET_MTG_CARD", this.config);
start: async function() {
this.getData();
if(this.config.interval > 0){
setInterval(() => {
this.sendSocketNotification("GET_MTG_CARD", this.config);
}, this.config.interval);
},

socketNotificationReceived: function (notification, payload) {
switch (notification) {
case "UPDATE_CARD_DATA":
break;
default:
this.getData();
}, this.config.interval * 60000);
}
},

getDom: async function () {
var imgContainer = document.createElement("div");
if(this.imageUri){
var imgElement = document.createElement("img");
imgElement.src = this.imageUri;
imgElement.style.maxHeight = this.config.sizePx;
imgContainer.appendChild(imgElement);
}
this.updateDom();
},
});

return imgContainer;
},

async function GetCard(uri){
var imgUrl;
getData: async function () {
if(this.config.showCommandersOnly){
this.sendSocketNotification("GET_MTG_CARD", api + commander);
}else{
this.sendSocketNotification("GET_MTG_CARD", api + random);
}
},

await fetch(api + uri).then((response) => response.json())
.then(data => {
imgUrl = data;
})
.then(() => {
item.src = imgUrl.image_uris.normal;
document.body.appendChild(imgElement);
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case "UPDATE_CARD_DATA":
this.imageUri = payload.src;
this.updateDom();
break;
default:
break;
}
}
);
return item.src;
}
});
40 changes: 28 additions & 12 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@ const NodeHelper = require("node_helper");
const Log = require("logger");

module.exports = NodeHelper.create({

start: function startModule () {
start: function () {
Log.info("Starting module " + this.name);
},

socketNotificationReceived: async function (notification, payload) {
const self = this;
switch (notification) {
case "GET_MTG_CARD":

break;
default:
Log.error("Switch item {} is missing", notification);
}
socketNotificationReceived: function (notification, payload) {
Log.info("received " + notification + " : " + payload)
switch(notification){
case "GET_MTG_CARD":
GetCard(payload, this);
break;
default:
break;
}
},
});

async function GetCard(uri, sender){
var imgUrl;
await fetch(uri).then((response) => response.json())
.then(data => {
imgUrl = data;
})
.then(() => {
try{
imgUrl.src = imgUrl.image_uris.border_crop;
sender.sendSocketNotification("UPDATE_CARD_DATA", imgUrl);
}
catch(error){
Log.error(error);
}
}
);
}

0 comments on commit 0019dcd

Please sign in to comment.