Skip to content

Commit

Permalink
refactor: moved minutesToDate procedure to utilsui2
Browse files Browse the repository at this point in the history
FossilOrigin-Name: 8f82f14782d51701da878d11086b22be1eb91f30b9cf94903c2b203fe19d933c
  • Loading branch information
thindil committed Jan 18, 2024
1 parent 10bccec commit b4ec0a7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 64 deletions.
64 changes: 0 additions & 64 deletions nim/src/ui/utilsui.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,6 @@ import ../[bases, config, crew2, crewinventory, events2, game, game2,
maps, messages, missions2, shipscargo, shipscrew, tk, types]
import combatui, coreui, dialogs, mapsui, shipsuicrew, shipsuimodules

proc minutesToDate*(minutes: int; infoText: var string) {.sideEffect, raises: [],
tags: [].} =
## Convert the game minutes to the game time in days, hours, etc
##
## * minutes - the amount of minutes to convert
## * infoText - the string to which the converted time will be added
##
## Returns the updated infoText paramater with converted minutes to the game
## time
var
travelTime: DateRecord
minutesDiff: int = minutes
while minutesDiff > 0:
if minutesDiff > 518_400:
minutesDiff = minutesDiff - 518_400
travelTime.year.inc()
elif minutesDiff in 43_201 .. 518_400:
minutesDiff = minutesDiff - 43_200
travelTime.month.inc()
if travelTime.month > 12:
travelTime.year.inc()
travelTime.month = 1
elif minutesDiff in 1_441..43_200:
minutesDiff = minutesDiff - 1_440
travelTime.day.inc()
if travelTime.day > 31:
travelTime.month.inc()
if travelTime.month > 12:
travelTime.year.inc()
travelTime.month = 1
elif minutesDiff in 61..1_440:
minutesDiff = minutesDiff - 60
travelTime.hour.inc()
if travelTime.hour > 23:
travelTime.hour = 0
travelTime.day.inc()
if travelTime.day > 31:
travelTime.day = 1
travelTime.month.inc()
if travelTime.month > 12:
travelTime.month = 1
travelTime.year.inc()
else:
travelTime.minutes = minutesDiff
minutesDiff = 0
if travelTime.year == 4_000_000:
break
if travelTime.year > 0:
infoText = infoText & " " & $travelTime.year & "y"
if travelTime.month > 0:
infoText = infoText & " " & $travelTime.month & "m"
if travelTime.day > 0:
infoText = infoText & " " & $travelTime.day & "d"
if travelTime.hour > 0:
infoText = infoText & " " & $travelTime.hour & "h"
if travelTime.minutes > 0:
infoText = infoText & " " & $travelTime.minutes & "mins"

proc resizeCanvasCommand(clientData: cint; interp: PInterp; argc: cint;
argv: openArray[cstring]): TclResults {.sideEffect, raises: [], tags: [].} =
## Resize the selected canvas
Expand Down Expand Up @@ -329,9 +271,3 @@ proc addAdaUtilsCommands() {.raises: [], tags: [], exportc.} =
proc deleteAdaWidgets*(startIndex, endIndex: cint; frame: cstring) {.exportc,
gcsafe, sideEffect, raises: [], tags: [].} =
deleteWidgets(startIndex, endIndex, $frame)

proc minutesAdaToDate*(minutes: cint; infoText: var cstring) {.exportc, gcsafe,
sideEffect, raises: [], tags: [].} =
var nimText = $infoText
minutesToDate(minutes, nimText)
infoText = nimText.cstring
64 changes: 64 additions & 0 deletions nim/src/ui/utilsui2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,64 @@ proc travelInfo*(distance: Positive): array[1 .. 2, Natural] {.sideEffect,
result[1] = minutesDiff + (rests * restTime)
result[2] = abs(distance * countFuelNeeded()) + (rests * (restTime / 10).int)

proc minutesToDate*(minutes: int; infoText: var string) {.sideEffect, raises: [],
tags: [].} =
## Convert the game minutes to the game time in days, hours, etc
##
## * minutes - the amount of minutes to convert
## * infoText - the string to which the converted time will be added
##
## Returns the updated infoText paramater with converted minutes to the game
## time
var
travelTime: DateRecord
minutesDiff: int = minutes
while minutesDiff > 0:
if minutesDiff > 518_400:
minutesDiff = minutesDiff - 518_400
travelTime.year.inc()
elif minutesDiff in 43_201 .. 518_400:
minutesDiff = minutesDiff - 43_200
travelTime.month.inc()
if travelTime.month > 12:
travelTime.year.inc()
travelTime.month = 1
elif minutesDiff in 1_441..43_200:
minutesDiff = minutesDiff - 1_440
travelTime.day.inc()
if travelTime.day > 31:
travelTime.month.inc()
if travelTime.month > 12:
travelTime.year.inc()
travelTime.month = 1
elif minutesDiff in 61..1_440:
minutesDiff = minutesDiff - 60
travelTime.hour.inc()
if travelTime.hour > 23:
travelTime.hour = 0
travelTime.day.inc()
if travelTime.day > 31:
travelTime.day = 1
travelTime.month.inc()
if travelTime.month > 12:
travelTime.month = 1
travelTime.year.inc()
else:
travelTime.minutes = minutesDiff
minutesDiff = 0
if travelTime.year == 4_000_000:
break
if travelTime.year > 0:
infoText = infoText & " " & $travelTime.year & "y"
if travelTime.month > 0:
infoText = infoText & " " & $travelTime.month & "m"
if travelTime.day > 0:
infoText = infoText & " " & $travelTime.day & "d"
if travelTime.hour > 0:
infoText = infoText & " " & $travelTime.hour & "h"
if travelTime.minutes > 0:
infoText = infoText & " " & $travelTime.minutes & "mins"

# Temporary code for interfacing with Ada

proc showAdaScreen(newScreenName: cstring) {.exportc, raises: [], tags: [].} =
Expand All @@ -213,3 +271,9 @@ proc travelAdaInfo(distance: cint; res: var array[1 .. 2, cint]) {.exportc,
res = [nimRes[1].cint, nimRes[2].cint]
except:
discard

proc minutesAdaToDate*(minutes: cint; infoText: var cstring) {.exportc, gcsafe,
sideEffect, raises: [], tags: [].} =
var nimText = $infoText
minutesToDate(minutes, nimText)
infoText = nimText.cstring

0 comments on commit b4ec0a7

Please sign in to comment.