1
0
Эх сурвалжийг харах

Refactoring the `set_access` code - I think it's easier to read now.

Tried to make it easier to consume.
Introduced 5 new variables to use for the access codes instead of the numeric values:
```
NO_ACCESS  = 0
RENTED         = 1
OWNED         = 2
TENANTS       = 3
ACCESSIBLE   = 4
PURCHASED  = 5
```
Added some comments to make it clearer why some things are done.
netuttki 3 сар өмнө
parent
commit
16a1768f02

+ 89 - 52
locations/homes_properties.qsrc

@@ -1,5 +1,13 @@
 # homes_properties
 
+!! Easier to remember than which number is which.
+NO_ACCESS = 0
+RENTED = 1
+OWNED = 2
+TENANTS = 3
+ACCESSIBLE = 4
+PURCHASED = 5 // Used only for generic checks where both OWNED and TENANTS fulfill the criterion. 
+
 if ARRSIZE('homes') < 16 or ARRSIZE('home_name') < 7:
 	gs 'homes_properties_attr'
 end
@@ -75,12 +83,12 @@ end
 !!			- empty strying   ->  '' - will work as if a location was passed, but will use the actual value of $loc
 !!			- A number as str -> '1', '2' - this should be the id of the property
 !!			  					NOTE: only added in case someone really want to use the old ids
-!! ARGS[2]: ownership type -> status code
+!! ARGS[2]: ownership type -> access code
 !!			0: no acces  -> 'no access'
 !!			1: rented - a rented property
 !!			2: owned     -> 'owned'
-!!			3: owned and rented out -> 'tenants'
-!!			4: can live there -> not owned, not rented, but can live there - parents, friends, dorm, etc.
+!!			3: tenants -> a property that is owned, but inaccessible because it has been rented out
+!!			4: accessible -> not owned, not rented, but can live there - parents, friends, dorm, etc.
 !! ARGS[3]: construction status - optional
 !!			0: land only
 !!			1: foundation, frames built, utilities are connected
@@ -89,110 +97,139 @@ end
 !!			0: not renovated
 !!			1: fully renovated
 if $ARGS[0] = 'set_access':
-	$propcode = iif($ARGS[1] = '', $home_name[$loc], $home_name[$ARGS[1]])
-	orig_status_code = accessible_property[$propcode]
-	new_status_code = ARGS[2]
-
-	if orig_status_code ! new_status_code:
-		if $accessible_property[$propcode] = '' or $accessible_property['<<$propcode>>-display'] = '' or $accessible_property['<<$propcode>>-name'] = '':
-			if $property['code'] ! $propcode:
-				gs 'homes_properties_attr', 'get_property_attr', $propcode
+
+	$property_code =  iif($ARGS[1] = '', $home_name[$loc], $home_name[$ARGS[1]])
+	original_access_code = accessible_property[$property_code]
+	new_access_code = ARGS[2]
+
+
+
+	if original_access_code ! new_access_code:
+		!! Should only run if the access code has changed
+
+		!! seems unnecessary, but cuts down on typo related bugs.
+		$_display = $property_code+"-display"
+		$_name = $property_code+"-name"
+		$_type = $property_code+"-type"
+		$_construction_state = $property_code+"-construction-status"
+		$_is_home = $property_code+"-is-home"
+		$_is_rental = $property_code+"-rental"
+		$_renovated = $property_code+"-renovated"
+		$_sales_price = $property_code+"-sales-price"
+		$_rent = $property_code+"-rent"
+		$_tenant_day = $property_code+"-tenant-day"
+		$_tenant_month = $property_code+"-tenant-month"
+
+		!! If the below is true, then Sveta never had access to property (or something wiped out the accessible property/home list).
+		if $accessible_property[$property_code] = '' or $accessible_property[$_display] = '' or $accessible_property[$_name] = '':
+			!! This should happen only the first time Sveta gets access to the property.
+			!! Adds the property - with all of its attributes - to the accessible_property list.
+			if $property['code'] ! $property_code: 
+				gs 'homes_properties_attr', 'get_property_attr', $property_code
 			else
 				nokill = 1
 			end
