Esempio n. 1
0
class Controller:
    def __init__(self, data: str = None):
        self.state = GameState()
        self.matchers = SentenceMatcherFabric(self).get_all()
        self.data = data

    def notify(self, match: Match):
        if match.action == 'add_player':
            self.state.add_player(match.results['player'])
        if match.action == 'stock_round':
            self.state.set_stock_round(match.results['stock_round'])
            self.state.store_valuation()
        if match.action == 'operation_round':
            self.state.set_operation_round(match.results['operation_round'])
            self.state.store_valuation()
        if match.action == 'par':
            self.state.par_company(match.results['corporation'],
                                   int(match.results['value']))
        if match.action == 'buy_shares':
            self.state.buy_shares(match.results['player'],
                                  match.results['corporation'],
                                  int(match.results['value']),
                                  int(match.results['shares']) / 10)
        if match.action == 'sell_shares':
            self.state.sell_shares(match.results['player'],
                                   match.results['corporation'],
                                   int(match.results['value']),
                                   int(match.results['number_of_shares']))
        if match.action == 'corporation_share_price_change':
            self.state.set_company_price(match.results['corporation'],
                                         int(match.results['value']))
        if match.action == 'player_receives':
            self.state.player_receives(match.results['player'],
                                       int(match.results['value']))
        if match.action == 'player_buys_private':
            self.state.player_receives(match.results['player'],
                                       int(match.results['value']) * -1)
        if match.action == 'player_collects':
            self.state.player_receives(match.results['player'],
                                       int(match.results['value']))

    def run(self):
        if self.data is None:
            raise ValueError('No data to calculate')

        for sentence in self.data:
            for matcher in self.matchers:
                matcher.match(sentence)
Esempio n. 2
0
 def test_evaluate_player(self):
     game = GameState(initial_cash_for_players=400)
     game.add_player(player_name='Cake')
     self.assertEqual(400, game.evaluate_player('Cake'))
     game.par_company(company_name='NYC', price=50)
     game.buy_shares(player_name='Cake',
                     company_name='NYC',
                     price=50,
                     quantity=2)
     self.assertEqual(400, game.evaluate_player('Cake'))
     game.set_company_price(company_name='NYC', new_price=100)
     self.assertEqual(500, game.evaluate_player('Cake'))
     game.sell_shares(player_name='Cake',
                      company_name='NYC',
                      price=200,
                      quantity=1)
     self.assertEqual(600, game.evaluate_player('Cake'))