f# in social gaming (codemash 15)

180
F# in Social Gaming F# in Social Gaming Yan Cui (@theburningmonk)

Upload: yan-cui

Post on 12-Jul-2015

1.310 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: F# in social gaming (CodeMash 15)

F# in Social GamingF# in Social GamingYan Cui (@theburningmonk)

Page 2: F# in social gaming (CodeMash 15)

agenda

Page 3: F# in social gaming (CodeMash 15)

Hi, my name is Yan Cui.

Page 4: F# in social gaming (CodeMash 15)
Page 5: F# in social gaming (CodeMash 15)

1MILLION USERS

ACTIVEDAILY

Page 6: F# in social gaming (CodeMash 15)

250MILLION DAY

PERREQUEST

Page 7: F# in social gaming (CodeMash 15)

why F#?

Page 8: F# in social gaming (CodeMash 15)

time to market

Page 9: F# in social gaming (CodeMash 15)

correctness

Page 10: F# in social gaming (CodeMash 15)

efficient

Page 11: F# in social gaming (CodeMash 15)

tame complexity

Page 12: F# in social gaming (CodeMash 15)

Implementing a slots engine

Page 13: F# in social gaming (CodeMash 15)

Collectables

Wager Size

Special SymbolAvg Wager Size

Web Server call

Page 14: F# in social gaming (CodeMash 15)

• 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 15: F# in social gaming (CodeMash 15)

What symbols should land?!Any special symbol wins?!

Did the player win anything?

Page 16: F# in social gaming (CodeMash 15)

What the player’s new average wager?

Page 17: F# in social gaming (CodeMash 15)

Should the player receive collectables?

Page 18: F# in social gaming (CodeMash 15)

type Symbol = Standard! of string!! ! ! | Wild

Page 19: F# in social gaming (CodeMash 15)

type Symbol = Standard! of string!! ! ! | Wild

e.g.! Standard “hat”!! Standard “shoe”!! Standard “bonus”!! …

Page 20: F# in social gaming (CodeMash 15)

type Symbol = Standard! of string!! ! ! | Wild

i.e.! Wild

Page 21: F# in social gaming (CodeMash 15)

!

!

!

type Win = LineWin of int * Symbol * int!! | ScatterWin of Symbol * int

Page 22: F# in social gaming (CodeMash 15)

!

!

!

type Win = LineWin of int * Symbol * int!! | ScatterWin of Symbol * int

e.g.! LineWin (5, Standard “shoe”, 4)

Page 23: F# in social gaming (CodeMash 15)

!

!

!

type Win = LineWin of int * Symbol * int!! | ScatterWin of Symbol * int

e.g.! ScatterWin (Standard “bonus”, 3)

Page 24: F# in social gaming (CodeMash 15)

type LineNum = int!type Count! = int!!

type Win = LineWin of LineNum * Symbol * Count!! | ScatterWin of Symbol * Count

Page 25: F# in social gaming (CodeMash 15)

type LineNum = int!type Count! = int!!

type Win = LineWin of LineNum * Symbol * Count!! | ScatterWin of Symbol * Count

e.g.! LineWin (5, Standard “shoe”, 4)

Page 26: F# in social gaming (CodeMash 15)

type LineNum = int!type Count! = int!!

type Win = LineWin of LineNum * Symbol * Count!! | ScatterWin of Symbol * Count

e.g.! ScatterWin (Standard “bonus”, 3)

Page 27: F# in social gaming (CodeMash 15)

make invalid states unrepresentable

Page 28: F# in social gaming (CodeMash 15)

NASA orbiter crashed because one engineer accidentally used miles instead of kilometres

Page 29: F# in social gaming (CodeMash 15)

you’re never too smart to make mistakes

Page 30: F# in social gaming (CodeMash 15)

[<Measure>]!type Pence

e.g.! 42<Pence>!! 153<Pence>!! …

Page 31: F# in social gaming (CodeMash 15)

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

