def render_JSON(self, request): request.setHeader("content-type", "application/json") game = IGame(self) player = request.getSession() return json.dumps({ 'room': self.name, 'game': game.__class__.__name__, 'data': game.render_JSON(request), 'you': player.name, 'your_score': player.score, })
def render(self, request): if request.args: self.gotArgs(request) IGame(self).gotArgs(request) if hasattr(request, 'wantsJSON'): return self.render_JSON(request) else: return self.render_HTML(request)
def render_HTML(self, request): if request.args: head = '<head><meta http-equiv="refresh" content="1; url=/play/%s" ></head>' % self.name else: head = '<head><meta http-equiv="refresh" content="1" ></head>' game = IGame(self) room = IGameRoom(self) player = request.getSession() text = ["Hello %s, welcome to %s." % (player.name, self.name)] text.append('You can <a href="/">go back</a> or play a game.<br>') text.append('You have %s points.' % player.score) text.append('People in this room: %s' % [p.name for p in room.players]) text.append("This game is called %r." % game.title) text.append("Try it yourself, it's fun!") text.append('') text.append("<pre>%s</pre>" % game.render_HTML(request)) body = "<body>%s</body>" % "<br>".join(text) return "<html>%s%s</html>" % (head, body)
def render_HTML(self, request): if request.args: head = '<head><meta http-equiv="refresh" content="1; url=/play/%s" ></head>' % self.name else: head = '<head><meta http-equiv="refresh" content="1" ></head>' game = IGame(self) room = IGameRoom(self) player = request.getSession() text = [ "Hello %s, welcome to %s." % (player.name, self.name) ] text.append('You can <a href="/">go back</a> or play a game.<br>') text.append('You have %s points.' % player.score) text.append('People in this room: %s' % [p.name for p in room.players]) text.append("This game is called %r." % game.title) text.append("Try it yourself, it's fun!") text.append('') text.append("<pre>%s</pre>" % game.render_HTML(request)) body = "<body>%s</body>" % "<br>".join(text) return "<html>%s%s</html>" % (head, body)
def update(self, data): name = data['game'] if not name == self.ongoing: self.switchGame(name) game = IGame(self) game.update(data) game.handle()
def link(tup): name, room = tup title = IGame(room).title names = [p.name for p in room.players] return '<a href="/play/%s">%s: %s</a> - %d player(s): %s' % ( name, name, title, len(names), ", ".join(names))
def stopService(self): log.msg("Room close: %s" % self.name) IGame(self).stopService() return Service.stopService(self)
def startService(self): log.msg("Room open: %s" % self.name) IGame(self).startService() Service.startService(self)
def chainload(self, game): room = self.original room.setAdapter(IGame, game) IGame(room).startService() self.stopService()