def get_group(group_id): tmp = hashlib.md5(str(mktime(datetime.now().timetuple()))).hexdigest() + "group_" + str(group_id) zscore = rs.zscore("group_ids", group_id) if zscore is not None: rs.zadd(tmp, group_id, zscore) res = get_groups_arr_by_set(tmp, 0, 1) rs.delete(tmp) return {} if len(res) == 0 else res[0] else: rs.delete(tmp) return {}
def battleRejectUser(battle_id, user_id): if rs.exists("battle:" + str(battle_id)) != 1: return -1 uid = loggedUserUid() if rs.hget("battle:%s" % battle_id, "uid") != str(uid): return -2 if uid == user_id: return -3 rs.zadd('battle:%s:users' % battle_id, user_id, 0) rs.srem('battle:%s:accepted' % battle_id, user_id) create_battle_notification(uid, user_id, battle_id, NTFY_BATTLE_REJECT) return rs.scard('battle:%s:accepted' % battle_id)
def get_battle(battle_id): tmp = hashlib.md5(str(mktime( datetime.now().timetuple()))).hexdigest() + "battle_" + str(battle_id) zscore = rs.zscore("battles_ids", battle_id) if zscore is not None: rs.zadd(tmp, battle_id, 10 * zscore) res = get_battles_arr_by_set(tmp, None, 0, 1) rs.delete(tmp) return {} if len(res) == 0 else res[0] else: rs.delete(tmp) return {}
def battleAcceptUser(battle_id, user_id, admin=False): if rs.exists("battle:" + str(battle_id)) != 1: return -1 uid = loggedUserUid() if rs.hget("battle:%s" % battle_id, "uid") != str(uid) and not admin: return -2 rs.zadd('battle:%s:users' % battle_id, user_id, 1) rs.sadd('battle:%s:accepted' % battle_id, user_id) rs.zadd('user:%s:battles' % user_id, battle_id, rs.zscore("battles_ids", battle_id)) if uid != user_id: create_battle_notification(loggedUserUid(), user_id, battle_id, NTFY_BATTLE_ACCEPT) return rs.scard('battle:%s:accepted' % battle_id)
def update_battle(battle_id): uid = loggedUserUid() if uid == 0: return -2 if str(uid) != rs.hget("battle:%s" % battle_id, 'uid'): return -1 try: data = json.loads(request.stream.read()) except: return -3 battle = json.loads(rs.hget("battle:%s" % battle_id, 'data')) battle_old = battle.copy() fulldata = True for k in battle_model: if data.get(k, None) is None: fulldata = False break else: battle[k] = data[k] if not fulldata: return "Few data" if data['type'] not in battle_types: return "Wrong battle type" if battle_old['type'] != data['type']: rs.zrem("battles:%s" % battle_old['type'], battle_id) if battle_old['privacy'] != data['privacy']: rs.zrem("privacy:%s" % battle_old['privacy'], battle_id) tanks = rs.smembers("battle:%s:tanks" % battle_id) for tank_id in tanks: rs.srem("tank:%s:battles" % tank_id, battle_id) rs.delete("battle:%s:tanks" % battle_id) from warg.views.jobs.battle_reminder import remove_battle_reminders remove_battle_reminders(battle_id) rs.delete("battle:%s:reminders" % battle_id) process_battle_db(battle_id, uid, battle, data.get("reminders", default_reminders)) users = rs.zrange('battle:%s:users' % battle_id, 0, -1) for user_id in users: rs.zadd('user:%s:battles' % user_id, battle_id, battle['battle_date']) #from uhelp.views.full_text import storeLookInIndex #storeLookInIndex(look, None, True) rs.sadd("whoosh:battles:updated", battle_id) return battle_id
def battleFollowUser(battle_id): if rs.exists("battle:" + str(battle_id)) != 1: return -1 uid = loggedUserUid() if uid == 0: return -2 if rs.sismember('battle:%s:invited' % battle_id, uid): create_battle_notification(uid, 0, battle_id, NTFY_INVITE_ACCEPT) rs.srem('battle:%s:invited' % battle_id, uid) rs.zadd('battle:%s:users' % battle_id, uid, 1) rs.sadd('battle:%s:accepted' % battle_id, uid) rs.zadd('user:%s:battles' % uid, battle_id, rs.zscore("battles_ids", battle_id)) return 1 else: create_battle_notification(uid, 0, battle_id, NTFY_BATTLE_FOLLOW) return followBattleByUser(battle_id, uid)
def create_group_battle(group_id): uid = loggedUserUid() if uid == 0: return -2 if rs.sismember("group:%s:users" % group_id, uid) == 0: return -1 try: data = json.loads(request.stream.read()) except: return -3 data["privacy"] = "PRIVATE" from warg.views.battle import do_create_battle battle_id = do_create_battle(uid, data) if int(battle_id) > 0: zscore = rs.zscore("battles_ids", battle_id) rs.zadd("group:%s:battles" % group_id, battle_id, zscore) return battle_id
def on_clan_message(uid, msg): clan_id = int(msg.get('clan_id', 0)) if clan_id == 0 or len(msg.get('text', "")) == 0 or rs.sismember( "clan:%s:users" % clan_id, uid) == 0: return chid = "clan:%s:message:%s:" % (clan_id, uid) mid = rs.incr(chid + "counter") chid = chid + str(mid) score = calendar.timegm(datetime.utcnow().timetuple()) chatm = { "id": mid, "text": msg.get('text'), 'sid': uid, 'clan_id': clan_id, "type": "clan_chat" } chatm['user'] = json.loads(rs.get("users:%s" % uid)) rs.hmset(chid, chatm) rs.zadd("clan:%s:messages" % clan_id, chid, score) chatm["create_date"] = score message = json.dumps({"type": "clan_chat", "content": chatm}) members = rs.smembers("clan:%s:users" % clan_id) for user_id in members: send_message_to_user(user_id, message)
def process_battle_db(bid, uid, bdata, reminders): timedelta = int(rs.get('user:%s:timedelta' % uid) or 0) bdata['battle_date'] = int(bdata['battle_date']) + timedelta rs.hmset( "battle:%s" % bid, { 'data': json.dumps(bdata), 'id': bid, 'uid': uid, 'battle_date': bdata['battle_date'], 'type': bdata['type'], 'privacy': bdata['privacy'] }) rs.zadd("battles:%s" % bdata['type'], bid, bdata['battle_date']) rs.zadd("user_battles:" + str(uid), bid, bdata['battle_date']) rs.zadd("battles_ids", bid, bdata['battle_date']) rs.zadd("privacy:%s" % bdata['privacy'], bid, bdata['battle_date']) for remmin in reminders: rs.sadd("battle:%s:reminders" % bid, remmin) from warg.views.jobs.battle_reminder import init_battle_reminders init_battle_reminders(bid, bdata['battle_date'])
def on_chat_message(uid, msg): rid = int(msg.get('rid', 0)) if rid == 0 or len(msg.get('text', "")) == 0: return chid = "chat:message:%s:%s:" % (uid, rid) mid = rs.incr(chid + "counter") chid = chid + str(mid) score = calendar.timegm(datetime.utcnow().timetuple()) chatm = { "id": mid, "text": msg.get('text'), 'is_read': 'false', 'sid': uid, 'rid': rid, "type": "chat" } rs.hmset(chid, chatm) rs.zadd("chat:user:%s:unread" % rid, chid, score) rs.sadd("chat:user:%s:ntfy" % rid, chid) dialog = "chat:dialog:%s:%s" % (min(int(uid), rid), max(int(uid), rid)) rs.zadd(dialog, chid, score) rs.zadd("chat:user:%s:dialogs" % uid, dialog, score) rs.zadd("chat:user:%s:dialogs" % rid, dialog, score) chatm["create_date"] = score message = json.dumps({"type": "chat", "content": chatm}) send_message_to_user(uid, message) send_message_to_user(rid, message) ntfs = rs.scard("chat:user:%s:ntfy" % rid) unread = get_unread._original() unread_message = "" if ntfs > 0 and len(unread) > 0: unread_message = json.dumps({ "type": "unread", "content": { "count": len(unread), "message": unread[0] } }) send_message_to_user(rid, unread_message)
def followBattleByUser(battle_id, by_user_id): rs.zadd('battle:%s:users' % battle_id, by_user_id, 0) rs.zadd('user:%s:battles' % by_user_id, battle_id, rs.zscore("battles_ids", battle_id)) return rs.zcard('battle:%s:users' % battle_id)
def group_del_user(group_id, user_id): rs.srem("group:%s:users" % group_id, user_id) rs.zrem("user:%s:groups" % user_id, group_id) user_cnt = rs.scard('group:%s:users' % group_id) rs.zadd("group_ids", group_id, user_cnt) return user_cnt
def group_add_user(group_id, user_id): rs.sadd("group:%s:users" % group_id, user_id) rs.zadd("user:%s:groups" % user_id, group_id, rs.zcard("user:%s:groups" % user_id) + 1) user_cnt = rs.scard('group:%s:users' % group_id) rs.zadd("group_ids", group_id, user_cnt) return user_cnt
def create_battle_notification(from_user, to_user, battle_id, template): battle = get_battle._original(battle_id) to_user = battle['user']["id"] if int(to_user) == 0 else to_user #timedelta = int(rs.get('user:%s:timedelta' % to_user) or 0) #zonedelta = int(rs.get('user:%s:zonedelta' % to_user) or 0) battle_date = datetime.fromtimestamp( int(battle["battle_date"]), tz=pytz.utc).strftime('UTC %H:%M %d/%m') message = None if template == NTFY_BATTLE_INVITE: message = NTFY_BATTLE_INVITE % (battle['user']['nickname'], battle_date, battle["descr"]) if template == NTFY_BATTLE_FOLLOW: usr = user_datail._original(from_user) to_user = battle['user']["id"] message = NTFY_BATTLE_FOLLOW % (usr["nickname"], battle_date, battle["descr"]) if template == NTFY_BATTLE_UFLLOW: usr = user_datail._original(from_user) to_user = battle['user']["id"] message = NTFY_BATTLE_UFLLOW % (usr["nickname"], battle_date, battle["descr"]) if template == NTFY_BATTLE_KICK: message = NTFY_BATTLE_KICK % (battle['user']['nickname'], battle_date, battle["descr"]) if template == NTFY_BATTLE_ACCEPT: message = NTFY_BATTLE_ACCEPT % (battle_date, battle["descr"]) if template == NTFY_BATTLE_REJECT: message = NTFY_BATTLE_REJECT % (battle_date, battle["descr"]) if template == NTFY_INVITE_ACCEPT: usr = user_datail._original(from_user) to_user = battle['user']["id"] message = NTFY_INVITE_ACCEPT % (usr["nickname"], battle_date, battle["descr"]) if template == NTFY_INVITE_REJECT: usr = user_datail._original(from_user) to_user = battle['user']["id"] message = NTFY_INVITE_REJECT % (usr["nickname"], battle_date, battle["descr"]) if message is None: message = template if rs.sismember("users:virtual", to_user): return chid = "chat:message:%s:%s:" % (from_user, to_user) mid = rs.incr(chid + "counter") chid = chid + str(mid) score = calendar.timegm(datetime.utcnow().timetuple()) chatm = { "id": mid, "text": message, 'is_read': 'false', 'sid': from_user, 'rid': to_user, "type": "battle", "battle_id": battle_id } rs.hmset(chid, chatm) rs.zadd("chat:user:%s:unread" % to_user, chid, score) rs.sadd("chat:user:%s:ntfy" % to_user, chid) rs.zadd("battle:%s:unread" % battle_id, chid, score) ucount = rs.zcard("chat:user:%s:unread" % to_user) unread = get_user_unread(to_user) if len(unread) > 0: unread_message = json.dumps({ "type": "unread", "content": { "count": ucount, "message": unread[0] } }) send_message_to_user(to_user, unread_message)
def clan_del_user(clan_id, user_id): rs.srem("clan:%s:users" % clan_id, user_id) rs.delete("user:%s:clan" % user_id) user_cnt = rs.scard('clan:%s:users' % clan_id) rs.zadd("clan_ids", clan_id, user_cnt) return user_cnt
def clan_add_user(clan_id, user_id): rs.sadd("clan:%s:users" % clan_id, user_id) rs.set("user:%s:clan" % user_id, clan_id) user_cnt = rs.scard('clan:%s:users' % clan_id) rs.zadd("clan_ids", clan_id, user_cnt) return user_cnt