├── app.yaml ├── index.yaml └── main.py /app.yaml: -------------------------------------------------------------------------------- 1 | application: flixel-lobby 2 | version: 1 3 | runtime: python 4 | api_version: 1 5 | 6 | handlers: 7 | - url: .* 8 | script: main.py 9 | -------------------------------------------------------------------------------- /index.yaml: -------------------------------------------------------------------------------- 1 | indexes: 2 | 3 | # AUTOGENERATED 4 | 5 | # This index.yaml is automatically updated whenever the dev_appserver 6 | # detects that a new type of query is run. If you want to manage the 7 | # index.yaml file manually, remove the above marker line (the line 8 | # saying "# AUTOGENERATED"). If you want to manage some indexes 9 | # manually, move them above the marker line. The index.yaml file is 10 | # automatically uploaded to the admin console when you next deploy 11 | # your application using appcfg.py. 12 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # Copyright 2007 Google Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | from google.appengine.ext import webapp 18 | from google.appengine.ext.webapp import util 19 | from google.appengine.api import memcache 20 | from google.appengine.ext import db 21 | import time 22 | 23 | class Game(db.Model): 24 | host = db.StringProperty() 25 | size = db.IntegerProperty() 26 | name = db.StringProperty() 27 | players = db.StringListProperty(str) 28 | created = db.DateTimeProperty(auto_now_add=True) 29 | 30 | def remaining(self): 31 | return self.size - len(self.players) 32 | 33 | def __str__(self): 34 | return ','.join(['-1', self.host, self.name, str(self.remaining())]) 35 | 36 | def join(self, id): 37 | self.players.append(id) 38 | self.put() 39 | if len(self.players) >= self.size: 40 | self.delete() 41 | return ','.join([str(self.players.index(id)), self.host, self.name or '', str(self.remaining())]) 42 | 43 | class JoinHandler(webapp.RequestHandler): 44 | def post(self): 45 | players = self.request.get('players', '2') 46 | game = self.request.get('game', None) 47 | id = self.request.get('id') 48 | 49 | q = Game.all().filter('size =', int(players)) 50 | if game: 51 | q.filter('name =', game) 52 | game = q.get() 53 | if game: 54 | self.response.out.write(game.join(id)) 55 | else: 56 | self.error(404) 57 | #self.response.out.write('') 58 | 59 | class HostHandler(webapp.RequestHandler): 60 | def post(self): 61 | players = self.request.get('players', '2') 62 | id = self.request.get('id') 63 | gid = self.request.get('game', hex(int(time.time()*1000))[2:]) 64 | 65 | game = Game(host=id, size=int(players), name=gid) 66 | game.put() 67 | self.response.out.write(str(game)) 68 | 69 | 70 | 71 | def main(): 72 | application = webapp.WSGIApplication([ 73 | ('/host', HostHandler), 74 | ('/join', JoinHandler), ], debug=True) 75 | util.run_wsgi_app(application) 76 | 77 | 78 | if __name__ == '__main__': 79 | main() 80 | --------------------------------------------------------------------------------