This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/elegion/setwith.me
- Loading branch information
Showing
21 changed files
with
418 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# -*- coding: utf-8 -*- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# 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 model 'Game' | ||
db.create_table('game_game', ( | ||
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), | ||
('finished', self.gf('django.db.models.fields.BooleanField')(default=False)), | ||
('start', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)), | ||
('end', self.gf('django.db.models.fields.DateTimeField')(default=None, null=True)), | ||
('cards', self.gf('django.db.models.fields.CommaSeparatedIntegerField')(max_length=250)), | ||
)) | ||
db.send_create_signal('game', ['Game']) | ||
|
||
# Adding model 'GameSession' | ||
db.create_table('game_gamesession', ( | ||
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), | ||
('game', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['game.Game'], null=True)), | ||
('state', self.gf('django.db.models.fields.IntegerField')(default=0)), | ||
('user', self.gf('django.db.models.fields.CharField')(max_length=50)), | ||
('sets_found', self.gf('django.db.models.fields.IntegerField')(default=0)), | ||
('failures', self.gf('django.db.models.fields.IntegerField')(default=0)), | ||
)) | ||
db.send_create_signal('game', ['GameSession']) | ||
|
||
|
||
def backwards(self, orm): | ||
|
||
# Deleting model 'Game' | ||
db.delete_table('game_game') | ||
|
||
# Deleting model 'GameSession' | ||
db.delete_table('game_gamesession') | ||
|
||
|
||
models = { | ||
'game.game': { | ||
'Meta': {'object_name': 'Game'}, | ||
'cards': ('django.db.models.fields.CommaSeparatedIntegerField', [], {'max_length': '250'}), | ||
'end': ('django.db.models.fields.DateTimeField', [], {'default': 'None', 'null': 'True'}), | ||
'finished': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'start': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}) | ||
}, | ||
'game.gamesession': { | ||
'Meta': {'object_name': 'GameSession'}, | ||
'failures': ('django.db.models.fields.IntegerField', [], {'default': '0'}), | ||
'game': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['game.Game']", 'null': 'True'}), | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'sets_found': ('django.db.models.fields.IntegerField', [], {'default': '0'}), | ||
'state': ('django.db.models.fields.IntegerField', [], {'default': '0'}), | ||
'user': ('django.db.models.fields.CharField', [], {'max_length': '50'}) | ||
} | ||
} | ||
|
||
complete_apps = ['game'] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import datetime | ||
from django.db import models | ||
from django.contrib.auth.models import User | ||
|
||
|
||
attributes_order = ('color', 'symbol', 'number', 'shading') | ||
attributes = {'color': ('red', 'green', 'purple'), | ||
'symbol': ('oval', 'squiggle', 'diamond'), | ||
'number': (1, 2, 3), | ||
'shading': ('solid', 'open', 'striped')} | ||
|
||
|
||
class Game(models.Model): | ||
|
||
finished = models.BooleanField(default=False) | ||
start = models.DateTimeField(default=datetime.datetime.now) | ||
end = models.DateTimeField(null=True, default=None) | ||
cards = models.CommaSeparatedIntegerField(max_length=250) | ||
|
||
def __init__(self, *args, **kwargs): | ||
models.Model.__init__(self, *args, **kwargs) | ||
self.cards = ','.join(map(str, xrange(81))) | ||
|
||
def remove_cards(self, first, second, third): | ||
cards = map(int, self.cards.split(',')) | ||
cards.remove(first) | ||
cards.remove(second) | ||
cards.remove(third) | ||
self.cards = ','.join(map(str, cards)) | ||
self.save() | ||
|
||
def get_cards(self): | ||
return map(int, self.cards.split(',')) | ||
|
||
|
||
class GameSession(models.Model): | ||
|
||
game = models.ForeignKey(Game, null=True) | ||
state = models.IntegerField(default=0) | ||
#user = models.ForeignKey(User) | ||
user = models.CharField(max_length=50) # Session key | ||
sets_found = models.IntegerField(default=0) | ||
failures = models.IntegerField(default=0) | ||
|
||
def serialize(self): | ||
return {'game': self.game_id, | ||
'user': self.user, | ||
'sets_found': self.sets_found, | ||
'failures': self.failures} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import uuid | ||
from django.test import TestCase | ||
from game.utils import * | ||
from game.models import * | ||
|
||
class IsSetTestCase(TestCase): | ||
|
||
def test_match(self): | ||
self.assertTrue(match(Color.red, Color.red, Color.red)) | ||
self.assertTrue(match(Color.red, Color.green, Color.purple)) | ||
self.assertFalse(match(Color.red, Color.green, Color.green)) | ||
|
||
def test_set(self): | ||
first = Card(1, Shading.opened, Color.green, Symbol.diamond) | ||
second = Card(2, Shading.opened, Color.green, Symbol.diamond) | ||
third = Card(3, Shading.opened, Color.green, Symbol.diamond) | ||
self.assertTrue(is_set(first, second,third)) | ||
|
||
first = Card(1, Shading.opened, Color.green, Symbol.diamond) | ||
second = Card(2, Shading.solid, Color.purple, Symbol.oval) | ||
third = Card(3, Shading.striped, Color.red, Symbol.squiggle) | ||
self.assertTrue(is_set(first, second,third)) | ||
|
||
first = Card(1, Shading.opened, Color.green, Symbol.diamond) | ||
second = Card(2, Shading.opened, Color.purple, Symbol.oval) | ||
third = Card(3, Shading.striped, Color.red, Symbol.squiggle) | ||
self.assertFalse(is_set(first, second,third)) | ||
|
||
|
||
class GameModelTestCase(TestCase): | ||
|
||
def test_game_session_creation(self): | ||
user_id = uuid.uuid4().hex | ||
game_session, created = GameSession.objects.get_or_create(user=user_id) | ||
self.assertTrue(created) | ||
self.assertIsNotNone(game_session) | ||
game_session, created = GameSession.objects.get_or_create(user=user_id) | ||
self.assertFalse(created) | ||
self.assertIsNotNone(game_session) | ||
|
||
def test_game_creation(self): | ||
user_id_1 = uuid.uuid4().hex | ||
game_session_1 = GameSession.objects.get_or_create(user=user_id_1)[0] | ||
user_id_2 = uuid.uuid4().hex | ||
game_session_2 = GameSession.objects.get_or_create(user=user_id_2)[0] | ||
game = Game.objects.create() | ||
self.assertIsNotNone(game) | ||
game_session_1.game = game | ||
game_session_2.game = game | ||
game_session_1.save() | ||
game_session_2.save() | ||
|
||
def test_game_get_cards(self): | ||
game = Game.objects.create() | ||
cards = game.get_cards() | ||
self.assertEqual(list(xrange(81)), cards) | ||
|
||
def test_game_remove_cards(self): | ||
game = Game.objects.create() | ||
game.remove_cards(1, 4, 7) | ||
lst = range(81) | ||
lst.remove(1) | ||
lst.remove(4) | ||
lst.remove(7) | ||
cards = game.get_cards() | ||
self.assertEqual(lst, cards) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from django.conf.urls.defaults import * | ||
|
||
urlpatterns = patterns('game.views', | ||
url(r'^waiting_screen/$', 'waiting_screen'), | ||
url(r'^game_screen/$', 'game_screen'), | ||
url(r'^create/$', 'start_game'), | ||
url(r'^status/$', 'status'), | ||
url(r'^$', 'waiting_screen', name='index'), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
def match(first, second, third): | ||
return (first == second and second == third) or \ | ||
(first != second and first != third and second != third) | ||
|
||
|
||
class Color: | ||
red = 1 | ||
green = 2 | ||
purple = 3 | ||
|
||
class Symbol: | ||
oval = 1 | ||
squiggle = 2 | ||
diamond = 3 | ||
|
||
class Shading: | ||
solid = 1 | ||
opened = 2 | ||
striped = 3 | ||
|
||
|
||
class Card: | ||
def __init__(self, number, shading, color, symbol): | ||
self.number = number | ||
self.shading = shading | ||
self.color = color | ||
self.symbol = symbol | ||
|
||
|
||
attributes = ('number', 'shading', 'color', 'symbol') | ||
def is_set(first, second, third): | ||
return all(match( | ||
getattr(first, a_name), | ||
getattr(second, a_name), | ||
getattr(third, a_name)) for a_name in attributes) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
import datetime | ||
from django.contrib.sessions.models import Session | ||
from django.views.decorators.http import require_POST | ||
from game.models import Game, GameSession | ||
from annoying.decorators import ajax_request, render_to | ||
from django.shortcuts import redirect | ||
|
||
|
||
@render_to('game/waiting_screen.html') | ||
def waiting_screen(request): | ||
sessions = Session.objects.filter(expire_date__gt=datetime.datetime.now()).all() | ||
users = [s.session_key for s in sessions if s.session_key != request.session.session_key] | ||
return {'waiting_users': users} | ||
|
||
|
||
@render_to('game/game_screen.html') | ||
def game_screen(request): | ||
return {} | ||
|
||
|
||
@require_POST | ||
def start_game(request): | ||
opponent = request.POST['opponent'] | ||
g_session = GameSession.objects.get_or_create( | ||
user=request.session.session_key)[0] | ||
g_session_other = GameSession.objects.get_or_create(user=opponent)[0] | ||
game = Game.objects.create() | ||
g_session.game = game | ||
g_session_other.game = game | ||
g_session.save() | ||
g_session_other.save() | ||
return redirect(game_screen) | ||
|
||
|
||
@ajax_request | ||
def status(request): | ||
game_session, created = GameSession.objects.\ | ||
get_or_create(user=request.session.session_key) | ||
return {'session': game_session.serialize()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# -*- coding: utf-8 -*- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# 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 model 'UserProfile' | ||
db.create_table('users_userprofile', ( | ||
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), | ||
('user', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['auth.User'], unique=True)), | ||
)) | ||
db.send_create_signal('users', ['UserProfile']) | ||
|
||
|
||
def backwards(self, orm): | ||
|
||
# Deleting model 'UserProfile' | ||
db.delete_table('users_userprofile') | ||
|
||
|
||
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'}, | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'}) | ||
} | ||
} | ||
|
||
complete_apps = ['users'] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.db import models | ||
from django.contrib.auth.models import User | ||
|
||
class UserProfile(models.Model): | ||
|
||
user = models.OneToOneField(User, db_index=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from django.conf.urls.defaults import * | ||
|
||
urlpatterns = patterns('users.views', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# -*- coding: utf-8 -*- | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ Django==1.3 | |
epio==0.2.8 | ||
httplib2==0.6.0 | ||
simplejson==2.1.6 | ||
South==0.7.3 | ||
django-annoying==0.7.6 | ||
|
Oops, something went wrong.