Package horizons :: Package util :: Module inventorychecker
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.inventorychecker

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5   
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  from copy import deepcopy 
23   
24  from horizons.scheduler import Scheduler 
25   
26   
27 -class InventoryChecker:
28
29 - def __init__(self, message_class, storage_component, check_interval):
30 """Message class is a subclass of message that this checker will broadcast when the storage_component given has change in its inventory. 31 This check is done every check_interval ticks.""" 32 self.__message_class = message_class 33 self.__storage_component = storage_component 34 self.__inventory_copy = deepcopy(storage_component.inventory._storage) 35 36 self.__check_interval = check_interval 37 38 # Check for updates every few ticks 39 Scheduler().add_new_object(self.check_inventory_changed, self, loops=-1, loop_interval=self.__check_interval)
40
41 - def check_inventory_changed(self):
42 """Function that checks whether the settlements inventory has changed from the last time checked""" 43 inventory = self.__storage_component.inventory._storage 44 if inventory != self.__inventory_copy: 45 self.__message_class.broadcast(self) 46 self.__inventory_copy = deepcopy(inventory)
47
48 - def remove(self):
49 """Clean up""" 50 Scheduler().rem_all_classinst_calls(self) 51 self.__inventory_copy = None 52 self.__storage_component = None 53 self.__message_class = None
54