Skip to content

Commit

Permalink
Replace prints with a log module.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwatsonforster committed Sep 25, 2023
1 parent 1a7de97 commit 417081e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 52 deletions.
79 changes: 40 additions & 39 deletions airpower/aircraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import airpower.draw as apdraw
import airpower.hex as aphex
import airpower.hexcode as aphexcode
import airpower.log as aplog
import airpower.map as apmap
import airpower.turn as apturn

Expand Down Expand Up @@ -95,27 +96,27 @@ def _formatposition(self):
def _formatifp(self):
return "FP %d" % (self._hfp + self._vfp)

def _report(self, s):
print("%s: turn %-2d : %s" % (self._name, apturn.turn(), s))
def _log(self, s):
aplog.log("%s: turn %-2d : %s" % (self._name, apturn.turn(), s))

def _reportbreak(self):
print()
def _logbreak(self):
aplog.logbreak()

def _reportposition(self):
self._report("%-5s : %-16s : %s" % ("", "", self._formatposition()))
def _logposition(self):
self._log("%-5s : %-16s : %s" % ("", "", self._formatposition()))

def _reportactionsandposition(self, action):
self._report("%-5s : %-16s : %s" % (self._formatifp(), action, self._formatposition()))
def _logactionsandposition(self, action):
self._log("%-5s : %-16s : %s" % (self._formatifp(), action, self._formatposition()))

def _reportevent(self, s):
self._report("%-5s : %s" % (self._formatifp(), s))
def _logevent(self, s):
self._log("%-5s : %s" % (self._formatifp(), s))

#############################################################################

# Elements

def _A(self, what):
self._reportevent("attack with %s." % what)
self._logevent("attack with %s." % what)

def _C(self, altitudechange):

Expand Down Expand Up @@ -165,7 +166,7 @@ def _H(self):
self._x, self._y = aphex.nextposition(self._x, self._y, self._facing)

def _K(self):
self._reportevent("aircraft has been killed.")
self._logevent("aircraft has been killed.")
self._destroyed = True

def _S(self, spbrfp):
Expand Down Expand Up @@ -364,7 +365,7 @@ def _doaction(self, action):
assert aphex.isvalidfacing(self._x, self._y, self._facing)
assert apaltitude.isvalidaltitude(self._altitude)

self._reportactionsandposition(action)
self._logactionsandposition(action)
self._drawflightpath(lastx, lasty)

self.checkforterraincollision()
Expand All @@ -390,12 +391,12 @@ def checkforterraincollision(self):
if self._altitude <= altitudeofterrain:
self._altitude = altitudeofterrain
self._altitudecarry = 0
self._reportevent("aircraft has collided with terrain at altitude %d." % altitudeofterrain)
self._logevent("aircraft has collided with terrain at altitude %d." % altitudeofterrain)
self._destroyed = True

def checkforleavingmap(self):
if not apmap.iswithinmap(self._x, self._y):
self._reportevent("aircraft has left the map.")
self._logevent("aircraft has left the map.")
self._leftmap = True

##############################################################################
Expand Down Expand Up @@ -470,7 +471,7 @@ def startmove(self, flighttype, powerap, actions):
self._sustainedturnap = 0
self._altitudeap = 0

self._report("--- start of move --")
self._log("--- start of move --")

if self._destroyed:
self._endmove()
Expand All @@ -480,9 +481,9 @@ def startmove(self, flighttype, powerap, actions):
self._endmove()
return

self._report("flight type is %s and power setting is %s (%+.1f APs)." % (self._flighttype, powersetting, powerap))
self._log("flight type is %s and power setting is %s (%+.1f APs)." % (self._flighttype, powersetting, powerap))

