-
Notifications
You must be signed in to change notification settings - Fork 0
/
Db.nim
104 lines (87 loc) · 2.82 KB
/
Db.nim
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
import random
type
Want* = enum
Buy, Sell
ItemType* = enum
Sword, Bow, Knife, Axe, Ore, Potion, HeavyArmor, LightArmor
# Информация о создаваемом предмете
ItemInfo* = tuple
name: string
# itemType:ItemType
costMin: int
costMax: int
Item* = tuple
name: string
itemType: ItemType
cost: int
ClassType = tuple
name: string
itemsType: seq[ItemType]
Client = tuple
classType: ClassType
want: Want
item: Item
var
classes: seq[ClassType]
db_items* = [
ItemType.Sword: (name:"Меч", costMin:5, costMax:15),
ItemType.Bow: (name:"Лук", costMin:8, costMax:18),
ItemType.Knife: (name:"Нож", costMin:3, costMax:13),
ItemType.Axe: (name:"Топор", costMin:5, costMax:17),
ItemType.Ore: (name:"Железная руда", costMin:3, costMax:20),
ItemType.Potion: (name:"Зелье здоровья", costMin:3, costMax:10),
ItemType.HeavyArmor:(name:"Тяжелые доспехи", costMin:10, costMax:20),
ItemType.LightArmor:(name:"Легкие доспехи", costMin:5, costMax:18)]
classes = @[
(
"Воин",
@[ItemType.Sword, ItemType.Knife, ItemType.Potion, ItemType.HeavyArmor]
),
(
"Лучник",
@[ItemType.Bow, ItemType.Knife, ItemType.Potion, ItemType.LightArmor]
),
(
"Кузнец",
@[ItemType.Sword, ItemType.Ore, ItemType.HeavyArmor, ItemType.LightArmor]
),
(
"Берсерк",
@[ItemType.Axe, ItemType.HeavyArmor]
)
]
# classes.add(("Воин", @[ItemType.Sword, ItemType.Knife]))
# classes.add(("Лучник", @[ItemType.Bow, ItemType.Knife]))
proc createItem(itemInfo:ItemInfo, itemType:ItemType):Item =
var r = rand(itemInfo.costMin..itemInfo.costMax)
result.itemType = itemType
result.name = itemInfo.name
result.cost = r
randomize()
proc randWant(): Want =
var r = rand(1)
case r:
of 0: return Want.Buy
of 1: return Want.Sell
else: discard
proc getRandomClient*(): Client =
# Выбираем случайный класс для клиента
assert(classes.len != 0, "Error: Classes not found!")
var r = rand(high(classes))
let class: ClassType = classes[r]
# Выбираем случайный предмет, который клиент будет покупать или продавать
assert(class.itemsType.len != 0, "Error: Class haven't db_items type")
r = rand(high(class.itemsType))
let itemType = class.itemsType[r]
let item: Item = createItem(db_items[itemType], itemType)
# Случайно выбираем, хочет ли клиент купить или продать
let want: Want = randWant()
# "Собираем" клиента
result.want = want
result.classType = class
result.item = item
when isMainModule:
echo classes
echo getRandomClient()
echo getRandomClient()
echo getRandomClient()