Page 32: F# in social gaming (CodeMash 15)

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

Page 33: F# in social gaming (CodeMash 15)

type Wager = int64<Pence>!type Multiplier = int!!

type Payout = Coins! of Wager!! ! | MultipliedCoins of Multiplier * Wager!! ! | Multi! of Payout list!! ! | BonusGame

Page 34: F# in social gaming (CodeMash 15)

type Wager = int64<Pence>!type Multiplier = int!!

type Payout = Coins! of Wager!! ! | MultipliedCoins of Multiplier * Wager!! ! | Multi! of Payout list!! ! | BonusGame

Page 35: F# in social gaming (CodeMash 15)

type Wager = int64<Pence>!type Multiplier = int!!

type Payout = Coins! of Wager!! ! | MultipliedCoins of Multiplier * Wager!! ! | Multi! of Payout list!! ! | BonusGame

Page 36: F# in social gaming (CodeMash 15)

type State = ! {!

AvgWager! : Wager!SpecialSymbol : Symbol!Collectables : Map<Collectable, Count>!

}

Page 37: F# in social gaming (CodeMash 15)
Page 38: F# in social gaming (CodeMash 15)

New avg wager Got a Collectable!

A pay line win!

Page 39: F# in social gaming (CodeMash 15)
Page 40: F# in social gaming (CodeMash 15)

Betting small reduces avg wager!

Bonus Game!

Page 41: F# in social gaming (CodeMash 15)
Page 42: F# in social gaming (CodeMash 15)
Page 43: F# in social gaming (CodeMash 15)

type MonopolyBoardSpace = ! | RailRoad! ! of bool! | Property! ! of int! | ElectricCompany! of bool! | WaterCompany! of bool! | Chance! | CommunityChest! | GotoJail! | Jail! | FreeParking! | Go

Page 44: F# in social gaming (CodeMash 15)

type MonopolyBoardSpace = ! | RailRoad! ! of bool! | Property! ! of int! | ElectricCompany! of bool! | WaterCompany! of bool! | Chance! | CommunityChest! | GotoJail! | Jail! | FreeParking! | Go

Page 45: F# in social gaming (CodeMash 15)

type MonopolyBoardSpace = ! | RailRoad! ! of bool! | Property! ! of int! | ElectricCompany! of bool! | WaterCompany! of bool! | Chance! | CommunityChest! | GotoJail! | Jail! | FreeParking! | Go

Page 46: F# in social gaming (CodeMash 15)

type MonopolyBoardSpace = ! | RailRoad! ! of bool! | Property! ! of int! | ElectricCompany! of bool! | WaterCompany! of bool! | Chance! | CommunityChest! | GotoJail! | Jail! | FreeParking! | Go

Page 47: F# in social gaming (CodeMash 15)

[<Measure>]!type Position!!type MonopolyBoard =! {! Spaces : MonopolyBoardSpace [ ]! }! member this.Item with ! get (pos : int<Position>) =! this.Spaces.[int pos]

Page 48: F# in social gaming (CodeMash 15)

[<Measure>]!type Position!!type MonopolyBoard =! {! Spaces : MonopolyBoardSpace [ ]! }! member this.Item with ! get (pos : int<Position>) = ! this.Spaces.[int pos]

Page 49: F# in social gaming (CodeMash 15)
Page 50: F# in social gaming (CodeMash 15)

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 51: F# in social gaming (CodeMash 15)

type BonusGameState =! {! Board ! : MonopolyBoard! Position ! : int<Position>! Lives ! : int<Life>! Doubles! : int<Double>! CoinSize! : Wager! TotalWin! : Wager! }

Page 52: F# in social gaming (CodeMash 15)

Coin size brought over from main game

Page 53: F# in social gaming (CodeMash 15)

type BonusGameState =! {! Board ! : MonopolyBoard! Position ! : int<Position>! Lives ! : int<Life>! Doubles! : int<Double>! CoinSize! : Wager! TotalWin! : Wager! }

