支撐英雄聯盟戰績網的那條巨蟒
   Python behind loltw.net
           by toki
關於我
Something about me
寫過AlienBBS (OSX, Cocoa)
 
 
 
 
 
 
 
                Author of AlienBBS
寫過抓漫畫的軟體 (C#)
 
 
 
 
 
 
 
        Author of TPhotoRipper
寫過抓漫畫的Chrome Ext (JS)
 
 
 
 
 
 
 
 Author of 8comic link generator Chrome ext
台達電子 - 雲端技術中心
 
 
 
 
 
 
 
        Delta Electronic, CTBU
只有壹年Python經驗
 
 
 
 
 
 
 
      1 year Python experience only
什麼是英雄聯盟?
 
 
 
 
 
 
 
      What is League of Legends?
Game
    Editor


                       魔獸三國
DotA Allstars   中文變種
    鬥塔

                       魔獸信長
     Riot
    Game
5 players VS 5 players
 
 
 
 
 
 
目標是克服萬難打爆對面的基地!!!
什麼是英雄聯盟戰績網?
 
 
 
 
 
 
 
       What can loltw.net do?
不開遊戲也可查詢玩家的資料
 
 
 
 
 
 
 
    Query player info even not in game
顯示遊戲裡查不到的資料
 
 
 
 
 
 
 
    Reveal what server hides from you.
顯示官方網站沒有的排名
 
 
 
 
 
 
 
  Reveal ranks not shown on official site
查詢頂尖玩家的遊戲配置
 
 
 
 
 
 
 
    Show top players' game settings
以及我最愛的 - 統計 :-P
 
 
 
 
 
 
 
         & my favorite - statistics
這樣子的網站會有人用嗎?
 
 
 
 
 
 
 
      How many people use it?
玩家們用戰績網做什麼?
 
 
 
 
 
 
 
      How people use loltw.net?
查詢隊友有多坑 :-P
 
        YOU ARE HERE

 
 
 
 
 
 
    Querying how troll teammates are :-P
為了解你的隊友並且配合他們
為了學習高手的遊戲配置
 
 
 
 
 
 
 
             For better team play
     For learning from top players
戰績網怎麼拿到玩家資料的?
 
 
 
 
 
 
 
    How loltw.net get players' info?
戰績網V1 - 使用者上傳
 
 
 
 
 
 
 
       loltw.net V1 - user upload
戰績網V2 - 偽裝成遊戲程式
 
 
 
 
 
 
 
     loltw.net V2 - fake game client
構成戰績網的OSS
 
 
 
 
 
 
 
    List of OSS help building loltw.net
中秋節太無聊怎麼辦?
 
 
 
 
 
 
 
       bored in vacation?
來看看遊戲的log檔裡有什麼
 
 
 
 
 
 
 
     find something in game log
有log parser了,再來呢?
 
 
 
 
 
 
 
        Ok, we got a log parser, then?
django? 那是啥?
 
 
 
 
 
 
 
               What's django?
快速的MVC的網站開發環境
 
 
 
 
 
 
 
      A fast MVC web framework
Model                 Model
how to define data    資料如何定義
                       
Controller            Controller
how to fetch/modify   資料如何取出/修改
data                   
                       
View                  View
how to present data   資料如何呈現
 
 
使用資料庫就像使用一般物件
class Author(models.Model):
  name = models.CharField(max_length=60)
class Article(models.Model):
  title = models.CharField(max_length=60)
   body = models.TextField()
  created = models.DateTimeField(auto_now_add=True)
   author = models.ForeignKey('Author')
 
  = Article(title='測試文章'
author = Author.objects.create(name='toki')
article
            body='測試文章內容....',
            author = author)
 
article .save()

                   ORM - Object Relation Mapping
快速而方便的URL dispatcher
 
 ^blog/$                => /blog/
 ^blog/(?P<id>d+)/$    => /blog/1/
                        => /blog/2/

 ^blog/(?P<author>w+)/$ =>/blog/author/
                        =>/blog/teeeeeemo/
 
 
           Fast & convenience URL dispatcher
簡易好用的樣板系統
 <html>
 <body>
 {% for a in articles %}
 <h1>{{a.title}}</h1>
 {{a.body}}
 <div>

 <div style="text-align: right;">
 </div>

 </div>
 Author: {{a.author}}

 </body>
 {% endfor %}

 </html>

                 easy and good template system
擴充你的樣板系統
 SERVER_LOOKUP = {
  'TW': '台灣',
 }   'NA': '北美',

 @register.filter
 @stringfilter
 def server_region(o):
     return SERVER_LOOKUP.get(o)
  
 ------
 <div>{{'TW'|server_region}}</div> => <div>台灣</div>
 
           With extensible template filters / tags
內建多語系處理,講英文嘛A通
 
 
 
 
 
 
 
     built-in multi language support
內建cache機制
 from django.views.decorators.cache
 import cache_page
 @cache_page(60*60*24)
 def blah blah blah ...
     some_view_func(rq):

 
 
 
 
                             built-in cache processor
內建登入以及session處理
 
 
 
 
 
 
 
     built-in login & session processor
安裝簡單,架構清晰
文件齊全,學習容易
 
 
 
 
 
    easy to install, clear architecture
     well documented, easy to learn
log一直改版怎麼辦?
 
 
 
 
 
 
 
      log format changes so often!
來吃點mongo吧
 
 
 
 
 
 
 
            Try mongoDB
mongo有什麼好吃的?
  Schema free
  No join
  JSON compatible
  Python friendly
 
  Horizontal scalability with replica
  Fast (& eat all your memory :-P)

 
                                 Why mongoDB?
用JSON來記錄Blog文章
 
  {
  "title": "這是blog title",
  "body": "blog文章,很長很長...",
    "create": "2012-6-10T10:00:00.000",
  "author": "toki"
 }
 
 
        How to describe a blog article in JSON?
用Python dict來記錄Blog文章
 
  {
  "title": "這是blog title",
  "body": "blog文章,很長很長...",
    "create": "2012-6-10T10:00:00.000",
  "author": "toki"
 }
 
 
      How to describe it in Python dictionary?
如何存進mongodb (in python)
 
  from pymongo import Connection
 a = {
  "title": "這是blog title",
     "article": "blog文章,很長很長...",
  "body": "2012-6-10T10:00:00.000",
  "author": "toki"
  }
 Connection('localhost')['blog']['article'].save(a)
                  connection   database   collection

 
          How to save into mongodb via python?
如何搜尋blog文章
  from pymongo import Connection
  all_article = Connection('localhost')['blog']['article'].
  find()
   
  all_article_title = Connection('localhost')['blog']
  ['article'].find(
    {}, {title: 1})
  article =
  Connection('localhost')['blog']['article'].find_one(
  {'author': 'toki'})
 
                                            How to search?
如果你突然想幫blog加上tag
 
  from pymongo import Connection
  
  Connection('localhost')['blog']['article'].update(
  {
       {'author': 'toki'},
       {'$set': {'tags': ['a', 'b', 'c']}},
  }
 )
 
              Wanna add tags fields to blog data?
難道mongoDB沒有缺點嗎?
 Schema free
  Everyone needs to know how schema looks like
  每個人都得知道資料長什麼樣子
 No join
  Waste of space
  浪費空間
 Fragment
  Scheduled defragment maintenance required
   需要定時重整資料庫
  
 
                                 Cons of mongoDB?
還有該死的BFGL!!!
         Write will lock DB globally
         寫入動作會鎖住整個資料庫
 
         Solution
         解法

         Add more RAM
         加RAM
         Multi DB instance
         開多個資料庫
         Wait newer version(2.2)
         等新版(2.2以後)
 
   And DXXN BFGL(Big Fxxking Global Lock)!!!
RIOT 我的log呢? QQ
 
 
 
 
 
 
 
            RIOT, where is my log?
是時候來點扭曲的蟒蛇了
 
 
 
 
 
 
 
     Time for something TWISTED
LoL的通訊協定是....?
 
 ● Built by Flash (Adobe AIR)
 ● RTMPS ( Real Time Message Protocol)
 ● RTMPS = RTMP + SSL Encryption
 ● Wrote by Actionscript = Event Driven
 
 
     What's the protocol of League of Legends?
Twisted 是什麼?
 
Officially:
 
Twisted is an event-driven networking engine
written in Python
 
 
 
官方說法:
Twisted 是一個事件驅動的網路引擎
 
 
 
 
 
                             What is Twisted?
Twisted寫echo server要多久
from twisted.internet import protocol, reactor
 
 
class Echo(protocol.Protocol):
  def dataReceived(self, data):
       self.transport.write(data)
 
 
class EchoFactory(protocol.Factory):
  def buildProtocol(self, addr):
       return Echo()
 
 
reactor.listenTCP(1234, EchoFactory())
 
reactor.run()
 
How long takes writing echo server in Twisted?
要改成SSL加密版本又要多久?
from twisted.internet import protocol, reactor, ssl
 
 
class Echo(protocol.Protocol):
  def dataReceived(self, data):
       self.transport.write(data)
  EchoFactory(protocol.Factory):
class
   def buildProtocol(self, addr):
  return Echo()
 
 
reactor.listenSSL(1234, EchoFactory(),
   ssl.DefaultOpenSSLContextFactory('key', 'crt'))
 
reactor.run()
 
    How long takes to rewrite it to SSL version?
Protocol提供了哪些事件?
 
       ● connectionMade
         連上時要做什麼
 
       ● connectionLost
         失去連線時要做什麼

       ● dataReceived
         收到資料時要做什麼

 
   What events does Protocol class provide?
其它寫bot會用到的函數
  ● reactor.callLater()
    讓你等下再call某個function
 
  ● reactor.callInThread()
  讓你在另一個thread call某個function
  ● task.LoopingCall class
  讓你每隔一定時間就去call某個function
   
 
 
         Other functions for writing a lol bot
一段簡單的client程式
from twisted.internet import protocol, reactor, task
  
class SimpleClient(protocol.Protocol):
   def dataReceived(self, data):
       print data
   def connectionMade(self):
       self.callLater(3.0, self.send_hello)
   def send_nop(self):
       self.transport.write('nop')
   def send_hello(self):
       self.transport.write('hello')
   l = task.LoopingCall(self.send_nop)
       l.start(1.0)
  
                                    Simple client code
一段簡單的client程式(續)
class SimpleClientFactory(protocol.ClientFactory):
  def buildProtocol(self, addr):
       return SimpleClient()
 
 
reactor.connectTCP('localhost', 7777,
 
SimpleClientFactory())
 
 
# for SSL support, call below line instead
# reactor.connectSSL('localhost', 7777,
 
SimpleClientFactory(), ssl.ClientContextFactory())
 
 
reactor.run()
 
                     Simple client code (continued)
