diff options
Diffstat (limited to 'src/models')
| -rw-r--r-- | src/models/EventInfo.ts | 88 | ||||
| -rw-r--r-- | src/models/Lab.ts | 59 | ||||
| -rw-r--r-- | src/models/PeerTeacher.ts | 85 |
3 files changed, 140 insertions, 92 deletions
diff --git a/src/models/EventInfo.ts b/src/models/EventInfo.ts index 962ec70..b0ac25b 100644 --- a/src/models/EventInfo.ts +++ b/src/models/EventInfo.ts @@ -1,46 +1,60 @@ -export default class EventInfo { - days: string; - - start: number; - - end: number; - - constructor(days: string = '', start = 0, end = 0) { - this.days = days; - this.start = start; - this.end = end; - } - - static timeToStr(time: number) { - let hour = Math.floor(time / 100); - const minute = time % 100; - const meridiem = (hour < 12) ? 'AM' : 'PM'; +interface EventInfoSerializeInfo { + days: string, + start: number, + end: number +} - if (hour === 0) { - hour = 12; - } else if (hour > 12) { - hour -= 12; +export default class EventInfo { + days: string; + start: number; + end: number; + + constructor(days: string, start: number | string, end: number | string) { + if (typeof start === "string") { + start = parseInt(start, 10); + } + if (typeof end === "string") { + end = parseInt(end, 10); + } + + this.days = days; + this.start = start; + this.end = end; } - if (minute < 10) { - return `${hour}:0${minute} ${meridiem}`; + static fromJSON({days, start, end}: EventInfoSerializeInfo) { + return new EventInfo(days, start, end); } - return `${hour}:${minute} ${meridiem}`; - } - conflictsWith(event: EventInfo) { - const daysConflict = event.days.match(new RegExp(`[${this.days}]`)); + static timeToStr(time: number) { + let hour = Math.floor(time / 100); + const minute = time % 100; + const meridiem = (hour < 12) ? 'AM' : 'PM'; + + if (hour === 0) { + hour = 12; + } else if (hour > 12) { + hour -= 12; + } + + if (minute < 10) { + return `${hour}:0${minute} ${meridiem}`; + } + return `${hour}:${minute} ${meridiem}`; + } - if (daysConflict) { - return (this.start <= event.end) && (event.start <= this.end); + conflictsWith(event: EventInfo) { + const daysConflict = event.days.match(new RegExp(`[${this.days}]`)); + return daysConflict && this.start <= event.end && event.start <= this.end; } - return false; - } - get info() { - if (this.days === '') { - return 'WEB'; + get info() { + if(this.days === "") { + return `WEB`; + }else if(this.start === -1 || this.end === -1) { + return `${this.days}`; + } else { + return `${this.days} ${EventInfo.timeToStr(this.start)}-${EventInfo.timeToStr(this.end)}`; + } } - return `${this.days} ${EventInfo.timeToStr(this.start)}-${EventInfo.timeToStr(this.end)}`; - } -} +}
\ No newline at end of file diff --git a/src/models/Lab.ts b/src/models/Lab.ts index a6972ef..d23cffb 100644 --- a/src/models/Lab.ts +++ b/src/models/Lab.ts @@ -1,23 +1,50 @@ -import EventInfo from '@/models/EventInfo'; +import EventInfo from "./EventInfo"; + +interface LabSerializeInfo { + course: number, + section: number, + event: { + days: string, + start: number, + end: number + }, + building: string, + room: string +} export default class Lab { - course: number; + id: number; + course: number; + section: number; + event: EventInfo; + building: string; + room: string; - section: number; + constructor(course: number | string, section: number | string, event: EventInfo, building = "", room = "") { + if(typeof course === "string") { + course = parseInt(course, 10); + } + if(typeof section === "string") { + section = parseInt(section, 10); + } - event: EventInfo; + this.id = parseInt(`${course}${section}`, 10); + this.course = course; + this.section = section; + this.event = event; + this.building = building; + this.room = room; + } - constructor(course = 0, section = 0, event = new EventInfo()) { - this.course = course; - this.section = section; - this.event = event; - } + static fromJSON({course, section, event, building, room}: LabSerializeInfo) { + return new Lab(course, section, EventInfo.fromJSON(event), building, room); + } - get id() { - return `${this.course}-${this.section}`; - } + get time() { + return this.event.info; + } - get fullInfo() { - return `${this.id} ${this.event.info}`; - } -} + get location() { + return `${this.building}-${this.room}`; + } +}
\ No newline at end of file diff --git a/src/models/PeerTeacher.ts b/src/models/PeerTeacher.ts index f387431..5d955fd 100644 --- a/src/models/PeerTeacher.ts +++ b/src/models/PeerTeacher.ts @@ -1,41 +1,48 @@ -import EventInfo from './EventInfo'; +import EventInfo from "./EventInfo"; + +interface PeerTeacherSerializeInfo { + id: number, + firstname: string, + lastname: string, + events: { + days: string, + start: number, + end: number + }[], + labs: number[] +} export default class PeerTeacher { - firstname: string; - - lastname: string; - - uin: number; - - events: EventInfo[]; - - assignedLabs: Set<string>; - - constructor(firstname = '', lastname = '', uin = 0) { - this.firstname = firstname; - this.lastname = lastname; - this.uin = uin; - this.events = []; - this.assignedLabs = new Set(); - } - - conflictsWith(event: EventInfo) { - let conflicts = false; - this.events.every((item) => { - if (item.conflictsWith(event)) { - conflicts = true; - return false; - } - return true; - }); - return conflicts; - } - - get name() { - return `${this.firstname} ${this.lastname}`; - } - - get id() { - return this.uin; - } -} + id: number; + firstname: string; + lastname: string; + events: EventInfo[]; + labs: Set<number>; + + constructor(id: number | string, firstname: string, lastname: string) { + if(typeof id === "string") { + id = parseInt(id, 10); + } + + this.id = id; + this.firstname = firstname; + this.lastname = lastname; + this.events = []; + this.labs = new Set(); + } + + static fromJSON({id, firstname, lastname, events, labs}: PeerTeacherSerializeInfo) { + const pt = new PeerTeacher(id, firstname, lastname); + pt.events = events.map(e => EventInfo.fromJSON(e)); + pt.labs = new Set(labs); + return pt; + } + + conflictsWith(event: EventInfo) { + return this.events.some(item => item.conflictsWith(event)); + } + + get name() { + return `${this.firstname} ${this.lastname}`; + } +}
\ No newline at end of file |
