place.ts 3.5 KB
Newer Older
1
import { Day } from './day';
mdsecurity's avatar
mdsecurity committed
2
import { Time } from './time';
mdsecurity's avatar
mdsecurity committed
3
import { SpecialSchedule } from './special-schedule';
mdsecurity's avatar
mdsecurity committed
4

mdsecurity's avatar
mdsecurity committed
5
export class Place {
mdsecurity's avatar
mdsecurity committed
6
	main_schedule_times: Day[] = [];
mdsecurity's avatar
mdsecurity committed
7
	special_schedules: SpecialSchedule[] = [];
mdsecurity's avatar
mdsecurity committed
8
9
10
11
12
	id: number;
	last_modified: string;
	name: string;
	category: number;
	location: string;
13
	constructor(main_schedule_times?: Day[], special_schedules?: SpecialSchedule[], id?: number, last_modified?: string,
mdsecurity's avatar
mdsecurity committed
14
		name?: string, category?: number, location?: string) {
mdsecurity's avatar
mdsecurity committed
15

mdsecurity's avatar
mdsecurity committed
16
		this.main_schedule_times = main_schedule_times || [];
mdsecurity's avatar
mdsecurity committed
17
		this.special_schedules = special_schedules || [];
mdsecurity's avatar
mdsecurity committed
18
19
20
21
22
		this.id = id || 0;
		this.last_modified = last_modified || '';
		this.name = name || '';
		this.category = category || 0;
		this.location = location || '';
mdsecurity's avatar
mdsecurity committed
23
24
25
26
27
	}
	// This method does NOT work with Special schedules
	isOpen(): boolean {
		const currTime = new Date();
		const inSeconds = currTime.getHours() * 60 * 60 + currTime.getMinutes() * 60 + currTime.getSeconds();
28
29
		const dayOfWeekShift = [6, 5, 4, 3, 2, 1, 0];
		const dayOfWeek = dayOfWeekShift[currTime.getDay()];
30
31
		const useSpecialSchedule = this.useSpecial();
		let schedule;
32
		if (useSpecialSchedule === -1) {
33
			schedule = this.main_schedule_times;
34
		} else {
35
36
37
			schedule = this.special_schedules[useSpecialSchedule].openTimes;
		}
		for (let i = 0; i < schedule.length; i++) {
mdsecurity's avatar
mdsecurity committed
38
			const day = schedule[i];
39
40
41
42
43
44
45
			// change the order of if statements at some point
			if (day.start_day !== day.end_day) {
				if (day.end_day === dayOfWeek) {
					if (day.end_time.inSeconds() > inSeconds) {
						return true;
					}
				} else if (day.start_day === dayOfWeek) {
mdsecurity's avatar
mdsecurity committed
46
47
					return true;
				}
48
49
50
51
52
			} else {
				if (day.start_day === dayOfWeek) {
					if (day.end_time.inSeconds() > inSeconds) {
						return true;
					}
mdsecurity's avatar
mdsecurity committed
53
54
55
56
57
				}
			}
		}
		return false;
	}
58

mdsecurity's avatar
mdsecurity committed
59
	openFor(): Time {
mdsecurity's avatar
mdsecurity committed
60
		const currTime = new Date();
61
62
		const today = currTime.getDay() - 1;
		const inSeconds = currTime.getHours() * 60 * 60 + currTime.getMinutes() * 60 + currTime.getSeconds();
63
64
		const dayOfWeekShift = [6, 5, 4, 3, 2, 1, 0];
		const dayOfWeek = dayOfWeekShift[currTime.getDay()];
mdsecurity's avatar
mdsecurity committed
65
66
		const useSpecialSchedule = this.useSpecial();

67
68
		let timeTilClose = new Time();
		let sumSeconds = 0;
mdsecurity's avatar
mdsecurity committed
69
70

		let schedule;
71
		if (useSpecialSchedule === -1) {
mdsecurity's avatar
mdsecurity committed
72
			schedule = this.main_schedule_times;
73
		} else {
mdsecurity's avatar
mdsecurity committed
74
75
			schedule = this.special_schedules[useSpecialSchedule].openTimes;
		}
76
		// clean up if statements later
77
		// if (this.isOpen()) {
mdsecurity's avatar
mdsecurity committed
78
79
			for (let i = 0; i < schedule.length; i++) {
				const day = schedule[i];
80
81
82
83
				if (day.end_day !== day.start_day) {
					if (dayOfWeek === day.end_day) {
						sumSeconds = day.end_time.inSeconds() - inSeconds - 86400;
					} else if (dayOfWeek === day.start_day) {
mdsecurity's avatar
mdsecurity committed
84
						sumSeconds = day.end_time.inSeconds() + 86400 - inSeconds;
85
86
87
88
					}
				} else {
					sumSeconds = day.end_time.inSeconds() - inSeconds;
				}
mdsecurity's avatar
mdsecurity committed
89

mdsecurity's avatar
mdsecurity committed
90
				return timeTilClose.fromSeconds(sumSeconds);
91
			}
92
		// } else {
mdsecurity's avatar
mdsecurity committed
93
			return timeTilClose;
94
		// }
mdsecurity's avatar
mdsecurity committed
95
	}
mdsecurity's avatar
mdsecurity committed
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
	useSpecial(): number {
		const todaysDate = new Date();
		for (let i = 0; i < this.special_schedules.length; i++) {
			let parsedStart = this.special_schedules[i].validStart.split('-');
			let parsedEnd = this.special_schedules[i].validEnd.split('-');
			if (todaysDate.getFullYear() >= Number(parsedStart[0]) && todaysDate.getFullYear() <= Number(parsedEnd[0])) {
				if (todaysDate.getMonth() >= Number(parsedStart[1]) && todaysDate.getMonth() <= Number(parsedEnd[1])) {
					if (todaysDate.getDate() >= Number(parsedStart[2]) && todaysDate.getDate() <= Number(parsedEnd[2])) {
						return i;
					}
				}
			}
		}
		return -1;
	}
mdsecurity's avatar
mdsecurity committed
112
}
113

114