<commit_before># Stellar Magnate - A space-themed commodity trading game
# Copyright (C) 2016-2017 <NAME> <<EMAIL>>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Main window onto the Stellar Magnate Client

This handles the toplevel window and its direct subelements.
"""

from functools import partial

import urwid

from ...market import CommodityType
from .gamemenu_dialog import GameMenuDialog
from .info_win import InfoWindow
from .market_display import MarketDisplay
from .menu_bar_win import MenuBarWindow
from .message_win import MessageWindow
from .order_dialog import OrderDialog
from .sideless_linebox import SidelessLineBox
from .travel_display import TravelDisplay

# ui elements:
# [x] Travel Menu
#     [x] Export signals for selecting entries
#     [x] hook up signal for opening the travel menu
#     [_] Jump
# [_] Menubar
#     [x] Basic entries
#     [_] Export signals for selecting the menu entries
#     [x] Travel
#     [_] Finance
#     [x] Market
#     [_] Port 
# [_] Game Menu
#   [x] Quit
#   [_] Save
#   [_] Load
# [_] commodities market
#   [x] Hold space
#   [_] Warehouse space
# [_] Info window
#   [_] Warehouse
#   [-] Bank
#   [_] Loan
# [_] Financial menu
# [_] Financial  action menu
# [_] Fleet menu
# [_] Ship market
# [_] Ship purchase
# [_] Weapons market
# [_] Weapon purchase

class StatusBar(urwid.Columns):
    """Display a horizontal border with a field for username and location"""
    _selectable = False
    def __init__(self, pubpen, spacer=u'u2500'):
        self.pubpen = pubpen

        self.who = urwid.Text(u'\u2524 Name: \u251C')
        self.where = urwid.Text(u'\u2524 Location: \u251C')

        self.left = urwid.Columns([self.who])
        self.right = urwid.Columns([self.where])
        super().__init__((('pack', self.who),
                          ('weight', 1, urwid.Divider(spacer)),
                          ('pack', self.where),
                         ))

        # Connect to backend events
        self.pubpen.subscribe('user.info', self.handle_user_info)
        self.pubpen.subscribe('user.login_success', self.handle_login)
        self.pubpen.subscribe('ship.moved', self.handle_ship_moved)

    #
    # Widget methods
    #
    def update_username(self, username):
        """Update the user's name in the statusbar"""
        self.who.set_text('\u2524 Name: {} \u251C'.format(username))

    def update_location(self, location):
        """Update the ship's location in the statusbar"""
        self.where.set_text('\u2524 Location: {} \u251C'.format(location))

    #
    # Handlers
    #
    def handle_user_info(self, username, cash, location):
        """Update both username and location when we've explicitly requested the information"""
        self.update_username(username)
        self.update_location(location)

    def handle_login(self, username):
        """Update the user's name when they log in"""
        self.update_username(username)

    def handle_ship_moved(self, new_location, *args):
        """Update the ship's location when the ship moves"""
        self.update_location(new_location)


from .commodity_catalog import CatalogColumn, CommodityCatalog
class PortDisplay(CommodityCatalog):
    """Display for the user to manage their ship and equipment"""

    signals = ['close_port_display', 'open_equip_order_dialog']

    def __init__(self, pubpen):
        auxiliary_cols = [CatalogColumn('Price', 13, money=True),
                          #CatalogColumn('Amount', 20),
                          CatalogColumn('Currently Owned', 20),
                         ]

        #
        # Indexes to special columns
        #
        self.owned_col_idx = 1

        super().__init__(pubpen, 'ui.urwid.equip_order_info',
                         primary_title='Equipment', auxiliary_cols=auxiliary_cols,
                         price_col_idx=0, types_traded=frozenset((CommodityType['equipment'], CommodityType['property'])))

        #
        # Event handlers
        #
        self.pubpen.subscribe('ship.info', self.handle_ship_info)
        self.pubpen.subscribe('ship.cargo.update', self.handle_cargo_update)

    #
    # Handle updates to the displayed info
    #
    def handle_ship_info(self, ship_type, free_space, filled_space, *args): #pylint: disable=unused-argument
        """Update the display with total hold space owned"""
        self.auxiliary_cols[self.owned_col_idx].data_map['Cargo Module (100 units)'] = free_space + filled_space
        self._construct_commodity_list(self.auxiliary_cols[self.owned_col_idx].data_map)

    def handle_cargo_update(self, cargo, free_space, filled_space): #pylint: disable=unused-argument
        """Update the display with total hold space owned"""
        self.auxiliary_cols[self.owned_col_idx].data_map['Cargo Module (100 units)'] = free_space + filled_space
        self._construct_commodity_list(self.auxiliary_cols[self.owned_col_idx].data_map)

    def handle_equip_info(self, equip_info):
        """Update the display with the equipment that can be bought at this locastion"""
        pass

    def handle_new_warehouse_info(self, warehouse_info):
        """Update the equipment display with total warehouse space owned"""
        pass

    def handle_new_location(self, new_location, *args):
        """
        Update the market display when the ship moves

        :arg new_location: The location the ship has moved to
        """
        super().handle_new_location(new_location, *args)

        # Get holdspace info
        self.pubpen.publish('query.ship.info')
        pass
        ### TODO: events geared towards the equipment, not cargo
        # Sync up information
        #self.pubpen.subscribe('warehouse.{}.update'.format(new_location)) => handle new warehouse info
        #self.pubpen.publish('query.warehouse.{}.info'.format(new_location))


class FinancialDisplay(urwid.WidgetWrap):
    """Display for the user to manage their bank and loan amounts"""
    _selectable = True
