comparison visualclient/visclient.py @ 14:3bc0521eff28

some server updates
author gyorgyf
date Fri, 22 Jun 2012 13:41:40 +0100
parents 9d9169751aba
children 92903c908539
comparison
equal deleted inserted replaced
13:7c00be58d9cf 14:3bc0521eff28
28 scol = (0,255,0,255) 28 scol = (0,255,0,255)
29 ecol = (0,0,0,255) 29 ecol = (0,0,0,255)
30 30
31 # X,Y=1140,900 31 # X,Y=1140,900
32 X,Y = 600,400 32 X,Y = 600,400
33 BLOBSIZE = 20
34 G=110
33 35
34 class Blob(object): 36 class Blob(object):
35 37
36 def __init__(self,bg,x,y,color=(255,255,255),mood=None): 38 def __init__(self,bg,x,y,color=(255,255,255),mood=None):
37 self.x = x 39 self.x = x
38 self.y = y 40 self.y = y
39 self.xs = x * X 41 self.xs = x * X
40 self.ys = Y - (y * Y) 42 self.ys = Y - (y * Y)
41 self.bg = bg 43 self.bg = bg
42 self.size = 20 44 self.size = BLOBSIZE
43 self.time = time.time() 45 self.time = time.time()
44 self.alpha = 255 46 self.alpha = 255
45 self.c = color 47 self.c = color
48 self.count = 1
46 self.visible = True 49 self.visible = True
47 if mood and mood.color : 50 if mood and mood.color :
48 self.c = mood.color 51 self.c = mood.color
49 52
50 def __cmp__(self,other): 53 def __cmp__(self,other):
53 return 0 56 return 0
54 else : 57 else :
55 return -1 58 return -1
56 59
57 def draw(self): 60 def draw(self):
58 self.bg.blit(gradients.radial(self.size, (self.c[0],self.c[1],self.c[2],self.alpha), (0,0,0,self.alpha)), (self.xs,self.ys)) 61 d=int(self.size)
62 self.bg.blit(gradients.radial(self.size, (self.c[0],self.c[1],self.c[2],self.alpha), (0,0,0,self.alpha)), (self.xs-d,self.ys-d))
59 self.alpha = 255 - int(self.age()*15) 63 self.alpha = 255 - int(self.age()*15)
60 if self.alpha < 5 : 64 if self.alpha < 5 :
61 self.alpha = 1 65 self.alpha = 1
62 self.visible = False 66 self.visible = False
63 67
64 def age(self): 68 def age(self):
65 return time.time() - self.time 69 return time.time() - self.time
66 70
67 def increment(self,count): 71 def increment(self,count):
68 self.size = int(self.size * int(count/1.5)) 72 self.count = count
73 self.size = int(BLOBSIZE * int(self.count/1.5))
69 74
70 def get_distance(self,x,y): 75 def get_distance(self,x,y):
71 return math.sqrt( math.pow((self.x-x),2) + math.pow((self.y-y),2) ) 76 return math.sqrt( math.pow((self.x-x),2) + math.pow((self.y-y),2) )
72 77
73 78
121 126
122 with open('moods.csv') as mf: 127 with open('moods.csv') as mf:
123 data = mf.readlines()[1:] 128 data = mf.readlines()[1:]
124 for line in data : 129 for line in data :
125 l = line.split(',') 130 l = line.split(',')
126 mood = Mood(l[1],l[3],l[5]) 131 mood = Mood(l[0],l[1],l[2])
127 self.moods.append(mood) 132 self.moods.append(mood)
128 with open('feelings.txt') as ff: 133 with open('feelings.txt') as ff:
129 data = ff.readlines() 134 data = ff.readlines()
130 data = map(lambda x: x.split('\t'),data) 135 data = map(lambda x: x.split('\t'),data)
131 for mood in self.moods : 136 for mood in self.moods :
151 self.conn.putrequest("GET","/moodconductor/result", skip_host=True) 156 self.conn.putrequest("GET","/moodconductor/result", skip_host=True)
152 self.conn.putheader("Host", "www.isophonics.net") 157 self.conn.putheader("Host", "www.isophonics.net")
153 self.conn.endheaders() 158 self.conn.endheaders()
154 res = self.conn.getresponse() 159 res = self.conn.getresponse()
155 data = res.read() 160 data = res.read()
156 print data
157 data = eval(data) 161 data = eval(data)
158 if not data : 162 if not data :
159 self.conn.close() 163 self.conn.close()
160 return False 164 return False
161 for d in data : 165 for d in data :
162 print d 166 # coordstxt = "x:%s y:%s c:%s" %d
163 coordstxt = "x:%s y:%s c:%s" %d 167 x,y,c = d
164 x = d[0]
165 y = d[1]
166 c = d[2]
167 168
168 cmood = None 169 cmood = None
169 d = cd = sys.float_info.max 170 d = cd = sys.float_info.max
170 for mood in self.moods : 171 for mood in self.moods :
171 d = mood.get_distance(x,y) 172 d = mood.get_distance(x,y)
172 if d < cd : 173 if d < cd :
173 cd = d 174 cd = d
174 cmood = mood 175 cmood = mood
175 176
176 new = Blob(self.bg,x,y,mood=cmood) 177 new = Blob(self.bg,x,y,mood=cmood)
177 if not new in self.blobs : 178 if not new in self.blobs :
178 self.blobs.insert(0,new) 179 self.blobs.insert(0,new)
179 elif c > 2: 180 elif c > self.blobs[self.blobs.index(new)].count:
180 self.blobs[self.blobs.index(new)].increment(c) 181 self.blobs[self.blobs.index(new)].increment(c)
181 182
182 self.conn.close() 183 self.conn.close()
183 self.blobs = self.blobs[:5] 184 self.blobs = self.blobs[:5]
184 return True 185 return True
185 186
186 187
187 def draw(self): 188 def draw(self):
188 # bg.blit(gradients.radial(19, ecol, ecol), (rect_xp,rect_yp))
189 # rect_xp,rect_yp = rect_x,rect_y
190 self.bg.fill((0,0,0)) 189 self.bg.fill((0,0,0))
191 # self.bg.blit(gradients.radial(19, self.scol, self.ecol), (rect_x,rect_y)) 190 # self.bg.blit(gradients.radial(19, self.scol, self.ecol), (rect_x,rect_y))
192 l = copy.copy(self.blobs) 191 l = copy.copy(self.blobs)
193 l.reverse() 192 l.reverse()
194 for blob in l : 193 for blob in l :
195 blob.draw() 194 blob.draw()
196 195
196 # axis
197 pg.draw.line(self.bg, (G,G,G), (int(X/2.0),0),(int(X/2.0),Y), 1)
198 pg.draw.line(self.bg, (G,G,G), (0,int(Y/2.0)),(X,int(Y/2.0)), 1)
199
200
197 201
198 def run(self): 202 def run(self):
199 # conn = ht.HTTPConnection("192.168.2.184:8030") 203 # conn = ht.HTTPConnection("192.168.2.184:8030")
200 self.conn = ht.HTTPConnection("138.37.95.215") 204 self.conn = ht.HTTPConnection("138.37.95.215")
201 205
207 # pg.draw.circle(screen, pg.Color(255,0,0), (300,50),20,0) 211 # pg.draw.circle(screen, pg.Color(255,0,0), (300,50),20,0)
208 # screen.blit(gradients.radial(99, scol, ecol), (401, 1)) 212 # screen.blit(gradients.radial(99, scol, ecol), (401, 1))
209 213
210 for event in pg.event.get() : 214 for event in pg.event.get() :
211 if event.type == QUIT: 215 if event.type == QUIT:
212 conn.close() 216 self.conn.close()
213 pg.quit() 217 pg.quit()
214 sys.exit() 218 sys.exit()
215 elif event.type == KEYDOWN : 219 elif event.type == KEYDOWN :
216 if event.key == K_ESCAPE : 220 if event.key == K_ESCAPE :
217 pg.event.post(pg.event.Event(QUIT)) 221 pg.event.post(pg.event.Event(QUIT))
223 227
224 # update from the server 228 # update from the server
225 counter += 1 229 counter += 1
226 if counter % 12 : 230 if counter % 12 :
227 counter = 0 231 counter = 0
228 self.update() 232 try :
229 233 self.update()
230 # try : 234 except:
231 # rect_x,rect_y = self.update() 235 pass
232 # except :
233 # pass
234
235 # update display 236 # update display
236 self.screen.blit(self.bg, (0, 0)) 237 self.screen.blit(self.bg, (0, 0))
237 pg.display.flip() 238 pg.display.flip()
238 self.fpsClock.tick(50) 239 self.fpsClock.tick(50)
239 pass 240 pass