转: 页游开发中的 Python 组件与模式Presentation Transcript

  • 1. 页游开发中的 Python 组件与模式 赖勇浩( http://laiyonghao.com ) 2012-10-21 上海
  • 2. 去年我来过……
  • 3. 回顾……• 幻灯: http://www.slideshare.net/laiyonghao/py thon-webgame-10452102• 录像(上海 45 分钟版): http://e.gensee.com/v_3df867_14• 录像(广州 91 分钟版): http://v.youku.com/v_playlist/f16785412 o1p4.html 偏向于“最佳实践”的经验分享
  • 4. 今天不一样……
  • 5. 直奔主题!class Player(object): def signin(self, usr, pwd): ... self._signin = True def do_sth(self): if not self._signin: self.client.need_signin() return ...
  • 6. 有什么问题?def do_sth_1(...def do_sth_2(...def do_sth_3(...def do_sth_4(...…
  • 7. 一般这样解决掉……@ensure_signindef do_sth(self, *a, **kw): … Decorator !!!
  • 8. 还有什么问题?def do_sth_1(... if notdef do_sth_2(... self._signin: ...def do_sth_3(... if not self._in_battle: ...def do_sth_4(...… if not self._is_dead: ... ...
  • 9. 还是这样解决掉?@ensure_signin@ensure_in_battle@ensuer_is_alivedef do_sth(self, *a, **kw): … ???
  • 10. 好像哪里不对……• 戴太多“帽子”不好看• method 的数量没有减少。 需要一点新思路!
  • 11. python-state@stateful class Signin(State):class Player(object): @behavior class def move(self, NeedSignin(State): dst): ... default = True @behavior @behavior def atk(self, def signin(self, other): ... usr, pwd): @behavior ... def … switch(self, Player.Signin)
  • 12. python-state@stateful class Signin(State):class Player(object): @behavior class def move(self, NeedSignin(State): dst): ... default = True @behavior @behavior def atk(self, other): ... def signin(self, usr, pwd): @behavior ... def … switch(self, Player.Signin)
  • 13. python-state@stateful class Signin(State):class Player(object): @behavior class def move(self, NeedSignin(State): dst): ... default = True @behavior @behavior def atk(self, x): def signin(self, ... usr, pwd): @behavior ... def … switch(self, Player.Signin)
  • 14. python-state@stateful class Signin(State):class Player(object): @behavior class def move(self, NeedSignin(State): dst): ... default = True @behavior @behavior def atk(self, def signin(self, other): ... usr, pwd): @behavior ... def … switch(self, Player.Signin)
  • 15. python-state@stateful class Signin(State):class Player(object): @behavior class def move(self, NeedSignin(State): dst): ... default = True @behavior @behavior def atk(self, def signin(self, other): ... usr, pwd): @behavior ... def … switch(self, Player.Signin)
  • 16. 适用场景• 根据状态授权特定的 RPC API 访问权限 – 例如未登陆不能调用攻击• 编写网络协议、文本的 parser• FSM-based Game AI ? – No ! 没有比协程更适合做这件事的机制了。
  • 17. 小结一下……• 跟 decorator 一样去掉了 if 语句 – 但是摘掉了许多帽子 – 而且真正地没有写 if 语句噢!
  • 18. 小结一下……• 跟 decorator 一样去掉了 if 语句 – 但是摘掉了许多帽子 – 而且真正地没有写 if 语句噢!• 把很多 method 分到多个 State 类中 – 重用 – 划分功能
  • 19. 更大的好处是——• 更容易修正错误…… – 因为出错信息是 AttributeError !
  • 20. 更容易修正错误……• 因为出错信息是 AttributeError ! http://www.slideshare.net/wilhelmshen/py-art 2010-5-30 shanghai
  • 21. 更容易修正错误……• 因为出错信息是 AttributeError !
  • 22. 好,继续!什么引起状态的切换(属性变化)?
  • 23. 有事情发生!怎么知道有事情发生?
  • 24. 上网、读报、看电视、听收音机…… 大海捞针
  • 25. 好莱坞原则Don’t call me, I’ll call you.
  • 26. 一个简单的状态切换场景
  • 27. 建个模吧!
  • 28. 建个模吧!class Lamp(object): is_on = Falseclass Swith(object): def turn(self): ...
  • 29. 建个模吧!class Lamp(object): class Line(object): is_on = False ...class Swith(object): line = Line() def turn(self): lamp = Lamp(line) swith = Swith(line) self.line.lamp.is_on line.connect(lamp, = True swith) swith.turn() assert lamp.is_on
  • 30. 好像感觉哪里不对……• 现实世界中需要真实的 Line ,编程中也 是吗?• 如果一个 Switch 对应着很多 Lamp ?• 循环引用? 隐形的 Line ?可扩展的 Line ?
  • 31. python-messageclass Switch(object): Turn = state.examples.Switch.Trun def turn(self): message.pub(Switch.Turn, self)
  • 32. python-message@stateful class Lamp(object):class Lamp(object): def bind(self, s): class Off(State): self._switch = s default = True @behavior message.sub(Switch.Turn, def _on_turn(self, s): self.on_turn) def on_turn(self, s): switch(self, Lamp.On) self._on_turn(s) class On(State): @behavior def _on_turn(self, s): switch(self, Lamp.Off)
  • 33. python-messages = Switch() <__main__.Lamp object atl = Lamp() 0x7f6b4dd2f590> begin Off state.l.bind(s)s.turn() <__main__.Lamp object at 0x7f6b4dd2f590> end Off state.s.turn() <__main__.Lamp object at 0x7f6b4dd2f590> begin On state. <__main__.Lamp object at 0x7f6b4dd2f590> end On state. <__main__.Lamp object at 0x7f6b4dd2f590> begin Off state.
  • 34. 解耦
  • 35. 应用场景• 任务 – 获得道具时…… – 怪物死亡时……• 世界状态 – 玩家(好友)上下线……• 网络编程 – 数据可读……• ……
  • 36. 更多功能
  • 37. 取消与中止import message import messagedef hello(name): def hello(name): print hello, %s.%name print hello %s % name message.unsub(greet, ctx = message.Context() hello) ctx.discontinued = Truemessage.sub(greet, hello) return ctxmessage.pub(greet, lai) def hi(name):message.pub(greet, u print u cannt c me. cannt c me.) message.sub(greet, hello) message.sub(greet, hi) message.pub(greet, lai)
  • 38. 进阶• 改变调用次序 – 在调用 sub 的时候加上 front = True – sub(greet, hello, front = True)• 订阅过去的消息 – declare/retract – declare(topic, *a, **kw) 用来向“公告栏”发布一 个消息 – 把“公告栏”的消息撤消用 retract(topic) 函数 – get_declarations()/has_declaration(topic)
  • 39. 退化——观察者模式from message import observabledef greet(people): print hello, %s.%people.name@observableclass Foo(object): def __init__(self, name): self.name = name self.sub(greet, greet)foo = Foo(lai)foo.pub(greet, foo)
  • 40. 现在 玩家有了状态也能知晓世事变幻那玩家们如何交互?
  • 41. 网络通信 需要有个 rpc基于 google protobuf 实现一套就不错如果有 greenlet 实现同步编程就更好了
  • 42. abu.rpcclass EchoService(echo.EchoService): @abu.rpc.ret def Echo(self, controller, request): resp = echo.Packet(text = request.text) return respservice = EchoService()handler = abu.rpc.Handler(abu.rpc.Transport, service)server = gevent.server.StreamServer((, 10086), handler)app = abu.rpc.Application(server)print serving...app.run()
  • 43. abu.rpcconn = gevent.socket.create_connection((127.0.0.1, 10086))channel = abu.rpc.Channel(abu.rpc.Transport(conn))server = abu.rpc.Proxy(echo.EchoService_Stub(channel))req = echo.Packet(text = hello*30)resp = server.Echo(req)assert resp.text == req.text
  • 44. 放心,不是 abu.rpc 教程! 讲 abu.rpc 在真实应用场景中遇 到的问题及解决方法。
  • 45. 这世界太复杂了……
  • 46. 抽象传输层@message.observableclass Transport(object): TRANSPORT_DATA = abu.rpc.transport.TRANSPORT_DATA‘ def _recv_func(self): while True: self.buff = self._ll_transport.recv(4096) if not self.buff: break self.pub(self.TRANSPORT_DATA)
  • 47. 很多的传输层class TgwServerMixin(object): def recv(self): data = Transport.recv(self) if self._handshaked: return data return self._do_handshake(data) def _do_handshake(self, data): …class TgwClientMixin(object): …
  • 48. 很多协议,随意组合。class TgwServerTransport(TgwServerMixin, Transport): …class TgwClientTransport(TgwClientMixin, Transport): …class SecTgwServerTransport(SecServerMixin, TgwServerTransport): …class SecTgwClientTransport(SecClientMixin, TgwClientTransport): …
  • 49. Mixin 威武! socketserver.ForkingMixInsocketserver.ThreadingMixIn
  • 50. 好,大概就是这些了…… state publish-subscribe observer mixin
  • 51. 使用这些组件……• http://pypi.python.org/pypi/message• http://pypi.python.org/pypi/state• http://pypi.python.org/pypi/abu.rpc
  • 52. 够了吗?
  • 53. 为何不改变语法?object Earth(Object): state default Day(State): behavior tick(self): if not Sun.visible: switch(self, self.Night) state Night(State): behavior tick(self): if Sun.visible: switch(self, self.Day)
  • 54. 如果你愿意……明年我来讲这个。
  • 55. 谢谢大家!

[转]页游开发中的 Python 组件与模式Presentation Transcript的更多相关文章

  1. [译]如何在Web开发中使用Python

    [译]如何在Web开发中使用Python 原文:HOWTO Use Python in the Web 摘要 这篇文档展示了Python如何融入到web中.它介绍了几种Python结合web服务器的方 ...

  2. Unity3D研究院之手游开发中所有特殊的文件夹(转)

    这里列举出手游开发中用到了所有特殊文件夹. 1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor就可以.比如目录:/xxx/xxx/Editor  和 /Edi ...

  3. (转)Unity3D研究院之手游开发中所有特殊的文件夹(assetbundle与Application.persistentDataPath)

    这里列举出手游开发中用到了所有特殊文件夹. 1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor就可以.比如目录:/xxx/xxx/Editor  和 /Edi ...

  4. Unity3D 手游开发中所有特殊的文件夹

    这里列举出手游开发中用到了所有特殊文件夹. 1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor就可以.比如目录:/xxx/xxx/Editor  和 /Edi ...

  5. Unity3D研究院之手游开发中所有特殊的文件夹

    这里列举出手游开发中用到了所有特殊文件夹. 1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor就可以.比如目录:/xxx/xxx/Editor  和 /Edi ...

  6. android开发中调用python代码(带参数)

    android开发主要用到的是java代码,但是当开发涉及到一些算法时,往往用python可以提高软件的运行速度,也更加便捷,这里分享自己项目调用python代码的方式,主要有以下几个步骤(个人方法, ...

  7. 在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式

    在进行项目开发的时候,刚好需要用到对字符串表达式进行求值的处理场景,因此寻找了几个符合要求的第三方组件LambdaParser.DynamicExpresso.Z.Expressions,它们各自功能 ...

  8. iPhone 和 iPad的ios 开发中 利用 WebViewJavascriptBridge组件,通过 UIWebView 对Html进行双向通讯

    本文转载至 http://blog.csdn.net/remote_roamer/article/details/7261490 WebViewJavascriptBridge 项目的 官网 http ...

  9. android app开发中的常用组件

    1 Activity 1.1 Activity的启动 第一,android manifest中指定的主activity,点击app的icon启动进入. 第二,使用intent,在另外一个activit ...

随机推荐

  1. VK Cup 2018 - Round 1+Codeforces Round #470

    A. Primal Sport 题意:有两个人轮流玩游戏.给出数X(i-1),轮到的人需要找到一个小于X(i-1)的素数x,然后得到Xi,Xi是x的倍数中大于等于X(i-1)的最小的数.现在已知X2, ...

  2. MyBatis For .NET学习- 初识MyBatis

    MyBatis的框架. Introduction MyBatis本是apache的一个开源项目iBatis,2010年这个项目由 apache software foundation迁移到了googl ...

  3. JAVA垃圾回收笔记

    一.分析GC日志 /** * @author : Hejinsheng * @date : 2019/1/18 0018 * @Description: 模拟FULL GC/YOUNG GC * -X ...

  4. 扯一扯 C#委托和事件?策略模式?接口回调?

    早前学习委托的时候,写过一点东西,今天带着新的思考和认知,再记点东西.这篇文章扯到设计模式中的策略模式,观察者模式,还有.NET的特性之一--委托.真的,请相信我,我只是在扯淡...... 场景练习 ...

  5. springmvc map

    /** * 目标方法可以添加 Map 类型(实际上也可以是 Model 类型或 ModelMap 类型)的参数. * @param map * @return */ @RequestMapping(& ...

  6. JS相关方法总计

    1. 锚点的使用: 简单使用: <a href="#001">跳到001</a> ...文字省略 <a name="001" id ...

  7. 通过自动回复机器人学Mybatis:搭建核心架构

    imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154 MessageDao.java package com.imooc.dao; impor ...

  8. SQL-ALTER-change和modify区别

      ALTER 对于列的应用:   1.更改列名      格式:CHANGE old_col_name new_col_name column_definition      保留old和new列名 ...

  9. Linux下捕捉信号

    关于 信号signal的知识铺垫 点这里 信号由三种处理方式: 忽略 执行该信号的默认处理动作 捕捉信号 如果信号的处理动作是用户自定义函数,在信号递达时就调用这个自定义函数,这称为捕捉信号. 进程收 ...

  10. 记一次网卡报错ERROR,some other host already uses address

    提示IP地址冲突,但是此IP确实没有被其他Server占用 解决如下: 编辑此文件 搜索arping 将下面几行注释掉 保存退出 激活网卡 此时IP地址已生效 下面是我的系统版本 (一般应该不会出现这 ...