forked from s444417/ProjektAI
waiter goes to kitchen after collected orders
This commit is contained in:
parent
90a9644a32
commit
a2e474d520
@ -77,8 +77,8 @@ drawableManager.add(waiter1)
|
||||
|
||||
|
||||
# TODO: create kitchen
|
||||
#kitchen = Kitchen(5, GridCountX-5, 5, GridCountY-5, CellSize, PaintOffset)
|
||||
#drawableManager.add(kitchen)
|
||||
kitchen = Kitchen(5, GridCountX-5, 5, GridCountY-5, CellSize, PaintOffset)
|
||||
drawableManager.add(kitchen)
|
||||
|
||||
#My comment
|
||||
# initialize a number of tables given in range
|
||||
|
@ -10,22 +10,23 @@ import os
|
||||
class Kitchen(Drawable):
|
||||
def __init__(self, minX, maxX, minY, maxY, cellSize, offset):
|
||||
# call base class constructor
|
||||
super().__init__(0, 0, minX, maxX, minY, maxY, cellSize, offset)
|
||||
super().__init__(14, 0, minX, maxX, minY, maxY, cellSize, offset)
|
||||
self._preparing_orders = []
|
||||
self._ready_orders = []
|
||||
self._photos_path = os.path.join(os.getcwd(), 'foodImages')
|
||||
|
||||
def get_new_orders(self, orders):
|
||||
self._preparing_orders.append(orders)
|
||||
pass
|
||||
|
||||
|
||||
def draw(self, screen):
|
||||
xBase = self.getX() * self.getCellSize() + self.getOffset()
|
||||
yBase = self.getY() * self.getCellSize() + self.getOffset()
|
||||
tableXYOffset = int(0.2 * self.getCellSize())
|
||||
# tableXYOffset = int(0.2 * self.getCellSize())
|
||||
tableXYOffset = 0
|
||||
image = self.getImage(Images.Kitchen)
|
||||
screen.blit(image, (xBase - tableXYOffset, yBase - tableXYOffset))
|
||||
|
||||
|
||||
def drawAux(self, screen):
|
||||
pass
|
||||
|
||||
|
@ -21,8 +21,9 @@ class Waiter(Drawable):
|
||||
super().__init__(x, y, minX, maxX, minY, maxY, cellSize, offset)
|
||||
self.__dx = Direction.Down[0]
|
||||
self.__dy = Direction.Down[1]
|
||||
self.__acceptedOrders = []
|
||||
self.__acceptedOrders = dict()
|
||||
self.__currentPath = []
|
||||
self._ready = True
|
||||
|
||||
def moveUp(self):
|
||||
if self.getY() > self.getMinY():
|
||||
@ -75,6 +76,7 @@ class Waiter(Drawable):
|
||||
def getDirection(self):
|
||||
return self.__dx, self.__dy
|
||||
|
||||
|
||||
def setDirection(self, dx, dy):
|
||||
self.__dx = dx
|
||||
self.__dy = dy
|
||||
@ -103,8 +105,8 @@ class Waiter(Drawable):
|
||||
|
||||
# accepts orders from the table and stores them in queue
|
||||
def addOrder(self, table, order):
|
||||
self.__acceptedOrders += [(table, order)]
|
||||
|
||||
self.__acceptedOrders[table] = order
|
||||
|
||||
def getAcceptedOrders(self):
|
||||
return self.__acceptedOrders
|
||||
|
||||
@ -117,6 +119,20 @@ class Waiter(Drawable):
|
||||
def popStepFromPath(self):
|
||||
return self.__currentPath.pop(0)
|
||||
|
||||
def get_current_path(self):
|
||||
return self.__currentPath
|
||||
|
||||
def make_busy(self):
|
||||
self._ready = False
|
||||
|
||||
def make_ready(self):
|
||||
self._ready = True
|
||||
|
||||
def get_state(self):
|
||||
return self._ready
|
||||
|
||||
|
||||
|
||||
def draw(self, screen):
|
||||
direction = self.getDirection()
|
||||
imageKind = None
|
||||
|
@ -3,6 +3,7 @@ from threading import Lock
|
||||
|
||||
from kelner.src.components.Table import Table
|
||||
from kelner.src.components.Waiter import Waiter
|
||||
from kelner.src.components.Kitchen import Kitchen
|
||||
|
||||
|
||||
# drawable objects manager
|
||||
@ -59,6 +60,14 @@ class DrawableCollection:
|
||||
result += [item]
|
||||
return result
|
||||
|
||||
def get_kitchen(self):
|
||||
kitchen = None
|
||||
for item in self.__drawables:
|
||||
if isinstance(item, Kitchen):
|
||||
kitchen = item
|
||||
return kitchen
|
||||
|
||||
|
||||
# gets all waiters from collection
|
||||
def getWaiters(self):
|
||||
result = []
|
||||
|
@ -4,20 +4,14 @@ import sys
|
||||
|
||||
|
||||
# creates new thread
|
||||
class KitchenManager(threading.Thread):
|
||||
class KitchenManager():
|
||||
|
||||
def __init__(self, drawable_manager, images_storage):
|
||||
super().__init__()
|
||||
def __init__(self, drawable_manager):
|
||||
self._drawable_manager = drawable_manager
|
||||
self._runThread = True
|
||||
self._images_storage = images_storage
|
||||
|
||||
def prepare_dish(self):
|
||||
pass
|
||||
|
||||
def run(self):
|
||||
while self._runThread:
|
||||
pass
|
||||
def receive_orders(self, orders, kitchen):
|
||||
kitchen.get_new_orders(orders)
|
||||
|
||||
def stop(self):
|
||||
self._runThread = False
|
||||
|
@ -3,7 +3,8 @@ import time
|
||||
import sys
|
||||
from kelner.src.components.Table import Status
|
||||
from kelner.src.algorithms.AStar.Finder import Finder
|
||||
|
||||
from kelner.src.algorithms.BFS.BFS import BFS
|
||||
from kelner.src.managers.KitchenManager import KitchenManager
|
||||
|
||||
# creates new thread
|
||||
class WaiterManager (threading.Thread):
|
||||
@ -13,8 +14,10 @@ class WaiterManager (threading.Thread):
|
||||
self.__drawableManager = drawableManager
|
||||
self.__waiters = waiters
|
||||
self.__runThread = True
|
||||
self._kitchen_manager = KitchenManager(drawableManager)
|
||||
|
||||
def __getNearestTargetPath(self, waiter):
|
||||
|
||||
def __getNearestTargetPath(self, waiter, target):
|
||||
distance = sys.maxsize
|
||||
nearestTargetPath = None
|
||||
tables = self.__drawableManager.getTables(Status.Ready)
|
||||
@ -22,17 +25,20 @@ class WaiterManager (threading.Thread):
|
||||
reservedPlaces = self.__drawableManager.getReservedPlaces(waiter)
|
||||
finder = Finder(reservedPlaces)
|
||||
origin = (waiter.getX(), waiter.getY())
|
||||
for table in tables:
|
||||
if table.hasOrder():
|
||||
targets = finder.getNeighbours((table.getX(), table.getY()), False)
|
||||
for target in targets:
|
||||
if target is not None:
|
||||
path = finder.getPath(origin, target, True)
|
||||
if path:
|
||||
result = len(path)
|
||||
if result < distance:
|
||||
distance = result
|
||||
nearestTargetPath = path
|
||||
if target == 'kitchen':
|
||||
path = finder.getPath(origin, (14, 1), True)
|
||||
else:
|
||||
for table in tables:
|
||||
if table.hasOrder():
|
||||
targets = finder.getNeighbours((table.getX(), table.getY()), False)
|
||||
for target in targets:
|
||||
if target is not None:
|
||||
path = finder.getPath(origin, target, True)
|
||||
if path:
|
||||
result = len(path)
|
||||
if result < distance:
|
||||
distance = result
|
||||
nearestTargetPath = path
|
||||
return nearestTargetPath
|
||||
|
||||
def __changeWaiterDirection(self, waiter, x, y):
|
||||
@ -52,7 +58,7 @@ class WaiterManager (threading.Thread):
|
||||
|
||||
def __collectOrder(self, waiter):
|
||||
doCollectOrder = True
|
||||
while doCollectOrder:
|
||||
while doCollectOrder and waiter.get_state():
|
||||
tables = self.__drawableManager.getNearestTables(waiter, Status.Ready)
|
||||
turns = sys.maxsize
|
||||
lessTurnsTable = None
|
||||
@ -77,24 +83,36 @@ class WaiterManager (threading.Thread):
|
||||
self.__drawableManager.forceRepaint()
|
||||
doCollectOrder = len(tables) > 0
|
||||
|
||||
def pass_orders_to_kitchen(self, orders):
|
||||
kitchen = self.__drawableManager.get_kitchen()
|
||||
self._kitchen_manager.receive_orders(orders, kitchen)
|
||||
|
||||
|
||||
# changes the status of a random table from NotReady to Ready
|
||||
def run(self):
|
||||
while self.__runThread:
|
||||
if self.__waiters:
|
||||
for waiter in self.__waiters:
|
||||
if len(waiter.getAcceptedOrders()) < 5:
|
||||
path = self.__getNearestTargetPath(waiter)
|
||||
waiter.setPath([] if path is None else path)
|
||||
target = 'table'
|
||||
if len(waiter.getAcceptedOrders()) > 0:
|
||||
target = 'kitchen'
|
||||
path = self.__getNearestTargetPath(waiter, target=target)
|
||||
waiter.make_busy()
|
||||
|
||||
if not waiter.isPathEmpty():
|
||||
step = waiter.popStepFromPath()
|
||||
self.__changeWaiterDirection(waiter, step[0], step[1])
|
||||
self.__moveWaiter(waiter, step[0], step[1])
|
||||
|
||||
if waiter.isPathEmpty():
|
||||
self.__collectOrder(waiter)
|
||||
else:
|
||||
waiter.goToKitchen()
|
||||
path = self.__getNearestTargetPath(waiter, target='table')
|
||||
waiter.setPath([] if path is None else path)
|
||||
|
||||
if not waiter.isPathEmpty():
|
||||
step = waiter.popStepFromPath()
|
||||
self.__changeWaiterDirection(waiter, step[0], step[1])
|
||||
self.__moveWaiter(waiter, step[0], step[1])
|
||||
|
||||
if len(waiter.get_current_path()) == 0 and target == 'kitchen':
|
||||
self.pass_orders_to_kitchen(waiter.getAcceptedOrders)
|
||||
print(waiter.getAcceptedOrders())
|
||||
else:
|
||||
self.__collectOrder(waiter)
|
||||
|
||||
def stop(self):
|
||||
self.__runThread = False
|
||||
|
Loading…
Reference in New Issue
Block a user