diff --git a/app/src/controller/princess/schedule.js b/app/src/controller/princess/schedule.js deleted file mode 100644 index 1edc5830..00000000 --- a/app/src/controller/princess/schedule.js +++ /dev/null @@ -1,62 +0,0 @@ -const ScheduleModel = require("../../model/princess/schedule"); -const ScheduleTemplate = require("../../templates/princess/schedule"); -const { recordSign } = require("../../util/traffic"); - -exports.showSchedule = async context => { - recordSign("showSchedule"); - const EventDatas = await ScheduleModel.getDatas(); - - var result = { - ExpireEvent: [], - HoldingEvent: [], - FutureEvent: [], - }; - let now = new Date().getTime(); - let strDisPrefix = ["天", "時", "分"]; - - EventDatas.forEach(data => { - let end_time = new Date(data.end_time).getTime(); - let start_time = new Date(data.start_time).getTime(); - let distance = "到期"; - - if (now > end_time) { - data.distance = distance; - if (result.ExpireEvent.length > 2) return; - result.ExpireEvent.push(data); - } else if (now > start_time) { - distance = getDistanceSpecifiedTime(now, end_time); - data.distance = distance - .map((d, index) => { - return d + strDisPrefix[index]; - }) - .join(" "); - result.HoldingEvent.push(data); - } else { - distance = getDistanceSpecifiedTime(now, start_time); - data.distance = distance - .map((d, index) => { - return d + strDisPrefix[index]; - }) - .join(" "); - result.FutureEvent.push(data); - } - }); - - ScheduleTemplate.showSchedule(context, result); -}; - -/** - * 小的放前,大的放後 - * @param {Date} start 起始日期 - * @param {Date} end 結束日期 - */ -function getDistanceSpecifiedTime(start, end) { - var EndTime = new Date(end); - var NowTime = new Date(start); - var t = EndTime.getTime() - NowTime.getTime(); - var d = Math.floor(t / 1000 / 60 / 60 / 24); - var h = Math.floor((t / 1000 / 60 / 60) % 24); - var m = Math.floor((t / 1000 / 60) % 60); - - return [d, h, m]; -} diff --git a/app/src/model/princess/schedule.js b/app/src/model/princess/schedule.js deleted file mode 100644 index ebc906f8..00000000 --- a/app/src/model/princess/schedule.js +++ /dev/null @@ -1,15 +0,0 @@ -const { default: axios } = require("axios"); -const redis = require("../../util/redis"); -const memoryKey = "RandosoruEvents"; - -/** - * 取得活動行事曆 - */ -exports.getDatas = async () => { - var datas = await redis.get(memoryKey); - if (datas !== null) return JSON.parse(datas); - - const res = await axios.get("https://pcredivewiki.tw/static/data/event.json"); - redis.set(memoryKey, JSON.stringify(res.data)); - return res.data; -}; diff --git a/app/src/templates/princess/schedule/index.js b/app/src/templates/princess/schedule/index.js deleted file mode 100644 index 7d493ff6..00000000 --- a/app/src/templates/princess/schedule/index.js +++ /dev/null @@ -1,127 +0,0 @@ -const { assemble } = require("../../common"); -const eventTPL = { - type: "bubble", - header: { - type: "box", - layout: "vertical", - contents: [ - { type: "text", text: "{TITLE}", weight: "bold", align: "center", size: "lg" }, - { - type: "text", - text: "所有資料取自於:蘭德索爾圖書館", - position: "absolute", - offsetTop: "7px", - offsetStart: "5px", - size: "xxs", - color: "#A9A9A9", - decoration: "underline", - }, - ], - }, - body: { type: "box", layout: "vertical", contents: [], spacing: "md" }, -}; -const eventBox = { - type: "box", - layout: "vertical", - contents: [ - { type: "text", text: "{title}" }, - { - type: "text", - contents: [ - { type: "span", text: "開始時間:" }, - { type: "span", text: "{start}", weight: "bold", color: "#616771" }, - ], - size: "xs", - margin: "md", - }, - { - type: "text", - contents: [ - { type: "span", text: "結束時間:" }, - { type: "span", text: "{end}", weight: "bold", color: "#616771" }, - ], - size: "xs", - }, - { - type: "text", - contents: [ - { type: "span", text: "{thirdTitle}:" }, - { type: "span", text: "{distance}", weight: "bold", color: "#616771" }, - ], - size: "xs", - }, - ], -}; - -exports.showSchedule = (context, EventDatas) => { - var objEventMsg = {}; - - if (EventDatas.HoldingEvent.length !== 0) { - objEventMsg.HoldingEvent = genEventMessage( - EventDatas.HoldingEvent, - "正在舉辦的活動", - "距離結束" - ); - objEventMsg.HoldingEvent.hero = { - type: "image", - url: "https://i.imgur.com/9lQy4UZ.jpg", - size: "full", - aspectMode: "fit", - aspectRatio: "16:9", - }; - } - - if (EventDatas.FutureEvent.length !== 0) { - objEventMsg.FutureEvent = genEventMessage(EventDatas.FutureEvent, "即將舉辦的活動", "距離開始"); - objEventMsg.FutureEvent.hero = { - type: "image", - url: "https://i.imgur.com/yPCcB13.jpg", - size: "full", - aspectMode: "fit", - aspectRatio: "16:9", - }; - } - - if (EventDatas.ExpireEvent.length !== 0) { - objEventMsg.ExpireEvent = genEventMessage(EventDatas.ExpireEvent, "已經結束的活動", "距離結束"); - objEventMsg.ExpireEvent.hero = { - type: "image", - url: "https://i.imgur.com/X78dOeL.jpg", - size: "full", - aspectMode: "fit", - aspectRatio: "16:9", - }; - } - - let sender = { - name: "公主秘書", - iconUrl: "https://i.imgur.com/BOzOY40.jpg", - }; - - context.replyFlex( - "公主活動", - { - type: "carousel", - contents: Object.keys(objEventMsg).map(key => objEventMsg[key]), - }, - { sender } - ); -}; - -function genEventMessage(EventData, title = "未設定", thirdTitle = "距離現在") { - var bubble = JSON.parse(assemble({ title: title }, JSON.stringify(eventTPL))); - - bubble.body.contents = EventData.map(function (data) { - let mapData = { - title: data.campaign_name, - start: data.start_time, - end: data.end_time, - thirdTitle: thirdTitle, - distance: data.distance, - }; - - return JSON.parse(assemble(mapData, JSON.stringify(eventBox))); - }); - - return bubble; -}