-			if new_status_code = 1 and property['rental'] = 0: new_status_code = 4
-			$accessible_property[$propcode] = $propcode
-			$accessible_property['<<$propcode>>-name'] = $property['name']
-			$accessible_property['<<$propcode>>-type'] = $property['type']
+
+			$accessible_property[$property_code] = $property_code
+			$accessible_property[$_name] = $property['name']
+			$accessible_property[$_type] = $property['type']
 
 			if property['construction_needed'] = 1:
-				accessible_property['<<$propcode>>-construction-status'] = ARGS[3]
-				$accessible_property['<<$propcode>>-display'] = $property['display_<<ARGS[3]>>']
+				accessible_property[$_construction_state] = ARGS[3]
+				$accessible_property[$_display] = $property['display_<<ARGS[3]>>']
 			else
-				accessible_property['<<$propcode>>-construction-status'] = 2
-				$accessible_property['<<$propcode>>-display'] = $property['display']
+				accessible_property[$_construction_state] = 2
+				$accessible_property[$_display] = $property['display']
 			end
 
-			if INSTR($property['type'], 'residence') > 0: accessible_property['<<$propcode>>-is-home'] = 1
-			accessible_property['<<$propcode>>-rental'] = property['rental']
-			accessible_property['<<$propcode>>-renovated'] = ARGS[4]
-			accessible_property['<<$propcode>>-sales-price'] = property['price']
-			accessible_property['<<$propcode>>-rent'] = property['rent']
+			if INSTR($property['type'], 'residence') > 0: accessible_property[$_is_home] = 1
+			accessible_property[$_is_rental] = property['rental']
+			accessible_property[$_renovated] = ARGS[4]
+			accessible_property[$_sales_price] = property['price']
+			accessible_property[$_rent] = property['rent']
 		end
-		accessible_property[$propcode] = new_status_code
+		!! If the property code is 4 (ACCESSIBLE) but it is a rental property, then the code should be 1 (RENTED).
+		if new_access_code = 1 and property['rental'] = 1: new_access_code = 1
+		accessible_property[$property_code] = new_access_code
 
-		if accessible_property[$propcode] = 0:
+		if accessible_property[$property_code] = NO_ACCESS:
 			!! If this was the current home then killing it
-			if $home['current'] = $propcode: killvar '$home'
+			if $home['current'] = $property_code: killvar '$home'
 			!! Updating home counts
 			!! NOTE: Valid transitions: 1 -> 0, 2 -> 0, 4 -> 0
-			if accessible_property['<<$propcode>>-is-home'] = 1:
-				if orig_status_code = 1:
+			if accessible_property[$_is_home] = 1:
+				if original_access_code = RENTED:
 					accessible_property['rental_home_count'] -= 1
 					accessible_property['available_for_rent_home_count'] += 1
 				end
-				if orig_status_code = 2: accessible_property['owned_home_count'] -= 1
-				if accessible_property['<<$propcode>>-construction-status'] = 2: accessible_property['furnishable_home_count'] -= 1
+				if original_access_code = OWNED: accessible_property['owned_home_count'] -= 1
+				if accessible_property[$_construction_state] = 2: accessible_property['furnishable_home_count'] -= 1
 				accessible_property['accessible_home_count'] -= 1
 			end
 			if accessible_property['accessible_home_count'] = 1:
 				gs 'homes_properties', 'get_accessible_properties', 'home'
-				if accessible_property['<<$ARGS[1]>>-is-home'] = 1: gs 'homes_properties', 'set_home', $property_code
+				if accessible_property[$_is_home] = 1: gs 'homes_properties', 'set_home', $property_code
 				gs 'homes_properties', 'clean_up_property_data'
 			end
 		!! Property is rented
-		elseif accessible_property[$propcode] = 1:
+		elseif accessible_property[$property_code] = RENTED:
 			!! Updating home counts
 			!! NOTE: Only valid transition is 0 -> 1
-			if accessible_property['<<$propcode>>-is-home'] = 1:
+			if accessible_property[$_is_home] = 1:
 				accessible_property['rental_home_count'] += 1
 				accessible_property['accessible_home_count'] += 1
 				accessible_property['available_for_rent_home_count'] -= 1
