Beispiel #1
0
    def post(self):
        # TODO(user): Secure this URL by using Cloud Endpoints.
        grid = self.request.get('grid')
        logging.info('Received server require for grid:' + grid)

        loadresp = LoadInfo.GetServerLoadInfo(grid)
        if not loadresp:
            logging.info('No server so starting server for grid:' + str(grid))
            ComputeEngineController().AddServer(grid)
            loadresp = {LoadInfo.STATUS: LoadInfo.STATUS_LOADING}
        self.response.out.write(json.dumps(loadresp))
Beispiel #2
0
    def get(self):
        user = users.get_current_user()
        # TODO: Check to make sure game account is up to date/paid for/whatever.

        # Determine what server to transfer player to
        servertoload = ""
        q = Entity.all()
        q.filter("user_id =", user.user_id())
        q.filter("__type =", "Player")
        userplayers = []
        for userplayer in q.run(limit=5):
            userplayers.append(userplayer)
        if len(userplayers) > 1:
            logging.error("UserID has more than one player")
            return
        elif len(userplayers) == 0:
            logging.info("No current players, attaching to root")
            servertoload = "0,0"
        else:
            servertoload = userplayers[0].gridkey

        logging.info("Loading server " + servertoload + " for " +
                     user.nickname())

        info = LoadInfo.GetServerLoadInfo(servertoload)
        if info:
            if info[LoadInfo.STATUS] == LoadInfo.STATUS_UP:
                ip = info[LoadInfo.IP_ADDRESS]
                port = info[LoadInfo.PORT]

                # Generate login token
                token_key = uuid.uuid4().hex
                expiration_time = datetime.datetime.now() + datetime.timedelta(
                    seconds=20)
                LoginToken(key_name=token_key,
                           expiration=expiration_time,
                           user_id=user.user_id()).put()

                # Send IP and token info
                self.response.out.write(
                    json.dumps({
                        'status': 'up',
                        'ipaddress': ip,
                        'port': port,
                        'token': token_key
                    }))
                IpAddressRequestLog(client_ip=self.request.remote_addr,
                                    server_ip=ip).put()
            else:
                self.response.out.write(json.dumps({'status': 'loading'}))
        else:
            ComputeEngineController().AddServer(servertoload)
            self.response.out.write(json.dumps({'status': 'loading'}))