complete refactoring

This commit is contained in:
2023-03-04 01:12:54 +04:00
parent 929fa76329
commit 5209f26ec5
18 changed files with 488 additions and 250 deletions

8
scripts/consts.gd Normal file
View File

@ -0,0 +1,8 @@
class_name Consts
const START := "start"
const STOP := "stop"
const NO_TIME := "00:00:00"
const ONGOING := "ongoing"
const THEME_OVERRIDE_START := "play_button"
const THEME_OVERRIDE_STOP := "stop_button"

77
scripts/time_entry.gd Normal file
View File

@ -0,0 +1,77 @@
## Describes a row in a timesheet
## Has a beginning and an end
class_name TimeEntry
var name := ""
var closed := false
var start_time := TimeStamp.new()
var end_time := TimeStamp.new()
var previous_total := 0
func start_recording() -> TimeEntry:
start_time = start_time.from_current_time()
end_time = end_time.from_current_time()
return self
func update() -> void:
end_time = end_time.from_current_time()
func get_elapsed_seconds() -> int:
var elapsed := end_time.get_difference(start_time)
return elapsed
func get_total_elapsed_seconds() -> int:
var elapsed := get_elapsed_seconds() + previous_total
return elapsed
func get_period() -> String:
var time_in_secs := get_elapsed_seconds()
return TimeEntry.time_to_period(time_in_secs)
func get_total_period() -> String:
var time_in_secs := get_total_elapsed_seconds()
return TimeEntry.time_to_period(time_in_secs)
static func time_to_period(time_in_secs: int) -> String:
var seconds := time_in_secs%60
@warning_ignore("integer_division")
var minutes := (time_in_secs/60)%60
@warning_ignore("integer_division")
var hours := (time_in_secs/60)/60
return "%02d:%02d:%02d" % [hours, minutes, seconds]
func to_csv_line() -> PackedStringArray:
return PackedStringArray([
name,
start_time,
end_time,
str(get_elapsed_seconds()) if closed else tr(Consts.ONGOING)
])
static func is_csv_line_valid(line: PackedStringArray) -> bool:
return line.size() > 3
func from_csv_line(line: PackedStringArray) -> TimeEntry:
name = line[0]
var start_time_string = line[1]
start_time.from_string(start_time_string)
var elapsed_seconds = int(line[3]) if line[3].is_valid_int() else 0
closed = elapsed_seconds > 0
if closed == true:
var end_time_string = line[2]
end_time.from_string(end_time_string)
else:
end_time.from_current_time()
return self

84
scripts/time_sheet.gd Normal file
View File

@ -0,0 +1,84 @@
class_name TimeSheet
var source_path := ""
var entries: Array[TimeEntry] = []
var entries_names := {}
var current_entry: TimeEntry
## Loads the data file
func load_file() -> bool:
var file := FileAccess.open(source_path, FileAccess.READ)
if file == null:
file = FileAccess.open(source_path, FileAccess.WRITE)
if file == null:
printerr("Failed to open file %s"%[ProjectSettings.globalize_path(source_path)])
return false
return true
while not file.eof_reached():
var line := file.get_csv_line()
if line.size() == 0 or "".join(line).length() == 0:
continue
if not TimeEntry.is_csv_line_valid(line):
push_warning("CSV Line `%s` is not conform"%[",".join(line)])
continue
var entry := TimeEntry.new().from_csv_line(line)
entries.append(entry)
if entry.closed == false:
current_entry = entry
if not entries_names.has(entry.name):
entries_names[entry.name] = 0
entries_names[entry.name] += entry.get_elapsed_seconds()
file.close()
return true
## Adds a new time entry to the tree and to the data file
func start_entry(entry_name: String) -> void:
current_entry = TimeEntry.new().start_recording()
current_entry.name = entry_name
current_entry.closed = false
if entry_name in entries_names:
current_entry.previous_total = entries_names[entry_name]
var file := FileAccess.open(source_path, FileAccess.READ_WRITE)
if file == null:
printerr("Could not open file")
entries.append(current_entry)
file.store_csv_line(current_entry.to_csv_line())
func update() -> void:
current_entry.update()
entries_names[current_entry.name] = current_entry.get_total_elapsed_seconds()
func close_entry() -> void:
current_entry.closed = true
save()
func get_period() -> String:
return current_entry.get_period()
func get_total_elapsed_seconds() -> int:
return current_entry.get_total_elapsed_seconds()
func save() -> void:
var file := FileAccess.open(source_path, FileAccess.WRITE)
if file == null:
printerr("Could not open file")
for time_entry in entries:
file.store_csv_line(time_entry.to_csv_line())
static func restore(file_path: String) -> TimeSheet:
var timesheet := TimeSheet.new()
timesheet.source_path = file_path
var success := timesheet.load_file()
if success:
return timesheet
return null

54
scripts/time_stamp.gd Normal file
View File

@ -0,0 +1,54 @@
## A simple proxy for the object returned by Godot's time functions
## Ensures proper typing
class_name TimeStamp
var year := 0
var month := 0
var day := 0
var weekday := 0
var hour := 0
var minute := 0
var second := 0
var unix := 0
func from_current_time() -> TimeStamp:
return from_dict(Time.get_datetime_dict_from_system())
func from_dict(time: Dictionary) -> TimeStamp:
year = time.year
month = time.month
day = time.day
weekday = time.weekday
hour = time.hour
minute = time.minute
second = time.second
unix = Time.get_unix_time_from_datetime_dict(time)
return self
func to_dict() -> Dictionary:
return {
year = year,
month = month,
day = day,
weekday = weekday,
hour = hour,
minute = minute,
second = second,
}
func get_difference(other_timestamp: TimeStamp) -> int:
return unix - other_timestamp.unix
func from_string(time_string: String) -> TimeStamp:
var time := Time.get_datetime_dict_from_datetime_string(time_string, true)
return from_dict(time)
func _to_string() -> String:
return Time.get_datetime_string_from_datetime_dict(to_dict(), false)