def is_valid_id(_id): Userdb = db() valid_id_list = Userdb.find_id() if _id in valid_id_list: return True else: return False
def random_for_C(): Userdb = db() win_id = get_win() if not win_id or win_id == 0: return luck_userc = list(Userdb.find({'group': int(win_id)})) luck_num = random.randint(0, len(luck_userc)) while luck_userc[luck_num].get('lucktag', 0) == 1: luck_num = random.randint(0, len(luck_userc)) luck_userc[luck_num]['lucktag'] = 1 Userdb.save(luck_userc[luck_num]) return luck_userc[luck_num]
def set_game_score(game_id, rank): Userdb = db() if game_id == 1: rank1_store = 20 rank_store = rank1_store else: rank1_store = 16 rank_store = rank1_store game_score = {} for i in rank: game_score[i] = rank_store rank_store -= rank1_store / 4 Userdb.set_gamewin(game_id, game_score)
def vote(user_vote): Userdb = db() if not is_valid_time(): return 1 #非法时间 if not is_valid_id(user_vote['nianhuiid']): return 2 #非法id if user_vote.get('vote1') not in [ 1, 2, 3, 4, 5 ] or user_vote.get('vote2') not in [1, 2, 3, 4, 5]: return 3 #投票内容不合法 if user_vote.get('vote1') == user_vote.get('vote2'): return 4 #重复投票 Userdb.save_vote(user_vote) return 0
def init_all(): Userdb = db() user_list = Userdb.find() for i in user_list: if i.get('name') == 'system': i['valid_time'] = 'True' i['win_id'] = 0 i['temp_result'] = None i['game1'] = None i['game2'] = None else: i['vote1'] = 0 i['vote2'] = 0 i['lucktag'] = 0 Userdb.save(i)
def get_result_now(): Userdb = db() result = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '0': 0} userlist = Userdb.find() for user in userlist: if user['name'] == 'system': continue result[str(user.get('vote1', 0))] += 1 result[str(user.get('vote2', 0))] += 1 game1, game2 = Userdb.get_gamewin() if game1: for key in result.keys(): result[key] += game1.get(str(key), 0) if game2: for key in result.keys(): result[key] += game2.get(str(key), 0) return result, get_temp_result()
def random_for_vote_C(): Userdb = db() win_id = get_win() if not win_id: return luck_voteuserc = set() userlist = list(Userdb.find()) print len(userlist) for user in userlist: if (user.get('vote1') == win_id or user.get('vote2') == win_id) and user.get('lucktag') != 1: luck_voteuserc.add(user.get('nianhuiid')) luck_voteuserc = list(luck_voteuserc) print len(luck_voteuserc) luckvote = random.randint(0, len(luck_voteuserc)) print luckvote luckuser = Userdb.find_one({'nianhuiid': luck_voteuserc[luckvote]}) luckuser['lucktag'] = 1 Userdb.save(luckuser) return luckuser
def insert_db_from_txt(): f = open('nianhuiid.txt', 'r') content = f.readlines() f.close() f = open(FILEPATH, 'r') namelist = f.readlines() f.close() name_dic = {} for nameline in namelist[1:]: user_info = nameline.split(',') try: name = user_info[1].decode('gb2312') except: #print user name = user_info[1].decode('gbk') group = GROUPDICT.get(user_info[-1].strip(), 5) name_dic[name] = group print name_dic Userdb = db() for i in content: user = {} ct = i.split(' ') user['nianhuiid'] = ct[0] if len(ct) > 2: user['name'] = ' '.join([ct[1], ct[2], ct[3], ct[4]]).replace('\n', '').replace('\r', '') else: user['name'] = i.split(' ')[1].replace('\n', '').replace('\r', '') user['group'] = name_dic.get(user['name'].decode('utf8'), 0) Userdb.insert_user(user) user = {'name': 'system', 'valid_time': 'True'} Userdb.insert_user(user)
def set_win(winid): Userdb = db() Userdb.set_win(winid)
def get_win(): Userdb = db() win_id = Userdb.get_win() return win_id
def set_valid_time(valid_time): Userdb = db() Userdb.set_valid_time(valid_time)
def is_valid_time(): Userdb = db() valid_time = Userdb.get_valid_time() return eval(valid_time)
def get_temp_result(): Userdb = db() return Userdb.get_temp_result()
def set_temp_result(): result_now, last_temp = get_result_now() Userdb = db() Userdb.set_temp_result(result_now)
###########保存当前投票结果########## def set_temp_result(): result_now, last_temp = get_result_now() Userdb = db() Userdb.set_temp_result(result_now) def get_temp_result(): Userdb = db() return Userdb.get_temp_result() if __name__ == "__main__": #init_all() Userdb = db() user_list = Userdb.find() for i in user_list: if i.get('name') == 'system': continue i['vote1'] = random.randint(1, 5) i['vote2'] = random.randint(1, 5) a = vote(i) ''' print get_result_now() set_win(3) print random_for_C() print random_for_vote_C() print random_for_vote_C() print random_for_vote_C() '''