Refactoring data model to new stuff

This commit is contained in:
Joseph Ferano 2022-02-16 00:36:29 +07:00
parent 9f7b1956b4
commit c1c51dcd99
4 changed files with 54 additions and 61 deletions

View File

@ -17,7 +17,8 @@ module Commands =
Arsenal = [| hack ; shield |] Arsenal = [| hack ; shield |]
Actions = [||] Actions = [||]
XP = 0 XP = 0
Stats = PlayerStats.empty Achievements = [||]
Traits = PlayerTraits.empty
Bank = 100<GBT> } Bank = 100<GBT> }
let addHackerRole (ctx : InteractionContext) = let addHackerRole (ctx : InteractionContext) =

View File

@ -133,7 +133,7 @@ let steal target amount (ctx : IDiscordContext) =
|> checkVictimStealingCooldown defender |> checkVictimStealingCooldown defender
>>= checkThiefCooldown >>= checkThiefCooldown
|> handleResultWithResponse ctx (fun _ -> async { |> handleResultWithResponse ctx (fun _ -> async {
let cappedPrize , winPercentage = calculateWinPercentage amount (int defender.Bank) attacker.Stats.Strength defender.Stats.Strength let cappedPrize , winPercentage = calculateWinPercentage amount (int defender.Bank) attacker.Traits.Strength defender.Traits.Strength
let embed = getStealEmbed winPercentage cappedPrize defender let embed = getStealEmbed winPercentage cappedPrize defender
do! ctx.FollowUp(embed) |> Async.AwaitTask do! ctx.FollowUp(embed) |> Async.AwaitTask

View File

@ -23,10 +23,7 @@ type DefenseAction =
type PlayerEntry = type PlayerEntry =
{ DiscordId : uint64 { DiscordId : uint64
Name : string Name : string
Arsenal : int array XP : int
Attacks : AttackAction array
Defenses : DefenseAction array
// XP : int
Bank : int } Bank : int }
let private actionToAttack (action : Action) (hack : AttackResult) = let private actionToAttack (action : Action) (hack : AttackResult) =
@ -52,46 +49,30 @@ let private defenseToAction (action : DefenseAction) =
let private playerMap (player : PlayerData) = { let private playerMap (player : PlayerData) = {
DiscordId = player.DiscordId DiscordId = player.DiscordId
Name = player.Name Name = player.Name
Arsenal = player.Arsenal |> Array.map (fun w -> w.Id) XP = player.XP
Attacks = player.Actions
|> Array.choose (fun a -> match a.Type with Attack ar -> Some (actionToAttack a ar) | _ -> None)
Defenses = player.Actions
|> Array.choose (fun a -> match a.Type with Defense -> Some (actionToDefense a) | _ -> None)
Bank = int player.Bank Bank = int player.Bank
} }
let private mapBack (player : PlayerEntry) : PlayerData = { let tryWithDefault (bson : BsonDocument) field (defaultValue : 'a) (map : BsonValue -> 'a) =
DiscordId = player.DiscordId let result , bval = bson.TryGetValue(field)
Name = player.Name if result then map bval else defaultValue
Arsenal = player.Arsenal |> Array.map (fun w -> Armory.battleItems |> Array.find (fun w' -> w = w'.Id))
Actions = let private mapBack (bson : BsonDocument) : PlayerData =
let atks = player.Attacks |> Array.map attackToAction { DiscordId = tryWithDefault bson "Player.DiscordId" 0uL (fun v -> v.AsInt64 |> uint64)
let dfns = player.Defenses |> Array.map defenseToAction Name = tryWithDefault bson "Player.Name" "Empty" (fun v -> v.AsString)
Array.append atks dfns Arsenal =
Stats = PlayerStats.empty tryWithDefault bson "Inventory" [||] (fun v ->
XP = 0 v.AsBsonArray
Bank = player.Bank * 1<GBT> |> Seq.map (fun (bv : BsonValue) -> bv.AsInt32)
|> Seq.map (fun w -> Armory.battleItems |> Array.find (fun w' -> w = w'.Id))
|> Seq.toArray)
Actions = tryWithDefault bson "Events" [||] (fun _ -> [||])
Traits = tryWithDefault bson "Traits" PlayerTraits.empty (fun _ -> PlayerTraits.empty)
Achievements = tryWithDefault bson "Achievements" [||] (fun _ -> [||])
XP = tryWithDefault bson "XP" 0 (fun _ -> 0)
Bank = tryWithDefault bson "Player.Bank" 0<GBT> (fun v -> v.AsInt32 * 1<GBT>)
} }
//let private mapBack (bson : BsonDocument) : PlayerData = {
// DiscordId = bson.GetValue("Player.DiscordId").AsInt64 |> uint64
// Name = bson.GetValue("Player.Name").AsString
// Arsenal =
// bson.GetValue("Inventory").AsBsonArray
// |> Seq.map (fun (bv : BsonValue) -> bv.AsInt32)
// |> Seq.map (fun w -> Armory.battleItems |> Array.find (fun w' -> w = w'.Id))
// |> Seq.toArray
// Actions =
// let atks = player.Attacks |> Array.map attackToAction
// let dfns = player.Defenses |> Array.map defenseToAction
// Array.append atks dfns
// Stats = PlayerStats.empty
// XP = 0
// Bank = player.Bank * 1<GBT>
//}
//
let mongo = MongoClient(Environment.GetEnvironmentVariable("CONN_STRING")) let mongo = MongoClient(Environment.GetEnvironmentVariable("CONN_STRING"))
let db = mongo.GetDatabase("degenz") let db = mongo.GetDatabase("degenz")
let players = db.GetCollection<BsonDocument>("players") let players = db.GetCollection<BsonDocument>("players")
@ -105,11 +86,22 @@ let tryFindPlayer (id : uint64) =
| p -> return p | p -> return p
.GetValue("Player") .GetValue("Player")
.ToBsonDocument() .ToBsonDocument()
|> BsonSerializer.Deserialize<PlayerEntry>
|> mapBack |> mapBack
|> Some |> Some
} }
let updatePlayer (player : PlayerData) =
async {
let filter = Builders<BsonDocument>.Filter.Eq("Player.DiscordId", player.DiscordId)
let update = Builders<BsonDocument>.Update
.Set("Player", playerMap player)
.AddToSet("Traits", player.Traits)
.AddToSet("Events", player.Actions)
.AddToSet("Achievements", player.Achievements)
.AddToSet("Inventory", player.Arsenal)
return! players.UpdateOneAsync(filter, update) |> Async.AwaitTask |> Async.Ignore
}
let getAchievements (id : uint64) = let getAchievements (id : uint64) =
async { async {
let filter = Builders<BsonDocument>.Filter.Eq("Player.DiscordId", id) let filter = Builders<BsonDocument>.Filter.Eq("Player.DiscordId", id)
@ -128,14 +120,19 @@ let getAchievements (id : uint64) =
let addAchievement (id : uint64) (achievement : string) = let addAchievement (id : uint64) (achievement : string) =
async { async {
let filter = Builders<BsonDocument>.Filter.Eq("Player.DiscordId", id) let filter = Builders<BsonDocument>.Filter.Eq("Player.DiscordId", id)
let update = Builders<BsonDocument>.Update.Push("achievements", achievement) let update = Builders<BsonDocument>.Update.Push("Achievements", achievement)
return! players.UpdateOneAsync(filter, update) |> Async.AwaitTask |> Async.Ignore return! players.UpdateOneAsync(filter, update) |> Async.AwaitTask |> Async.Ignore
} }
let insertNewPlayer (player : PlayerData) = let insertNewPlayer (player : PlayerData) =
async { async {
let p = playerMap player let dict = [
let dict = [ KeyValuePair("Player" , p.ToBsonDocument() :> Object) ] KeyValuePair("Player" , (playerMap player).ToBsonDocument() :> Object)
KeyValuePair("Events" , [||] :> Object)
KeyValuePair("Inventory" , [||] :> Object)
KeyValuePair("Traits" , PlayerTraits.empty.ToBsonDocument() :> Object)
KeyValuePair("XP" , 0 :> Object)
]
do! BsonDocument(dict) do! BsonDocument(dict)
|> players.InsertOneAsync |> players.InsertOneAsync
|> Async.AwaitTask |> Async.AwaitTask
@ -149,13 +146,6 @@ let insertNewPlayer (player : PlayerData) =
// |> Async.AwaitTask // |> Async.AwaitTask
// } // }
// //
let updatePlayer (player : PlayerData) =
async {
let filter = Builders<BsonDocument>.Filter.Eq("Player.DiscordId", player.DiscordId)
let update = Builders<BsonDocument>.Update.Set("Player", playerMap player)
return! players.UpdateOneAsync(filter, update) |> Async.AwaitTask |> Async.Ignore
}
//let getTopPlayers amount = //let getTopPlayers amount =
// async { // async {
// return! players.FindAsync() // return! players.FindAsync()

View File

@ -95,7 +95,7 @@ module Types =
| Strength = 0 | Strength = 0
| Cunning = 1 | Cunning = 1
type PlayerStats = { type PlayerTraits = {
Strength : int Strength : int
Focus : int Focus : int
} }
@ -106,14 +106,16 @@ module Types =
} }
[<CLIMutable>] [<CLIMutable>]
type PlayerData = type PlayerData = {
{ DiscordId : uint64 DiscordId : uint64
Name : string Name : string
Arsenal : BattleItem array Arsenal : BattleItem array
Actions : Action array Actions : Action array
Stats : PlayerStats Traits : PlayerTraits
XP : int Achievements : string array
Bank : int<GBT> } XP : int
Bank : int<GBT>
}
module Armory = module Armory =
let battleItems = let battleItems =