Skip to content

Commit

Permalink
Consider all running clocks when doing clock sync.
Browse files Browse the repository at this point in the history
This is simpler, and avoids going out of sync.

Fix precision issue in isDisplayChange.

Fixes #191
  • Loading branch information
brian-brazil committed May 25, 2019
1 parent 1ca97f5 commit fdbc329
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
26 changes: 8 additions & 18 deletions src/com/carolinarollergirls/scoreboard/core/impl/ClockImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ protected boolean isDisplayChange(long current, long last) {
//the frontend rounds values that are not full seconds to the earlier second
//i.e. 3600ms will be displayed as 3s on a count up clock and as 4s on a count down clock.
if (isCountDirectionDown()) {
return Math.floor(((float)current-1)/1000) != Math.floor(((float)last-1)/1000);
return Math.floor(((double)current-1)/1000) != Math.floor(((double)last-1)/1000);
} else {
return Math.floor((float)current/1000) != Math.floor((float)last/1000);
return Math.floor((double)current/1000) != Math.floor((double)last/1000);
}
}

Expand Down Expand Up @@ -314,10 +314,6 @@ protected boolean isSyncTime() {
return Boolean.parseBoolean(getScoreBoard().getSettings().get(SETTING_SYNC));
}

protected boolean isMasterClock() {
return getId() == ID_PERIOD || getId() == ID_TIMEOUT || getId() == ID_INTERMISSION;
}

protected long lastTime;
protected boolean isRunning = false;

Expand Down Expand Up @@ -363,18 +359,13 @@ public UpdateClockTimerTask() {

public void addClock(ClockImpl c, boolean quickAdd) {
synchronized (coreLock) {
if (c.isMasterClock()) {
masterClock = c;
}
if (c.isSyncTime() && !quickAdd) {
if (c.isSyncTime() && !quickAdd && !clocks.isEmpty()) {
// This syncs all the clocks to change second at the same time
// with respect to the master clock
long nowMs = currentTime % 1000;
if (masterClock != null) {
nowMs = masterClock.getTime() % 1000;
if (masterClock.isCountDirectionDown()) {
nowMs = (1000 - nowMs) % 1000;
}
// with respect to the running clocks.
ClockImpl masterClock = clocks.get(0);
long nowMs = masterClock.getTime() % 1000;
if (masterClock.isCountDirectionDown()) {
nowMs = (1000 - nowMs) % 1000;
}

long timeMs = c.getTime() % 1000;
Expand Down Expand Up @@ -447,7 +438,6 @@ public long getCurrentTime() {
private long currentTime = 0;
private long startSystemTime = 0;
private long ticks = 0;
protected ClockImpl masterClock = null;
ArrayList<ClockImpl> clocks = new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public void testDefaults() {

assertEquals(ID, clock.getId());
assertEquals(ID, clock.getName());
assertFalse(clock.isMasterClock());
assertFalse(clock.isCountDirectionDown());
assertFalse(clock.isRunning());

Expand Down Expand Up @@ -133,7 +132,6 @@ public void testRestoreSnapshot() {

@Test
public void testSetting_ClockSync() {
//add a master clock
ClockImpl clock2 = new ClockImpl(sb, Clock.ID_TIMEOUT);
sb.getSettings().set(Clock.SETTING_SYNC, String.valueOf(true));
clock.setMaximumTime(10000);
Expand All @@ -145,8 +143,12 @@ public void testSetting_ClockSync() {
clock.setTime(4200);
assertEquals(4200, clock.getTime());

//when the clocks are started the non-master clock is synced to the master clock
//the first clock started has its time rounded
clock2.start();
assertEquals(3000, clock2.getTime());
advance(400);

//when the clocks are started the just started clock is synced to the already running
clock.start();
assertEquals(4400, clock.getTime());

Expand Down

0 comments on commit fdbc329

Please sign in to comment.