diff options
Diffstat (limited to 'src/LivingVillage.Desktop/PlayerTrade.fs')
| -rw-r--r-- | src/LivingVillage.Desktop/PlayerTrade.fs | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/src/LivingVillage.Desktop/PlayerTrade.fs b/src/LivingVillage.Desktop/PlayerTrade.fs index e1ea53e..540445d 100644 --- a/src/LivingVillage.Desktop/PlayerTrade.fs +++ b/src/LivingVillage.Desktop/PlayerTrade.fs @@ -100,25 +100,22 @@ module PlayerTrade = let private replaceNpc (npc: Npc) (world: World) : World = { world with Npcs = world.Npcs |> Array.map (fun current -> if current.Id = npc.Id then npc else current) } - let private recordTrade - (buyer: NpcId) - (seller: NpcId) - (item: ItemKind) - (quantity: int) - (unitPrice: float32) - (world: World) - : World = - let event = - { Tick = world.Tick - Kind = TradeEvent(buyer, seller, item, quantity, unitPrice) } + let private recordTrade (event: InteractionEvent) (world: World) : World = let entry = { Tick = world.Tick Kind = TradeAnnal event Summary = - sprintf "trade tick=%d buyer=%A seller=%A item=%A quantity=%d price=%.2f" - world.Tick buyer seller item quantity unitPrice } + match event.Kind with + | TradeEvent(buyer, seller, item, quantity, unitPrice) -> + sprintf "trade tick=%d buyer=%A seller=%A item=%A quantity=%d price=%.2f" + world.Tick buyer seller item quantity unitPrice + | _ -> sprintf "trade tick=%d" world.Tick } Sim.appendAnnal entry { world with Events = world.Events @ [ event ] } + /// P54:交易事件同时推进当日任务(Offered 须先接受;判定走 Kernel 纯函数)。 + let private advanceOccupation (event: InteractionEvent) (occupation: Occupation.State) : Occupation.State = + Occupation.advanceTask event occupation + let private failure (state: Occupation.State option) (world: World) (message: string) : Outcome = { World = world; Occupation = state; Succeeded = false; UnitPrice = None; Message = message } @@ -158,13 +155,19 @@ module PlayerTrade = { seller.Mind with Needs = { seller.Mind.Needs with Money = seller.Mind.Needs.Money + total } Memory = recordMemory world.Tick (Sold(playerId, item, quantity, unitPrice)) seller.Mind.Memory } } + let tradeEvent = + { Tick = world.Tick + Kind = TradeEvent(playerId, counterparty, item, quantity, unitPrice) } let nextWorld = world |> (fun w -> { w with Avatar = { w.Avatar with Mind = avatarMind } }) |> replaceNpc updatedSeller - |> recordTrade playerId counterparty item quantity unitPrice + |> recordTrade tradeEvent + let nextOccupation = + { occupation with Backpack = changeBackpack item quantity occupation.Backpack } + |> advanceOccupation tradeEvent { World = nextWorld - Occupation = Some { occupation with Backpack = changeBackpack item quantity occupation.Backpack } + Occupation = Some nextOccupation Succeeded = true UnitPrice = Some unitPrice Message = sprintf "买入%s ×%d,单价 %.2f" (itemName item) quantity unitPrice } @@ -205,13 +208,19 @@ module PlayerTrade = { buyer.Mind with Needs = { buyer.Mind.Needs with Money = buyer.Mind.Needs.Money - total } Memory = recordMemory world.Tick (Bought(playerId, item, quantity, unitPrice)) buyer.Mind.Memory } } + let tradeEvent = + { Tick = world.Tick + Kind = TradeEvent(counterparty, playerId, item, quantity, unitPrice) } let nextWorld = world |> (fun w -> { w with Avatar = { w.Avatar with Mind = avatarMind } }) |> replaceNpc updatedBuyer - |> recordTrade counterparty playerId item quantity unitPrice + |> recordTrade tradeEvent + let nextOccupation = + { occupation with Backpack = changeBackpack item -quantity occupation.Backpack } + |> advanceOccupation tradeEvent { World = nextWorld - Occupation = Some { occupation with Backpack = changeBackpack item -quantity occupation.Backpack } + Occupation = Some nextOccupation Succeeded = true UnitPrice = Some unitPrice Message = sprintf "卖出%s ×%d,单价 %.2f" (itemName item) quantity unitPrice } |
