成果物‎ > ‎その他‎ > ‎

ランキング情報共有Webサービスrank-ws

ランキング情報共有Webサービスrank-wsとは

今後、勉強会参加者がゲーム等のアプリを開発した際に、簡単にランキング情報を記録共有できるようにするための簡易Webサービスです。

rank-ws v0.1 Webサービス仕様

共通

リクエスト
  •  書式
  • パラメータ

ゲームの作成

リクエスト

レスポンス

  • 正常時

    {"data": {"owner": "3t14", "game": "spa2"}, "result": true, "error_msg": null}

  • エラー
    • パラメータが不足する場合

      {"data": {"owner": "hoge", "game": ""}, "result": false, "error_msg": "Wrong parameter(s): name: owner:hoge"}

    • 既に指定した名称が登録されている場合

      {"data": {"owner": "hoge", "game": "hoge"}, "result": false, "error_msg": "Already exists"}

スコア情報の更新

書式

出力結果


ランキング情報の取得

書式

出力結果

JSON形式でランキング情報が出力される(予定)


第2回勉強会で開発したGoogle AppEngine用プログラム

#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db

class Game(db.Model):
    name = db.StringProperty(required=True)
    owner = db.StringProperty(required=True)

class Ranking(db.Model):
    game = db.StringProperty(required=True)
    score = db.IntegerProperty()
    username = db.StringProperty(required=True)
    data1 = db.StringProperty()
    data2 = db.StringProperty()
    data3 = db.StringProperty()

class CreateGameHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('Create Game = ' + self.request.get('name') +' <br />')
        name = self.request.get('name')
        owner = self.request.get('owner')

        if name == '' or owner == '':
            self.response.out.write('ERROR')
            return
        query = Game.all()
        query.filter('name =', name)

        result = query.get()

        if result == None:
            self.response.out.write('result == None <br />')
            game = Game(name=name,
                    owner=owner)
            game.put()
        else:
            self.response.out.write('result = ' + result.name+' ' + result.owner)
        query = Game.all()
        for game in query.fetch(20):
            self.response.out.write(game.name + ' :  '+game.owner+ '<br />')
        self.response.out.write('fin')

class UpdateScoreHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('Update Score username = ' + self.request.get('username') +' <br />')
        game = self.request.get('game')
        username = self.request.get('username')
        score = self.request.get('score')

        if game == '' or username == '':
            self.response.out.write('No game or user ERROR')
            return
        query = Ranking.all()
        query.filter('game =', game)
        query.filter('username =', username)

        result = query.get()

        if result == None:
            ''' Newly create '''
            self.response.out.write('result == None <br />')
            ranking = Ranking(game=game,
                    username=username,
                    score=int(score))
            ranking.put()
            result = ranking;
        else:
            result.score = int(score)
            result.put()

        self.response.out.write('fin')


class GetRankingHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('Get Ranking<br />')

        query = Ranking.all()

        query.order("-score")

        rank = 1
        for ranking in query.fetch(20):
            self.response.out.write("Rank " + str(rank) + ' :  '+ranking.username+ ': score = ' + str(ranking.score)+'<br />')
            rank = rank + 1

class MainHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('Rank Web Service')


def main():
    application = webapp.WSGIApplication([('/', MainHandler),
                                ('/create-game', CreateGameHandler),
                                ('/update-score', UpdateScoreHandler),
                                ('/get-ranking', GetRankingHandler)], debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()

Comments