Page 54: F# in social gaming (CodeMash 15)

And a pay line win!

Houses = multiplier on wins

Page 55: F# in social gaming (CodeMash 15)

type BonusGameState =! {! Board ! : MonopolyBoard! Position ! : int<Position>! Lives ! : int<Life>! Doubles! : int<Double>! CoinSize! : Wager! TotalWin! : Wager! }

Page 56: F# in social gaming (CodeMash 15)
Page 57: F# in social gaming (CodeMash 15)
Page 58: F# in social gaming (CodeMash 15)

Collected in the bonus game. Gives player extra ‘lives’.

Page 59: F# in social gaming (CodeMash 15)

type BonusGameState =! {! Board ! : MonopolyBoard! Position ! : int<Position>! Lives ! : int<Life>! Doubles! : int<Double>! CoinSize! : Wager! TotalWin! : Wager! }

Page 60: F# in social gaming (CodeMash 15)

0<Position>

Page 61: F# in social gaming (CodeMash 15)

let jail = 6<Position>

Page 62: F# in social gaming (CodeMash 15)

let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> !! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …

Page 63: F# in social gaming (CodeMash 15)

let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> !! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …

Page 64: F# in social gaming (CodeMash 15)

let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> !! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …

Page 65: F# in social gaming (CodeMash 15)

let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> !! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …

Page 66: F# in social gaming (CodeMash 15)

let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> !! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …

Page 67: F# in social gaming (CodeMash 15)

let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> !! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …

Page 68: F# in social gaming (CodeMash 15)

let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> !! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …

Page 69: F# in social gaming (CodeMash 15)

let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> !! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …

Page 70: F# in social gaming (CodeMash 15)

let rec move newPos state =! match state.Board.[newPos] with! …! | GotoJail when state.Lives = 0<Life> -> !! move jail state |> gameOver! | GotoJail ! ! -> move jail state! …

Page 71: F# in social gaming (CodeMash 15)

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! …

Page 72: F# in social gaming (CodeMash 15)

let rec move newPos state =! match state.Board.[newPos] with! …! | CommunityChest! -> playCommunityChestCard state! | Chance ! ! -> playChanceCard state! …

Page 73: F# in social gaming (CodeMash 15)

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

Page 74: F# in social gaming (CodeMash 15)

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

Page 75: F# in social gaming (CodeMash 15)

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

Page 76: F# in social gaming (CodeMash 15)
Page 77: F# in social gaming (CodeMash 15)

Recap

Page 78: F# in social gaming (CodeMash 15)

lightweight syntax to create types and

hierarchies

Page 79: F# in social gaming (CodeMash 15)

great for domain modelling

Page 80: F# in social gaming (CodeMash 15)

make invalid states unrepresentable

Page 81: F# in social gaming (CodeMash 15)

clear, concise way to handle branching

Page 82: F# in social gaming (CodeMash 15)

better correctness

Page 83: F# in social gaming (CodeMash 15)

order of magnitude increase in productivity

Page 84: F# in social gaming (CodeMash 15)

Stateful Server

Page 85: F# in social gaming (CodeMash 15)
Page 86: F# in social gaming (CodeMash 15)

player states are big

Page 87: F# in social gaming (CodeMash 15)

Stateless Server DatabaseClient

Page 88: F# in social gaming (CodeMash 15)

1:1 read-write ratio

Page 89: F# in social gaming (CodeMash 15)

stateless = !Inefficient & Expensive

Page 90: F# in social gaming (CodeMash 15)

ServerClient

Session 1

Session 2

Page 91: F# in social gaming (CodeMash 15)

ServerClient Database

Page 92: F# in social gaming (CodeMash 15)

Elastic Load Balancer

S3

Auto scaling Group

Server A Server B

...

EC2

CloudFront

Stateful Server

Page 93: F# in social gaming (CodeMash 15)

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 94: F# in social gaming (CodeMash 15)

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