<commit_msg>Change calculation of hold space in port display to be by number of units of 100<commit_after># Stellar Magnate - A space-themed commodity trading game
# Copyright (C) 2016-2017 <NAME> <<EMAIL>>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Main window onto the Stellar Magnate Client

This handles the toplevel window and its direct subelements.
"""

from functools import partial

import urwid

from .gamemenu_dialog import GameMenuDialog
from .info_win import InfoWindow
from .market_display import MarketDisplay
from .menu_bar_win import MenuBarWindow
from .message_win import MessageWindow
from .order_dialog import OrderDialog
from .sideless_linebox import SidelessLineBox
from .travel_display import TravelDisplay

# ui elements:
# [x] Travel Menu
#     [x] Export signals for selecting entries
#     [x] hook up signal for opening the travel menu
#     [_] Jump
# [_] Menubar
#     [x] Basic entries
#     [_] Export signals for selecting the menu entries
#     [x] Travel
#     [_] Finance
#     [x] Market
#     [_] Port 
# [_] Game Menu
#   [x] Quit
#   [_] Save
#   [_] Load
# [_] commodities market
#   [x] Hold space
#   [_] Warehouse space
# [_] Info window
#   [_] Warehouse
#   [-] Bank
#   [_] Loan
# [_] Financial menu
# [_] Financial  action menu
# [_] Fleet menu
# [_] Ship market
# [_] Ship purchase
# [_] Weapons market
# [_] Weapon purchase

class StatusBar(urwid.Columns):
    """Display a horizontal border with a field for username and location"""
    _selectable = False
    def __init__(self, pubpen, spacer=u'u2500'):
        self.pubpen = pubpen

        self.who = urwid.Text(u'\u2524 Name: \u251C')
        self.where = urwid.Text(u'\u2524 Location: \u251C')

        self.left = urwid.Columns([self.who])
        self.right = urwid.Columns([self.where])
        super().__init__((('pack', self.who),
                          ('weight', 1, urwid.Divider(spacer)),
                          ('pack', self.where),
                         ))

        # Connect to backend events
        self.pubpen.subscribe('user.info', self.handle_user_info)
        self.pubpen.subscribe('user.login_success', self.handle_login)
        self.pubpen.subscribe('ship.moved', self.handle_ship_moved)

    #
    # Widget methods
    #
    def update_username(self, username):
        """Update the user's name in the statusbar"""
        self.who.set_text('\u2524 Name: {} \u251C'.format(username))

    def update_location(self, location):
        """Update the ship's location in the statusbar"""
        self.where.set_text('\u2524 Location: {} \u251C'.format(location))

    #
    # Handlers
    #
    def handle_user_info(self, username, cash, location):
        """Update both username and location when we've explicitly requested the information"""
        self.update_username(username)
        self.update_location(location)

    def handle_login(self, username):
        """Update the user's name when they log in"""
        self.update_username(username)

    def handle_ship_moved(self, new_location, *args):
        """Update the ship's location when the ship moves"""
        self.update_location(new_location)


from ...market import CommodityType
from .commodity_catalog import CatalogColumn, CommodityCatalog
class PortDisplay(CommodityCatalog):
    """Display for the user to manage their ship and equipment"""

    signals = ['close_port_display', 'open_equip_order_dialog']

    def __init__(self, pubpen):
        auxiliary_cols = [CatalogColumn('Price', 13, money=True),
                          #CatalogColumn('Amount', 20),
                          CatalogColumn('Currently Owned', 20),
                         ]

        #
        # Indexes to special columns
        #
        self.owned_col_idx = 1

        super().__init__(pubpen, 'ui.urwid.equip_order_info',
                         primary_title='Equipment', auxiliary_cols=auxiliary_cols,
                         price_col_idx=0, types_traded=frozenset((CommodityType['equipment'], CommodityType['property'])))

        #
        # Event handlers
        #
        self.pubpen.subscribe('ship.info', self.handle_ship_info)
        self.pubpen.subscribe('ship.cargo.update', self.handle_cargo_update)

    #
    # Handle updates to the displayed info
    #
    def handle_ship_info(self, ship_type, free_space, filled_space, *args): #pylint: disable=unused-argument
        """Update the display with total hold space owned"""
        self.auxiliary_cols[self.owned_col_idx].data_map['Cargo Module (100 units)'] = (free_space + filled_space) // 100
        self._construct_commodity_list(self.auxiliary_cols[self.owned_col_idx].data_map)

    def handle_cargo_update(self, cargo, free_space, filled_space): #pylint: disable=unused-argument
        """Update the display with total hold space owned"""
        self.auxiliary_cols[self.owned_col_idx].data_map['Cargo Module (100 units)'] = (free_space + filled_space) // 100
        self._construct_commodity_list(self.auxiliary_cols[self.owned_col_idx].data_map)

    def handle_new_warehouse_info(self, warehouse_info):
        """Update the equipment display with total warehouse space owned"""
        pass

    def handle_new_location(self, new_location, *args):
        """
        Update the market display when the ship moves

        :arg new_location: The location the ship has moved to
        """
        super().handle_new_location(new_location, *args)

        # Get holdspace info
        self.pubpen.publish('query.ship.info')
        pass
        ### TODO: events geared towards the equipment, not cargo
        # Sync up information
        #self.pubpen.subscribe('warehouse.{}.update'.format(new_location)) => handle new warehouse info
        #self.pubpen.publish('query.warehouse.{}.info'.format(new_location))


class FinancialDisplay(urwid.WidgetWrap):
    """Display for the user to manage their bank and loan amounts"""
    _selectable = True

{'fraction_non_alphanumeric': 0.10116553668012493, 'fraction_numerical': 0.009217643025824637, 'mean_word_length': 3.077018633540373, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3690011', 'n_tokens_mistral': 3898, 'n_tokens_neox': 3687, 'n_words': 1116}