Permalink
Browse files

[+] User rating

  • Loading branch information...
1 parent e17a641 commit 7c4c53b7e7d616e7d0bccada0f2ddc0318de3e9c Igor Kobylin committed Jul 31, 2011
@@ -10,16 +10,18 @@
from django.views.decorators.http import require_POST
from game.models import Game, GameSession, ClientConnectionState, Facts
-from users.models import WaitingUser
+from users.models import UserProfile, WaitingUser
@render_to('core/index.html')
def home(request):
ONLINE_TIMEOUT = datetime.timedelta(minutes=30)
now = datetime.datetime.now()
guard = now - ONLINE_TIMEOUT
+ top_users = UserProfile.objects.order_by('-games_win', 'games_total')[:3]
return {'players': User.objects.filter(last_login__gt=guard).count(),
- 'games_online': Game.objects.filter(finished=False).count()}
+ 'games_online': Game.objects.filter(finished=False).count(),
+ 'top_users': top_users}
@login_required
@@ -26,8 +26,7 @@ def handle(self, *args, **options):
for gs in game.gamesession_set.all():
gs.client_state = ClientConnectionState.LOST
gs.save()
- game.finished = True
- game.save()
+ game.finish()
cleaned_cnt += 1
if cleaned_cnt:
print "Cleaned %s zombie games" % cleaned_cnt
@@ -154,10 +154,22 @@ def is_finished(self):
.exclude(state=GameSessionState.LEFT).count()
is_finished = (active_players < 2) or (not has_cards and not has_sets)
if is_finished:
- self.finished = True
- self.save()
+ self.finish()
return is_finished
+ def finish(self):
+ if self.finished:
+ return
+ for gs in self.gamesession_set.all():
+ if gs.user.pk == self.leader:
+ gs.user.userprofile.games_win += 1
+ else:
+ gs.user.userprofile.games_loss += 1
+ gs.user.userprofile.games_total += 1
+ gs.user.userprofile.save()
+ self.finished = True
+ self.save()
+
@property
def leader(self):
"""Determines leading user,
@@ -262,6 +262,5 @@ def leave_game(request, game_id):
gs.state = GameSessionState.LEFT
gs.left = True
gs.save()
- gs.game.finished = True
- gs.game.save()
+ gs.game.finish()
return redirect('/')
@@ -0,0 +1,87 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Adding field 'UserProfile.games_total'
+ db.add_column('users_userprofile', 'games_total', self.gf('django.db.models.fields.PositiveIntegerField')(default=0), keep_default=False)
+
+ # Adding field 'UserProfile.games_win'
+ db.add_column('users_userprofile', 'games_win', self.gf('django.db.models.fields.PositiveIntegerField')(default=0), keep_default=False)
+
+ # Adding field 'UserProfile.games_loss'
+ db.add_column('users_userprofile', 'games_loss', self.gf('django.db.models.fields.PositiveIntegerField')(default=0), keep_default=False)
+
+
+ def backwards(self, orm):
+
+ # Deleting field 'UserProfile.games_total'
+ db.delete_column('users_userprofile', 'games_total')
+
+ # Deleting field 'UserProfile.games_win'
+ db.delete_column('users_userprofile', 'games_win')
+
+ # Deleting field 'UserProfile.games_loss'
+ db.delete_column('users_userprofile', 'games_loss')
+
+
+ models = {
+ 'auth.group': {
+ 'Meta': {'object_name': 'Group'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+ 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+ },
+ 'auth.permission': {
+ 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+ 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ },
+ 'auth.user': {
+ 'Meta': {'object_name': 'User'},
+ 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+ 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+ },
+ 'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ 'users.userprofile': {
+ 'Meta': {'object_name': 'UserProfile'},
+ 'games_loss': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+ 'games_total': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+ 'games_win': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'}),
+ 'user_pic': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '300'})
+ },
+ 'users.waitinguser': {
+ 'Meta': {'object_name': 'WaitingUser'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'last_poll': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
+ }
+ }
+
+ complete_apps = ['users']
@@ -25,6 +25,9 @@ def __unicode__(self):
class UserProfile(models.Model):
user = models.OneToOneField(User)
user_pic = models.CharField(max_length=300, default='')
+ games_total = models.PositiveIntegerField(default=0)
+ games_win = models.PositiveIntegerField(default=0)
+ games_loss = models.PositiveIntegerField(default=0)
def get_user_json(user):
@@ -146,6 +146,59 @@ a.pseudo:hover {
margin: 0.5em 0 0 0;
}
+.main .top_users_container {
+ position: absolute;
+ top: 0;
+ right: 0;
+ background: #81868a;
+ border-radius: 5px;
+ -moz-border-radius: 5px;
+ padding: 15px 25px 28px;;
+ margin-top: 35px;
+}
+ .main .top_users_container .header {
+ background: #81868a;
+ font-family: 'Lobster', cursive;
+ font-size: 1.35em;
+ color: #42494f;
+ text-shadow: #9fa4a8 0 1px 0;
+ }
+ .main .top_users_container .top_users {
+ margin-top: 20px;
+ width: 14em;
+ }
+ .main .top_users_container .top_users .separator {
+ display: block;
+ height: 2px;
+ margin: 18px 0 15px;
+ background: url(../images/bg_separator_top_users.png);
+ }
+ .main .top_users_container .top_user .info {
+ margin-top: -62px;
+ margin-left: 80px;
+ }
+ .main .top_users_container .top_user .photo {
+ border: #fff 6px solid;
+ border-radius: 3px;
+ -moz-border-radius: 3px;
+ margin-right: 10px;
+ }
+ .main .top_users_container .top_user .name {
+ font-family: 'Lobster', cursive;
+ font-size: 1em;
+ color: #f9f9f9;
+ text-shadow: #a5a7a8 0 -1px 1px;
+ }
+ .main .top_users_container .top_user .stats {
+ margin: 1em 0 0;
+ width: auto;
+ color: #d2d2d2;
+ font-size: 0.8em;
+ font-style: italic;
+ font-weight: bold;
+ text-shadow: #6a6e71 0 -1px 0;
+ }
+
.big_button {
display: block;
width: 15em;
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -58,4 +58,21 @@
</div>
{% endif %}
</div>
+{% if top_users %}
+<div class="top_users_container">
+ <span class="header">Top users</span>
+ <ul class="top_users">
+ {% for user in top_users %}
+ <li class="top_user">
+ <img class="photo" src="{{ user_profile.user_pic }}">
+ <div class="info">
+ <div class="name">{{ user.user.first_name }} {{ user.user.last_name }}</div>
+ <div class="stats">win {{ user.games_win }}, loss {{ user.games_loss }}</div>
+ </div>
+ </li>
+ {% if not forloop.last %}<li class="separator"></li>{% endif %}
+ {% endfor %}
+ </ul>
+</div>
+{% endif %}
{% endblock %}
@@ -11,7 +11,7 @@
<img class="photo" src="{{ user_profile.user_pic }}">
<div class="info">
<div class="name">{{ request.user.first_name }} {{ request.user.last_name }}</div>
- <div class="stat">win 0 | lose 0</div>
+ <div class="stat">win {{ request.user.userprofile.games_win }} | lose {{ request.user.userprofile.games_loss }}</div>
</div>
<form class="logout" action="{% url core.views.logout %}" method="POST">
{% csrf_token %}

0 comments on commit 7c4c53b

Please sign in to comment.