def delete_battle(battle_id, admin=0): if rs.exists("battle:%s" % battle_id) == 1 and (admin == 1 or str( loggedUserUid()) == rs.hget("battle:%s" % battle_id, 'uid')): uid = rs.hget("battle:%s" % battle_id, 'uid') rs.zrem("user_battles:" + uid, battle_id) rs.zrem("battles_ids", battle_id) battle = json.loads(rs.hget("battle:%s" % battle_id, 'data')) rs.zrem("battles:%s" % battle['type'], battle_id) rs.zrem("privacy:%s" % battle['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) users = rs.zrange('battle:%s:users' % battle_id, 0, -1) from warg.views.battle_followers import unFollowBattleByUser from warg.views.notifications import create_battle_notification, NTFY_BATTLE_KICK for user_id in users: unFollowBattleByUser(battle_id, int(user_id)) if user_id != uid: create_battle_notification(uid, user_id, battle_id, NTFY_BATTLE_KICK) 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) rs.delete("battle:%s" % battle_id) rs.sadd("whoosh:battles:deleted", battle_id) return 1 return 0
def send_battle_reminder(battle_id, delta): battle = json.loads(rs.hget("battle:%s" % battle_id, 'data')) user_id = rs.hget("battle:%s" % battle_id, 'uid') accepted = rs.smembers('battle:%s:accepted' % battle_id) for member in accepted: create_battle_notification(user_id, member, battle_id, REMINDER_BTL_START % (battle['descr'], delta_to_left(delta))) hours, minutes = delta.seconds // 3600, delta.seconds % 3600 / 60 key = "battle:%s:job:%s" % (battle_id, hours * 60 + minutes) reminders[key] = None
def init_battle_reminders(battle_id, bdate): remitems = rs.smembers("battle:%s:reminders" % battle_id) for remmin in remitems: #remmin = int(remmin) delta = timedelta(minutes=int(remmin)) date = datetime.fromtimestamp(bdate) - delta if int(calendar.timegm(datetime.now().timetuple())) <= int(calendar.timegm(date.timetuple())): print "battle", battle_id, "remind", remmin, datetime.fromtimestamp(bdate), date reminders["battle:%s:job:%s" % (battle_id, remmin)] = sched.add_date_job(send_battle_reminder, date, args=[battle_id, delta])
def delete_group(group_id, admin=0): if rs.exists("group:%s" % group_id) == 1 and (admin == 1 or str(loggedUserUid()) == rs.hget("group:%s" % group_id, 'uid')): users = rs.smembers("group:%s:users" % group_id) for user_id in users: rs.srem("user:%s:groups" % user_id, group_id) rs.delete("group:%s:users" % group_id) rs.delete("group:%s" % group_id) return 1 else: return 0
def remove_battle_reminders(battle_id): remitems = rs.smembers("battle:%s:reminders" % battle_id) for remmin in remitems: key = "battle:%s:job:%s" % (battle_id, remmin) job = reminders.get(key) if job is not None: try: sched.unschedule_job(job) except: pass reminders[key] = None
def notify_online_status(uid, is_online): followers = rs.smembers('user:'******':followers') message = json.dumps({ "type": "online_status", "content": { "user_id": uid, "online": is_online } }) for f in followers: send_message_to_user(f, message)
def detail(user_id): #return request.headers.get('Cookie') u = None if (user_id is not None) and rs.exists('users:' + str(user_id)) == 1: u = json.loads(rs.get("users:" + str(user_id))) u['soc_links'] = list(rs.smembers('user_soc_links:' + str(user_id))) u['is_online'] = int(rs.sismember('users_online', user_id)) u['is_follow'] = int(rs.sismember('user:%s:followers' % user_id, loggedUserUid())) u['virtual'] = int(rs.sismember('users:virtual', user_id)) u['memo'] = rs.get('user:%s:memo:%s' % (user_id, loggedUserUid())) clan_id = int(rs.get("user:%s:clan" % user_id) or 0) if clan_id > 0: u['clan'] = json.loads(rs.hget("clan:%s" % clan_id, 'data')) #u = json.dumps(u) return u or {}
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 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)