69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
|
import colors
|
||
|
import json
|
||
|
|
||
|
|
||
|
class GarageDisplay:
|
||
|
def __init__(self, characterDisplay, topRow):
|
||
|
self.characterDisplay = characterDisplay
|
||
|
self.topRow = topRow
|
||
|
self.westDoorStartColumn = 0
|
||
|
self.eastDoorStartColumn = 5
|
||
|
self.currentState = {}
|
||
|
self.closedOutlineColor = colors.RED
|
||
|
self.closedFillColor = colors.GRAY
|
||
|
self.openOutlineColor = colors.WHITE
|
||
|
self.openFillColor = colors.BLUE
|
||
|
self.garageFile = 'garage.json'
|
||
|
|
||
|
def showGarageState(self):
|
||
|
state = self.getGarageState()
|
||
|
|
||
|
if state != self.currentState:
|
||
|
self.currentState = state
|
||
|
self.characterDisplay.clearRow(self.topRow, rowHeight=4)
|
||
|
|
||
|
if self.isGarageStateValid(state):
|
||
|
self.showState(state)
|
||
|
|
||
|
def getGarageState(self):
|
||
|
try:
|
||
|
with open(self.garageFile) as file:
|
||
|
return json.load(file)
|
||
|
except Exception as e:
|
||
|
print(e, flush=True)
|
||
|
return {'error': True}
|
||
|
|
||
|
def isGarageStateValid(self, state):
|
||
|
return not 'error' in state
|
||
|
|
||
|
def showState(self, state):
|
||
|
if state["west-door"] == "closed":
|
||
|
self.characterDisplay.displaySquare(
|
||
|
x=self.westDoorStartColumn,
|
||
|
y=self.topRow,
|
||
|
outlineColor=self.closedOutlineColor,
|
||
|
fillColor=self.closedFillColor
|
||
|
)
|
||
|
else:
|
||
|
self.characterDisplay.displaySquare(
|
||
|
x=self.westDoorStartColumn,
|
||
|
y=self.topRow,
|
||
|
outlineColor=self.openOutlineColor,
|
||
|
fillColor=self.openFillColor
|
||
|
)
|
||
|
|
||
|
if state["east-door"] == "closed":
|
||
|
self.characterDisplay.displaySquare(
|
||
|
x=self.eastDoorStartColumn,
|
||
|
y=self.topRow,
|
||
|
outlineColor=self.closedOutlineColor,
|
||
|
fillColor=self.closedFillColor
|
||
|
)
|
||
|
else:
|
||
|
self.characterDisplay.displaySquare(
|
||
|
x=self.eastDoorStartColumn,
|
||
|
y=self.topRow,
|
||
|
outlineColor=self.openOutlineColor,
|
||
|
fillColor=self.openFillColor
|
||
|
)
|