-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtypes_functions.go
102 lines (87 loc) · 2.39 KB
/
types_functions.go
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
package dicescript
import (
"errors"
"sort"
)
func (d *VMDictValue) V() *VMValue {
return (*VMValue)(d)
}
func (d *VMDictValue) Store(key string, value *VMValue) {
if dd, ok := d.V().ReadDictData(); ok {
dd.Dict.Store(key, value)
}
}
func (d *VMDictValue) Range(callback func(key string, value *VMValue) bool) {
if dd, ok := d.V().ReadDictData(); ok {
dd.Dict.Range(callback)
}
}
// Load value为变量的值,ok代表是否找到变量
func (d *VMDictValue) Load(key string) (value *VMValue, ok bool) {
if dd, ok := d.V().ReadDictData(); ok {
return dd.Dict.Load(key)
}
return nil, false
}
func (d *VMDictValue) ToString() string {
return d.V().ToString()
}
func (v *VMValue) ArrayItemGet(ctx *Context, index IntType) *VMValue {
if v.TypeId == VMTypeArray {
arr, _ := v.ReadArray()
index = getRealIndex(ctx, index, IntType(len(arr.List)))
if ctx.Error != nil {
return nil
}
return arr.List[index]
}
ctx.Error = errors.New("此类型无法取下标")
return nil
}
func (v *VMValue) ArrayItemSet(ctx *Context, index IntType, val *VMValue) bool {
if v.TypeId == VMTypeArray {
arr, _ := v.ReadArray()
index = getRealIndex(ctx, index, IntType(len(arr.List)))
if ctx.Error != nil {
return false
}
arr.List[index] = val.Clone()
return true
}
ctx.Error = errors.New("此类型无法赋值下标")
return false
}
func (v *VMValue) ArrayFuncKeepBase(ctx *Context, pickNum IntType, orderType int) (isAllInt bool, ret float64) {
arr, _ := v.ReadArray()
var nums []float64
isAllInt = true
for _, i := range arr.List {
switch i.TypeId {
case VMTypeInt:
nums = append(nums, float64(i.MustReadInt()))
case VMTypeFloat:
isAllInt = false
nums = append(nums, i.MustReadFloat())
}
}
if orderType == 0 {
sort.Slice(nums, func(i, j int) bool { return nums[i] > nums[j] }) // 从大到小
} else if orderType == 1 {
sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] }) // 从小到大
}
num := float64(0)
for i := IntType(0); i < pickNum; i++ {
// 当取数大于上限 跳过
if i >= IntType(len(nums)) {
continue
}
num += nums[i]
}
return isAllInt, num
}
func (v *VMValue) ArrayFuncKeepHigh(ctx *Context, pickNum IntType) (isAllInt bool, ret float64) {
return v.ArrayFuncKeepBase(ctx, pickNum, 0)
}
func (v *VMValue) ArrayFuncKeepLow(ctx *Context, pickNum IntType) (isAllInt bool, ret float64) {
return v.ArrayFuncKeepBase(ctx, pickNum, 1)
}