Change Json format for #2#14
Conversation
…igned at these points)
meygerjos
left a comment
There was a problem hiding this comment.
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.)
| 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(); |
There was a problem hiding this comment.
What if delta is negative? In this case it should be incremented by 7.
| 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)); |
There was a problem hiding this comment.
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.
| for (i in lecture.days) { | ||
| times.push(i.time); | ||
| weekdays.push(i.weekday); | ||
| } |
There was a problem hiding this comment.
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?
| private var workRate:Float; | ||
| private var lectures:Array<Lecture>; | ||
| private var lectures:Lecture.LectureObject; | ||
| private var lectureNum:Int; |
There was a problem hiding this comment.
You can delete lectureNum if you are no longer using it.
|
|
||
| //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; |
There was a problem hiding this comment.
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.
| if (delta <= 0) { | ||
| delta = 7 + delta; | ||
| } | ||
| delta = DateTools.days(delta) + DateTools.hours(nextStart[0] - gameDate.getHours()) + DateTools.minutes(nextStart[1] - gameDate.getMinutes()); |
There was a problem hiding this comment.
This could cause problems with daylight savings.
| 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], ":"); | ||
|
|
There was a problem hiding this comment.
This should be [1] because the homework gets assigned when the lecture ends.
| } | ||
| 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)) |
There was a problem hiding this comment.
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?
| lecture.curLectureNum++; | ||
| continue; | ||
| } | ||
| var index = lecture.weekdays.indexOf(j); |
There was a problem hiding this comment.
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.
| public var homework:Array<Array<Int>>; | ||
| public var title:String; | ||
| public var exceptions:Array<Int>; | ||
| public var curLectureNum:Int; |
There was a problem hiding this comment.
Cool idea to include curLectureNum as a field of LectureObject. Maybe LectureObject could be an Iterator.
There was a problem hiding this comment.
It occurs to me that curLectureNum is only used in Schedule, but not in Bar.
There was a problem hiding this comment.
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?
|
|
||
| var sunday = DateTools.delta(endDate, -DateTools.days(7)); | ||
| for (lecture in scheduleObject) { | ||
| var lecCursor:Int = lecture.curLectureNum; |
There was a problem hiding this comment.
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.
|
|
||
| var delta:Float = next - gameDate.getDay(); | ||
|
|
||
| if (delta < 0 || lectures.weekdays.length == 1) { |
There was a problem hiding this comment.
What if lectures.weekdays = [2,2]? Then this if block will never run!
There was a problem hiding this comment.
Maybe you could have nextIndex == 0 as the condition here instead.
There was a problem hiding this comment.
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
|
|
||
| lectureCols.push(lectureCol); | ||
| if (lecture.weekdays.length == 1) break; | ||
| } while (lecture.weekdays[lecCursor % lecture.weekdays.length] >= lecture.weekdays[(lecCursor - 1) % lecture.weekdays.length]); |
There was a problem hiding this comment.
What if lecture.weekdays == [2,2]? Then the while condition will always be satisfied, and the break statements will never be run.
There was a problem hiding this comment.
A better condition is lecCursor % lecture.weekdays.length == 0.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
| if (lecture.exceptions.indexOf(lecCursor) != -1) { | ||
| lecCursor++; | ||
| if (lecture.weekdays.length == 1) break; | ||
| continue; |
There was a problem hiding this comment.
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.
|
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. |
|
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 |
# Conflicts: # Source/Bar.hx # Source/Schedule.hx
|
This line in Lecture.hx |
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.