0 people like it.

quest

  1: 
  2: 
  3: 
  4: 
  5: 
  6: 
  7: 
  8: 
  9: 
 10: 
 11: 
 12: 
 13: 
 14: 
 15: 
 16: 
 17: 
 18: 
 19: 
 20: 
 21: 
 22: 
 23: 
 24: 
 25: 
 26: 
 27: 
 28: 
 29: 
 30: 
 31: 
 32: 
 33: 
 34: 
 35: 
 36: 
 37: 
 38: 
 39: 
 40: 
 41: 
 42: 
 43: 
 44: 
 45: 
 46: 
 47: 
 48: 
 49: 
 50: 
 51: 
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: 
 59: 
 60: 
 61: 
 62: 
 63: 
 64: 
 65: 
 66: 
 67: 
 68: 
 69: 
 70: 
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: 
 78: 
 79: 
 80: 
 81: 
 82: 
 83: 
 84: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
100: 
101: 
102: 
103: 
104: 
105: 
106: 
107: 
108: 
109: 
110: 
111: 
open System
open System.Collections.Generic

// event handler
type Handler = (unit->unit)

// imitates dialog system
let Dialogs = new Dictionary<string*string, bool>()
let DialogHandlers = new Dictionary<string*string, Handler>()
// imitates killing events
let Kills = new Dictionary<string, int>()
let KillHandlers = new Dictionary<string, Handler>()

type Player() =
    member x.GiveXP n = 
        printfn "Gained %d experience points" n
    member x.StartQuest(quest) =
        printfn "Starting quest '%s'" quest
    member x.GiveReward(reward) =
        printfn "Rewarding player with '%s'" reward

    // simulating players actions
    member x.TalkTo(npc, node) =
        if Dialogs.[(npc, node)] then
            try DialogHandlers.[npc, node]() with | :? KeyNotFoundException -> printfn "No event is attached to the node"
        else
            printfn "Dialog node '%s' is not attached to npc '%s'" node npc
    member x.Kill(npc) =
        if not (Kills.ContainsKey(npc)) then Kills.[npc] <- 0
        Kills.[npc] <- Kills.[npc] + 1
        KillHandlers.[npc]()
    // simulating conditions
    member x.KillCount(npc) =
        try Kills.[npc] with | :? KeyNotFoundException -> 0

type Dialog(node, npc, at) = // for now we scrap 'at'. we don't care about structure yet
    do 
        if not (Dialogs.ContainsKey(npc, at)) then 
            printfn "Attaching dialog '%s' for npc '%s' at node '%s'" node npc at
            Dialogs.[(npc, node)] <- true

    member x.Disable() =
        printfn "Disabling dialog node '%s' for npc '%s'" node npc
        Dialogs.[(npc, node)] <- false

type Event =
    | Talk of Player * string * string // player talks with npc using given node
    | Kill of Player * string // player kills npc
type Quest = 
    | Phase of Event * Handler
    | Nothing

type QuestBuilder() =
    member x.Bind(v : Event, f) = 
        match v with
        | Talk(player, npc, node) -> 
            printfn "Player needs to talk with %s using node %s" npc node
            DialogHandlers.[(npc, node)] <- f // assign handler to dialog
            Talk(player, npc, node), f // or we could use that return type to do that?
        | Kill(player, npc) -> 
            if player.KillCount(npc) > 0 then
                printfn "Player already killed %s" npc
                // fire event immediately then
                f()
            else
                printfn "Player needs to kill %s" npc
                KillHandlers.[npc] <- f
            Kill(player, npc), f
        |> ignore
    member x.Bind(v : Event * Event, f) =
        match v with
            | e1, e2 ->
                x.Bind(e1, f)
                printfn "or"
                x.Bind(e2, f)

    member x.Zero() = 
        printfn "Zero"
        ()


let talk player npc node = Talk(player, npc, node)
let kill player npc = Kill(player, npc)
    
let quest = new QuestBuilder()

let beast = quest {
    let player = new Player()

    let intro = new Dialog("Beast", "Rudolf", "Hello") // let's attach dialog named "Beast", to the given npc at given node

    // this is the beginning, we require player to talk with an npc using specified dialog node
    do! talk player "Rudolf" "Beast", kill player "Beast"
    player.StartQuest("Beast") // and then, the rest will be called when player does this
    intro.Disable(); // we no longer want it to be accessible, instead of we will attach:
    let about = new Dialog("AboutThatBeast", "Rudolf", "Hello")

    do! kill player "Beast"
    if player.KillCount("Beast") > 2 then
        player.GiveXP(200)
        about.Disable()
        let reward = new Dialog("BeastReward", "Rudolf", "Hello")
        do! talk player "Rudolf" "BeastReward"
        reward.Disable();
        player.GiveReward("Awesome sword of Moonshinin'");
}

// let's simulate player
let player = new Player()
//player.TalkTo("Rudolf", "KillBeast")
//player.Kill("Beast")
namespace System
namespace System.Collections
namespace System.Collections.Generic
Multiple items
type Handler = unit -> unit

Full name: Script.Handler

--------------------
type Handler<'T> =
  delegate of obj * 'T -> unit