其它各種幫助戰績網的資源
 
 
 
 
 
 
 
  Other resources help building loltw.net
懶得寫網站的最新消息頁面?
 
 
 
 
 
 
 
     Too lazy for site news pages?
使用者懶的上傳該怎麼辦
 
 
 
 
 
 
 
 How to help lazy users upload their logs?
使用者亂傳假的log該怎麼辦
 
 
 
 
 
 
 
       How to deal with fake log?
做一個漂亮的HTML按鈕要多久
 
          =?
 
  With

 
  <a href="" class="btn btn-primary">
  Primary action</a>
 
     How long takes for a pretty html button?
來談談我們的VPS
  19.95 美金 (600 台幣) 你會得到
  512MB 記憶體
     20GB 硬碟空間
  200GB 對外流量 (傳到server免錢!!)
 某HxCloud光100GB流量就要1350台幣(45美金)!!
     6 個不同地理位置(美歐日)的資料中心可以選擇
     超級快的線上咨詢
  19.95 USD (600 NTD) you get
HxCloud charges 1350NTD(45USD) for ONLY 100GB bandwidth !!!!


  512MBHDD
     20GB
            RAM

  200GB outgoing bandwidth (incoming is freeeee!)
     6 geolocations(JP/US/EU) data centers to choice
  ultra fast online support
                                    Let's talk about our VPS
感謝各位 :-)
 
 
  問與答時間&工商服務
 
  Q&A & Hiring Info
 
 
 
           Thx for your listening :-)
台達電子雲端研發中心徵人中
 熱愛寫程式             Love coding
 
 熱愛OSS             Love OSS
                  Familiar with U*ix system is a plus
 加分項目
  熟U*ix系統         Familiar
                  Familiar
                             with
                             with
                                    Python is a plus
                                    C/C++ is a plus
                  Familiar   with   Virtualization is a plus
  熟Python         Familiar   with   Web Application is a plus
   熟C/C++
  熟Virtualization
  熟Web Application
  
 請寄履歷至: YVONNE.WJ.CHEN@delta.com.tw
   We're hiring (Delta Electronic CTBU)

支撐英雄聯盟戰績網的那條巨蟒