-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
145 lines (118 loc) · 3.79 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import hashlib
from django.db import models
class Bug(object):
def __init__(self, level):
self.level = level
self.dots = level.dots
self.position = level.start
self.direction = 0
@property
def real_direction(self):
return self.direction % 4
def move(self, move):
if not self.dots:
return "@"
elif move == "s":
return self.step_forward()
elif move == "r":
return self.turn_right()
elif move == "l":
return self.turn_left()
def step_forward(self):
moves = (
(-1, 0), (0, 1), (1, 0), (0, -1)
)
next_move = moves[self.real_direction]
next_step = self.level.get(
self.position[0] + next_move[0],
self.position[1] + next_move[1]
)
if next_step in (".", "o", "u"):
self.position = (
self.position[0] + next_move[0],
self.position[1] + next_move[1]
)
if next_step == "o":
self.level.lines[self.position[0]][self.position[1]] = "."
self.dots.remove(self.position)
return "s"
return "s"
else:
return "x"
def turn_left(self):
self.direction-=1
return "l"
def turn_right(self):
self.direction+=1
return "r"
class Level(models.Model):
hash = models.CharField(max_length=40, unique=True, db_index=True)
name = models.CharField(max_length=64)
content = models.TextField()
points = models.IntegerField()
maxsize = models.IntegerField()
def __init__(self, *args, **kwargs):
super(Level, self).__init__(*args, **kwargs)
self.lines = [map(str, line) for line in self.content.split("\n")]
def get(self, x, y):
if x >= 0 and y >= 0:
try:
return self.lines[x][y]
except IndexError:
pass
return None
@property
def start(self):
for i, line in enumerate(self.lines):
try:
index = line.index("u")
return (i, index)
except ValueError:
pass
@property
def dots(self):
dots = []
for y, line in enumerate(self.lines):
for x, element in enumerate(line):
if element == "o":
dots.append((y, x))
return dots
@property
def next(self):
n = False
for level in Level.objects.all():
if n == True:
return level
if level == self:
n = True
return None
def all_previous(self):
return Level.objects.filter(id__lte=self.pk)
@models.permalink
def get_absolute_url(self):
return ('level', [self.hash])
def save(self, *args, **kwargs):
if not self.pk:
self.hash = hashlib.sha1(self.content).hexdigest()[:10]
super(Level, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
def best_result(self):
try:
return Result.objects.filter(level=self).order_by('program_length')[0]
except IndexError:
return None
class Result(models.Model):
level = models.ForeignKey('Level')
program = models.TextField()
program_length = models.IntegerField()
username = models.CharField(max_length=16, blank=True)
email = models.EmailField(blank=True)
commited = models.DateTimeField(auto_now_add=True)
def gravatar(self):
if self.email:
import hashlib
return hashlib.md5(self.email).hexdigest()
return ""
def __unicode__(self):
return "%s:%s" % (self.level.name, self.program_length)