Full name: Microsoft.FSharp.Control.Handler<_>
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val Dialogs : Dictionary<(string * string),bool>

Full name: Script.Dialogs
Multiple items
type Dictionary<'TKey,'TValue> =
  new : unit -> Dictionary<'TKey, 'TValue> + 5 overloads
  member Add : key:'TKey * value:'TValue -> unit
  member Clear : unit -> unit
  member Comparer : IEqualityComparer<'TKey>
  member ContainsKey : key:'TKey -> bool
  member ContainsValue : value:'TValue -> bool
  member Count : int
  member GetEnumerator : unit -> Enumerator<'TKey, 'TValue>
  member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
  member Item : 'TKey -> 'TValue with get, set
  ...
  nested type Enumerator
  nested type KeyCollection
  nested type ValueCollection

Full name: System.Collections.Generic.Dictionary<_,_>

--------------------
Dictionary() : unit
Dictionary(capacity: int) : unit
Dictionary(comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : unit
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : unit
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
type bool = Boolean

Full name: Microsoft.FSharp.Core.bool
val DialogHandlers : Dictionary<(string * string),Handler>

Full name: Script.DialogHandlers
val Kills : Dictionary<string,int>

Full name: Script.Kills
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val KillHandlers : Dictionary<string,Handler>

Full name: Script.KillHandlers
Multiple items
type Player =
  new : unit -> Player
  member GiveReward : reward:string -> unit
  member GiveXP : n:int -> unit
  member Kill : npc:string -> unit
  member KillCount : npc:string -> int
  member StartQuest : quest:string -> unit
  member TalkTo : npc:string * node:string -> unit

Full name: Script.Player

--------------------
new : unit -> Player
val x : Player
member Player.GiveXP : n:int -> unit

Full name: Script.Player.GiveXP
val n : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
member Player.StartQuest : quest:string -> unit

Full name: Script.Player.StartQuest
val quest : string
member Player.GiveReward : reward:string -> unit

Full name: Script.Player.GiveReward
val reward : string
member Player.TalkTo : npc:string * node:string -> unit

Full name: Script.Player.TalkTo
val npc : string
val node : string
Multiple items
type KeyNotFoundException =
  inherit SystemException
  new : unit -> KeyNotFoundException + 2 overloads

Full name: System.Collections.Generic.KeyNotFoundException

--------------------
KeyNotFoundException() : unit
KeyNotFoundException(message: string) : unit
KeyNotFoundException(message: string, innerException: exn) : unit
member Player.Kill : npc:string -> unit

Full name: Script.Player.Kill
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
Dictionary.ContainsKey(key: string) : bool
member Player.KillCount : npc:string -> int

Full name: Script.Player.KillCount
Multiple items
type Dialog =
  new : node:string * npc:string * at:string -> Dialog
  member Disable : unit -> unit

Full name: Script.Dialog

--------------------
new : node:string * npc:string * at:string -> Dialog
val at : string
Dictionary.ContainsKey(key: string * string) : bool
val x : Dialog
member Dialog.Disable : unit -> unit

Full name: Script.Dialog.Disable
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event =
  | Talk of Player * string * string
  | Kill of Player * string

Full name: Script.Event

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
union case Event.Talk: Player * string * string -> Event
union case Event.Kill: Player * string -> Event
type Quest =
  | Phase of Event * Handler
  | Nothing

Full name: Script.Quest
union case Quest.Phase: Event * Handler -> Quest
union case Quest.Nothing: Quest
Multiple items
type QuestBuilder =
  new : unit -> QuestBuilder
  member Bind : v:Event * f:Handler -> unit
  member Bind : v:(Event * Event) * f:Handler -> unit
  member Zero : unit -> unit

Full name: Script.QuestBuilder

--------------------
new : unit -> QuestBuilder
val x : QuestBuilder
member QuestBuilder.Bind : v:Event * f:Handler -> unit

Full name: Script.QuestBuilder.Bind
val v : Event
val f : Handler
val player : Player
member Player.KillCount : npc:string -> int
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
member QuestBuilder.Bind : v:(Event * Event) * f:Handler -> unit

Full name: Script.QuestBuilder.Bind
val v : Event * Event
val e1 : Event
val e2 : Event
member QuestBuilder.Bind : v:Event * f:Handler -> unit
member QuestBuilder.Bind : v:(Event * Event) * f:Handler -> unit
member QuestBuilder.Zero : unit -> unit

Full name: Script.QuestBuilder.Zero
val talk : player:Player -> npc:string -> node:string -> Event

Full name: Script.talk
val kill : player:Player -> npc:string -> Event

Full name: Script.kill
val quest : QuestBuilder

Full name: Script.quest
val beast : unit

Full name: Script.beast
val intro : Dialog
member Player.StartQuest : quest:string -> unit
member Dialog.Disable : unit -> unit
val about : Dialog
member Player.GiveXP : n:int -> unit
val reward : Dialog
member Player.GiveReward : reward:string -> unit
val player : Player

Full name: Script.player
Raw view Test code New version

More information

Link:http://fssnip.net/5b
Posted:15 years ago
Author:
Tags: