def addStudent(self): surname = str( self.ui.surname.text() ) others = str( self.ui.othernames.text() ) department = str( self.ui.studentDept.currentText() ) campusID = str( self.ui.campusID.text() ) avatar = str( self.ui.studentImage.text() ) # Form Validation if surname == '': self.ui.studentSaved.setText("Surname field is required") self.ui.studentSaved.show() elif others == '': self.ui.studentSaved.setText("Other names field is required") self.ui.studentSaved.show() elif campusID == '': self.ui.studentSaved.setText("Campus ID is required") self.ui.studentSaved.show() elif campusID in self.databag['students']: self.ui.studentSaved.setText("A student exists with that ID") self.ui.studentSaved.show() else: self.databag['students'][campusID] = [ surname + ' ' + others, department,avatar,''] function.talk('Student has been added') self.ui.studentSaved.setText("Student Saved") self.ui.studentSaved.show() function.dump_data(self.databag) self.ui.surname.clear() self.ui.othernames.clear() self.ui.campusID.clear() self.ui.studentImage.clear() QtCore.QTimer.singleShot(1000 * 3, self.ui.studentSaved.hide)
def addStudent(self): surname = str(self.ui.surname.text()) others = str(self.ui.othernames.text()) department = str(self.ui.studentDept.currentText()) campusID = str(self.ui.campusID.text()) avatar = str(self.ui.studentImage.text()) # Form Validation if surname == '': self.ui.studentSaved.setText("Surname field is required") self.ui.studentSaved.show() elif others == '': self.ui.studentSaved.setText("Other names field is required") self.ui.studentSaved.show() elif campusID == '': self.ui.studentSaved.setText("Campus ID is required") self.ui.studentSaved.show() elif campusID in self.databag['students']: self.ui.studentSaved.setText("A student exists with that ID") self.ui.studentSaved.show() else: self.databag['students'][campusID] = [ surname + ' ' + others, department, avatar, '' ] function.talk('Student has been added') self.ui.studentSaved.setText("Student Saved") self.ui.studentSaved.show() function.dump_data(self.databag) self.ui.surname.clear() self.ui.othernames.clear() self.ui.campusID.clear() self.ui.studentImage.clear() QtCore.QTimer.singleShot(1000 * 3, self.ui.studentSaved.hide)
def on_pubmsg(self, conn, e): msg = normalize_message(e.arguments[0]) user_id = e.source.split("!")[0] user_name = e.tags[4]["value"] is_mod = e.tags[8]["value"] is_subscriber = e.tags[10]["value"] timestamp_ms = e.tags[11]["value"] # do not talk to myself if user_id != self.user_id: say_hi(conn, self.irc_channel, self.channel_id, user_id, user_name) logging.info(f"{user_id:>20}: {msg}") logging.info(f"checking if {user_id} in {self.ban_targets}...") if user_id in self.ban_targets: time.sleep(3) talk(conn, self.irc_channel, f"/timeout {user_id} 1") logging.info(f"/timeout {user_id} 1") if user_id == "f1yshadow" and msg == "莉芙溫 下午好~ KonCha": talk(conn, self.irc_channel, f"飛影飄泊 下午好~ KonCha") if user_id == "harnaisxsumire666" and self.gbf_code_re.fullmatch(msg): logging.info(f"GBF room id detected: {msg}") self.data["gbf_room_id_cache"]["user_id"] = msg self.data["gbf_room_num"] += 1 self.trend_talking(conn, msg=msg) if msg.startswith("!"): cmd = msg.split(" ")[0][1:] logging.info("Received command: " + cmd) self.do_command(cmd, user_id) if msg == "馬娘": uma_call(conn, self.irc_channel, self.channel_id, user_name) return
def deleteDepartment(self): currentIndex = self.ui.deptEditSelect.currentIndex() currentDept = str( self.ui.deptEditSelect.currentText() ) self.databag['departments'].pop(currentDept) function.dump_data(self.databag) self.ui.editUpdateNotif.show() function.talk('department deleted') QtCore.QTimer.singleShot(1000 * 10, self.ui.editUpdateNotif.hide) self.ui.deptEditSelect.removeItem(currentIndex)
def recommend_restaurant(self, restaurant, order=[]): restaurant_name = restaurant["restaurantname"] food = restaurant["food"] area = restaurant["area"] pricerange = restaurant["pricerange"] prefs = self.ordered_prefs(order, food, area, pricerange) recommendation = "{} is a restaurant {} {} {}.".\ format(restaurant_name, *prefs) talk(recommendation)
def deleteDepartment(self): currentIndex = self.ui.deptEditSelect.currentIndex() currentDept = str(self.ui.deptEditSelect.currentText()) self.databag['departments'].pop(currentDept) function.dump_data(self.databag) self.ui.editUpdateNotif.show() function.talk('department deleted') QtCore.QTimer.singleShot(1000 * 10, self.ui.editUpdateNotif.hide) self.ui.deptEditSelect.removeItem(currentIndex)
def deleteCourse(self): currentIndex = self.ui.courseEditSelect.currentIndex() currentDept = str( self.ui.deptEditSelect.currentText() ) currentCourse = str( self.ui.courseEditSelect.currentText() ) self.databag['departments'][currentDept].remove(currentCourse) function.dump_data(self.databag) self.ui.editUpdateNotif.show() function.talk('course has been deleted') QtCore.QTimer.singleShot(1000 * 10, self.ui.editUpdateNotif.hide) self.ui.courseEditSelect.removeItem(currentIndex)
def deleteCourse(self): currentIndex = self.ui.courseEditSelect.currentIndex() currentDept = str(self.ui.deptEditSelect.currentText()) currentCourse = str(self.ui.courseEditSelect.currentText()) self.databag['departments'][currentDept].remove(currentCourse) function.dump_data(self.databag) self.ui.editUpdateNotif.show() function.talk('course has been deleted') QtCore.QTimer.singleShot(1000 * 10, self.ui.editUpdateNotif.hide) self.ui.courseEditSelect.removeItem(currentIndex)
def editDept(self): currentIndex = self.ui.deptEditSelect.currentIndex() currentDept = str( self.ui.deptEditSelect.currentText() ) value = str( self.ui.DeptEdit.text() ) self.databag['departments'][value] = self.databag['departments'].pop(currentDept) function.dump_data(self.databag) function.talk('department name changed') self.ui.editUpdateNotif.show() QtCore.QTimer.singleShot(1000 * 10, self.ui.editUpdateNotif.hide) self.ui.deptEditSelect.setItemText(currentIndex, value)
def ask_preference(type="", repeat=False): if repeat: talk("Sorry, nothing like that exists in my database.") talk("Could you specify something else?") elif type == "food": talk("What type of food would you prefer?") elif type == "pricerange": talk("What pricerange are you looking for?") elif type == "area": talk("In which area are you looking for a restaurant?") return uinput()
def editDept(self): currentIndex = self.ui.deptEditSelect.currentIndex() currentDept = str(self.ui.deptEditSelect.currentText()) value = str(self.ui.DeptEdit.text()) self.databag['departments'][value] = self.databag['departments'].pop( currentDept) function.dump_data(self.databag) function.talk('department name changed') self.ui.editUpdateNotif.show() QtCore.QTimer.singleShot(1000 * 10, self.ui.editUpdateNotif.hide) self.ui.deptEditSelect.setItemText(currentIndex, value)
def courseUpdates(self): offered = '' for x in xrange(1,16,1): if getattr(self.ui, 'courseCheck{}'.format(x)).checkState() == 2: offered += str(x) + ':' offered = offered.rstrip(':') studentIndex = str(self.ui.studentID.text()) databag = function.dict_object('data.json') databag['students'][studentIndex][3] = offered function.dump_data(databag) function.talk('Your courses have been updated') self.ui.courseStatus.setText('Courses updated')
def addCourses(self): courseName = str( self.ui.addCourse.text() ) if courseName == '': function.talk('Empty Input') else: selectedDepartment = str( self.ui.courseAddDeptList.currentText() ) self.databag['departments'][selectedDepartment].append(courseName) function.dump_data(self.databag) self.ui.addCourse.clear() self.ui.courseAdded.show() function.talk("Course added") QtCore.QTimer.singleShot(1000 * 3, self.ui.courseAdded.hide)
def addCourses(self): courseName = str(self.ui.addCourse.text()) if courseName == '': function.talk('Empty Input') else: selectedDepartment = str(self.ui.courseAddDeptList.currentText()) self.databag['departments'][selectedDepartment].append(courseName) function.dump_data(self.databag) self.ui.addCourse.clear() self.ui.courseAdded.show() function.talk("Course added") QtCore.QTimer.singleShot(1000 * 3, self.ui.courseAdded.hide)
def courseUpdates(self): offered = '' for x in xrange(1, 16, 1): if getattr(self.ui, 'courseCheck{}'.format(x)).checkState() == 2: offered += str(x) + ':' offered = offered.rstrip(':') studentIndex = str(self.ui.studentID.text()) databag = function.dict_object('data.json') databag['students'][studentIndex][3] = offered function.dump_data(databag) function.talk('Your courses have been updated') self.ui.courseStatus.setText('Courses updated')
def request_inform(self, restaurant, req): noinfo = [] requests = [] for request in req: if restaurant[request]: requests.append(request) else: noinfo.append(request) inform = "" for i, request in enumerate(requests): if i == 0: rest = "of {}".format(restaurant["restaurantname"]) conn = "" elif i == len(requests) - 1: rest = "" conn = " and " else: rest = "" conn = ", " if request == "addr" and "postcode" not in requests: inform += "{}the address {} is {}".\ format(conn, rest, restaurant["addr"]) if request == "addr" and "postcode" in requests: inform += "{}the address {} is {}, {}".\ format(conn, rest, restaurant["addr"], restaurant["postcode"]) if request == "postcode" and "addr" not in requests: inform += "{}the postcode {} is {}".\ format(conn, rest, restaurant["postcode"]) if request == "phone" and rest == "": inform += "{}their phone number is {}".\ format(conn, restaurant["phone"]) if request == "phone" and rest != "": inform += "{}the phone number {} is {}".\ format(conn, rest, restaurant["phone"]) if request == "food" and rest == "": inform += "{}they serve {} food".\ format(conn, restaurant["food"]) if request == "food" and rest != "": inform += "{}{} serves {} food".\ format(conn, restaurant["restaurantname"], restaurant["food"]) if request == "pricerange" and rest == "": inform += "{}it is {} restaurant".\ format(conn, self.pricerange_str(restaurant)) if request == "pricerange" and rest != "": inform += "{}{} is {} restaurant".\ format(conn, restaurant["restaurantname"], self.pricerange_str(restaurant)) talk(inform) if noinfo: self.inform_noinfo(restaurant["restaurantname"], noinfo)
def logout(self): # say goodbye function.talk("Logging out") for x in xrange(1,16,1): # uncheck all checked boxes from session getattr(self.ui, 'courseCheck{}'.format(x)).setCheckState(0) # close course by default getattr(self.ui, 'course{}'.format(x)).hide() # close course checkbox by default getattr(self.ui, 'courseCheck{}'.format(x)).hide() self.ui.courseStatus.setText('Check the courses you want to offer this semester') self.hide() window.show()
def verify(self): __user__ = str(self.ui.adminUname.text()) __pass__ = str(self.ui.adminPass.text()) databag = function.dict_object('data.json') __spass__ = databag['auth'] if __user__ == 'admin' and function.computeHash(__pass__) == __spass__: self.ui.authError.hide() self.close() window.hide() manageCourse.hide() administration.show() else: function.talk("Invalid authentication") self.ui.authError.show()
def logout(self): # say goodbye function.talk("Logging out") for x in xrange(1, 16, 1): # uncheck all checked boxes from session getattr(self.ui, 'courseCheck{}'.format(x)).setCheckState(0) # close course by default getattr(self.ui, 'course{}'.format(x)).hide() # close course checkbox by default getattr(self.ui, 'courseCheck{}'.format(x)).hide() self.ui.courseStatus.setText( 'Check the courses you want to offer this semester') self.hide() window.show()
def changePass(self): """Changes administrator's password""" newpass = str( self.ui.passwordChange.text() ) if newpass == '': self.ui.passwordChanged.setText('Password Field cannot be left blank') self.ui.passwordChanged.show() function.talk('password empty!') else: self.databag['auth'] = str(function.computeHash(newpass)) function.dump_data(self.databag) self.ui.passwordChanged.setText('Password Updated') self.ui.passwordChanged.show() self.ui.passwordChange.clear() function.talk('password updated')
def changePass(self): """Changes administrator's password""" newpass = str(self.ui.passwordChange.text()) if newpass == '': self.ui.passwordChanged.setText( 'Password Field cannot be left blank') self.ui.passwordChanged.show() function.talk('password empty!') else: self.databag['auth'] = str(function.computeHash(newpass)) function.dump_data(self.databag) self.ui.passwordChanged.setText('Password Updated') self.ui.passwordChanged.show() self.ui.passwordChange.clear() function.talk('password updated')
def addDepartment(self): deptInput = str( self.ui.addDept.text() ) if not deptInput: self.ui.addDeptNotice.setText("Empty Input") function.talk('Empty Input') else: self.databag['departments'][deptInput] = [] function.dump_data(self.databag) self.ui.addDeptNotice.setText('Department Saved') self.ui.addDept.clear() self.ui.courseAddDeptList.addItem(deptInput) self.ui.deptEditSelect.addItem(deptInput) self.ui.studentDept.addItem(deptInput) self.ui.ed_department.addItem(deptInput) function.talk("Department saved")
def addDepartment(self): deptInput = str(self.ui.addDept.text()) if not deptInput: self.ui.addDeptNotice.setText("Empty Input") function.talk('Empty Input') else: self.databag['departments'][deptInput] = [] function.dump_data(self.databag) self.ui.addDeptNotice.setText('Department Saved') self.ui.addDept.clear() self.ui.courseAddDeptList.addItem(deptInput) self.ui.deptEditSelect.addItem(deptInput) self.ui.studentDept.addItem(deptInput) self.ui.ed_department.addItem(deptInput) function.talk("Department saved")
def trend_talking(self, conn, msg): # partial matching for word in TREND_WORDS_SUBSTRING: if word in msg: if word not in self.push_trend_cache: self.push_trend_cache[word] = 1 else: self.push_trend_cache[word] += 1 logging.info( f"[COUNTER] {word}:{ self.push_trend_cache[word]}") if self.push_trend_cache[word] >= self.trend_threshold: talk(conn, self.irc_channel, word) self.push_trend_cache[word] = -10 # full matching if msg in TREND_WORDS_EXACT_MATCH: if msg not in self.push_trend_cache: self.push_trend_cache[msg] = 1 else: self.push_trend_cache[msg] += 1 logging.info(f"[COUNTER] {msg}:{ self.push_trend_cache[msg]}") if self.push_trend_cache[msg] >= self.trend_threshold: talk(conn, self.irc_channel, msg)
def inform_noinfo(restaurant, noinfo): inform = "" req = "" for i, request in enumerate(noinfo): if request == "phone": req = "a phone number" elif request == "addr": req = "an address" elif request == "postcode": req = "a postcode" elif request == "food": req = "information about the kitchen" elif request == "pricerange": req = "information about the price" if i == 0: inform += "I don't have {}".format(req) elif i == len(noinfo) - 1: inform += " or {}".format(req) else: inform += ", {}".format(req) inform += " for {}".format(restaurant) talk(inform)
def checkCampusID(self): databag = function.dict_object('data.json') campusID = str(self.ui.matricInput.text()) if campusID != '': if databag['students'].has_key(campusID): self.ui.invalidID.hide() studentName = databag['students'][campusID][0] function.talk("Hello %s, let us register your courses" % studentName) self.hide() manageCourse.grab(campusID) else: self.ui.invalidID.show() function.talk("There is no student with that campus ID in our records") else: self.ui.invalidID.show() function.talk("Your input is empty. Please type your campus ID to log in as a student")
def dizzy(self): now = int(time.time()) if self.dizzy_start_ts == 0: return if self.dizzy_ban_end_ts == 0 and self.dizzy_start_ts + ONBOARDING_PERIOD < now: if not self.dizzy_users: logging.info(f"沒人上船,開船失敗!") talk(self.connection, self.irc_channel, f"沒人上船,開船失敗!") self.dizzy_users = [] self.ban_targets = [] self.dizzy_start_ts = 0 self.dizzy_ban_end_ts = 0 else: n_dizzy_users = (1 if len(self.dizzy_users) < 20 else math.ceil(len(self.dizzy_users) * 0.05)) self.ban_targets = random.sample(self.dizzy_users, n_dizzy_users) ban_targets_str = ", ".join( [f"@{t}" for t in self.ban_targets]) logging.info(f"抓到了 {ban_targets_str} 你就是暈船仔!") talk( self.connection, self.irc_channel, f"抓到了 {ban_targets_str} 你就是暈船仔!我看你五分鐘內都會神智不清亂告白,只好幫你湮滅證據了。", ) self.dizzy_ban_end_ts = (self.dizzy_start_ts + ONBOARDING_PERIOD + BAN_PERIOD) elif now <= self.dizzy_ban_end_ts: ban_targets_str = ", ".join([f"@{t}" for t in self.ban_targets]) logging.info(f"暈船仔 {ban_targets_str} 還在暈") elif now > self.dizzy_ban_end_ts > 0: ban_targets_str = ", ".join([f"@{t}" for t in self.ban_targets]) logging.info(f"放 {ban_targets_str} 下船") talk(self.connection, self.irc_channel, f"放 {ban_targets_str} 下船") self.dizzy_users = [] self.ban_targets = [] self.dizzy_start_ts = 0 self.dizzy_ban_end_ts = 0 else: logging.info( f"now: {now}, dizzy_start_ts: {self.dizzy_start_ts}, dizzy_ban_end_ts: {self.dizzy_ban_end_ts}" )
def checkCampusID(self): databag = function.dict_object('data.json') campusID = str(self.ui.matricInput.text()) if campusID != '': if databag['students'].has_key(campusID): self.ui.invalidID.hide() studentName = databag['students'][campusID][0] function.talk("Hello %s, let us register your courses" % studentName) self.hide() manageCourse.grab(campusID) else: self.ui.invalidID.show() function.talk( "There is no student with that campus ID in our records") else: self.ui.invalidID.show() function.talk( "Your input is empty. Please type your campus ID to log in as a student" )
def do_command(self, cmd, user_id): if cmd == "code": gbf_room_id = self.data["gbf_room_id_cache"].get("user_id") if gbf_room_id: logging.info(f"GBF room: {gbf_room_id}") talk( self.connection, self.irc_channel, f"ㄇㄨ的房號 {gbf_room_id} 這是開台第{self.data['gbf_room_num']}間房 maoThinking", ) if cmd == "船來了": if user_id != self.channel_id: logging.info(f"沒有權限") return now = int(time.time()) if now <= self.dizzy_ban_end_ts: logging.info(f"還在上一次暈船懲罰中喔") return talk( self.connection, self.irc_channel, f"在一分鐘內輸入 !上船 讓溫泉蛋找出誰是暈船仔,被抓到的暈船仔會不斷在三秒後被消音,直到五分鐘結束為止", ) self.dizzy_start_ts = now logging.info(f"開始登記上船時間為 {self.dizzy_start_ts}") if cmd == "上船": now = int(time.time()) if self.dizzy_start_ts == 0: logging.info(f"船還沒來喔!") return if now > self.dizzy_start_ts + ONBOARDING_PERIOD: logging.info( f"現在是 {now} 已經超過開船時間 {self.dizzy_start_ts + ONBOARDING_PERIOD}" ) return if user_id in self.dizzy_users: logging.info(f"{user_id} 已經在船上了!") return self.dizzy_users.append(user_id) logging.info(f"乘客 {user_id} 成功上船!") talk(self.connection, self.irc_channel, f"乘客 {user_id} 成功上船!")
def greetWelcome(self): function.talk("Welcome to Registron")
def addSchool(self): self.databag['school'] = str(self.ui.schoolName.text()) function.dump_data(self.databag) self.ui.schoolSaved.show() QtCore.QTimer.singleShot(1000 * 5, self.ui.schoolSaved.hide) function.talk('School name saved')
def no_restaurant(): talk("I'm sorry, " "there are no restaurants that satisfy your requirements.")
def error(): talk("I'm sorry Dave, I'm afraid I can't do that")
def goodbye(): talk("Glad to be of service,") talk("Good bye!")
def atyourservice(): talk("Glad to be of service!")
def hello(): talk("Hi! How can I help?")
def welcome(): talk("Hello, welcome to our restaurant system.") talk("You can ask for a restaurant by area, price range or food type.") print("") talk("How may I help you?")
def addSchool(self): self.databag['school'] = str( self.ui.schoolName.text() ) function.dump_data(self.databag) self.ui.schoolSaved.show() QtCore.QTimer.singleShot(1000 * 5, self.ui.schoolSaved.hide) function.talk('School name saved')