diff --git a/src/logic/sunset.ts b/src/logic/sunset.ts new file mode 100644 index 00000000..3949a568 --- /dev/null +++ b/src/logic/sunset.ts @@ -0,0 +1,51 @@ +export interface Time { + hour: number; + minute: number; + second: number; +} + +function degToRad(deg: number): number { + return deg * Math.PI / 180; +} + +function radToDeg(rad: number): number { + return rad * 180.0 / Math.PI; +} + +function isLeap(year: number): boolean { + return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)); +} + +function numberOfDay(year: number, month: number, day: number): number { + const days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + let n = day; + if (month > 2) n += isLeap(year) ? 1 : 0; + while ((--month) > 0) n += days[month]; + return n; +} + +function sunset(year: number, month: number, day: number, latitude: number): number { + const dec = -degToRad(23.44) * Math.cos(2 * Math.PI / (365 + (isLeap(year) ? 1 : 0)) * (numberOfDay(year, month, day) + 10)); + return -Math.tan(degToRad(latitude)) * Math.tan(dec); +} + +function radToTime(rad: number): Time { + if (rad > Math.PI) rad -= 2 * Math.PI; + const sec = Math.floor(43200 + (rad * 43200 / Math.PI)); + const hour = Math.floor(sec / 3600); + const minute = Math.floor((sec - 3600 * hour) / 60); + const second = Math.floor((sec - 3600 * hour) % 60); + return { + hour: hour, + minute: minute, + second: second, + } +} + +export function sunsetTime(year: number, month: number, day: number, latitude: number): Time { + return radToTime(Math.acos(sunset(year, month, day, latitude))); +} + +export function sunriseTime(year: number, month: number, day: number, latitude: number): Time { + return radToTime(-Math.acos(sunset(year, month, day, latitude))); +} \ No newline at end of file diff --git a/src/views/Profile.vue b/src/views/Profile.vue index e1bc21ef..9470bc7a 100644 --- a/src/views/Profile.vue +++ b/src/views/Profile.vue @@ -20,6 +20,7 @@ import {balloons, dataHost, Lang, limit, peopleUrl, replaceUrlVars, setLang, t} import {parsePeopleJson, Person} from "@/logic/data"; import {handleEasterEgg} from '@/logic/easterEgg' import {fetchWithLang, randint, scheduledTask, trim} from "@/logic/helper"; +import {sunriseTime, sunsetTime} from "@/logic/sunset"; import ProfileComments from "@/views/ProfileComments.vue"; import Swal from 'sweetalert2'; import urljoin from "url-join"; @@ -90,7 +91,29 @@ export default class Profile extends Vue { checkViewLimit(): boolean | void { if (this.screenshotMode) return - const config = limit + const config = (() => { + const now = new Date(); + const sunset = sunsetTime(now.getFullYear(), now.getMonth() + 1, now.getDate(), 45.0); + const sunrise = sunriseTime(now.getFullYear(), now.getMonth() + 1, now.getDate(), 45.0); + + if (now.getTime() > (new Date(now.getFullYear(), now.getMonth(), now.getDate(), sunset.hour + 2, sunset.minute, sunset.second).getTime())) { + return { + warningLimit: Math.floor(limit.warningLimit / 2), + errorLimit: Math.floor(limit.errorLimit / 2), + cooldown: limit.cooldown + } + } + else if (now.getTime() < (new Date(now.getFullYear(), now.getMonth(), now.getDate(), Math.floor(sunrise.hour * 0.5 + 2), sunrise.minute, sunrise.second).getTime())) { + return { + warningLimit: Math.floor(limit.warningLimit / 2), + errorLimit: Math.floor(limit.errorLimit / 2), + cooldown: limit.cooldown + } + } + return limit + })() + + console.log(config) const now = new Date() const [_time, _entries] = [localStorage.getItem("view_limit_time"), localStorage.getItem("view_limit_entries")]