Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from google.appengine.ext import webapp
- from google.appengine.ext.webapp.util import run_wsgi_app
- from datetime import datetime, date, time
- class gdata:
- firstRun=True
- N,H,W=0,10,10 #number of running games,H,W
- games_nam,mapp=[],{}
- p1,p2=[],[] #//player1,player2 hosts/IPs
- free=[]
- g=[] #new int[1000,50,50];
- dC=[0,0,-1,1]
- dR=[-1,1,0,0]
- cell_owner=[] #new string[1000,50,50]; //cell owner ;P1 or P2 via IP
- lastPlay=[] #new string[1000];
- exp=[] #ExpiresDate datetime[1000]
- playAgain=[] #false;
- vis=[] #[1000,H,W]
- class games(webapp.RequestHandler):
- d=gdata
- def clr(self):
- self.d.N=0
- for i in range(1000):
- self.d.exp.append(datetime.now()) #It's expired since it was born :)
- self.d.games_nam.append('')
- self.d.free.append(False)
- self.d.p1.append('')
- self.d.p2.append('')
- self.d.lastPlay.append('')
- self.d.playAgain.append(False)
- self.d.cell_owner.append([])
- self.d.g.append([])
- self.d.vis.append([])
- for j in range(self.d.H):
- self.d.cell_owner[i].append([])
- self.d.g[i].append([])
- self.d.vis[i].append([])
- for k in range(self.d.W):
- self.d.cell_owner[i][j].append('0.0.0.0')
- self.d.g[i][j].append(0) #Open cells
- self.d.vis[i][j].append(False)
- self.d.g[i][j].append(0) #Open cells
- self.d.vis[i][j].append(False)
- self.d.firstRun=False
- def homee(self):
- cnt=0
- other=''
- for i in range(self.d.N):
- if self.d.free[i]:
- cnt+=1
- txt="<html><head><title>Available Games:</title></head><body><p aligen='center'>Available Games("+str(cnt)+"):<br><table align='center'>"
- for i in range(self.d.N):
- if self.d.free[i]:
- txt+="<tr><td align='center'><a href='?nam="+self.d.games_nam[i]+"'><font size=40>"+self.d.games_nam[i]+"</font></a></td></tr>"
- else:
- other+=',<a href="/?nam='+self.d.games_nam[i]+'">'+self.d.games_nam[i]+'</a>'
- txt+="</table>"+"<br><a href='javascript:location.href=\"?nam=\"+prompt(\"Game Name:\");'>New Game!</a><br></p><br>Running("+str(self.d.N)+")"+other+"</body></html>"
- return txt
- def floodFill(self,gamid,sx,sy):
- for i in range(self.d.H):
- for j in range(self.d.W):
- self.d.vis[gamid][i][j]=False
- q=[sy,sx]
- while len(q)>0:
- x=q.pop()
- y=q.pop()
- self.d.vis[gamid][x][y]=True
- for i in range(4):
- if (self.d.g[gamid][x][y]&(1<<i))==0:
- nx=x+self.d.dR[i]
- ny=y+self.d.dC[i]
- if 0<nx and nx<self.d.H and 0<ny and ny<self.d.W:
- if not self.d.vis[gamid][nx][ny]:
- q.append(ny)
- q.append(nx)
- else: return False
- return True
- def set_owner(self,gamid,ip,sx,sy):
- for i in range(self.d.H):
- for j in range(self.d.W):
- self.d.vis[gamid][i][j]=False
- q=[sy,sx]
- while len(q)>0:
- x=q.pop()
- y=q.pop()
- if x<0 or y<0 or x>=self.d.H or y>=self.d.W:
- return False
- self.d.vis[gamid][x][y]=True
- self.d.cell_owner[gamid][x][y]=ip
- for i in range(4):
- if (self.d.g[gamid][x][y]&(1<<i))==0:
- nx=x+self.d.dR[i]
- ny=y+self.d.dC[i]
- if 0<nx and nx<self.d.H and 0<ny and ny<self.d.W and not self.d.vis[gamid][nx][ny]:
- q.append(ny)
- q.append(nx)
- return True
- def play(self,nam,host,x,y,v):
- if not self.d.mapp.has_key(nam): #set a new game?!
- return homee()
- #check this host was last player or not!..HERE<
- ind=self.d.mapp[nam]
- self.d.exp[ind]=datetime.now()
- if self.d.exp[ind].hour+1<24:
- self.d.exp[ind]=self.d.exp[ind].replace(hour=self.d.exp[ind].hour+1)
- else:
- self.d.exp[ind]=self.d.exp[ind].replace(hour=1,day=self.d.exp[ind].day+1)
- if self.d.lastPlay[ind]==host and not self.d.playAgain[ind]:
- return "<html><head><title>re-directing...</title></head><body><script>alert('Plz w8 4 other Player!');location.href='?nam="+nam+"';</script></body></html>"
- if self.d.playAgain[ind]:
- self.d.playAgain[ind]=False
- #Console.WriteLine("\tPLAY \t"+host+"\t"+x+","+y+","+(v==8 ? "L":"U") );
- #handle other near cells opening
- self.d.g[ind][x][y] |=v
- if (v&(1<<0))>0 and x-1>=0:
- self.d.g[ind][x-1][y] |=(1<<1) #upper side is closed
- if (v&(1<<2))>0 and y-1>=0: #left
- self.d.g[ind][x][y-1] |=(1<<3) #leftside is closed
- if (v&(1<<3))>0 and y+1<self.d.W: #right
- self.d.g[ind][x][y+1] |=(1<<2) #rightside is closed
- if (v&(1<<1))>0 and x+1<self.d.H: #down
- self.d.g[ind][x+1][y] |=(1<<0) #bottom is closed
- # IFF it doesn't exceed the boundaries,then this region exists :)
- if self.floodFill(ind,x,y):
- self.d.playAgain[ind]=True
- #Make him play 1 more time...HERE<
- self.set_owner(ind,host,x,y)
- for k in range(4):
- # IFF it doesn't exceed the boundaries,then this new region exists :)
- if self.floodFill(ind,x+self.d.dR[k],y+self.d.dC[k]):
- self.d.playAgain[ind]=True
- #Make him play 1 more time...HERE<
- self.set_owner(ind,host,x+self.d.dR[k],y+self.d.dC[k])
- self.d.lastPlay[ind]=host
- return self.showgame(nam,host)
- def showgame(self,nam,host):
- if nam=="":
- return homee()
- res=""
- if not self.d.mapp.has_key(nam): #New Game ya3ny & this is player1 :)
- if self.d.N+1 >=1000:
- t=datetime.now()
- for i in range(self.d.N):
- if self.d.exp[i]<t:
- self.d.games_nam=nam
- self.d.mapp[nam]=i
- self.d.free[i]=True
- self.d.p1[i]=host
- self.d.p2[i]=''
- for j in range(self.d.H):
- for k in range(self.d.W):
- self.d.cell_owner[i][j][k]='0.0.0.0'
- self.d.g[i][j][k]=0 #Open cells
- break
- #return "<html><head><title>'?'!</title></head><body><script>alert('sorry,memory limit! :-(');</body></script></html>"
- else:
- self.d.mapp[nam]=self.d.N #let N the index of the new game
- self.d.free[self.d.N]=True
- self.d.games_nam[self.d.N]=nam
- self.d.N+=1
- if self.d.p1[self.d.mapp[nam]]=='':
- # sw.WriteLine("New Player1:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
- # Console.WriteLine("New Player1:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
- self.d.p1[self.d.mapp[nam]]=host
- else: #2nd player & the game is full!
- if not self.d.p1[self.d.mapp[nam]]==host and self.d.p2[self.d.mapp[nam]]=='':
- # Console.WriteLine(">>>>>>>PLAYER 2<<<<<<<<<<=>"+host);
- self.d.free[self.d.mapp[nam]]=False
- # sw.WriteLine("New Player2:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
- # Console.WriteLine("New Player2:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
- self.d.p2[self.d.mapp[nam]]=host
- # if(p1.ContainsKey(host)) color="Blue";
- # else color="red";
- # V= 0001 = up=1<<0
- # _________________
- #1<<2=left|<0100 |
- # | >1000| =right=1<<3
- # |________________|
- # ^ 0010=down=1<<1
- ind=self.d.mapp[nam]
- r1=0
- r2=0
- color=""
- res="<html><head><title>"+self.d.games_nam[ind]+"</title></head><body><table>";
- for i in range(self.d.H):
- res+="<tr>"
- for j in range(self.d.W):
- if self.d.p1[ind]==self.d.cell_owner[ind][i][j]:
- color="blue"
- r1+=1
- elif self.d.p2[ind]==self.d.cell_owner[ind][i][j]:
- color="red"
- r2+=1
- else: color="white"
- res+="<td><table>"
- if (self.d.g[ind][i][j]&(1<<0)) >0: #up
- res+="<tr><td bgcolor='"+color+"' width='70%'>====</td><td bgcolor='"+color+"' width='30%'>O</td></tr>"
- else:
- res+="<tr><td bgcolor='"+color+"' width='70%'><a href='?nam="+self.d.games_nam[ind]+"&r="+str(i)+"&c="+str(j)+"&v="+str(1<<0)+"'>====</a></td><td bgcolor='"+color+"' width='30\%'>0</td></tr>"
- if (self.d.g[ind][i][j]&(1<<3)) >0: #right
- res+="<tr><td bgcolor='"+color+"' width='70\%'></td><td bgcolor='"+color+"' width='30\%'>||</td></tr>";
- else:
- res+="<tr><td bgcolor='"+color+"' width='70%'></td><td bgcolor='"+color+"' width='30%'><a href='?nam="+self.d.games_nam[ind]+"&r="+str(i)+"&c="+str(j)+"&v="+str((1<<3))+"'>||</a></td></tr>"
- res+="</table></td>"
- res+="</tr>"
- res+="</table><p aligen='center'>P1:"+str(r1)+"<br>P2:"+str(r2)+"<br>"+str(self.d.exp[ind]-datetime.now())+" to Expire</p><script>setTimeout('location.href=\"?nam="+self.d.games_nam[ind]+"\"; ',5000);</script></body></html>"
- return res
- def get(self):
- if self.d.firstRun:
- self.clr()
- firstRun=False
- host=self.request.remote_addr
- nam=self.request.get("nam")
- try:
- x=int(self.request.get("r"))
- y=int(self.request.get("c"))
- v=int(self.request.get("v"))
- except:
- x=y=v=''
- if x=='' or y=='' or v=='':
- if nam=='':
- self.response.out.write(self.homee())
- else:
- self.response.out.write(self.showgame(nam,host))
- else:
- self.response.out.write(self.play(nam,host,x,y,v))
- application = webapp.WSGIApplication(
- [('/', games)],
- debug=True)
- def main():
- run_wsgi_app(application)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement