Browse Source

Fix up ts3/forum points stuff

development
Brandon Cornejo 10 years ago
parent
commit
6f97bba84d
  1. 37
      app/models.py
  2. 4
      app/teamspeak.py
  3. 34
      migrations/versions/50e77b6e7331_.py
  4. 4
      run.py

37
app/models.py

@ -1,5 +1,5 @@
import simplejson as json
from datetime import datetime
from datetime import datetime, timedelta
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.ext.mutable import Mutable
@ -86,7 +86,8 @@ class User(db.Model):
ts3_starttime = db.Column(db.DateTime)
ts3_endtime = db.Column(db.DateTime)
ts3_rewardtime = db.Column(db.DateTime)
ts3_connections = db.Column(MutableDict.as_mutable(Json))
ts3_stretch_award_time = db.Column(db.DateTime)
ts3_longest_stretch = db.Column(db.Interval)
last_post_reward = db.Column(db.Integer)
winrate_data = db.Column(MutableDict.as_mutable(Json))
@ -112,8 +113,8 @@ class User(db.Model):
def __init__(self, steam_id):
self.steam_id = steam_id
self.az_completions = 0
self.ts3_connections = {'list':[]}
self.ts3_rewardtime = datetime.utcnow()
self.ts3_longest_stretch = timedelta()
self.created = datetime.utcnow()
self.last_seen = datetime.utcnow()
self.bio_text = None
@ -128,21 +129,28 @@ class User(db.Model):
now = datetime.utcnow()
self.ts3_starttime = self.ts3_starttime or now
self.ts3_endtime = now
# Add general TS3 points here
if self.ts3_endtime and self.ts3_rewardtime:
delta = (self.ts3_endtime - self.ts3_rewardtime)
duration = (delta.seconds % 3600) // 60
if duration > reward_threshold:
self.ts3_rewardtime = datetime.utcnow()
self.points_from_ts3 += 1
else:
self.ts3_rewardtime = datetime.utcnow()
delta = (self.ts3_endtime - self.ts3_rewardtime)
duration = (delta.seconds % 3600) // 60
if duration > reward_threshold:
self.ts3_rewardtime = datetime.utcnow()
self.points_from_ts3 += 1
# Update last_seen for web profile
self.last_seen = datetime.utcnow()
db.session.commit();
def finalize_connection(self):
self.ts3_connections['list'].append({'starttime': self.ts3_starttime, 'endtime': self.ts3_endtime})
self.ts3_startime = None
# Check for longest!
if self.ts3_endtime and self.ts3_starttime:
current_stretch = self.ts3_endtime - self.ts3_starttime
if current_stretch > self.ts3_longest_stretch:
self.ts3_longest_stretch = current_stretch
self.ts3_stretch_award_time = datetime.utcnow()
# Reset values
self.ts3_starttime = None
self.ts3_endtime = None
db.session.commit();
@ -151,6 +159,7 @@ class User(db.Model):
posts = board.Users.select().where(board.Users.id == int(self.forum_id))[0].posts
if self.last_post_reward:
num_points = (posts - self.last_post_reward) / reward_threshold
print("Old: {0}, New: {1}, ({0} - {1}) / {2}, {3}, {4}".format(self.last_post_reward, posts, reward_threshold, num_points, self.nickname))
if num_points > 0:
self.points_from_forum += num_points
self.last_post_reward += num_points * reward_threshold
@ -266,7 +275,7 @@ class Event(db.Model):
@property
def expired(self):
current_time = datetime.utcnow()
return self.end_time < curent_time
return self.end_time < current_time
def add_participant(self, user):
entry = self.participants.setdefault(user, {'start_time': datetime.utcnow() })

4
app/teamspeak.py

@ -348,6 +348,7 @@ def award_idle_ts3_points(server):
for channel in list_response.data:
if exempt_check(channel['cid']):
exempt_cids.append(channel['cid'])
# Get list of clients
clientlist = server.clientlist()
for clid, client in clientlist.iteritems():
@ -366,6 +367,7 @@ def award_idle_ts3_points(server):
active_users.add(doob)
except KeyError:
pass
doobs = set(models.User.query.filter(models.User.ts3_starttime != None).all())
for doob in doobs.intersection(active_users):
for doob in doobs.difference(active_users):
doob.finalize_connection()

34
migrations/versions/50e77b6e7331_.py

@ -0,0 +1,34 @@
"""empty message
Revision ID: 50e77b6e7331
Revises: 9e520de441f
Create Date: 2014-11-17 13:21:23.705195
"""
# revision identifiers, used by Alembic.
revision = '50e77b6e7331'
down_revision = '9e520de441f'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('user', sa.Column('ts3_longest_stretch', sa.Interval(), nullable=True))
op.add_column('user', sa.Column('ts3_stretch_award_time', sa.DateTime(), nullable=True))
op.drop_column('user', u'random_heroes')
op.drop_column('user', u'ts3_connections')
op.drop_column('user', u'az_completions')
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('user', sa.Column(u'az_completions', mysql.INTEGER(display_width=11), autoincrement=False, nullable=True))
op.add_column('user', sa.Column(u'ts3_connections', mysql.TEXT(), nullable=True))
op.add_column('user', sa.Column(u'random_heroes', mysql.TEXT(), nullable=True))
op.drop_column('user', 'ts3_stretch_award_time')
op.drop_column('user', 'ts3_longest_stretch')
### end Alembic commands ###

4
run.py

@ -104,6 +104,10 @@ def ts3_process_events():
tsServer = createTeamspeakInstance()
process_ts3_events(tsServer)
@manager.command
def forum_award_points():
for user in models.User.query.filter(models.User.forum_id != None).all():
user.update_forum_posts()
if __name__ == '__main__':
manager.run()
Loading…
Cancel
Save