place.ts 5.53 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
		const dayOfWeekShift = [6, 0, 1, 2, 3, 4, 5];
29
		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
			schedule = this.special_schedules[useSpecialSchedule].openTimes;
		}
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
		// for (let i = 0; i < schedule.length; i++) {
		// 	const day = schedule[i];
		// 	// 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) {
		// 			return true;
		// 		}
		// 	} else {
		// 		if (day.start_day === dayOfWeek) {
		// 			if (day.end_time.inSeconds() > inSeconds) {
		// 				return true;
		// 			}
		// 		}
		// 	}
		// }
56
		for (let i = 0; i < schedule.length; i++) {
mdsecurity's avatar
mdsecurity committed
57
			const day = schedule[i];
58
59
60
			if ((dayOfWeek === day.start_day || dayOfWeek === day.end_day) && day.start_day === day.end_day) {
				if (inSeconds >= day.start_time.inSeconds()) {
					if (inSeconds <= day.end_time.inSeconds()) {
61
62
						return true;
					}
63
					return false;
mdsecurity's avatar
mdsecurity committed
64
				}
65
66
67
68
				return false;
			} else if (dayOfWeek >= day.start_day && dayOfWeek <= day.end_day) {
				if (dayOfWeek === day.start_day) {
					if (inSeconds >= day.start_time.inSeconds()) {
69
70
						return true;
					}
71
72
73
74
75
76
77
78
					return false;
				} else if (dayOfWeek === day.end_day) {
					if (inSeconds >= day.end_time.inSeconds()) {
						return false;
					}
					return true;
				} else {
					return true;
mdsecurity's avatar
mdsecurity committed
79
80
81
82
83
				}
			}
		}
		return false;
	}
84

85
	openFor(): any {
mdsecurity's avatar
mdsecurity committed
86
		const currTime = new Date();
87
		const inSeconds = currTime.getHours() * 60 * 60 + currTime.getMinutes() * 60 + currTime.getSeconds();
88
		const dayOfWeekShift = [6, 0, 1, 2, 3, 4, 5];
89
		const dayOfWeek = dayOfWeekShift[currTime.getDay()];
mdsecurity's avatar
mdsecurity committed
90
		const useSpecialSchedule = this.useSpecial();
91
92
		let timeTilClose = new Time();
		let sumSeconds = 0;
mdsecurity's avatar
mdsecurity committed
93
94

		let schedule;
95
		if (useSpecialSchedule === -1) {
mdsecurity's avatar
mdsecurity committed
96
			schedule = this.main_schedule_times;
97
		} else {
mdsecurity's avatar
mdsecurity committed
98
99
			schedule = this.special_schedules[useSpecialSchedule].openTimes;
		}
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
		// for (let i = 0; i < schedule.length; i++) {
		// 	const day = schedule[i];
		// 	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) {
		// 			sumSeconds = day.end_time.inSeconds() + 86400 - inSeconds;
		// 		}
		// 	} else {
		// 		sumSeconds = day.end_time.inSeconds() - inSeconds;
		// 	}

		// 	return timeTilClose.fromSeconds(sumSeconds);
		// }
		
		for (let i = 0; i < schedule.length; i++) {
			const day = schedule[i];
			if ((dayOfWeek === day.start_day || dayOfWeek === day.end_day) && day.start_day === day.end_day) {
				if (inSeconds >= day.start_time.inSeconds()) {
					if (inSeconds <= day.end_time.inSeconds()) {
						sumSeconds = day.end_time.inSeconds() - inSeconds;
					} else {
						//not done
						sumSeconds = 86400 - inSeconds;
124
125
					}
				} else {
126
					sumSeconds = day.start_time.inSeconds() - inSeconds;
127
				}
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
				// console.log(sumSeconds);
				return timeTilClose.fromSeconds(sumSeconds);
			} else if (dayOfWeek >= day.start_day && dayOfWeek <= day.end_day) {
				if (dayOfWeek === day.start_day) {
					if (inSeconds >= day.start_time.inSeconds()) {
						sumSeconds = 86400 - inSeconds + (86400 * (day.end_day - day.start_day - 1)) + day.end_time.inSeconds();
					} else {
						sumSeconds = day.start_time.inSeconds() - inSeconds;
					}
				} else if (dayOfWeek === day.end_day) {
					if (inSeconds >= day.end_time.inSeconds()) {
						//not done
						sumSeconds = 86400 - inSeconds;
					} else {
						sumSeconds = day.end_time.inSeconds() - inSeconds;
					}
				} else {
					sumSeconds = 86400 - inSeconds + (86400 * (day.end_day - dayOfWeek - 2)) + day.end_time.inSeconds();
				}
				// console.log(sumSeconds);
mdsecurity's avatar
mdsecurity committed
148
				return timeTilClose.fromSeconds(sumSeconds);
149
			}
150
151
152

		}
		return { "hour": "closed" };
mdsecurity's avatar
mdsecurity committed
153
	}
mdsecurity's avatar
mdsecurity committed
154

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
	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
170
}
171

172