-				if accessible_property['<<$propcode>>-construction-status'] = 2: accessible_property['furnishable_home_count'] +=1
+				if accessible_property[$_construction_state] = 2: accessible_property['furnishable_home_count'] +=1
 			end
 		!! Property is owned
-		elseif accessible_property[$propcode] = 2:
+		elseif accessible_property[$property_code] = OWNED:
 			!! Updating home counts
 			!! NOTE: Valid transitions: 0 -> 2, 1 -> 2, 3-> 2
-			if accessible_property['<<$propcode>>-is-home'] = 1:
-				if orig_status_code = 0:
+			if accessible_property[$_is_home] = 1:
+				if original_access_code = 0:
 					accessible_property['accessible_home_count'] += 1
-				elseif orig_status_code = 1:
+				elseif original_access_code = RENTED:
 					accessible_property['rental_home_count'] -= 1
 					accessible_property['available_for_rent_home_count'] += 1
-				elseif orig_status_code = 3:
+				elseif original_access_code = TENANTS:
 					accessible_property['tenant_home_count'] -= 1
-					accessible_property['<<$propcode>>-tenant-day'] = -1
-					accessible_property['<<$propcode>>-tenant-month'] = -1
-					if accessible_property['<<$propcode>>-construction-status'] = 2: accessible_property['furnishable_home_count'] += 1
+					accessible_property[$_tenant_day] = -1
+					accessible_property[$_tenant_month] = -1
+					if accessible_property[$_construction_state] = 2: accessible_property['furnishable_home_count'] += 1
 				end
-				if accessible_property['<<$propcode>>-construction-status'] = 2 and orig_status_code ! 1: accessible_property['furnishable_home_count'] += 1
+				if accessible_property[$_construction_state] = 2 and original_access_code ! 1: accessible_property['furnishable_home_count'] += 1
 				accessible_property['owned_home_count'] += 1
 			end
 		!! Property has tenants
-		elseif accessible_property[$propcode] = 3:
+		elseif accessible_property[$property_code] = TENANTS:
 			!! Updating home counts
 			!! NOTE: Valid transitions: 2 -> 3
-			if accessible_property['<<$propcode>>-is-home'] = 1:
-				if $home['current'] = $propcode: killvar '$home'
+			if accessible_property[$_is_home] = 1:
+				if $home['current'] = $property_code: killvar '$home'
 
 				accessible_property['tenant_home_count'] += 1
 				accessible_property['owned_home_count'] -= 1
 				accessible_property['accessible_home_count'] -= 1
 
-				if accessible_property['<<$propcode>>-construction-status'] = 2: accessible_property['furnishable_home_count'] -= 1
+				if accessible_property[$_construction_state] = 2: accessible_property['furnishable_home_count'] -= 1
 			end
-		!! Property is just accessible
-		elseif accessible_property[$propcode] = 4:
+		!! Property is just accessible - like the parents' home, or the Meynold's home, etc.
+		elseif accessible_property[$property_code] = ACCESSIBLE:
 			!! Updating home counts
 			!! NOTE: Valid transitions: 0 -> 4
-			if accessible_property['<<$propcode>>-is-home'] = 1:
+			if accessible_property[$_is_home] = 1:
 				accessible_property['accessible_home_count'] += 1
 			end
 		end
 		if nokill = 0 : killvar 'property'
+		
+		killvar '_display' & killvar '_name' & killvar '_type' & killvar '_construction_state'
+		killvar '_is_home' & killvar '_is_rental' & killvar '_renovated' & killvar '_sales_price'
+		killvar '_rent' & 	killvar '_tenant_day' & killvar '_tenant_month' & killvar 'nokill'
 	end
-	killvar 'propcode' & killvar 'orig_status_code' & killvar 'new_status_code' & killvar 'nokill'
+
+	!! Killvar segment
+	killvar 'property_code' & killvar 'original_access_code' & killvar 'new_access_code'
 end
 
 !! Call: dyneval($is_homeless)