Page 95: F# in social gaming (CodeMash 15)

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 96: F# in social gaming (CodeMash 15)

Stateful Server

Game Server

Player A

Player B

S3Worker C

Worker B

GatekeeperR

eque

st H

andl

ers

Asynchronous

Page 97: F# in social gaming (CodeMash 15)

Stateful Server

Game Server

Worker C

Worker B

Gatekeeper

Worker Aok

Req

uest

Han

dler

sPlayer A

Player B

Asynchronous

S3

Page 98: F# in social gaming (CodeMash 15)

Stateful Server

Game Server

S3Worker C

Worker B

Gatekeeper

Worker AR

eque

st H

andl

ers

Player A

Player B

Asynchronous

Page 99: F# in social gaming (CodeMash 15)

Stateful Server

Game Server

S3Worker C

Gatekeeper

Worker AR

eque

st H

andl

ers

Player A

Player B

Asynchronous

Page 100: F# in social gaming (CodeMash 15)

Stateful Server

Game Server

Worker C

Worker A

Gatekeeper

error

Req

uest

Han

dler

sPlayer A

Player B

S3

Asynchronous

Page 101: F# in social gaming (CodeMash 15)

type Agent<‘T> = MailboxProcessor<‘T>!!!!

Page 102: F# in social gaming (CodeMash 15)

type Agent<‘T> = MailboxProcessor<‘T>!!type Message = ! | Get of AsyncReplyChannel<…>! | Put of State * Version * AsyncReplyChannel<…>

Page 103: F# in social gaming (CodeMash 15)

type Agent<‘T> = MailboxProcessor<‘T>!!type Message = ! | Get of AsyncReplyChannel<…>! | Put of State * Version * AsyncReplyChannel<…>

Page 104: F# in social gaming (CodeMash 15)

type Result<‘T> =! | Success! of ’T! | Failure! of Exception!!!!

Page 105: F# in social gaming (CodeMash 15)

type Result<‘T> =! | Success! of ’T! | Failure! of Exception!!type GetResult = Result<State * Version>!type PutResult = Result<unit>!

Page 106: F# in social gaming (CodeMash 15)

type Agent<‘T> = MailboxProcessor<‘T>!!type Message = ! | Get of AsyncReplyChannel<GetResult>! | Put of State * Version * AsyncReplyChannel<PutResult>

Page 107: F# in social gaming (CodeMash 15)

type Agent<‘T> = MailboxProcessor<‘T>!!type Message = ! | Get of AsyncReplyChannel<GetResult>! | Put of State * Version * AsyncReplyChannel<PutResult>

Page 108: F# in social gaming (CodeMash 15)

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))

Page 109: F# in social gaming (CodeMash 15)

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))

Page 110: F# in social gaming (CodeMash 15)

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))

Page 111: F# in social gaming (CodeMash 15)

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))

Page 112: F# in social gaming (CodeMash 15)

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))

Page 113: F# in social gaming (CodeMash 15)

let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None -> ! do! persist state! return! closedState()! …! }!

Page 114: F# in social gaming (CodeMash 15)

let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None -> ! do! persist state! return! closedState()! …! }!

Page 115: F# in social gaming (CodeMash 15)

let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None -> ! do! persist state! return! closedState()! …! }!

Page 116: F# in social gaming (CodeMash 15)

non-blocking I/O

Page 117: F# in social gaming (CodeMash 15)

let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None -> ! do! persist state! return! closedState()! …! }!

Page 118: F# in social gaming (CodeMash 15)

let rec workingState (state, version) = ! async { ! let! msg = inbox.TryReceive(60000)! match msg with! | None -> ! do! persist state! return! closedState()! …! }!

Page 119: F# in social gaming (CodeMash 15)

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)! …! }!

Page 120: F# in social gaming (CodeMash 15)

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)! …! }

Page 121: F# in social gaming (CodeMash 15)

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)! }

