Skip to content

Change Json format for #2#14

Open
IsaacPD wants to merge 17 commits into
MetaStudents:masterfrom
IsaacPD:master
Open

Change Json format for #2#14
IsaacPD wants to merge 17 commits into
MetaStudents:masterfrom
IsaacPD:master

Conversation

@IsaacPD
Copy link
Copy Markdown

@IsaacPD IsaacPD commented Aug 4, 2018

Resolves #2 . See test.json for the new format.
"project" - has an int value that represents the total work required to complete the project
"homework" - is an array of int arrays with 2 values [size, duration], homeworks of that size will be assigned for duration # of classes [0, x] will "assign" no HW for x days.
"exceptions" - is an array of days which there are no lectures or hw assigned. i.e. for [x, y, z] the x class from the 0th class is canceled.

@IsaacPD IsaacPD changed the title resolves #2 Change Json format for #2 Aug 4, 2018
Copy link
Copy Markdown
Contributor

@meygerjos meygerjos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about the daylight savings stuff if you don't want. The real solution (as opposed to a quick fix, which I had done) to it is to have a Day class with no time component which keeps tracks of numbers of days in months and leap years. Doing this real solution now is out of the scope of this PR. (You could do a quick fix though if you feel like it.)

Comment thread Source/Lecture.hx
var start = Util.splitAndParseInt(lecture.days[0].time.split("-")[0], ":");

this.startDate = new Date(lecture.start.year, lecture.start.month, lecture.start.day, start[0], start[1], 0);
var delta = lecture.days[0].weekday - startDate.getDay();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if delta is negative? In this case it should be incremented by 7.

Comment thread Source/Lecture.hx
this.startDate = new Date(lecture.start.year, lecture.start.month, lecture.start.day, start[0], start[1], 0);
var delta = lecture.days[0].weekday - startDate.getDay();

startDate = DateTools.delta(this.startDate, DateTools.days(delta));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not guaranteed that startDate.getHours() does not change in this step. You are adding a whole multiple of 24 hours, but some days have 23 or 25 hours.

