Advertisement

F# in social gaming (CodeMash 15)

Yan Cui
Speaker at Self
Jan. 8, 2015
Advertisement

More Related Content

Advertisement
Advertisement

F# in social gaming (CodeMash 15)

  1. F# in Social GamingF# in Social Gaming Yan Cui (@theburningmonk)
  2. agenda
  3. Hi, my name is Yan Cui.
  4. 1MILLION USERS ACTIVE DAILY
  5. 250MILLION DAY PER REQUEST
  6. why F#?
  7. time to market
  8. correctness
  9. efficient
  10. tame complexity
  11. Implementing a slots engine
  12. Collectables Wager Size Special Symbol Avg Wager Size Web Server call
  13. • 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
  14. What symbols should land?! Any special symbol wins?! Did the player win anything?
  15. What the player’s new average wager?
  16. Should the player receive collectables?
  17. type Symbol = Standard! of string! ! ! ! | Wild
  18. type Symbol = Standard! of string! ! ! ! | Wild e.g.! Standard “hat”! ! Standard “shoe”! ! Standard “bonus”! ! …
  19. type Symbol = Standard! of string! ! ! ! | Wild i.e.! Wild
  20. ! ! ! type Win = LineWin of int * Symbol * int! ! | ScatterWin of Symbol * int
  21. ! ! ! type Win = LineWin of int * Symbol * int! ! | ScatterWin of Symbol * int e.g.! LineWin (5, Standard “shoe”, 4)
  22. ! ! ! type Win = LineWin of int * Symbol * int! ! | ScatterWin of Symbol * int e.g.! ScatterWin (Standard “bonus”, 3)
  23. type LineNum = int! type Count! = int! ! type Win = LineWin of LineNum * Symbol * Count! ! | ScatterWin of Symbol * Count
  24. type LineNum = int! type Count! = int! ! type Win = LineWin of LineNum * Symbol * Count! ! | ScatterWin of Symbol * Count e.g.! LineWin (5, Standard “shoe”, 4)
  25. type LineNum = int! type Count! = int! ! type Win = LineWin of LineNum * Symbol * Count! ! | ScatterWin of Symbol * Count e.g.! ScatterWin (Standard “bonus”, 3)
  26. make invalid states unrepresentable
  27. NASA orbiter crashed because one engineer accidentally used miles instead of kilometres
  28. you’re never too smart to make mistakes
  29. [<Measure>]! type Pence e.g.! 42<Pence>! ! 153<Pence>! ! …
  30. 10<Meter> / 2<Second> ! = 5<Meter/Second>! 10<Meter> * 2<Second> ! = 20<Meter Second> ! 10<Meter> + 10<Meter> ! = 20<Meter>! 10<Meter> * 10! ! ! = 100<Meter>! 10<Meter> * 10<Meter> ! = 100<Meter2>! 10<Meter> + 2<Second> ! // error! 10<Meter> + 2 ! ! ! // error
  31. 10<Meter> / 2<Second> ! = 5<Meter/Second>! 10<Meter> * 2<Second> ! = 20<Meter Second> ! 10<Meter> + 10<Meter> ! = 20<Meter>! 10<Meter> * 10! ! ! = 100<Meter>! 10<Meter> * 10<Meter> ! = 100<Meter2>! 10<Meter> + 2<Second> ! // error! 10<Meter> + 2 ! ! ! // error
  32. type Wager = int64<Pence>! type Multiplier = int! ! type Payout = Coins! of Wager! ! ! | MultipliedCoins of Multiplier * Wager! ! ! | Multi! of Payout list! ! ! | BonusGame
  33. type Wager = int64<Pence>! type Multiplier = int! ! type Payout = Coins! of Wager! ! ! | MultipliedCoins of Multiplier * Wager! ! ! | Multi! of Payout list! ! ! | BonusGame
  34. type Wager = int64<Pence>! type Multiplier = int! ! type Payout = Coins! of Wager! ! ! | MultipliedCoins of Multiplier * Wager! ! ! | Multi! of Payout list! ! ! | BonusGame
  35. type State = ! {! AvgWager! : Wager! SpecialSymbol : Symbol! Collectables : Map<Collectable, Count>! }
  36. New avg wager Got a Collectable! A pay line win!
  37. Betting small reduces avg wager! Bonus Game!
  38. type MonopolyBoardSpace = ! | RailRoad! ! of bool! | Property! ! of int! | ElectricCompany! of bool! | WaterCompany! of bool! | Chance! | CommunityChest! | GotoJail! | Jail! | FreeParking! | Go
  39. type MonopolyBoardSpace = ! | RailRoad! ! of bool! | Property! ! of int! | ElectricCompany! of bool! | WaterCompany! of bool! | Chance! | CommunityChest! | GotoJail! | Jail! | FreeParking! | Go
  40. type MonopolyBoardSpace = ! | RailRoad! ! of bool! | Property! ! of int! | ElectricCompany! of bool! | WaterCompany! of bool! | Chance! | CommunityChest! | GotoJail! | Jail! | FreeParking! | Go
  41. type MonopolyBoardSpace = ! | RailRoad! ! of bool! | Property! ! of int! | ElectricCompany! of bool! | WaterCompany! of bool! | Chance! | CommunityChest! | GotoJail! | Jail! | FreeParking! | Go
  42. [<Measure>]! type Position! ! type MonopolyBoard =! {! Spaces : MonopolyBoardSpace [ ]! }! member this.Item with ! get (pos : int<Position>) =! this.Spaces.[int pos]
  43. [<Measure>]! type Position! ! type MonopolyBoard =! {! Spaces : MonopolyBoardSpace [ ]! }! member this.Item with ! get (pos : int<Position>) = ! this.Spaces.[int pos]
  44. 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’.
  45. type BonusGameState =! {! Board ! : MonopolyBoard! Position ! : int<Position>! Lives ! : int<Life>! Doubles! : int<Double>! CoinSize! : Wager! TotalWin! : Wager! }
  46. Coin size brought over from main game
  47. type BonusGameState =! {! Board ! : MonopolyBoard! Position ! : int<Position>! Lives ! : int<Life>! Doubles! : int<Double>! CoinSize! : Wager! TotalWin! : Wager! }
  48. And a pay line win! Houses = multiplier on wins
  49. type BonusGameState =! {! Board ! : MonopolyBoard! Position ! : int<Position>! Lives ! : int<Life>! Doubles! : int<Double>! CoinSize! : Wager! TotalWin! : Wager! }
  50. Collected in the bonus game. Gives player extra ‘lives’.
  51. type BonusGameState =! {! Board ! : MonopolyBoard! Position ! : int<Position>! Lives ! : int<Life>! Doubles! : int<Double>! CoinSize! : Wager! TotalWin! : Wager! }
  52. 0<Position>
  53. let jail = 6<Position>
  54. let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …
  55. let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …
  56. let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …
  57. let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …
  58. let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …
  59. let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …
  60. let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …
  61. let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …
  62. let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …
  63. let rec move newPos state =! match state.Board.[newPos] with! …! | RailRoad true ! -> awardRailRoadMultiplier state! | Property n when n > 0 -> awardPropertyMultiplier n state! | ElectricityCompany true! | WaterCompany true -> awardUtilityMultiplier state! …
  64. let rec move newPos state =! match state.Board.[newPos] with! …! | CommunityChest! -> playCommunityChestCard state! | Chance ! ! -> playChanceCard state! …
  65. let rec move newPos state =! match state.Board.[newPos] with! | RailRoad true ! -> awardRailRoadMultiplier state! | Property n when n > 0 -> awardPropertyMultiplier n state! | ElectricityCompany true! | WaterCompany true -> awardUtilityMultiplier state! | CommunityChest! -> playCommunityChestCard state! | Chance ! ! -> playChanceCard state! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! | _ ! ! ! -> state
  66. let rec move newPos state =! match state.Board.[newPos] with! | RailRoad true ! -> awardRailRoadMultiplier state! | Property n when n > 0 -> awardPropertyMultiplier n state! | ElectricityCompany true! | WaterCompany true -> awardUtilityMultiplier state! | CommunityChest! -> playCommunityChestCard state! | Chance ! ! -> playChanceCard state! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! | _ ! ! ! -> state
  67. let rec move newPos state =! match state.Board.[newPos] with! | RailRoad true ! -> awardRailRoadMultiplier state! | Property n when n > 0 -> awardPropertyMultiplier n state! | ElectricityCompany true! | WaterCompany true -> awardUtilityMultiplier state! | CommunityChest! -> playCommunityChestCard state! | Chance ! ! -> playChanceCard state! | GotoJail when state.Lives = 0<Life> -> ! ! move jail state |> gameOver! | GotoJail ! ! -> move jail state! | _ ! ! ! -> state
  68. Recap
  69. lightweight syntax to create types and hierarchies
  70. great for domain modelling
  71. make invalid states unrepresentable
  72. clear, concise way to handle branching
  73. better correctness
  74. order of magnitude increase in productivity
  75. Stateful Server
  76. player states are big
  77. Stateless Server DatabaseClient
  78. 1:1 read-write ratio
  79. stateless = ! Inefficient & Expensive
  80. ServerClient Session 1 Session 2
  81. ServerClient Database
  82. Elastic Load Balancer S3 Auto scaling Group Server A Server B ... EC2 CloudFront Stateful Server
  83. 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
  84. 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! – Designate how to handle the next message
  85. 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
  86. Stateful Server Game Server Player A Player B S3 Worker C Worker B Gatekeeper RequestHandlers Asynchronous
  87. Stateful Server Game Server Worker C Worker B Gatekeeper Worker A ok RequestHandlers Player A Player B Asynchronous S3
  88. Stateful Server Game Server S3 Worker C Worker B Gatekeeper Worker A RequestHandlers Player A Player B Asynchronous
  89. Stateful Server Game Server S3 Worker C Gatekeeper Worker A RequestHandlers Player A Player B Asynchronous
  90. Stateful Server Game Server Worker C Worker A Gatekeeper error RequestHandlers Player A Player B S3 Asynchronous
  91. type Agent<‘T> = MailboxProcessor<‘T>! ! ! !
  92. type Agent<‘T> = MailboxProcessor<‘T>! ! type Message = ! | Get of AsyncReplyChannel<…>! | Put of State * Version * AsyncReplyChannel<…>
  93. type Agent<‘T> = MailboxProcessor<‘T>! ! type Message = ! | Get of AsyncReplyChannel<…>! | Put of State * Version * AsyncReplyChannel<…>
  94. type Result<‘T> =! | Success! of ’T! | Failure! of Exception! ! ! !
  95. type Result<‘T> =! | Success! of ’T! | Failure! of Exception! ! type GetResult = Result<State * Version>! type PutResult = Result<unit>!
  96. type Agent<‘T> = MailboxProcessor<‘T>! ! type Message = ! | Get of AsyncReplyChannel<GetResult>! | Put of State * Version * AsyncReplyChannel<PutResult>
  97. type Agent<‘T> = MailboxProcessor<‘T>! ! type Message = ! | Get of AsyncReplyChannel<GetResult>! | Put of State * Version * AsyncReplyChannel<PutResult>
  98. type Worker (playerId) =! let agent = Agent<Message>.Start(fun inbox ->! let state = getCurrentState playerId! ! let rec workingState (state, version) = ! async { … }! and closedState () =! async { … }! ! workingState (state, 1))
  99. type Worker (playerId) =! let agent = Agent<Message>.Start(fun inbox ->! let state = getCurrentState playerId! ! let rec workingState (state, version) = ! async { … }! and closedState () =! async { … }! ! workingState (state, 1))
  100. type Worker (playerId) =! let agent = Agent<Message>.Start(fun inbox ->! let state = getCurrentState playerId! ! let rec workingState (state, version) = ! async { … }! and closedState () =! async { … }! ! workingState (state, 1))
  101. type Worker (playerId) =! let agent = Agent<Message>.Start(fun inbox ->! let state = getCurrentState playerId! ! let rec workingState (state, version) = ! async { … }! and closedState () =! async { … }! ! workingState (state, 1))
  102. type Worker (playerId) =! let agent = Agent<Message>.Start(fun inbox ->! let state = getCurrentState playerId! ! let rec workingState (state, version) = ! async { … }! and closedState () =! async { … }! ! workingState (state, 1))
  103. let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None -> ! do! persist state! return! closedState()! …! }!
  104. let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None -> ! do! persist state! return! closedState()! …! }!
  105. let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None -> ! do! persist state! return! closedState()! …! }!
  106. non-blocking I/O
  107. let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None -> ! do! persist state! return! closedState()! …! }!
  108. let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None -> ! do! persist state! return! closedState()! …! }!
  109. let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! …! | Some(Get(reply)) ->! reply.Reply <| Success(state, version)! return! workingState(state, version)! …! }!
  110. let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! …! | Some(Put(newState, v, reply)) when version = v ->! reply.Reply <| Success()! return! workingState(newState, version+1)! …! }
  111. let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! …! | Some(Put(_, v, reply)) ->! reply.Reply <| Failure(VersionMismatch(version, v))! return! workingState(state, version)! }
  112. let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None ! ! ! ! ! ! ! -> …! | Some(Get(reply)) ! ! ! ! ! -> …! | Some(Put(newState, v, reply)) when version = v ! -> …! | Some(Put(_, v, reply)) ! ! ! ! -> …! }
  113. and closedState () = ! async { ! let! msg = inbox.Receive()! match msg with! | Get(reply) ->! reply.Reply <| Failure(WorkerStopped)! return! closedState()! | Put(_, _, reply) ->! reply.Reply <| Failure(WorkerStopped)! return! closedState()! }
  114. and closedState () = ! async { ! let! msg = inbox.Receive()! match msg with! | Get(reply) ->! reply.Reply <| Failure(WorkerStopped)! return! closedState()! | Put(_, _, reply) ->! reply.Reply <| Failure(WorkerStopped)! return! closedState()! }
  115. 5x efficient improvement
  116. 60% latency drop
  117. no databases
  118. need to ensure server affinity
  119. need to balance load
  120. need to avoid hotspots
  121. • Persist player state after short inactivity! • Move player to another server after persistence
  122. need to gracefully scale down
  123. Recap
  124. Agents! • no locks! • async message passing! !
  125. Agents! • no locks! • async message passing! • self-contained! • easy to reason with
  126. Agents! • low level! • exceptions kills agents! • primitive error monitoring! • no supervision! • no distribution
  127. MS Orleans Cricket akka.Net
  128. Async Workflow! • non-blocking I/O! • no callbacks
  129. Pattern Matching! • clear & concise
  130. Replacing a large class hierarchy
  131. Caught a Gnome
  132. EXP Item Gold Quest Progress Caught a Gnome
  133. Level Up Quest Progress EXP Item Gold Caught a Gnome Quest Complete
  134. Level Up Quest Progress EXP Item Gold Caught a Gnome New Quest Quest Complete
  135. Quest Progress EXP Item Gold Caught a Gnome Quest Complete New Quest Level Up
  136. Quest Progress New Quest Achievement Progress EXP Item Gold Quest Complete Level Up Caught a Gnome
  137. Level Up Quest Progress EXP Item Gold Caught a Gnome Quest Complete New Quest Achievement Progress Achievement Complete
  138. Level Up Quest Progress EXP Item Gold Caught a Gnome Quest Complete New Quest Achievement Progress Achievement Complete
  139. 100+ actions
  140. triggered by different abstraction layers
  141. non-functional requirements
  142. message-broker pattern
  143. Caught Gnome Trapping Queue Levelling Quests Achievements Analytics Partner Reporting
  144. Caught Gnome Trapping Queue Levelling Quests Achievements Analytics Partner Reporting Ignore Process Process Process Process Ignore
  145. Caught Gnome Trapping Queue Levelling Quests Achievements Analytics Partner Reporting EXP Item Gold
  146. Caught Gnome Trapping Queue Levelling Quests Achievements Analytics Partner Reporting EXP Item Gold
  147. Caught Gnome Trapping Queue Levelling Quests Achievements Analytics Partner Reporting EXP Item Gold Process Ignore Ignore Ignore Process Ignore
  148. Caught Gnome Trapping Queue Levelling Quests Achievements Analytics Partner Reporting EXP Item Gold Level Up
  149. Caught Gnome Trapping Queue Levelling Quests Achievements Analytics Partner Reporting EXP Item Gold Level Up
  150. need lots of facts
  151. type Fact =! | GotExp! ! of Exp! | GotGold! ! of Gold! | GotItem! ! of Item * Count! | CaughtMonster! of Monster * Bait * Location! | LevelUp! ! of OldLevel * NewLevel! …
  152. type Reward =! | GotExp! ! of Exp! | GotGold! ! of Gold! | GotItem! ! of Item * Count! ! type StateChange =! | LevelUp! ! of OldLevel * NewLevel! …
  153. type Fact =! | StateChange! of StateChange! | Reward! ! of Reward! | Trapping! ! of Trapping! | Fishing! ! of Fishing! …
  154. let process fact =! match fact with! | StateChange(stateChange)! -> …! | Reward(reward)! ! ! -> …! | Trapping(trapping)! ! -> …! …
  155. C# interop
  156. …! var fact = Fact.NewStateChange(! ! StateChange.NewLevelUp(oldLvl, newLvl));! …
  157. type IFact = interface end! ! type Reward =! | GotExp! ! of Exp! | GotGold! ! of Gold! | GotItem! ! of Item * Count! interface IFact
  158. type IFact = interface end! ! type Reward =! | GotExp! ! of Exp! | GotGold! ! of Gold! | GotItem! ! of Item * Count! interface IFact
  159. let process (fact : IFact) =! match fact with! | :? StateChange ! as stateChange!-> …! | :? Reward ! ! as reward! ! -> …! | :? Trapping! ! as trapping! -> …! …! | _ -> raise <| NotSupportedFact fact
  160. let process (fact : IFact) =! match fact with! | :? StateChange ! as stateChange!-> …! | :? Reward ! ! as reward! ! -> …! | :? Trapping! ! as trapping! -> …! …! | _ -> raise <| NotSupportedFact fact
  161. simple
  162. flexible
  163. extensible
  164. saver
  165. April 2015
  166. Fri 8.30am, Salon H @theburningmonk / theburningmonk.com
  167. Fri 8.30am, Salon H Fri 2.45pm, Guava + Tamarind @theburningmonk / theburningmonk.com
Advertisement