Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
[+] Init desk in view and return sets count in status request.
Browse files Browse the repository at this point in the history
  • Loading branch information
Serge Travin committed Jul 31, 2011
1 parent d063015 commit 841ca20
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
29 changes: 25 additions & 4 deletions setwithme/apps/game/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@
'shading': ('solid', 'open', 'striped')}


def default_remaining_cards():
def initial_cards(desk=True):
ids = range(81)
random.shuffle(ids)
return ','.join(map(str, ids))
def get_desk():
return ','.join(
map(str,
ids[:constants.CARDS_ON_DESK]))
def get_remaining():
return ','.join(
map(str,
ids[constants.CARDS_ON_DESK:]))
return get_desk if desk else get_remaining



class Game(models.Model):
Expand All @@ -33,8 +42,10 @@ class Game(models.Model):
start = models.DateTimeField(default=datetime.datetime.now)
end = models.DateTimeField(null=True, default=None)
remaining_cards = models.CommaSeparatedIntegerField(
max_length=250, default=default_remaining_cards)
desk_cards = models.CommaSeparatedIntegerField(max_length=250, default='')
max_length=250, default=initial_cards(desk=False))
desk_cards = models.CommaSeparatedIntegerField(
max_length=250,
default=initial_cards(desk=True))

@property
def rem_cards_list(self):
Expand Down Expand Up @@ -126,6 +137,16 @@ def has_sets(self):
return True
return False

def count_sets(self):
cnt = 0
cards = sorted(self.desk_cards_list)
for ncard1, card1 in enumerate(cards):
for ncard2, card2 in enumerate(cards[ncard1+1:]):
for ncard3, card3 in enumerate(cards[ncard2+ncard1+1+1:]):
if is_set(Card(id=card1), Card(id=card2), Card(id=card3)):
cnt += 1
return cnt

def is_finished(self):
has_cards = len(self.remaining_cards) > 0
has_sets = self.has_sets()
Expand Down
20 changes: 19 additions & 1 deletion setwithme/apps/game/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import datetime
import json
from django.contrib.auth.decorators import login_required

from django.core.urlresolvers import reverse
Expand All @@ -14,7 +15,22 @@

@render_to('game/game_screen.html')
def game_screen(request, game_id):
return {'game_id': game_id}
game = Game.objects.get(id=game_id)
users = [gs.serialize(request.user.id) for gs in \
game.gamesession_set.all()]
desc_cards = game.desk_cards_list
rem_cards_cnt = len(game.rem_cards_list)
initial_status= {'users': users,
'cards': [{'id': card_id,
'class': Card(id=card_id).as_text()} \
for card_id in desc_cards],
'cards_left': rem_cards_cnt,
'game': {
'is_finished': game.is_finished(),
'leader': game.leader},
'chat': []}
return {'game_id': game_id,
'initial_status': json.dumps(initial_status, separators=(',',':'))}


@ajax_request
Expand Down Expand Up @@ -80,6 +96,8 @@ def get_status(request, game_id):
for card_id in desc_cards],
'cards_left': rem_cards_cnt,
'game': {
'has_sets': game.has_sets(),
'sets_count': game.count_sets(),
'is_finished': game.is_finished(),
'leader': game.leader},
'chat': [cm.get_serialized(request.user) for cm in chat_qs]}
Expand Down
4 changes: 3 additions & 1 deletion setwithme/static/js/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ SetWithMe.Game = {
this._poller = new SetWithMe.Poller('/game/get_status/' + this._id);
this._poller.onSuccess = this._onStatusReceived.bind(this);
this._poller.onError = this._poller.onSuccess.bind(this);
this._poller.start();
this._onStatusReceived(SetWithMe.gameInitialStatus);
window.setTimeout(this._poller.start.bind(this._poller), 1000);


this._status = this.statuses.NORMAL;
this._statusInterval = setInterval(SetWithMe.Game._updateStatus, 1000);
Expand Down
1 change: 1 addition & 0 deletions setwithme/templates/game/game_screen.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
SetWithMe.Game.init('{{ game_id }}');
$('#js_set_button').click(SetWithMe.Game.markSet.bind(SetWithMe.Game));
});
SetWithMe.gameInitialStatus = {{ initial_status|safe }};
</script>
{% endblock %}

Expand Down

0 comments on commit 841ca20

Please sign in to comment.