Comment thread Source/Lecture.hx
for (i in lecture.days) {
times.push(i.time);
weekdays.push(i.weekday);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then why not have a times array and a weekdays array in the json like you originally suggested, since you are converting it to that anyway?

Comment thread Source/Bar.hx Outdated
private var workRate:Float;
private var lectures:Array<Lecture>;
private var lectures:Lecture.LectureObject;
private var lectureNum:Int;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete lectureNum if you are no longer using it.

Comment thread Source/Bar.hx Outdated

//Returns the next date that a homwork will be due/lecture will occur
private function nextHomework(gameDate:Date):Date {
var nextIndex = (lectures.weekdays.indexOf(gameDate.getDay()) + 1) % lectures.weekdays.length;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are two lectures on the same day? E.g. what if lectures.weekdays == [1,2,2,5]? You'll end up doing the Tuesday lecture over and over.

Comment thread Source/Bar.hx Outdated
if (delta <= 0) {
delta = 7 + delta;
}
delta = DateTools.days(delta) + DateTools.hours(nextStart[0] - gameDate.getHours()) + DateTools.minutes(nextStart[1] - gameDate.getMinutes());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could cause problems with daylight savings.

Comment thread Source/Bar.hx Outdated
var nextIndex = (lectures.weekdays.indexOf(gameDate.getDay()) + 1) % lectures.weekdays.length;
var next = lectures.weekdays[nextIndex];
var nextStart = Util.splitAndParseInt(lectures.times[nextIndex].split("-")[0], ":");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be [1] because the homework gets assigned when the lecture ends.

Comment thread Source/Schedule.hx Outdated
}
var index = lecture.weekdays.indexOf(j);
// If lecture is not held within the range of this week
if (!Util.DayinRange(lecture.startDate, lecture.endDate, cursorDate))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is cursorDate incremented at the start of the for loop? Wouldn't this cause the last lecture to be canceled if it occurs on lecture.endDate?

Comment thread Source/Schedule.hx Outdated
lecture.curLectureNum++;
continue;
}
var index = lecture.weekdays.indexOf(j);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work if lecture.weekdays is something like [1,3,5,2,4]. We could perhaps set index = lecture.weekdays[lecture.curLectureNum % lecture.weekdays.length instead.

Comment thread Source/Lecture.hx Outdated
public var homework:Array<Array<Int>>;
public var title:String;
public var exceptions:Array<Int>;
public var curLectureNum:Int;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool idea to include curLectureNum as a field of LectureObject. Maybe LectureObject could be an Iterator.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It occurs to me that curLectureNum is only used in Schedule, but not in Bar.

Copy link
Copy Markdown
Contributor

@meygerjos meygerjos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an alternative approach that might be easier. First parse the json into an object, then convert that object into an array of lectures, similar to what was in the original json format. This conversion would handle all the mod stuff and exceptions and everything all at once, and you wouldn't need to handle it twice in Bar and Schedule. The only problem with this is it wouldn't as easily generalize to an "infinite mode" where you have the same schedule every week for eternity; but we might not care about such a mode. What do you think?

Comment thread Source/Schedule.hx Outdated

var sunday = DateTools.delta(endDate, -DateTools.days(7));
for (lecture in scheduleObject) {
var lecCursor:Int = lecture.curLectureNum;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering why we never increment lecture.curLectureNum but I get it finally! Bar.getHomework() increments lecture.curLectureNum, so that after a week has gone by, it has incremented lecture.weekdays.length times. Then when Schedule.makeCols() is called, it is already incremented the desired amount. makeCols() shouldn't increment it because that would mess up Bar.getHomework(), so instead it uses lecCursor, a temporary variable.

I don't know if this is the best approach but it works for now.

Comment thread Source/Bar.hx Outdated

var delta:Float = next - gameDate.getDay();

if (delta < 0 || lectures.weekdays.length == 1) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if lectures.weekdays = [2,2]? Then this if block will never run!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could have nextIndex == 0 as the condition here instead.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the issue here, nextIndex == 0 however wouldn't work in the case of lecture.weekdays = [1,3,5,2,2] so a better case is use the full delta on the line after the if statement and use delta <= 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea.

Comment thread Source/Schedule.hx Outdated

lectureCols.push(lectureCol);
if (lecture.weekdays.length == 1) break;
} while (lecture.weekdays[lecCursor % lecture.weekdays.length] >= lecture.weekdays[(lecCursor - 1) % lecture.weekdays.length]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if lecture.weekdays == [2,2]? Then the while condition will always be satisfied, and the break statements will never be run.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better condition is lecCursor % lecture.weekdays.length == 0.

Copy link
Copy Markdown
Author

@IsaacPD IsaacPD Aug 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better condition I propose is similar to that I mentioned above. That is check if delta between this lecture and the next one is < 0. This will additionally allow cycles such as these [1,3,4,4] where the 2nd 4 occurs during a time before the 1st 4. This however doesn't allow two week cycles of the type weekdays = [2,2], times = [10:30-11:30, 14:30-15:30] which is also currently not capable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point that such cycles are not capable. Maybe my idea of doing two week cycles like weekdays = [1, 3, 5, 2, 4] is shortsighted because it doesn't allow for periods of more than 7 days between consecutive classes. What if we used weekdays = [1, 3, 5, 9, 11] instead and also had a field cycleLength = 14?

Comment thread Source/Schedule.hx Outdated
if (lecture.exceptions.indexOf(lecCursor) != -1) {
lecCursor++;
if (lecture.weekdays.length == 1) break;
continue;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These if blocks were confusing me for a while but now I understand. They occur in 2 places: 1) right before continue statements and 2) right before the end of the do block. This is because in both places we are about to evaluate the while condition. So the if blocks are an additional condition tacked on to the condition in the while statement.

In general,

do {
    [stuff1]
    if (something) {
        [stuff2]
        if (otherCondition) break;
        continue;
    }
    [stuff3]
    }
    if (otherCondition) break;
} while (condition);

is equivalent to

do {
    [stuff1]
    if (something) {
        [stuff2]
        continue;
    }
    [stuff3]
    }
} while (!otherCondition && condition);

It would be cleaner to do it the second way.

But it turns out that !otherCondition && condition is not sufficient either: see the comment below on the while condition.

@IsaacPD
Copy link
Copy Markdown
Author

IsaacPD commented Aug 7, 2018

As for the alternative approach my main concern is how much memory it would take up to store all those arrays of lectures ahead of time. And you would additionally loose the functionality you mentioned above so it isn't really convincing me.

@meygerjos
Copy link
Copy Markdown
Contributor

OK here's an alternative alternative approach. Don't convert into an array of lectures, convert into an iterator object that you can interact with kind of like an array, but which can be infinite, and which doesn't store all the lectures internally. All the exception stuff could be dealt with internally. And Bar and Schedule could use two different copies of this object so that they don't step on each others toes.

@IsaacPD
Copy link
Copy Markdown
Author

IsaacPD commented Sep 3, 2018

This line in Lecture.hx delta += Datetools.days(7); presents issues with daylight savings as well as making it incompatible with lectures that have schedules in the form of [Week A Tues, Week B Monday,...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants