Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. '''This is the main class of the game.'''
  2.  
  3. import datetime
  4. import os
  5. import random
  6. import sys
  7. from blessings import Terminal
  8.  
  9. from bureaucrat import Bureaucrat
  10. from character import Character
  11.  
  12. t = Terminal()
  13.  
  14.  
  15. class Game:
  16.     '''The main logic of the game'''
  17.     def __init__(self):
  18.         self.clear_screen()
  19.         print(t.underline('LAM-40 0.4'))
  20.         self.character = Character()
  21.         self.bureaucrat = Bureaucrat()
  22.         self.timer = datetime.datetime(2023, 9, 17, 8, 0)
  23.         while True:
  24.             self.act()
  25.  
  26.     def act(self):
  27.         '''An action taken by the player.'''
  28.         # the following loop numerically lists all available actions
  29.         for number, action in enumerate(self.character.actions):
  30.             number = str(number + 1)
  31.             if int(number) < 10:
  32.                 print('0' + number + '. ' + action)
  33.             else:
  34.                 print(number + '. ' + action)
  35.         self.action = input('\nIt is ' +
  36.                             t.bold('{}'.format(self.timer.strftime('%H:%M'))) +
  37.                             '.\nWhat will you do? ').lower()
  38.         if self.action == 'q':  # quits the game
  39.             self.quit()
  40.         elif self.action == 'w':  # creates a new bureaucrat
  41.             self.timer_add(random.choice(range(800, 1001)))
  42.             self.evaluate_time()  # checks if the player ran out of time
  43.             self.bureaucrat = Bureaucrat()
  44.         elif self.action in 'abcdefhijklmpstyw':
  45.             self.react()  # finds out what type of action was chosen
  46.             self.evaluate_time()
  47.             self.calculate_odds()  # alters the odds of success
  48.             self.evaluate_success()  # determines success or failure
  49.         else:  # incorrect input is given
  50.             print('\nIncorrect action. Please try again.\n')
  51.  
  52.     def react(self):
  53.         '''A reaction to the choice of the player.'''
  54.         if self.action == 'bt':  # character.py for reference
  55.             self.character.probability_is_low()
  56.             self.character.asc_rank_asc_mood()
  57.             self.timer_add(random.choice(range(200, 401)))
  58.         elif self.action in 'cp':
  59.             self.character.probability_is_high()
  60.             self.character.desc_rank_desc_mood()
  61.             self.timer_add(random.choice(range(500, 701)))
  62.         elif self.action in 'dm':
  63.             self.character.probability_is_low()
  64.             self.character.desc_rank_desc_mood()
  65.             self.timer_add(random.choice(range(350, 551)))
  66.         elif self.action in 'efj':
  67.             self.character.probability_is_high()
  68.             self.character.asc_rank_asc_mood()
  69.             self.timer_add(random.choice(range(650, 851)))
  70.         elif self.action in 'hkly':
  71.             self.character.probability_is_low()
  72.             self.character.desc_rank_asc_mood()
  73.             self.timer_add(random.choice(range(350, 551)))
  74.         elif self.action in 'is':
  75.             self.character.probability_is_high()
  76.             self.character.asc_rank_desc_mood()
  77.             self.timer_add(random.choice(range(500, 701)))
  78.         else:  # 'a' (ask)
  79.             self.character.probability_is_high()
  80.             self.character.desc_rank_asc_mood()
  81.             self.timer_add(random.choice(range(650, 851)))
  82.  
  83.     def evaluate_time(self):
  84.         '''Checks whether you still have time left.'''
  85.         if self.timer - datetime.datetime(2023, 9, 17, 8, 0) >= \
  86.            datetime.timedelta(seconds=43200):
  87.             self.game_over()
  88.         else:
  89.             pass
  90.  
  91.     def calculate_odds(self):
  92.         '''Calculates the odds of a successful action.'''
  93.         # odds of success are randomly assigned to be 25-75% at first
  94.         self.odds = float(random.choice(range(25, 76)))
  95.         if self.bureaucrat.rank == 'low':
  96.             if self.character.asc_rank is True:  # character.py for reference
  97.                 self.odds /= self.character.divisor
  98.             else:
  99.                 self.odds *= self.character.multiplier
  100.         elif self.bureaucrat.rank == 'high':
  101.             if self.character.asc_rank is True:
  102.                 self.odds *= self.character.multiplier
  103.             else:
  104.                 self.odds /= self.character.divisor
  105.         else:
  106.             pass  # medium rank doesn't affect the odds
  107.         if self.bureaucrat.mood == 'bad':
  108.             if self.character.asc_mood is True:
  109.                 self.odds /= self.character.divisor
  110.             else:
  111.                 self.odds *= self.character.multiplier
  112.         elif self.bureaucrat.mood == 'good':
  113.             if self.character.asc_mood is True:
  114.                 self.odds *= self.character.multiplier
  115.             else:
  116.                 self.odds /= self.character.divisor
  117.         else:
  118.             pass  # average mood doesn't affect the odds
  119.  
  120.     def evaluate_success(self):
  121.         '''Evaluates whether the action is successful or not.'''
  122.         # the higher self.odds value
  123.         # the higher the chance the action will succeed
  124.         self.luck = random.choice(range(0, 101))
  125.         if self.luck > self.odds:
  126.             self.bureaucrat.react_negatively()
  127.             if self.action in 'bkty':  # your risky action was unsuccessful
  128.                 self.game_over()
  129.         else:
  130.             self.bureaucrat.react_positively()
  131.  
  132.     def timer_add(self, seconds_passed):
  133.         '''Adds the time the action took to the timer.'''
  134.         self.timer += datetime.timedelta(seconds=seconds_passed)
  135.  
  136.     def clear_screen(self):
  137.         '''Clears the screen of the terminal.'''
  138.         if os.name == 'nt':  # 'nt' equals Windows
  139.             os.system('cls')
  140.         else:
  141.             os.system('clear')  # Mac OS and Linux
  142.  
  143.     def game_over(self):
  144.         '''Called when the game is over.'''
  145.         self.clear_screen()
  146.         if self.action in 'bkty':
  147.             print('Game over! You were handed over to the police ' +
  148.               'and arrested.\nYou will have to try again next day.')
  149.         else:  # the time is up
  150.            print('Game over! The Instituion is now closed for today.' +
  151.               '\nYou will have to try again next day.')
  152.         self.choice = input("\nType 'r' if you want to restart: ").lower()
  153.         if self.choice == 'r':  # restarts the game from the beginning
  154.             os.execl(sys.executable, sys.executable, * sys.argv)
  155.         else:
  156.             self.quit()
  157.  
  158.     def quit(self):
  159.         '''Quits the game.'''
  160.         self.clear_screen()
  161.         sys.exit('Thank you for playing LAM-40.\n')
  162.  
  163. Game()