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

mdsecurity's avatar
mdsecurity committed
58
	openFor(): Time {
mdsecurity's avatar
mdsecurity committed
59
		const currTime = new Date();
60
61
62
63
64
65
66
67
68
69
70
71
72
		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
73
						sumSeconds = day.end_time.inSeconds() + 86400 - inSeconds;
74
75
76
77
					}
				} else {
					sumSeconds = day.end_time.inSeconds() - inSeconds;
				}
mdsecurity's avatar
mdsecurity committed
78

mdsecurity's avatar
mdsecurity committed
79
				return timeTilClose.fromSeconds(sumSeconds);
80
			}
mdsecurity's avatar
mdsecurity committed
81
82
		} else {
			return timeTilClose;
83
		}
mdsecurity's avatar
mdsecurity committed
84
	}
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	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
100
}
101

102