Download - F# in social gaming
![Page 1: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/1.jpg)
F# in Social GamingYan Cui (@theburningmonk)
![Page 2: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/2.jpg)
Who is Gamesys?
•Founded in 2001
•#1 in the UK
• Handle $5 Billion in turnover annually
•First company to launch real money gaming on Facebook
•Employ 1,000 globally
![Page 3: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/3.jpg)
![Page 4: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/4.jpg)
![Page 5: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/5.jpg)
![Page 6: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/6.jpg)
What is F#?
• Functional-first
• ML family of languages
• 1st class citizen on the .Net platform– also supported by Mono
![Page 7: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/7.jpg)
What is F#?
• Records
• Discriminated Unions
• MailboxProcessor (aka Agents)
• Computation Expressions (aka Workflows)
• Type Providers
• Quotations
• Units of Measure
![Page 8: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/8.jpg)
Why F#?
• Time to Market
• Efficiency
• Correctness
• Complexity
![Page 9: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/9.jpg)
Case Study #1
• Slots Engine– Written in F#– The ‘brain’– Enforces game rules and maths model
![Page 10: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/10.jpg)
Collectables
Wager Size
Special SymbolAvg Wager Size
Web Server call
![Page 11: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/11.jpg)
Winning at Slots
• Line Win– X number of matching symbols on adjacent columns– Positions have to be a ‘line’– Wild symbols substitute for other symbols
• Scatter Win– X number of matching symbols anywhere– Triggers bonus game
![Page 12: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/12.jpg)
What symbols should land?What lines did the player bet on?How much did the player wager?
Did the player win anything?Any special symbol wins?
Should the player receive collectables?
What’s the player’s new avg wager?
![Page 13: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/13.jpg)
![Page 14: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/14.jpg)
New avg wager Got a Collectable!
A pay line win!
![Page 15: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/15.jpg)
Betting small reduces avg wager!
Bonus Game!
![Page 16: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/16.jpg)
Use collectables
![Page 17: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/17.jpg)
![Page 18: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/18.jpg)
And a pay line win!
Coin size brought over from main game
Houses = multiplier on wins
GAME OVER
Collected in the bonus game.Gives player extra ‘lives’.
![Page 19: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/19.jpg)
![Page 20: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/20.jpg)
![Page 21: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/21.jpg)
![Page 22: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/22.jpg)
Why F#?
• Time to Market
• Efficiency
• Correctness
• Complexity
![Page 23: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/23.jpg)
Why F#?
• Record and Discriminated Unions– Lightweight syntax for creating types and hierarchies– Illegal states cannot be represented– Immutable by default
• Pattern matching– Clear and concise way to handle all branch conditions
• Unit of Measure– Prevents a whole class of errors related to misuse of units
![Page 24: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/24.jpg)
Case Study #2
• Stateful Server– Actor-based architecture
![Page 25: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/25.jpg)
Travel, Collect, Craft!
![Page 26: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/26.jpg)
Trap Monsters
![Page 27: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/27.jpg)
![Page 28: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/28.jpg)
Stateful Server
Elastic Load Balancer
S3
Auto scaling Group
Server A Server B
...
EC2
CloudFront
![Page 29: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/29.jpg)
Stateful Server
• Need to ensure Server affinity– All calls need to be routed to the affined server
• Need to balance load– Session lengths vary greatly– Some players are more active than others– Need to avoid hot spots
• Need to avoid players hogging a server– Need to be able to scale down!
![Page 30: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/30.jpg)
Stateful Server
• Persist player state after short inactivity
• Move player to another server after persistence
![Page 31: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/31.jpg)
Why Stateful Server?
• 500% efficiency increase
• 60% reduction in avg latency
• Fewer game servers
• No CouchBase cluster
• Huge saving on cost
![Page 32: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/32.jpg)
The Actor Model
An actor is the fundamental unit of computation which embodies the 3 things
• Processing• Storage• Communication
that are essential to computation.
-Carl Hewitt*
* http://bit.ly/HoNHbG
![Page 33: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/33.jpg)
The Actor Model
• Everything is an actor
• An actor has a mailbox
• When an actor receives a message it can:– Create new actors– Send messages to actors it has addresses for– Designate how to handle the next message it receives
![Page 34: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/34.jpg)
Stateful Server
• Gatekeeper– Manages the local list of active workers– Spawns new workers
• Worker– Manages the states for a player– Optimistic locking– Persist state after period of inactivity
![Page 35: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/35.jpg)
Stateful Server
Game Server
Player A
Player B
S3Worker C
Worker B
GatekeeperRe
ques
t Han
dler
s
Asynchronous
![Page 36: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/36.jpg)
Stateful Server
Game Server
Player A
Player B
S3Worker C
Worker B
Gatekeeper
Worker A
Requ
est H
andl
ers
Asynchronous
ok
![Page 37: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/37.jpg)
Stateful Server
Game Server
Player A
Player B
S3Worker C
Worker B
Gatekeeper
Worker A
Requ
est H
andl
ers
Asynchronous
![Page 38: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/38.jpg)
Stateful Server
Game Server
Player A
Player B
S3Worker C
Gatekeeper
Worker A
Requ
est H
andl
ers
Asynchronous
![Page 39: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/39.jpg)
Stateful Server
Game Server
Player A
Player B
S3Worker C
Worker A
Requ
est H
andl
ers
Gatekeeper
Asynchronous
error
![Page 40: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/40.jpg)
![Page 41: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/41.jpg)
MailboxProcessor<Message>
![Page 42: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/42.jpg)
Async<Message option>
![Page 43: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/43.jpg)
switch state
![Page 44: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/44.jpg)
![Page 45: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/45.jpg)
![Page 46: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/46.jpg)
Why F#?
• Time to Market
• Efficiency
• Correctness
• Complexity
![Page 47: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/47.jpg)
Why F#?
• Agents– No locks– Asynchronous message passing– Each actor is self-contained and easier to reason with
• Pattern matching– Clear and concise way to handle all branch conditions
![Page 48: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/48.jpg)
Why F#?
• Async Workflows– Non-blocking IO– Convert synchronous code into asynchronous code with
minimal code changes– Similar to C# 5’s async-await feature, but available in F#
since 2007!
![Page 49: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/49.jpg)
Case Study #3
• Quests & Achievements– Progress tied to most actions– Avoid scripting– Data driven
![Page 50: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/50.jpg)
Caught a Gnome
![Page 51: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/51.jpg)
EXP Item Gold
Quest Progress
Caught a Gnome
![Page 52: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/52.jpg)
Level Up
Quest Progress
EXP Item Gold
Caught a Gnome
Quest Complete
![Page 53: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/53.jpg)
Level Up
Quest Progress
EXP Item Gold
Caught a Gnome
Quest Complete
New Quest
![Page 54: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/54.jpg)
Level Up
Quest Progress
EXP Item Gold
Caught a Gnome
Quest Complete
New Quest
![Page 55: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/55.jpg)
Level Up
Quest Progress
EXP Item Gold
Caught a Gnome
Quest Complete
New QuestAchievement
Progress
![Page 56: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/56.jpg)
Level Up
Quest Progress
EXP Item Gold
Caught a Gnome
Quest Complete
New QuestAchievement
Progress
Achievement Complete
![Page 57: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/57.jpg)
Level Up
Quest Progress
EXP Item Gold
Caught a Gnome
Quest Complete
New QuestAchievement
Progress
Achievement Complete
![Page 58: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/58.jpg)
Quests & Achievements
• 100+ actions available in the game– Most can be tied to quests/achievements– Many yield rewards
• Triggered from different abstraction layers– Level controller– Trapping controller– ...
![Page 59: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/59.jpg)
Quests & Achievements
• Non-functional requirements– Analytics tracking– 3rd party reporting– ...
![Page 60: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/60.jpg)
Quests & Achievements
• Message broker pattern
• Something happened, it’s a FACT– Caught a Gnome– Received an item– Got some EXP– ...
![Page 61: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/61.jpg)
Caught Gnome Trapping
Queue
Levelling
Quests
Achievements
Analytics
Partner Reporting
![Page 62: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/62.jpg)
Caught Gnome Trapping
Queue
Levelling
Quests
Achievements
Analytics
Partner Reporting
Ignore
Process
Process
Process
Process
Ignore
![Page 63: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/63.jpg)
Caught Gnome Trapping
Queue
Levelling
Quests
Achievements
Analytics
Partner Reporting
EXPItemGold
![Page 64: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/64.jpg)
Caught Gnome Trapping
Queue
Levelling
Quests
Achievements
Analytics
Partner Reporting
EXPItemGold
![Page 65: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/65.jpg)
Caught Gnome Trapping
Queue
Levelling
Quests
Achievements
Analytics
Partner Reporting
EXPItemGold
Process
Ignore
Ignore
Ignore
Process
Ignore
![Page 66: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/66.jpg)
Caught Gnome Trapping
Queue
Levelling
Quests
Achievements
Analytics
Partner Reporting
EXPItemGold
Level Up
![Page 67: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/67.jpg)
Caught Gnome Trapping
Queue
Levelling
Quests
Achievements
Analytics
Partner Reporting
EXPItemGold
Level Up
![Page 68: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/68.jpg)
Message Broker Pattern
• Simple
• Flexible
• Extensible
• Requires many types of facts!
![Page 69: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/69.jpg)
OK for small number of DU cases
![Page 70: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/70.jpg)
![Page 71: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/71.jpg)
Why F#?
• Time to Market
• Efficiency
• Correctness
• Complexity
![Page 72: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/72.jpg)
Why F#?
• Discriminated Unions– Saved days/weeks in writing and maintaining a very large
class hierarchy
• Pattern Matching– Clear and concise way to handle all branch conditions
![Page 73: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/73.jpg)
Case Study #4
• DynamoDB.SQL*– SQL-like external DSL for working with Amazon DynamoDB– Built on top of FParsec
* http://theburningmonk.github.io/DynamoDb.SQL
![Page 74: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/74.jpg)
Amazon DynamoDB
• Fully managed NoSQL database
• Provisioned throughput
• Potentially infinitely scalable
• SSD-backed
• Fast, predictable performance
• Data replicated across data centres
![Page 75: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/75.jpg)
Amazon DynamoDB
• Semi-schema– Hash and Range key– Local Secondary Index
• Supports both strong or eventual consistency
• Supports ‘query’ and ‘scan’ operations
• Supports parallel scans
![Page 76: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/76.jpg)
Amazon DynamoDB
• API is cumbersome to use– Non-trivial queries are hard to express– .Net SDK doesn’t make it any easier...– Need an easier way to query for data
![Page 77: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/77.jpg)
DynamoDB.SQL
• Query with hash key only
SELECT Ticker, TimeStamp, Value FROM Prices WHERE Ticker = “MSFT”
SELECT * FROM Prices WHERE Ticker = “MST”
• Query with hash and range key
SELECT * FROM PricesWHERE Ticker = “MSFT”AND TimeStamp BEGINS WITH “2013-10”
![Page 78: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/78.jpg)
DynamoDB.SQL
• Ordering and Limiting number of results
SELECT * FROM Prices WHERE Ticker = “MSFT”ORDER ASCLIMIT 100
• Using eventual consistency and throttling
SELECT * FROM Prices WHERE Ticker = “MSFT”WITH (NoConsistentRead, PageSize(10))
![Page 79: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/79.jpg)
DynamoDB.SQL
• Counting without returning data
COUNT * FROM Prices WHERE Ticker = “MSFT”
COUNT * FROM Prices WHERE Ticker = “MSFT” AND TimeStamp BEGINS WITH “2013-10”
![Page 80: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/80.jpg)
Image by Mike Rohde
DynamoDB.SQ
L
![Page 81: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/81.jpg)
Why F#?
• Time to Market
• Efficiency
• Correctness
• Complexity
![Page 82: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/82.jpg)
Why F#?
• Record and Discriminated Unions– Lightweight syntax for creating types and hierarchies– Illegal states cannot be represented– Immutable by default
• Pattern matching– Clear and concise way to handle all branch conditions
![Page 83: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/83.jpg)
Why F#?
• Active Patterns– Extends the power of pattern matching– Composable
• Async Workflows– Non-blocking IO– Convert synchronous code into asynchronous code with
minimal code changes– Similar to C# 5’s async-await feature, but available in F#
since 2007!
![Page 84: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/84.jpg)
Thank You!
![Page 85: F# in social gaming](https://reader036.vdocument.in/reader036/viewer/2022081414/54907a15b47959b23e8b4638/html5/thumbnails/85.jpg)
JackpotJoy Slotshttp://apps.facebook.com/jackpotjoyslots
Bingo Lanehttp://apps.facebook.com/bingolane
Here Be Monstershttp://apps.facebook.com/herebemonsters
Building a MMORPGhttp://bit.ly/1hjqoL8http://slidesha.re/18MD4XY
Google I/O 2013 – Here Be BigQueryhttp://bit.ly/1fHjbce