Skip to content

Commit

Permalink
Fixed potential concurrency issue with timer
Browse files Browse the repository at this point in the history
Changed duration of Temtem hunt from long to AtomicLong to avoid
concurrency issues when updating timer at the same time as trying to
read time to update Temtem/h

Fixed temtem/h not getting set back to 0 when all Temtem in table have
been removed
  • Loading branch information
mculig committed Mar 9, 2020
1 parent 5308506 commit 477b351
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion TemtemTracker/config/userSettings.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"mainWindowWidth":603,"mainWindowHeight":215,"timeToLumaProbability":0.5,"saiparkMode":true,"saiparkTemtem1":"Ganki","saiparkTemtem2":"Saipat","saiparkTemtem1ChanceMultiplyer":2.5,"saiparkTemtem2ChanceMultiplyer":2.5}
{"mainWindowWidth":603,"mainWindowHeight":271,"timeToLumaProbability":0.5,"saiparkMode":false,"saiparkTemtem1":"Ganki","saiparkTemtem2":"Saipat","saiparkTemtem1ChanceMultiplyer":2.5,"saiparkTemtem2ChanceMultiplyer":2.5}
2 changes: 1 addition & 1 deletion TemtemTracker/src/menuBar/CSVExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String getDescription() {
bw.newLine();
bw.write("Time,Temtem/h");
bw.newLine();
bw.write(DurationFormatUtils.formatDuration(table.timer.durationTime, "HH:mm:ss") + "," + table.timer.temtemCount/(table.timer.durationTime/(double)3600000));
bw.write(DurationFormatUtils.formatDuration(table.timer.durationTime.get(), "HH:mm:ss") + "," + table.timer.temtemCount/(table.timer.durationTime.get()/(double)3600000));
bw.newLine();
bw.close();
} catch (FileNotFoundException e1) {
Expand Down
4 changes: 3 additions & 1 deletion TemtemTracker/src/temtemTableData/TimerData.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package temtemTableData;

import java.util.concurrent.atomic.AtomicLong;

public class TimerData {

public int temtemCount=0;
public long durationTime=0;
public AtomicLong durationTime;

}
13 changes: 8 additions & 5 deletions TemtemTracker/src/temtemTableUI/HuntingTimerUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public HuntingTimerUI(TimerData timerData) {
}

private void updateTimeDisplay() {
time = DurationFormatUtils.formatDuration(timerData.durationTime, "HH:mm:ss");
time = DurationFormatUtils.formatDuration(timerData.durationTime.get(), "HH:mm:ss");
timeLabel.setText(time);
this.revalidate();
this.repaint();
Expand All @@ -101,7 +101,7 @@ private void refreshDisplay() {

public void updateTime(){

timerData.durationTime += 1000;
timerData.durationTime.addAndGet(1000);
updateTimeDisplay();
}

Expand All @@ -111,10 +111,13 @@ public void updateCount(int temtemCount) {
}

public void updateCount() {
if(timerData.temtemCount!=0 && timerData.durationTime!=0) {
temtemH = String.format("%.2f", (timerData.temtemCount/((double)timerData.durationTime/ 3600000)));
if(timerData.temtemCount!=0 && timerData.durationTime.get()!=0) {
temtemH = String.format("%.2f", (timerData.temtemCount/((double)timerData.durationTime.get()/ 3600000)));
temtemHLabel.setText(temtemH);
}
else {
temtemHLabel.setText("0.0");
}

this.revalidate();
this.repaint();
Expand All @@ -123,7 +126,7 @@ public void updateCount() {
public void reset() {
time="00:00";
temtemH="0.0";
timerData.durationTime = 0;
timerData.durationTime.getAndSet(0);

timeLabel.setText(time);
temtemHLabel.setText(temtemH);
Expand Down
10 changes: 6 additions & 4 deletions TemtemTracker/src/temtemTableUI/TemtemTableUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicLong;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
Expand Down Expand Up @@ -50,11 +51,11 @@ public void addTemtem(String name) {
// Calculate stuff
temtemTable.total.encountered++;
temtemTable.total.lumaChance = calculator.calculateChance(temtemTable.total.encountered, temtemTable.total.name);
temtemTable.total.timeToLuma = calculator.getTimeToLuma(temtemTable.total.encountered, temtemTable.timer.durationTime, temtemTable.total.name);
temtemTable.total.timeToLuma = calculator.getTimeToLuma(temtemTable.total.encountered, temtemTable.timer.durationTime.get(), temtemTable.total.name);
targetRow.encountered++;
targetRow.lumaChance = calculator.calculateChance(targetRow.encountered, targetRow.name);
targetRow.encounteredPercent = targetRow.encountered / (double) temtemTable.total.encountered;
targetRow.timeToLuma = calculator.getTimeToLuma(targetRow.encountered, temtemTable.timer.durationTime, targetRow.name);
targetRow.timeToLuma = calculator.getTimeToLuma(targetRow.encountered, temtemTable.timer.durationTime.get(), targetRow.name);
UIRows.get(targetRow).update();
total.update();
UIRows.forEach((tableRow, UIElement) -> {
Expand Down Expand Up @@ -112,6 +113,7 @@ private void initializeTable(TemtemDataTable table) {
temtemTable.total.encounteredPercent = 1;
temtemTable.total.lumaChance = 0;
temtemTable.timer = new TimerData();
temtemTable.timer.durationTime = new AtomicLong(0);
} else {
temtemTable = table;
}
Expand Down Expand Up @@ -161,10 +163,10 @@ public TemtemDataTable getTable() {

public void recalculateLumaTimes() {
for(TableDataRow dataRow : temtemTable.rows) {
dataRow.timeToLuma = calculator.getTimeToLuma(dataRow.encountered, temtemTable.timer.durationTime, dataRow.name);
dataRow.timeToLuma = calculator.getTimeToLuma(dataRow.encountered, temtemTable.timer.durationTime.get(), dataRow.name);
UIRows.get(dataRow).update();
}
temtemTable.total.timeToLuma = calculator.getTimeToLuma(temtemTable.total.encountered, temtemTable.timer.durationTime, temtemTable.total.name);
temtemTable.total.timeToLuma = calculator.getTimeToLuma(temtemTable.total.encountered, temtemTable.timer.durationTime.get(), temtemTable.total.name);
total.update();
}

Expand Down

0 comments on commit 477b351

Please sign in to comment.