diff --git a/kelner/.idea/workspace.xml b/kelner/.idea/workspace.xml index 0a79d84..5b2d667 100644 --- a/kelner/.idea/workspace.xml +++ b/kelner/.idea/workspace.xml @@ -2,16 +2,15 @@ - - - + + + - - - - + + + - + + + + @@ -106,6 +108,13 @@ + + + + + + + 1584889744892 @@ -135,7 +144,14 @@ - @@ -155,10 +171,31 @@ + + + + + + file://$PROJECT_DIR$/src/managers/DrawableCollection.py + 69 + + + + + + + + + - + \ No newline at end of file diff --git a/kelner/images/srcWaiter.png b/kelner/images/srcWaiter.png new file mode 100644 index 0000000..512d036 Binary files /dev/null and b/kelner/images/srcWaiter.png differ diff --git a/kelner/main.py b/kelner/main.py index fc12e62..f99915b 100644 --- a/kelner/main.py +++ b/kelner/main.py @@ -1,9 +1,10 @@ import pygame from src.components.GridBoard import GridBoard from src.managers.DrawableCollection import DrawableCollection +from src.managers.MenuManager import MenuManager from src.components.Waiter import Waiter from src.components.Table import Table - +from src.managers.TaskManager import TaskManager #create screen consts CellSize = 50 #pixel size of 1 square cell in the grid @@ -18,17 +19,30 @@ gridBoard = GridBoard(ScreenWidth, ScreenHeight, CellSize) #initialize drawable objects manager drawableManager = DrawableCollection() +#initialize menu manager +menuManager = MenuManager() + #initialize waiter component waiter = Waiter(0, 0, 0, GridCountX - 1, 0, GridCountY - 1, CellSize) +#adds waiter to drawable collection +drawableManager.add(waiter) + #initialize a number of tables given in range -for i in range(1, 15): +for i in range(1, 20): table = Table(0, GridCountX - 1, 0, GridCountY - 1, CellSize) drawableManager.generatePosition(table) drawableManager.add(table) #main loop -doRepaint = True + +#object that controlls repainting of changed objects +doRepaint = [True] + +#new thread +task = TaskManager(drawableManager, menuManager, doRepaint) +task.start() + running = True while running: @@ -54,13 +68,13 @@ while running: # checks if new waiter's position down is not occupied by other object if drawableManager.isPositionAvailable(waiter.getX(), waiter.getY() + 1): waiter.moveDown() - doRepaint = True + doRepaint[0] = True - # repaints all objects to the screen - #is set only on initial paint or after keyboard event - if doRepaint: - gridBoard.reinitialize() - gridBoard.draw(drawableManager) - gridBoard.draw(waiter) - gridBoard.udpdate() - doRepaint = False \ No newline at end of file + # repaints all objects to the screen + # is set only on initial paint or after keyboard event + if doRepaint[0]: + gridBoard.reinitialize() + gridBoard.draw(drawableManager) + gridBoard.udpdate() + doRepaint[0] = False + drawableManager.collectOrders() \ No newline at end of file diff --git a/kelner/src/components/Drawable.py b/kelner/src/components/Drawable.py index 9a01e6a..6af5eb9 100644 --- a/kelner/src/components/Drawable.py +++ b/kelner/src/components/Drawable.py @@ -1,8 +1,9 @@ class Drawable: - BLUE = (0, 0, 255) - GREEN = (0, 255, 0) - RED = (255, 0, 0) + GREY = (128, 128, 128) + YELLOW = (255, 255, 0) + RED = (255, 0, 0) + GREEN = (0, 255, 0) def __init__(self, x, y, minX, maxX, minY, maxY, ratio): self.__minX = minX diff --git a/kelner/src/components/Table.py b/kelner/src/components/Table.py index adc2ca4..54812df 100644 --- a/kelner/src/components/Table.py +++ b/kelner/src/components/Table.py @@ -1,5 +1,5 @@ import pygame -import random +from enum import Enum from .Drawable import Drawable class Table(Drawable): @@ -7,8 +7,44 @@ class Table(Drawable): def __init__(self, minX, maxX, minY, maxY, ratio): #call base class constructor Drawable.__init__(self, 0, 0, minX, maxX, minY, maxY, ratio) + self.__order = [] + self.__status = Status.NotReady + + #sets table color based on it's status + def getColor(self): + color = None + if self.__status == Status.NotReady: + color = self.GREY + elif self.__status == Status.Ready: + color = self.YELLOW + elif self.__status == Status.Waiting: + color = self.RED + elif self.__status == Status.Served: + color = self.GREEN + return color def draw(self, screen): - pygame.draw.circle(screen, self.GREEN, ((self.getX() * self.getRatio()) + (self.getRatio() // 2), - (self.getY() * self.getRatio()) + (self.getRatio() // 2)), - (self.getRatio() // 3)) + pygame.draw.circle(screen, self.getColor(), ((self.getX() * self.getRatio()) + (self.getRatio() // 2), + (self.getY() * self.getRatio()) + (self.getRatio() // 2)), + (self.getRatio() // 3)) + def setOrder(self, order): + self.__order = order + + def getOrder(self): + return self.__order + + def delOrder(self): + self.setOrder([]) + + def isStatus(self, status): + return status == self.__status + + def setStatus(self, status): + self.__status = status + +#status of the table +class Status(Enum): + NotReady = 0 + Ready = 1 + Waiting = 2 + Served = 3 \ No newline at end of file diff --git a/kelner/src/components/Waiter.py b/kelner/src/components/Waiter.py index f28e0e0..3e6c829 100644 --- a/kelner/src/components/Waiter.py +++ b/kelner/src/components/Waiter.py @@ -7,6 +7,7 @@ class Waiter(Drawable): #call base class constructor Drawable.__init__(self, x, y, minX, maxX, minY, maxY, ratio) self.__image = self.__loadImg('./images/waiter.png') + self.__acceptedOrders = [] def moveUp(self): if self.getY() > self.getMinY(): @@ -36,6 +37,10 @@ class Waiter(Drawable): else: return False + #accepts orders from the table and stores them in queue + def addOrder(self, table): + self.__acceptedOrders += [(table, table.getOrder())] + def __loadImg(self, filePath): return pygame.image.load(filePath) diff --git a/kelner/src/managers/DrawableCollection.py b/kelner/src/managers/DrawableCollection.py index a7063f0..9350290 100644 --- a/kelner/src/managers/DrawableCollection.py +++ b/kelner/src/managers/DrawableCollection.py @@ -1,9 +1,11 @@ import random +from src.components.Table import Table, Status +from src.components.Waiter import Waiter #drawable objects manager class DrawableCollection: #const, minimal distance between objects - __MinDistance = 1 + __MinDistance = 0 def __init__(self): #collection that holds all drawable objects @@ -13,7 +15,7 @@ class DrawableCollection: def add(self, drawable): self.__drawables.append(drawable) - #generates and sets random x, y and cheks if it's not occupied by other object + #generates and sets random (x, y) and cheks if it's not occupied by other object def generatePosition(self, drawable): isPositionUnique = False while not isPositionUnique: @@ -40,4 +42,31 @@ class DrawableCollection: if item.getX() == x and item.getY() == y: isPositionAvailable = False break - return isPositionAvailable \ No newline at end of file + return isPositionAvailable + + #gets all tables by status from collection + def getTables(self, status): + result = [] + for item in self.__drawables: + if isinstance(item, Table) and item.isStatus(status): + result += [item] + return result + + #gets all waiters from collection + def getWaites(self): + result = [] + for item in self.__drawables: + if isinstance(item, Waiter): + result += [item] + return result + + #waiter collects order from the nearest table + def collectOrders(self): + waiters = self.getWaites() + for waiter in waiters: + tables = self.getTables(Status.Ready) + for table in tables: + if (table.getX() == waiter.getX() and abs(table.getY() - waiter.getY()) == 1) or (table.getY() == waiter.getY() and abs(table.getX() - waiter.getX()) == 1): + table.setStatus(Status.Waiting) + waiter.addOrder(table) + table.delOrder() \ No newline at end of file diff --git a/kelner/src/managers/MenuManager.py b/kelner/src/managers/MenuManager.py new file mode 100644 index 0000000..7b9222b --- /dev/null +++ b/kelner/src/managers/MenuManager.py @@ -0,0 +1,33 @@ +import random + +#contains all dishes and generates random order for the table +class MenuManager: + + #consts, min and max dishes oredered by the people sitting by the same table + __MinDishes = 1 + __MaxDishes = 3 + + def __init__(self): + self.__menuCard = ["PORK", + "FRENCH FRIES", + "PIZZA", + "CHICKEN", + "RIBS", + "FISH", + "SPAGHETTI", + "BEEF", + "STEAK", + "SALAD", + "GRILLED VEGETABLES", + "VEAL", + "CHOPS", + "EMPTY PLATE", + "BEER", + "CAKE"] + #generator + def generateOrder(self): + count = random.randint(self.__MinDishes, self.__MaxDishes) + order = [] + for i in range(0, count): + order += [(self.__menuCard[random.randint(0, len(self.__menuCard) - 1)])] + return order \ No newline at end of file diff --git a/kelner/src/managers/TaskManager.py b/kelner/src/managers/TaskManager.py new file mode 100644 index 0000000..686937c --- /dev/null +++ b/kelner/src/managers/TaskManager.py @@ -0,0 +1,25 @@ +import threading +import time +import random +from src.components.Table import Status + +#creates new threads +class TaskManager (threading.Thread): + + def __init__(self, drawableManager, menuManager, doRepaintObject): + threading.Thread.__init__(self) + self.__drawableManager = drawableManager + self.__menuManager = menuManager + self.__doRepaintObject = doRepaintObject + + #changes the status of a random table from NotReady to Ready + def run(self): + while True: + time.sleep(3) + tables = self.__drawableManager.getTables(Status.NotReady) + if tables != []: + tableIndex = random.randint(0, len(tables) - 1) + table = tables[tableIndex] + table.setStatus(Status.Ready) + table.setOrder(self.__menuManager.generateOrder()) + self.__doRepaintObject[0] = True \ No newline at end of file