Page 122: F# in social gaming (CodeMash 15)

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)) ! ! ! ! -> …! }

Page 123: F# in social gaming (CodeMash 15)

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()! }

Page 124: F# in social gaming (CodeMash 15)

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()! }

Page 125: F# in social gaming (CodeMash 15)
Page 126: F# in social gaming (CodeMash 15)

5x efficient improvement

Page 127: F# in social gaming (CodeMash 15)

60% latency drop

Page 128: F# in social gaming (CodeMash 15)

no databases

Page 129: F# in social gaming (CodeMash 15)

need to ensure server affinity

Page 130: F# in social gaming (CodeMash 15)

need to balance load

Page 131: F# in social gaming (CodeMash 15)

need to avoid hotspots

Page 132: F# in social gaming (CodeMash 15)

• Persist player state after short inactivity!• Move player to another server after persistence

Page 133: F# in social gaming (CodeMash 15)

need to gracefully scale down

Page 134: F# in social gaming (CodeMash 15)

Recap

Page 135: F# in social gaming (CodeMash 15)

Agents!• no locks!• async message passing!

!

Page 136: F# in social gaming (CodeMash 15)

Agents!• no locks!• async message passing!• self-contained!• easy to reason with

Page 137: F# in social gaming (CodeMash 15)

Agents!• low level!• exceptions kills agents!• primitive error monitoring!• no supervision!• no distribution

Page 138: F# in social gaming (CodeMash 15)

MS Orleans

Cricket

akka.Net

Page 139: F# in social gaming (CodeMash 15)
Page 140: F# in social gaming (CodeMash 15)

Async Workflow!• non-blocking I/O!• no callbacks

Page 141: F# in social gaming (CodeMash 15)

Pattern Matching!• clear & concise

Page 142: F# in social gaming (CodeMash 15)

Replacing a large class hierarchy

Page 143: F# in social gaming (CodeMash 15)
Page 144: F# in social gaming (CodeMash 15)

Caught a Gnome

Page 145: F# in social gaming (CodeMash 15)

EXP Item Gold

Quest Progress

Caught a Gnome

Page 146: F# in social gaming (CodeMash 15)

Level Up

Quest Progress

EXP Item Gold

Caught a Gnome

Quest Complete

Page 147: F# in social gaming (CodeMash 15)

Level Up

Quest Progress

EXP Item Gold

Caught a Gnome

New Quest

Quest Complete

Page 148: F# in social gaming (CodeMash 15)

Quest Progress

EXP Item Gold

Caught a Gnome

Quest Complete

New Quest

Level Up

Page 149: F# in social gaming (CodeMash 15)

Quest Progress

New QuestAchievement

Progress

EXP Item Gold

Quest CompleteLevel Up

Caught a Gnome

Page 150: F# in social gaming (CodeMash 15)

Level Up

Quest Progress

EXP Item Gold

Caught a Gnome

Quest Complete

New QuestAchievement

Progress

Achievement Complete

Page 151: F# in social gaming (CodeMash 15)

Level Up

Quest Progress

EXP Item Gold

Caught a Gnome

Quest Complete

New QuestAchievement

Progress

Achievement Complete

Page 152: F# in social gaming (CodeMash 15)

100+ actions

Page 153: F# in social gaming (CodeMash 15)

triggered by different abstraction layers

Page 154: F# in social gaming (CodeMash 15)

non-functional requirements

Page 155: F# in social gaming (CodeMash 15)

message-broker pattern

Page 156: F# in social gaming (CodeMash 15)

Caught Gnome Trapping

Queue

Levelling

Quests

Achievements

Analytics

Partner Reporting

Page 157: F# in social gaming (CodeMash 15)

Caught Gnome Trapping

Queue

Levelling

Quests

Achievements

Analytics

Partner Reporting

Ignore

Process

Process

Process

Process

Ignore

Page 158: F# in social gaming (CodeMash 15)

