From 6162fbccf434faaaae9460c06084009c95846631 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Sat, 23 Oct 2021 22:38:14 +0200 Subject: [PATCH] rozvrh go brr --- commands/ssps/rozvrh.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/commands/ssps/rozvrh.js b/commands/ssps/rozvrh.js index 12331ae..92fb964 100644 --- a/commands/ssps/rozvrh.js +++ b/commands/ssps/rozvrh.js @@ -2,6 +2,10 @@ const commando = require("@iceprod/discord.js-commando"); const { MessageEmbed } = require("discord.js"); const { DateTime } = require("luxon"); const api = require("../../utils/api"); +const Group = require("../../utils/models/group"); +const Subject = require("../../utils/models/subject"); +const Teacher = require("../../utils/models/teacher"); +const Timetable = require("../../utils/models/timetable"); const ssps = require("../../utils/ssps-server"); module.exports = class rozvrh extends commando.Command { @@ -37,31 +41,38 @@ module.exports = class rozvrh extends commando.Command { className = className.replace(".", "").toUpperCase(); className = api.map[className]; if(!className) return msg.reply("Třída není podporovaná."); - const sc = await api.getSchedule(className); let date = DateTime.now(); if(date.hour > 16) date = date.plus({ days: 1 }); // show tomorrow supplementations after 4PM if(date.weekday === 0 || date.weekday === 7) { date = date.plus({ days: date.weekday === 0 ? 2 : 1 }); } - const schedule = sc.schedule[date.weekday - 1]; + const schedule = await Timetable.findAll({ + where: { + day: date.weekday - 1, + classId: className + }, + order: [["hour", "ASC"]], + include: [Group, Subject, Teacher] + }) const embed = new MessageEmbed(); embed.setTitle("Rozvrh"); embed.setDescription(`Rozvrh pro třídu ${api.demap[className]} pro ${date.toFormat("cccc")}`); - for(let cellI in schedule) { - cellI = parseInt(cellI); - let cell = schedule[cellI]; - if(!cell) { - console.log("Wut?", cellI, cell); - continue; + var array = []; + for(let cell of schedule) { + if(!array[cell.hour]) array[cell.hour] = []; + array[cell.hour].push(cell); + } + for(let cellI in array) { + let cells = array[cellI]; + if(!cells) continue; + + for(const scell of cells) { + embed.addField(scell.subject.abbrev, `\`${api.formatRoom(scell.roomId) || "?"}\` - ${scell.teacher.name} - **${scell.group.name}**`, cells.length > 1); } - if(!Array.isArray(cell)) cell = [cell]; - for(const scell of cell) { - embed.addField(scell.Subject.Abbrev, `\`${api.formatRoom(scell.Room.Abbrev) || "?"}\` - ${scell.Teacher.Name} - **${scell.Group.Name}**`, cell.length > 1); - } - if(cell.length > 1 && Array.isArray(schedule[cellI + 1]) && schedule[cellI + 1].length > 1) embed.addField("\u200B", "\u200B", true); + if(cells.length > 1 && schedule[cellI + 1].length > 1) embed.addField("\u200B", "\u200B", true); } return msg.say(embed);