place.ts 2.4 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;
mdsecurity's avatar
mdsecurity committed
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
28
	}
	// 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();
		const dayOfWeek = currTime.getDay() - 1;
mdsecurity's avatar
mdsecurity committed
29

mdsecurity's avatar
mdsecurity committed
30
31
		for (let i = 0; i < this.main_schedule_times.length; i++) {
			const day = this.main_schedule_times[i];
32
33
34
35
36
37
38
			// 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
39
40
					return true;
				}
41
42
43
44
45
			} else {
				if (day.start_day === dayOfWeek) {
					if (day.end_time.inSeconds() > inSeconds) {
						return true;
					}
mdsecurity's avatar
mdsecurity committed
46
47
48
49
50
				}
			}
		}
		return false;
	}
51

mdsecurity's avatar
mdsecurity committed
52
	openFor(): Time {
mdsecurity's avatar
mdsecurity committed
53
		const currTime = new Date();
54
55
56
57
58
59
60
61
62
63
64
65
66
		const today = currTime.getDay() - 1;
		const inSeconds = currTime.getHours() * 60 * 60 + currTime.getMinutes() * 60 + currTime.getSeconds();
		const dayOfWeek = currTime.getDay() - 1;
		let timeTilClose = new Time();
		let sumSeconds = 0;
		// clean up if statements later
		if (this.isOpen()) {
			for (let i = 0; i < this.main_schedule_times.length; i++) {
				const day = this.main_schedule_times[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) {
mdsecurity's avatar
mdsecurity committed
67
						sumSeconds = day.end_time.inSeconds() + 86400 - inSeconds;
68
69
70
71
					}
				} else {
					sumSeconds = day.end_time.inSeconds() - inSeconds;
				}
mdsecurity's avatar
mdsecurity committed
72

mdsecurity's avatar
mdsecurity committed
73
				return timeTilClose.fromSeconds(sumSeconds);
74
			}
mdsecurity's avatar
mdsecurity committed
75
76
		} else {
			return timeTilClose;
77
		}
mdsecurity's avatar
mdsecurity committed
78
	}
mdsecurity's avatar
mdsecurity committed
79
}
mdsecurity's avatar
mdsecurity committed
80
	// useSpecial():
81