Caught Gnome Trapping

Queue

Levelling

Quests

Achievements

Analytics

Partner Reporting

EXPItemGold

Page 159: F# in social gaming (CodeMash 15)

Caught Gnome Trapping

Queue

Levelling

Quests

Achievements

Analytics

Partner Reporting

EXPItemGold

Page 160: F# in social gaming (CodeMash 15)

Caught Gnome Trapping

Queue

Levelling

Quests

Achievements

Analytics

Partner Reporting

EXPItemGold

Process

Ignore

Ignore

Ignore

Process

Ignore

Page 161: F# in social gaming (CodeMash 15)

Caught Gnome Trapping

Queue

Levelling

Quests

Achievements

Analytics

Partner Reporting

EXPItemGold

Level Up

Page 162: F# in social gaming (CodeMash 15)

Caught Gnome Trapping

Queue

Levelling

Quests

Achievements

Analytics

Partner Reporting

EXPItemGold

Level Up

Page 163: F# in social gaming (CodeMash 15)

need lots of facts

Page 164: F# in social gaming (CodeMash 15)

type Fact =! | GotExp! ! of Exp! | GotGold! ! of Gold! | GotItem! ! of Item * Count! | CaughtMonster! of Monster * Bait * Location! | LevelUp! ! of OldLevel * NewLevel! …

Page 165: F# in social gaming (CodeMash 15)

type Reward =! | GotExp! ! of Exp! | GotGold! ! of Gold! | GotItem! ! of Item * Count!!

type StateChange =! | LevelUp! ! of OldLevel * NewLevel! …

Page 166: F# in social gaming (CodeMash 15)

type Fact =! | StateChange! of StateChange! | Reward! ! of Reward! | Trapping! ! of Trapping! | Fishing! ! of Fishing! …

Page 167: F# in social gaming (CodeMash 15)

let process fact =! match fact with! | StateChange(stateChange)! -> …! | Reward(reward)! ! ! -> …! | Trapping(trapping)! ! -> …! …

Page 168: F# in social gaming (CodeMash 15)

C# interop

Page 169: F# in social gaming (CodeMash 15)

…!var fact = Fact.NewStateChange(!! StateChange.NewLevelUp(oldLvl, newLvl));!…

Page 170: F# in social gaming (CodeMash 15)

type IFact = interface end!!

type Reward =! | GotExp! ! of Exp! | GotGold! ! of Gold! | GotItem! ! of Item * Count! interface IFact

Page 171: F# in social gaming (CodeMash 15)

type IFact = interface end!!

type Reward =! | GotExp! ! of Exp! | GotGold! ! of Gold! | GotItem! ! of Item * Count! interface IFact

Page 172: F# in social gaming (CodeMash 15)

let process (fact : IFact) =! match fact with! | :? StateChange ! as stateChange!-> …! | :? Reward ! ! as reward! ! -> …! | :? Trapping! ! as trapping! -> …! …! | _ -> raise <| NotSupportedFact fact

Page 173: F# in social gaming (CodeMash 15)

let process (fact : IFact) =! match fact with! | :? StateChange ! as stateChange!-> …! | :? Reward ! ! as reward! ! -> …! | :? Trapping! ! as trapping! -> …! …! | _ -> raise <| NotSupportedFact fact

Page 174: F# in social gaming (CodeMash 15)

simple

Page 175: F# in social gaming (CodeMash 15)

flexible

Page 176: F# in social gaming (CodeMash 15)

extensible

Page 177: F# in social gaming (CodeMash 15)

saver

Page 178: F# in social gaming (CodeMash 15)

April 2015

Page 179: F# in social gaming (CodeMash 15)

Fri 8.30am, Salon H

@theburningmonk / theburningmonk.com

Page 180: F# in social gaming (CodeMash 15)

Fri 8.30am, Salon H

Fri 2.45pm, Guava + Tamarind

@theburningmonk / theburningmonk.com