place.ts 4.72 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
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
			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()) {
42
43
						return true;
					}
mdsecurity's avatar
mdsecurity committed
44
				}
45
46
47
			} else if (dayOfWeek >= day.start_day && dayOfWeek <= day.end_day) {
				if (dayOfWeek === day.start_day) {
					if (inSeconds >= day.start_time.inSeconds()) {
48
49
						return true;
					}
50
				} else if (dayOfWeek === day.end_day) {
51
52
					if (inSeconds <= day.end_time.inSeconds()) {
						return true;
53
54
55
					}
				} else {
					return true;
mdsecurity's avatar
mdsecurity committed
56
57
58
59
60
				}
			}
		}
		return false;
	}
61

62
	openFor(): any {
mdsecurity's avatar
mdsecurity committed
63
		const currTime = new Date();
64
		const inSeconds = currTime.getHours() * 60 * 60 + currTime.getMinutes() * 60 + currTime.getSeconds();
65
		const dayOfWeekShift = [6, 0, 1, 2, 3, 4, 5];
66
		const dayOfWeek = dayOfWeekShift[currTime.getDay()];
mdsecurity's avatar
mdsecurity committed
67
		const useSpecialSchedule = this.useSpecial();
68
69
		let timeTilClose = new Time();
		let sumSeconds = 0;
mdsecurity's avatar
mdsecurity committed
70
71

		let schedule;
72
		if (useSpecialSchedule === -1) {
mdsecurity's avatar
mdsecurity committed
73
			schedule = this.main_schedule_times;
74
		} else {
mdsecurity's avatar
mdsecurity committed
75
76
			schedule = this.special_schedules[useSpecialSchedule].openTimes;
		}
77
78
79
80
81
82
		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;
83
						return timeTilClose.fromSeconds(sumSeconds);
84
85
86
					} else {
						//not done
						sumSeconds = 86400 - inSeconds;
87
						return timeTilClose.fromSeconds(sumSeconds);
88
89
					}
				} else {
90
					sumSeconds = day.start_time.inSeconds() - inSeconds;
91
					return timeTilClose.fromSeconds(sumSeconds);
92
				}
93
94
95
96
97
				// console.log(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();
98
						return timeTilClose.fromSeconds(sumSeconds);
99
100
					} else {
						sumSeconds = day.start_time.inSeconds() - inSeconds;
101
						return timeTilClose.fromSeconds(sumSeconds);
102
103
104
105
106
					}
				} else if (dayOfWeek === day.end_day) {
					if (inSeconds >= day.end_time.inSeconds()) {
						//not done
						sumSeconds = 86400 - inSeconds;
107
						return timeTilClose.fromSeconds(sumSeconds);
108
109
					} else {
						sumSeconds = day.end_time.inSeconds() - inSeconds;
110
						return timeTilClose.fromSeconds(sumSeconds);
111
112
113
					}
				} else {
					sumSeconds = 86400 - inSeconds + (86400 * (day.end_day - dayOfWeek - 2)) + day.end_time.inSeconds();
114
					return timeTilClose.fromSeconds(sumSeconds);
115
116
				}
				// console.log(sumSeconds);
117
			}
118
119

		}
120
		return new Time();
mdsecurity's avatar
mdsecurity committed
121
	}
mdsecurity's avatar
mdsecurity committed
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
	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
138
}
139

140