比这篇新的文章: 发芽网Roadmap
比这篇旧的文章: php simplexml文档对象转换为标准数组

游戏发芽网的随机关卡链接

语言: Python, 标签: 发芽网 2009/04/23发布 3年前更新 更新记录
作者: 半瓶墨水, 点击1376次, 评论(0), 收藏者(0), , 打分:

背景
主题: 字体:
01 #in youxi.urls.py
02 #...
03     #random gate
04     (r'^(?P<gameabbr>[a-z]+)/rand/$',
05         'fayaa.youxi.views.index.random_gate'),
06 #...
07
08 #in youxi.views.index.py
09 def random_gate(request, gameabbr):
10     game = Game.objects.get(abbr=gameabbr)
11     import sys
12     gatename = game.abbr[0].upper() + game.abbr[1:] + "Gate"
13     mods = sys.modules['fayaa.youxi.models']
14     gates = getattr(mods, gatename).objects
15     gate_count = gates.count()
16     import random
17     chance = 10
18     while chance:
19         chance -= 1
20         #NOTICE: we will lose some record if some are empty
21         # because that time gate_count != max_id
22         gate_id = random.randint(0, gate_count - 1)
23         if gates.filter(id=gate_id).count():
24             return HttpResponseRedirect("/youxi/" + gameabbr + "/" + str(gate_id) + "/")
25     return HttpResponseRedirect(".")


所有评论,共0条:( 我也来说两句)


发表评论

注册登录后再发表评论