self._report("carrying %.1f FPs, %+.1f APs, and %s altitude levels." % (
self._log("carrying %.1f FPs, %+.1f APs, and %s altitude levels." % (
self._fpcarry, self._apcarry, apaltitude.formataltitudecarry(self._altitudecarry)
))

Expand All @@ -495,10 +496,10 @@ def startmove(self, flighttype, powerap, actions):
speed = "%.1f (LTS)" % self._speed
else:
speed = "%.1f" % self._speed
self._report("speed is %s and %.1f FPs are available." % (speed, self._fp))
self._log("speed is %s and %.1f FPs are available." % (speed, self._fp))

self._report("---")
self._reportactionsandposition("")
self._log("---")
self._logactionsandposition("")

self.continuemove(actions)

Expand All @@ -518,7 +519,7 @@ def continuemove(self, actions):
if fp + 1 > self._fp or self._destroyed or self._leftmap:

self._drawaircraft("end")
self._report("---")
self._log("---")
self._endmove()

else:
Expand All @@ -529,34 +530,34 @@ def _endmove(self):

if self._destroyed:

self._report("aircraft has been destroyed.")
self._log("aircraft has been destroyed.")

elif self._leftmap:

self._report("aircraft has left the map.")
self._log("aircraft has left the map.")

else:

self._report("used %d HFPs, %d VFPs, and %.1f SPBRFPs." % (self._hfp, self._vfp, self._spbrfp))
self._log("used %d HFPs, %d VFPs, and %.1f SPBRFPs." % (self._hfp, self._vfp, self._spbrfp))

if self._maxturnrate == None:
self._report("no turns.")
self._log("no turns.")
turnap = 0.0
else:
self._report("maximum turn rate is %s." % self._maxturnrate)
self._log("maximum turn rate is %s." % self._maxturnrate)
if self._maxturnrate == "EZ":
turnap = 0.0
else:
# TODO: Don't assume CL.
# TODO: Calculate this at the moment of the maximum turn, since it depends on the configuration.
turnap = -self._aircrafttype.turndragchart("CL")[self._maxturnrate]

self._report("power APs = %+.1f." % self._powerap)
self._report("turn APs = %+.1f and %+.1f." % (turnap, self._sustainedturnap))
self._report("altitude APs = %+.1f" % self._altitudeap)
self._report("SPBR APs = %+.1f." % (self._spbrap))
self._log("power APs = %+.1f." % self._powerap)
self._log("turn APs = %+.1f and %+.1f." % (turnap, self._sustainedturnap))
self._log("altitude APs = %+.1f" % self._altitudeap)
self._log("SPBR APs = %+.1f." % (self._spbrap))
ap = self._powerap + self._sustainedturnap + turnap + self._altitudeap + self._spbrap
self._report("total APs = %+.1f with %+.1f carry = %+.1f." % (ap, self._apcarry, ap + self._apcarry))
self._log("total APs = %+.1f with %+.1f carry = %+.1f." % (ap, self._apcarry, ap + self._apcarry))
ap += self._apcarry

initialspeed = self._speed
Expand All @@ -573,25 +574,25 @@ def _endmove(self):
self._speed -= 0.5 * (ap // aprate)
self._apcarry = ap % aprate
if self._speed != initialspeed:
self._report("speed changed from %.1f to %.1f." % (initialspeed, self._speed))
self._log("speed changed from %.1f to %.1f." % (initialspeed, self._speed))
else:
self._report("speed is unchanged at %.1f." % self._speed)
self._log("speed is unchanged at %.1f." % self._speed)

initialaltitudeband = self._altitudeband
self._altitudeband = apaltitude.altitudeband(self._altitude)
if self._altitudeband != initialaltitudeband:
self._report("altitude band changed from %s to %s." % (initialaltitudeband, self._altitudeband))
self._log("altitude band changed from %s to %s." % (initialaltitudeband, self._altitudeband))
else:
self._report("altitude band is unchanged at %s." % self._altitudeband)
self._log("altitude band is unchanged at %s." % self._altitudeband)

fp = self._hfp + self._vfp + self._spbrfp
self._fpcarry = self._fp - fp

self._report("carrying %.1f FPs, %+.1f APs, and %s altitude levels." % (
self._log("carrying %.1f FPs, %+.1f APs, and %s altitude levels." % (
self._fpcarry, self._apcarry, apaltitude.formataltitudecarry(self._altitudecarry)
))

self._save(apturn.turn())

self._report("--- end of move -- ")
self._reportbreak()
self._log("--- end of move -- ")
self._logbreak()
3 changes: 3 additions & 0 deletions airpower/azimuth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import airpower.log as aplog

_northfacing = 90

def setnorth(orientation):
Expand All @@ -18,6 +20,7 @@ def setnorth(orientation):
_northfacing = 270
else:
raise ValueError("\"%s\" is not a valid orientation for north.")
aplog.log("north is %s." % orientation)

def tofacing(azimuth):

Expand Down
5 changes: 5 additions & 0 deletions airpower/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def log(s):
print(s)

def logbreak():
print()
9 changes: 6 additions & 3 deletions airpower/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import airpower.draw as apdraw
import airpower.hex as aphex
import airpower.hexcode as aphexcode
import airpower.log as aplog

import math

Expand Down Expand Up @@ -34,9 +35,11 @@ def setmap(sheetgrid, compassrose, verbose=True):
_nysheetgrid = len(_sheetgrid)
_nxsheetgrid = len(_sheetgrid[0])

if verbose:
for iy in range (0, _nysheetgrid):
print("%s" % " ".join(sheetgrid[iy]))
aplog.log("sheet layout is:")
aplog.logbreak()
for iy in range (0, _nysheetgrid):
aplog.log(" %s" % " ".join(sheetgrid[iy]))
aplog.logbreak()

_sheetlist = []
for iy in range (0, _nysheetgrid):
Expand Down
14 changes: 8 additions & 6 deletions airpower/prolog.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import airpower.map as apmap
import airpower.azimuth as apazimuth
import airpower.log as aplog
import airpower.map as apmap
import airpower.turn as apturn

def startprolog(sheets, compassrose, north="up"):
print("--- start prolog ---")
apazimuth.setnorth(north)
aplog.log("--- start prolog ---")
aplog.logbreak()
apmap.setmap(sheets, compassrose)
apazimuth.setnorth(north)
apmap.drawmap()
apturn.restart()
print()
aplog.logbreak()

def endprolog():
print("--- end prolog ---")
print()
aplog.log("--- end prolog ---")
aplog.logbreak()
9 changes: 5 additions & 4 deletions airpower/turn.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import airpower.log as aplog
import airpower.map as apmap

_turn = None
Expand All @@ -16,14 +17,14 @@ def startturn(turn):
raise ValueError("attempt to start turn %d out of sequence." % turn)
_turn = turn
_maxturn = max(_maxturn, _turn)
print("--- start of turn %d ---" % _turn)
print("")
aplog.log("--- start of turn %d ---" % _turn)
aplog.logbreak()
apmap.drawmap()

def endturn():
global _turn
print("--- end of turn %d ---" % _turn)
print("")
aplog.log("--- end of turn %d ---" % _turn)
aplog.logbreak()
_turn = None

def turn():
Expand Down

0 comments on commit 417081e

Please sign in to comment.