Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.48 KB | None | 0 0
  1. from google.appengine.ext import webapp
  2. from google.appengine.ext.webapp.util import run_wsgi_app
  3.  
  4. from datetime import datetime, date, time
  5. class gdata:
  6.     firstRun=True
  7.     N,H,W=0,10,10       #number of running games,H,W
  8.     games_nam,mapp=[],{}
  9.     p1,p2=[],[]     #//player1,player2 hosts/IPs
  10.     free=[]
  11.     g=[]        #new int[1000,50,50];
  12.     dC=[0,0,-1,1]
  13.     dR=[-1,1,0,0]
  14.    
  15.     cell_owner=[]   #new string[1000,50,50]; //cell owner ;P1 or P2 via IP
  16.     lastPlay=[]     #new string[1000];
  17.     exp=[]      #ExpiresDate datetime[1000]
  18.     playAgain=[]        #false;
  19.     vis=[]      #[1000,H,W]
  20.  
  21.  
  22. class games(webapp.RequestHandler):
  23.     d=gdata
  24.     def clr(self):
  25.         self.d.N=0
  26.         for i in range(1000):
  27.             self.d.exp.append(datetime.now())   #It's expired since it was born :)
  28.             self.d.games_nam.append('')
  29.             self.d.free.append(False)
  30.             self.d.p1.append('')
  31.             self.d.p2.append('')
  32.             self.d.lastPlay.append('')
  33.             self.d.playAgain.append(False)
  34.             self.d.cell_owner.append([])
  35.             self.d.g.append([])
  36.             self.d.vis.append([])
  37.             for j in range(self.d.H):
  38.                 self.d.cell_owner[i].append([])
  39.                 self.d.g[i].append([])
  40.                 self.d.vis[i].append([])
  41.                 for k in range(self.d.W):
  42.                     self.d.cell_owner[i][j].append('0.0.0.0')
  43.                     self.d.g[i][j].append(0) #Open cells
  44.                     self.d.vis[i][j].append(False)
  45.                     self.d.g[i][j].append(0) #Open cells
  46.                     self.d.vis[i][j].append(False)
  47.         self.d.firstRun=False
  48.  
  49.     def homee(self):
  50.         cnt=0
  51.         other=''
  52.         for i in range(self.d.N):
  53.             if self.d.free[i]:
  54.                 cnt+=1
  55.         txt="<html><head><title>Available Games:</title></head><body><p aligen='center'>Available Games("+str(cnt)+"):<br><table align='center'>"
  56.         for i in range(self.d.N):
  57.             if self.d.free[i]:
  58.                 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>"
  59.             else:
  60.                 other+=',<a href="/?nam='+self.d.games_nam[i]+'">'+self.d.games_nam[i]+'</a>'
  61.         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>"
  62.         return txt
  63.  
  64.     def floodFill(self,gamid,sx,sy):
  65.         for i in range(self.d.H):
  66.             for j in range(self.d.W):
  67.                 self.d.vis[gamid][i][j]=False
  68.         q=[sy,sx]
  69.         while len(q)>0:
  70.             x=q.pop()
  71.             y=q.pop()
  72.             self.d.vis[gamid][x][y]=True
  73.             for i in range(4):
  74.                 if (self.d.g[gamid][x][y]&(1<<i))==0:
  75.                     nx=x+self.d.dR[i]
  76.                     ny=y+self.d.dC[i]
  77.                     if 0<nx and nx<self.d.H and 0<ny and ny<self.d.W:
  78.                         if not self.d.vis[gamid][nx][ny]:
  79.                             q.append(ny)
  80.                             q.append(nx)
  81.                     else:   return False
  82.         return True
  83.     def set_owner(self,gamid,ip,sx,sy):
  84.         for i in range(self.d.H):
  85.             for j in range(self.d.W):
  86.                 self.d.vis[gamid][i][j]=False
  87.         q=[sy,sx]
  88.         while len(q)>0:
  89.             x=q.pop()
  90.             y=q.pop()
  91.             if x<0 or y<0 or x>=self.d.H or y>=self.d.W:
  92.                 return False
  93.             self.d.vis[gamid][x][y]=True
  94.             self.d.cell_owner[gamid][x][y]=ip
  95.             for i in range(4):
  96.                 if (self.d.g[gamid][x][y]&(1<<i))==0:
  97.                     nx=x+self.d.dR[i]
  98.                     ny=y+self.d.dC[i]
  99.                     if 0<nx and nx<self.d.H and 0<ny and ny<self.d.W and not self.d.vis[gamid][nx][ny]:
  100.                         q.append(ny)
  101.                         q.append(nx)
  102.         return True
  103.  
  104.  
  105.  
  106.  
  107.     def play(self,nam,host,x,y,v):
  108.         if not self.d.mapp.has_key(nam): #set a new game?!
  109.             return homee()
  110.         #check this host was last player or not!..HERE<
  111.         ind=self.d.mapp[nam]
  112.         self.d.exp[ind]=datetime.now()
  113.         if self.d.exp[ind].hour+1<24:
  114.             self.d.exp[ind]=self.d.exp[ind].replace(hour=self.d.exp[ind].hour+1)
  115.         else:
  116.             self.d.exp[ind]=self.d.exp[ind].replace(hour=1,day=self.d.exp[ind].day+1)
  117.         if self.d.lastPlay[ind]==host and not self.d.playAgain[ind]:
  118.             return "<html><head><title>re-directing...</title></head><body><script>alert('Plz w8 4 other Player!');location.href='?nam="+nam+"';</script></body></html>"
  119.         if self.d.playAgain[ind]:
  120.             self.d.playAgain[ind]=False
  121.         #Console.WriteLine("\tPLAY \t"+host+"\t"+x+","+y+","+(v==8 ? "L":"U") );
  122.         #handle other near cells opening
  123.         self.d.g[ind][x][y] |=v
  124.        
  125.         if (v&(1<<0))>0 and x-1>=0:
  126.             self.d.g[ind][x-1][y] |=(1<<1) #upper side is closed
  127.        
  128.         if (v&(1<<2))>0 and y-1>=0: #left
  129.             self.d.g[ind][x][y-1] |=(1<<3) #leftside is closed
  130.        
  131.         if (v&(1<<3))>0 and y+1<self.d.W: #right
  132.             self.d.g[ind][x][y+1] |=(1<<2) #rightside is closed
  133.        
  134.        
  135.         if (v&(1<<1))>0 and x+1<self.d.H: #down
  136.             self.d.g[ind][x+1][y] |=(1<<0) #bottom is closed
  137.  
  138.         # IFF it doesn't exceed the boundaries,then this region exists :)
  139.         if self.floodFill(ind,x,y):
  140.                 self.d.playAgain[ind]=True
  141.                 #Make him play 1 more time...HERE<
  142.                 self.set_owner(ind,host,x,y)
  143.        
  144.         for k in range(4):         
  145.             # IFF it doesn't exceed the boundaries,then this new region exists :)
  146.             if self.floodFill(ind,x+self.d.dR[k],y+self.d.dC[k]):
  147.                 self.d.playAgain[ind]=True
  148.                 #Make him play 1 more time...HERE<
  149.                 self.set_owner(ind,host,x+self.d.dR[k],y+self.d.dC[k])
  150.  
  151.         self.d.lastPlay[ind]=host
  152.         return self.showgame(nam,host)
  153.  
  154.     def showgame(self,nam,host):
  155.         if nam=="":
  156.             return homee()
  157.         res=""
  158.        
  159.         if not self.d.mapp.has_key(nam): #New Game ya3ny & this is player1 :)
  160.             if self.d.N+1 >=1000:
  161.                 t=datetime.now()
  162.                 for i in range(self.d.N):
  163.                     if self.d.exp[i]<t:
  164.                         self.d.games_nam=nam
  165.                         self.d.mapp[nam]=i
  166.                         self.d.free[i]=True
  167.                         self.d.p1[i]=host
  168.                         self.d.p2[i]=''
  169.                         for j in range(self.d.H):
  170.                             for k in range(self.d.W):
  171.                                 self.d.cell_owner[i][j][k]='0.0.0.0'
  172.                                 self.d.g[i][j][k]=0 #Open cells
  173.                         break
  174.                 #return "<html><head><title>'?'!</title></head><body><script>alert('sorry,memory limit! :-(');</body></script></html>"
  175.             else:
  176.                 self.d.mapp[nam]=self.d.N   #let N the index of the new game
  177.                 self.d.free[self.d.N]=True
  178.                 self.d.games_nam[self.d.N]=nam
  179.                 self.d.N+=1
  180.                 if self.d.p1[self.d.mapp[nam]]=='':
  181. #               sw.WriteLine("New Player1:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
  182. #               Console.WriteLine("New Player1:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
  183.                     self.d.p1[self.d.mapp[nam]]=host
  184.         else: #2nd player & the game is full!
  185.             if not self.d.p1[self.d.mapp[nam]]==host and self.d.p2[self.d.mapp[nam]]=='':
  186. #               Console.WriteLine(">>>>>>>PLAYER 2<<<<<<<<<<=>"+host);
  187.                 self.d.free[self.d.mapp[nam]]=False
  188. #               sw.WriteLine("New Player2:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
  189. #               Console.WriteLine("New Player2:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
  190.                 self.d.p2[self.d.mapp[nam]]=host
  191.     #   if(p1.ContainsKey(host)) color="Blue";
  192. #       else color="red";
  193. #          V= 0001 = up=1<<0
  194. #         _________________
  195. #1<<2=left|<0100           |
  196. #        |           >1000| =right=1<<3
  197. #        |________________|
  198. #              ^ 0010=down=1<<1
  199.  
  200.         ind=self.d.mapp[nam]
  201.         r1=0
  202.         r2=0
  203.         color=""
  204.         res="<html><head><title>"+self.d.games_nam[ind]+"</title></head><body><table>";
  205.         for i in range(self.d.H):
  206.             res+="<tr>"
  207.             for j in range(self.d.W):
  208.                 if self.d.p1[ind]==self.d.cell_owner[ind][i][j]:
  209.                     color="blue"
  210.                     r1+=1
  211.                 elif self.d.p2[ind]==self.d.cell_owner[ind][i][j]:
  212.                     color="red"
  213.                     r2+=1
  214.                 else: color="white"
  215.                 res+="<td><table>"
  216.                 if (self.d.g[ind][i][j]&(1<<0)) >0: #up
  217.                     res+="<tr><td bgcolor='"+color+"' width='70%'>====</td><td bgcolor='"+color+"' width='30%'>O</td></tr>"
  218.                 else:
  219.                     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>"
  220.                
  221.                 if (self.d.g[ind][i][j]&(1<<3)) >0: #right
  222.                     res+="<tr><td bgcolor='"+color+"' width='70\%'></td><td bgcolor='"+color+"' width='30\%'>||</td></tr>";
  223.                 else:
  224.                     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>"
  225.                 res+="</table></td>"
  226.             res+="</tr>"
  227.         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>"
  228.        
  229.         return res
  230.  
  231.  
  232.     def get(self):
  233.         if self.d.firstRun:
  234.             self.clr()
  235.             firstRun=False
  236.         host=self.request.remote_addr
  237.         nam=self.request.get("nam")
  238.         try:
  239.             x=int(self.request.get("r"))
  240.             y=int(self.request.get("c"))
  241.             v=int(self.request.get("v"))
  242.         except:
  243.             x=y=v=''
  244.         if x=='' or y=='' or v=='':
  245.             if nam=='':
  246.                 self.response.out.write(self.homee())
  247.             else:
  248.                 self.response.out.write(self.showgame(nam,host))
  249.         else:
  250.             self.response.out.write(self.play(nam,host,x,y,v))
  251.  
  252. application = webapp.WSGIApplication(
  253.                     [('/', games)],
  254.                                      debug=True)
  255. def main():
  256.     run_wsgi_app(application)
  257.  
  258. if __name__ == "__main__":
  259.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement