Permalink
Browse files

store leaders, moderators, invites, and lock_count for groups

  • Loading branch information...
1 parent a4e830a commit 77dee8fe6cb46fda8f6c1e43e6e6ddb243bb524c @sbezboro committed May 29, 2014
Showing with 115 additions and 50 deletions.
  1. +14 −0 migrations/groups2.sql
  2. +10 −0 standardweb/models.py
  3. +91 −50 standardweb/scripts/query.py
@@ -0,0 +1,14 @@
+create table group_invite (
+ group_id int(11) not null,
+ invite varchar(20) not null,
+
+ primary key (group_id, invite),
+ foreign key (group_id) references `group` (id)
+) engine=InnoDB default charset=utf8;
+
+alter table playerstats
+ add column is_leader tinyint(1) default 0,
+ add column is_moderator tinyint(1) default 0;
+
+alter table `group`
+ add column lock_count int(11) not null;
View
@@ -142,6 +142,8 @@ class PlayerStats(db.Model, Base):
banned = db.Column(db.Boolean, default=False)
pvp_logs = db.Column(db.Integer, default=0)
group_id = db.Column(db.Integer, db.ForeignKey('group.id'))
+ is_leader = db.Column(db.Boolean, default=False)
+ is_moderator = db.Column(db.Boolean, default=False)
server = db.relationship('Server')
player = db.relationship('Player')
@@ -369,13 +371,21 @@ class Group(db.Model, Base):
land_count = db.Column(db.Integer)
land_limit = db.Column(db.Integer)
member_count = db.Column(db.Integer)
+ lock_count = db.Column(db.Integer)
server = db.relationship('Server')
members = db.relationship('Player',
secondary='join(PlayerStats, Player, PlayerStats.player_id == Player.id)',
primaryjoin='Group.id == PlayerStats.group_id',
secondaryjoin='PlayerStats.player_id == Player.id')
+class GroupInvite(db.Model, Base):
+ __tablename__ = 'group_invite'
+
+ group_id = db.Column(db.Integer, db.ForeignKey('group.id'), primary_key=True)
+ invite = db.Column(db.String(30), primary_key=True)
+
+ group = db.relationship('Group', backref=db.backref('invites'))
player_title = db.Table('player_title',
db.Column('player_id', db.Integer, db.ForeignKey('player.id')),
@@ -16,6 +16,96 @@
import rollbar
+def _handle_groups(server, server_groups):
+ group_uids = [x['uid'] for x in server_groups]
+ groups = Group.query.filter(Group.server == server, Group.uid.in_(group_uids))
+ group_map = {x.uid: x for x in groups}
+
+ for group_info in server_groups:
+ uid = group_info['uid']
+ name = group_info['name']
+ established = group_info['established']
+ land_count = group_info['land_count']
+ land_limit = group_info['land_limit']
+ lock_count = group_info['lock_count']
+ members = group_info['members']
+ leader = group_info['leader']
+ moderators = group_info['moderators']
+ invites = group_info['invites']
+
+ established = datetime.utcfromtimestamp(established / 1000)
+ moderators = set(moderators)
+ invites = set(invites)
+
+ group = group_map.get(uid)
+ if not group:
+ group = Group(uid=uid, server=server)
+
+ group.name = name
+ group.established = established
+ group.land_count = land_count
+ group.land_limit = land_limit
+ group.member_count = len(members)
+ group.lock_count = lock_count
+ group.save(commit=False)
+
+ stats = [p for p in PlayerStats.query.options(
+ joinedload(PlayerStats.player)
+ ).join(Player).filter(PlayerStats.server == server, Player.username.in_(members))]
+
+ for stat in stats:
+ if stat.player.username == leader:
+ stat.is_leader = True
+ stat.is_moderator = False
+ elif stat.player.username in moderators:
+ stat.is_leader = False
+ stat.is_moderator = True
+ else:
+ stat.is_leader = False
+ stat.is_moderator = False
+
+ stat.group = group
+ stat.save(commit=False)
+
+ if group.id:
+ removed_invites = GroupInvite.query.filter(GroupInvite.group_id == group.id,
+ not_(GroupInvite.invite.in_(invites)))
+ for group_invite in removed_invites:
+ db.session.delete(group_invite)
+
+ existing_invites = GroupInvite.query.filter_by(group=group)
+ existing_invites = set([x.invite for x in existing_invites])
+
+ for invite in (invites - existing_invites):
+ group_invite = GroupInvite(group=group, invite=invite)
+ group_invite.save(commit=False)
+
+ removed_group_ids = []
+ removed_groups = Group.query.filter(Group.server == server, not_(Group.uid.in_(group_uids)))
+ for group in removed_groups:
+ removed_group_ids.append(group.id)
+
+ if removed_group_ids:
+ groupless = PlayerStats.query.filter(PlayerStats.server == server,
+ PlayerStats.group_id.in_(removed_group_ids))
+
+ for stat in groupless:
+ stat.group = None
+ stat.is_leader = False
+ stat.is_moderator = False
+ stat.save(commit=False)
+
+ removed_invites = GroupInvite.query.filter(GroupInvite.group_id.in_(removed_group_ids))
+
+ for group_invite in removed_invites:
+ db.session.delete(group_invite)
+
+ db.session.flush()
+
+ for group in removed_groups:
+ db.session.delete(group)
+
+
def _query_server(server, mojang_status):
server_status = api.get_server_status(server) or {}
@@ -85,56 +175,7 @@ def _query_server(server, mojang_status):
'titles': titles
})
- server_groups = server_status.get('groups', [])
- group_uids = [x['uid'] for x in server_groups]
- groups = Group.query.filter(Group.server == server, Group.uid.in_(group_uids))
- group_map = {x.uid: x for x in groups}
-
- for group_info in server_groups:
- uid = group_info['uid']
- name = group_info['name']
- established = group_info['established']
- land_count = group_info['land_count']
- land_limit = group_info['land_limit']
- members = group_info['members']
-
- established = datetime.utcfromtimestamp(established / 1000)
-
- group = group_map.get(uid)
- if not group:
- group = Group(uid=uid, server=server)
-
- group.name = name
- group.established = established
- group.land_count = land_count
- group.land_limit = land_limit
- group.member_count = len(members)
- group.save(commit=False)
-
- stats = [p for p in PlayerStats.query.join(Player)
- .filter(PlayerStats.server == server, Player.username.in_(members))]
-
- for stat in stats:
- stat.group = group
- stat.save(commit=False)
-
- removed_group_ids = []
- removed_groups = Group.query.filter(Group.server == server, not_(Group.uid.in_(group_uids)))
- for group in removed_groups:
- removed_group_ids.append(group.id)
-
- if removed_group_ids:
- groupless = PlayerStats.query.filter(PlayerStats.server == server,
- PlayerStats.group_id.in_(removed_group_ids))
-
- for stat in groupless:
- stat.group = None
- stat.save(commit=False)
-
- db.session.flush()
-
- for group in removed_groups:
- db.session.delete(group)
+ _handle_groups(server, server_status.get('groups', []))
five_minutes_ago = datetime.utcnow() - timedelta(minutes=10)
result = PlayerStats.query.filter(PlayerStats.server == server,

0 comments on commit 77dee8f

Please sign in to comment.