def on_choose_item(self, data: dict): """Call on message CHOOSE_ITEM. Emit ITEM_CHOSEN in the room 'table_id'. Args : data = {user_id, table_id, item_id, username, individual, command_id} """ app.logger.debug("CHOOSE ITEM") app.logger.info( f"New item {data['item_id']} chosen on table {data['table_id']} received from {data['username']}" ) CommandService.add_user_order_to_command(data["command_id"], data["user_id"], data["individual"]) data["accumulated"] = CommandService.get_accumulated_order_amount( data["command_id"]) data["summary"] = asdict( CommandService.get_unique_command_by_table_id_and_item_id( data["table_id"], data["item_id"])) emit( socket_messages["ITEM_CHOSEN"], data, room=self.__get_table_room(data["table_id"]), )
def on_next_item(self, data: dict): """Call on message NEXT_ITEM. Emit ITEM_CHANGED in the room 'table/table_id'. Args : data = {user_id, table_id, index} """ app.logger.debug("NEXT ITEM") item_id = get_item_id_from_table_id_and_index(data["table_id"], data["index"]) if TableService.is_emperor(data["user_id"], data["table_id"]): command = CommandService.create_command(item_id, data["table_id"]) data["command_id"] = command.id data["accumulated"] = CommandService.get_accumulated_order_amount( data["command_id"]) data["summary"] = asdict( CommandService.get_unique_command_by_table_id_and_item_id( data["table_id"], item_id)) TableService.set_current_command_id(data["table_id"], data["command_id"]) data["item_id"] = item_id emit( socket_messages["ITEM_CHANGED"], data, room=self.__get_table_room(data["table_id"]), )
def __emit_command_started(current_command: dict, room: str): """Emit command started message after START_COMMAND and JOIN_COMMAND. Args: current_command: Command object as a dict room: socket room where you want to emit this message """ app.logger.debug("COMMAND_STARTED") item_index = get_item_index_from_table_id_and_id( current_command["table_id"], current_command["item_id"]) emit( socket_messages["COMMAND_STARTED"], { "table_id": current_command["table_id"], "current_command": current_command, "command_id": current_command["id"], "item_id": current_command["item_id"], "index": item_index, "accumulated": CommandService.get_accumulated_order_amount( current_command["id"]), "summary": asdict(CommandService.get_command(current_command["id"])), }, room=room, )
def get_command_by_user_and_table(table_id: int, user_id: int) -> Response: """Get commands for a user. Returns : {command: id, user_id, table_id} """ command = CommandService.get_command_by_user_and_table(table_id, user_id) if not command: return json_abort(404, f"Command not found") return jsonify({"command": command})
def get_command(command_id: int) -> Response: """Find a given command. Returns : {command : id, item_id, table_id, users} """ command = CommandService.get_command(command_id) if not command: return json_abort(404, f"No command with id {command_id}") return jsonify({"command": command})
def get_unique_command_by_table_id_and_item_id(table_id: int, item_id: int) -> Response: """Get a unique command for a table_id and item_id. Returns : {command: id, item_id, table_id, users} """ command = CommandService.get_unique_command_by_table_id_and_item_id( table_id, item_id) if not command: return json_abort(404, f"Command not found") return jsonify(command)
def create_command() -> Response: """Create a new command for an item and a table. Args : data = {item_id, table_id} Returns : {command : id, item_id, table_id, users} """ data = request.json if not data or "item_id" not in data or "table_id" not in data: abort(400) command = CommandService.create_command(data["item_id"], data["table_id"]) return jsonify({"command": command})
def add_user_order_to_command(command_id: int) -> Response: """ Add a new item for a user on the command of a table. Args : data = {user_id, order_amount} Returns : {command : id, table_id, users } """ data = request.json if (not data or "order_amount" not in data or "user_id" not in data): abort(400) command = CommandService.add_user_order_to_command( command_id=command_id, user_id=data["user_id"], order_amount=data["order_amount"], ) return jsonify({"command": command})
def create_table(user_id: int, jap_event_id: int) -> Table: """ Create a new table. Args : data = {user_id, jap_event_id} """ table = Table(emperor=user_id, jap_event_id=jap_event_id, status=0) member = User.query.filter(User.id.__eq__(user_id)).first() table.members.append(member) db.session.add(table) db.session.commit() table_id = table.id command = CommandService.create_command(1, table_id) table.current_command_id = command.id db.session.add(table, command) db.session.commit() return table