-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityList.js
102 lines (79 loc) · 1.76 KB
/
PriorityList.js
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
export {PriorityList, Action };
class Action
{
name;
priority;
base;
increment;
constructor(name="", priority=0, base=0, increment=0) {
this.name = name;
this.priority = priority;
this.base = base;
this.increment = increment;
}
incrementPriority(){
this.priority += this.increment;
}
resetPriority(){
this.priority = this.base;
}
serialize()
{
return JSON.stringify({
name : this.name,
priority : this.priority,
base : this.base,
increment : this.increment
});
}
static deserialize(obj)
{
return new Action(obj.name, obj.priority, obj.base, obj.increment);
}
}
class PriorityList
{
AIDict = [];
constructor(actions){
this.AIDict = actions;
}
addPriority()
{
for (const action of this.AIDict)
action.incrementPriority();
this.sortPriority();
}
resetPriority(i=0){
this.AIDict[i].resetPriority();
this.sortPriority();
}
sortPriority(){
this.AIDict.sort((a, b) => b.priority - a.priority);
}
getPriorityAction()
{
return this.AIDict[0];
}
serialize()
{
let str = '[';
for (let i = 0; i < this.AIDict.length; i++)
{
const action = this.AIDict[i];
str += c.serialize();
if (i != this.AIDict.length-1)
str += ', ';
}
str += ']';
return str;
}
static deserialize(obj)
{
let actions = [];
for (const action of obj)
{
actions.push(Action.deserialize(action));
}
return new PriorityList(actions);
}
}