-
Notifications
You must be signed in to change notification settings - Fork 7
/
Room.py
132 lines (111 loc) · 3.91 KB
/
Room.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""Module containing Room class."""
from general_funcs import print_line
from Item import Item
import json
import os
import sys
def all_rooms():
"""Get a list of all rooms in game.
Returns:
rooms -- list of all rooms
"""
fn = "rooms.json"
path = os.path.join(os.path.dirname(sys.argv[0]), fn)
with open(path) as r:
rooms = [room for room in json.loads(r.read())]
return rooms
class Room(object): # Basic class for the rooms in the game.
"""Room class."""
def __init__(self, name):
"""Room class constructor.
Arguments:
name -- name of room
"""
self.name = name
with open('rooms.json') as f:
parsed = json.loads(f.read())
try:
room = parsed[self.name]
self.risk = room['risk']
self.can_produce = room['can_produce']
self.assigned_limit = room['assigned_limit']
self.attribute = room['attribute']
self.produce = room['produce']
self.perk = room['perk']
self.components = room['components']
self.power_usage = room['power_usage']
self.wattage = room['power_usage']
except KeyError:
print("Unknown room {}. Please contact dev.".format(name))
self.level = 1
self.power_available = True
self.assigned = [] #list of names of people assigned
self.broken = False
if self.can_produce:
self.production = 0
self.can_rush = True
else:
self.can_rush = False
self.rushed = False
def __str__(self):
"""String representation of object.
Returns:
self.name -- eg. "Living Room"
"""
return "{} Room".format(self.name.title())
def print_(self):
"""Print room name and its attributes.
Arguments:
game -- Main game object.
"""
print_line(self)
if self.power_available:
power_availablility = "Working"
else:
power_availablility = "No power available"
if self.broken:
status = "Broken"
status = "Functional"
else:
status = "Functional"
print_line(" Risk: {}%".format(self.risk),
" Level: {}".format(self.level),
" Attribute: {}".format(self.attribute.title()),
" Power: {}".format(power_availablility),
" Status: {}".format(status))
if self.can_produce:
print_line(" Production: {}".format(self.production))
if self.can_produce or self.name == "trader":
print_line(" Assigned: ")
self.see_assigned()
print()
def rush(self):
"""Rush building of Room."""
self.rushed = True # Lets game know this room has been rushed.
self.risk += 20
print_line("{} room has been rushed!".format(self.name))
def fix(self):
"""Repair room if damaged."""
self.broken = False
def count_assigned(self):
"""Count inhabitants assigned to Room."""
return len(self.assigned)
def see_assigned(self):
"""Print names of inhabitants assigned to Room."""
if self.assigned:
for person_name in self.assigned:
print_line(" " + person_name)
else:
print_line(" None")
def count_component(self, component):
"""Count components required to build Room.
Arguments:
component -- component to count
Returns:
int -- amount of component required to build Room
"""
return self.components.count(str(component))
def use_power(self):
"""Consume player's power."""
for x in range(0, self.power_usage):
Item('watt').destroy("player")