def add_poll(self, value): test = time.strftime('%y-%m-%d %H%M-%S') if not ([item for item in self.poll_list if item[0] == value]): if len(self.poll_list) < self.server.config['poll_slots']: self.poll_list.append([value, test]) tmp = time.strftime('%y-%m-%d %H:%M:%S') newfile = { 'name': value, 'polldetail': None, 'multivote': False, 'choices': ["Yes", "No"], 'votes': {"yes": 0, "no": 0}, 'created': tmp, 'log': [], } with open('storage/poll/{} \'{}\'.yaml'.format(test, value), 'w') as file: yaml.dump(newfile, file, default_flow_style=False) logger.log_serverpoll('Poll \'{}\' added successfully.'.format(value)) else: logger.log_serverpoll('Failed to add poll. Reason: The poll queue is full.') raise ServerError('The Poll Queue is full!') else: logger.log_serverpoll('Failed to add poll. Reason: This poll already exists.') raise ServerError('This poll already exists.') self.write_poll_list()
def remove_poll(self, value): if ([i for i in self.poll_list if i[0] == "{}".format(value)]): self.poll_list = [i for i in self.poll_list if i[0] != "{}".format(value)] logger.log_serverpoll('Poll \'{}\' removed.'.format(value)) elif value == "all": self.poll_list = [] logger.log_serverpoll('All polls removed.') else: logger.log_serverpoll('Poll removal failed. Reason: The specified poll does not exist.') raise ServerError('The specified poll does not exist.') self.write_poll_list()
def add_vote(self, value, vote, client): tmp = time.strftime('%y-%m-%d %H:%M:%S') try: # Open that shit up and extract the important parts. poll_voting = [] for poll in self.poll_list: if poll[0].lower() == value.lower(): poll_voting = poll break if not poll_voting: raise ServerError('Poll not found.') stream = open('storage/poll/{} \'{}\'.yaml'.format(poll_voting[1], poll_voting[0]), 'r') self.vote = yaml.load(stream) log = self.vote['log'] if ([item for item in log if item[1] == client.ipid] or [item for item in log if item[2] == client.hdid]) and ( not self.vote['multivote']): # Now to log their failed vote self.vote['log'] += (['FAILED VOTE', tmp, client.ipid, client.hdid, vote, "{} ({}) at area {}".format(client.name, client.get_char_name(), client.area.name)],) self.write_votelist(poll_voting) logger.log_serverpoll( 'Vote in poll {} \'{}\' failed by {} ({}) in {}, with IP {} and HDID {}, at {}. Reason: Already voted.'.format( poll[0], vote, client.name, client.get_char_name(), client.area.name, client.ipid, client.hdid, tmp)) client.send_host_message('You have already voted in this poll.') elif [item for item in log if ((item[1] == client.ipid or item[2] == client.hdid) and (item[3].lower() == vote.lower()))]: self.vote['log'] += (['FAILED VOTE', tmp, client.ipid, client.hdid, vote, "{} ({}) at area {}".format(client.name, client.get_char_name(), client.area.name)],) self.write_votelist(poll_voting) logger.log_serverpoll( 'Vote in poll {} \'{}\' failed by {} ({}) in {}, with IP {} and HDID {}, at {}. Reason: Already voted.'.format( poll[0], vote, client.name, client.get_char_name(), client.area.name, client.ipid, client.hdid, tmp)) client.send_host_message('You have chosen this choice already.') else: # If they aren't a filthy rigger, they should get to this point if vote.lower() in [x.lower() for x in self.vote['choices']]: self.vote['votes'][vote.lower()] += 1 tmp = time.strftime('%y-%m-%d %H:%M:%S') self.vote['log'] += ([tmp, client.ipid, client.hdid, vote, "{} ({}) at area {}".format(client.name, client.get_char_name(), client.area.name)],) self.write_votelist(poll_voting) logger.log_serverpoll( 'Vote in poll {} \'{}\' added succesfully by {} ({}) in {}, with IP {} and HDID {}, at {}.'.format( poll[0], vote, client.name, client.get_char_name(), client.area.name, client.ipid, client.hdid, tmp)) client.send_host_message('You have successfully voted! Congratulations.') except FileNotFoundError: logger.log_serverpoll( 'Vote in poll {} \'{}\' failed by {} ({}) in {}, with IP {} and HDID {}, at {}. Reason:FileNotFound error.'.format( poll[0], vote, client.name, client.get_char_name(), client.area.name, client.ipid, client.hdid, tmp)) client.send_host_message('Voting Error - Poll does not exist.') raise ServerError('The specified poll does not have a file associated with it.') except IndexError: # todo: A bit redundant. There's probably a better way. if vote.lower() in [x.lower() for x in self.vote['choices']]: self.vote['votes'][vote.lower()] += 1 tmp = time.strftime('%y-%m-%d %H:%M:%S') self.vote['log'] += ([tmp, client.ipid, client.hdid, vote, "{} ({}) at area {}".format(client.name, client.get_char_name(), client.area.name)],) self.write_votelist(poll_voting) logger.log_serverpoll('Vote \'{}\' added successfully by {}'.format(vote, client.get_ip())) client.send_host_message('You have successfully voted! Congratulations.')