@@ -20,7 +20,7 @@ export class Engine<WINS extends string, LOSES extends string> {
20
20
this . m_wins_and_loses = [ ...config . wins , ...config . loses ] ;
21
21
}
22
22
23
- private getPercentage ( src : { [ _ in WINS | LOSES ] : number } ) : { [ _ in WINS | LOSES ] : number } {
23
+ private getPercentage ( src : { [ _ in WINS | LOSES ] : number ; } ) : { [ _ in WINS | LOSES ] : number ; } {
24
24
const total = this . getTotal ( src ) ;
25
25
const result = { ...src } ;
26
26
this . m_wins_and_loses . forEach ( ( key ) => {
@@ -29,12 +29,12 @@ export class Engine<WINS extends string, LOSES extends string> {
29
29
return result ;
30
30
} ;
31
31
32
- private getTotal ( src : { [ _ in WINS | LOSES ] : number } ) : number {
32
+ private getTotal ( src : { [ _ in WINS | LOSES ] : number ; } ) : number {
33
33
return this . m_config . wins . reduce ( ( acc , key ) => acc + src [ key ] , 0 ) +
34
34
this . m_config . loses . reduce ( ( acc , key ) => acc + src [ key ] , 0 ) ;
35
35
}
36
36
37
- private getPartialPercentage ( src : { [ _ in WINS | LOSES ] ?: number } ) : { [ _ in WINS | LOSES ] ?: number } {
37
+ private getPartialPercentage ( src : { [ _ in WINS | LOSES ] ?: number ; } ) : { [ _ in WINS | LOSES ] ?: number ; } {
38
38
const total = this . getPartialTotal ( src ) ;
39
39
const result = { ...src } ;
40
40
this . m_wins_and_loses . forEach ( ( key ) => {
@@ -46,90 +46,98 @@ export class Engine<WINS extends string, LOSES extends string> {
46
46
return result ;
47
47
} ;
48
48
49
- private getPartialTotal ( src : { [ _ in WINS | LOSES ] ?: number } ) : number {
49
+ private getPartialTotal ( src : { [ _ in WINS | LOSES ] ?: number ; } ) : number {
50
50
return this . m_wins_and_loses . reduce ( ( acc , key ) => {
51
51
const value = src [ key ] ;
52
52
return value !== undefined ? acc + value : acc ;
53
53
} , 0 )
54
54
}
55
55
56
- execute ( ) : WINS | LOSES {
57
- const ideal_percentage = this . getPercentage ( this . m_config . ratio ) ;
58
- // console.log("ideal_percentage", ideal_percentage);
59
-
60
- const past_data = this . m_data_repository . getPastResultHistogram ( this . getTotal ( this . m_config . ratio ) * 100 ) ;
61
- // console.log("past_data", past_data);
62
-
63
- const offset_past_data = ( ( ) => {
64
- const data = { ...past_data } ;
65
- const gain = this . m_config . gain ;
66
- const ratio = this . m_config . ratio ;
67
- this . m_wins_and_loses . forEach ( ( key ) => {
68
- data [ key ] += ratio [ key ] * gain ;
56
+ public execute ( ) : Promise < WINS | LOSES > {
57
+ return this . m_data_repository . getPastResultHistogram ( this . getTotal ( this . m_config . ratio ) * 100 )
58
+ . then ( ( past_data ) => {
59
+ return this . m_data_repository . getStocks ( )
60
+ . then ( ( stocks ) => {
61
+ return { past_data, stocks} ;
69
62
} ) ;
70
- return data ;
71
- } ) ( ) ;
72
- // console.log("offset_past_data", offset_past_data);
73
-
74
- const past_percentage = this . getPercentage ( offset_past_data ) ;
75
- // console.log("past_percentage", past_percentage);
76
-
77
- const availables = ( ( ) => {
78
- const result : { [ _ in WINS | LOSES ] ?: number } = { } ;
79
- const keys : ( WINS | LOSES ) [ ] = [ ] ;
80
- const stocks = this . m_data_repository . getStocks ( ) ;
81
- this . m_wins_and_loses . forEach ( ( key ) => {
82
- if ( past_percentage [ key ] < ideal_percentage [ key ] ) {
83
- if ( this . isWins ( key ) ) {
84
- if ( stocks [ key ] > 0 ) {
63
+ } )
64
+ . then ( ( { past_data, stocks} ) => {
65
+ // console.log("past_data", past_data);
66
+ // console.log("stocks", stocks);
67
+
68
+ const ideal_percentage = this . getPercentage ( this . m_config . ratio ) ;
69
+ // console.log("ideal_percentage", ideal_percentage);
70
+
71
+ const offset_past_data = ( ( ) => {
72
+ const data = { ...past_data } ;
73
+ const gain = this . m_config . gain ;
74
+ const ratio = this . m_config . ratio ;
75
+ this . m_wins_and_loses . forEach ( ( key ) => {
76
+ data [ key ] += ratio [ key ] * gain ;
77
+ } ) ;
78
+ return data ;
79
+ } ) ( ) ;
80
+ // console.log("offset_past_data", offset_past_data);
81
+
82
+ const past_percentage = this . getPercentage ( offset_past_data ) ;
83
+ // console.log("past_percentage", past_percentage);
84
+
85
+ const availables = ( ( ) => {
86
+ const result : { [ _ in WINS | LOSES ] ?: number ; } = { } ;
87
+ const keys : ( WINS | LOSES ) [ ] = [ ] ;
88
+ this . m_wins_and_loses . forEach ( ( key ) => {
89
+ if ( past_percentage [ key ] < ideal_percentage [ key ] ) {
90
+ if ( this . isWins ( key ) ) {
91
+ if ( stocks [ key ] > 0 ) {
92
+ keys . push ( key ) ;
93
+ }
94
+ }
95
+ else {
85
96
keys . push ( key ) ;
86
97
}
87
98
}
88
- else {
89
- keys . push ( key ) ;
90
- }
99
+ } ) ;
100
+
101
+ if ( keys . length === 0 ) {
102
+ this . m_wins_and_loses . forEach ( ( key ) => {
103
+ result [ key ] = this . m_config . ratio [ key ] ;
104
+ } ) ;
91
105
}
92
- } ) ;
93
-
94
- if ( keys . length === 0 ) {
106
+ else {
107
+ keys . forEach ( ( key ) => {
108
+ result [ key ] = this . m_config . ratio [ key ] ;
109
+ } ) ;
110
+ }
111
+ return result ;
112
+ } ) ( ) ;
113
+ // console.log("availables", availables);
114
+
115
+ const available_percentage = this . getPartialPercentage ( availables ) ;
116
+ // console.log("available_percentage", available_percentage);
117
+
118
+ const gacha_result = ( ( ) : WINS | LOSES | undefined => {
119
+ let random = Math . random ( ) ;
120
+ let result : WINS | LOSES | undefined = undefined ;
95
121
this . m_wins_and_loses . forEach ( ( key ) => {
96
- result [ key ] = this . m_config . ratio [ key ] ;
122
+ if ( result !== undefined ) return ;
123
+ const value = available_percentage [ key ] ;
124
+ if ( value !== undefined ) {
125
+ random -= value ;
126
+ if ( random <= 0 ) {
127
+ result = key ;
128
+ }
129
+ }
97
130
} ) ;
131
+ return result ;
132
+ } ) ( ) ;
133
+
134
+ if ( gacha_result === undefined ) {
135
+ throw new Error ( "logic error" ) ;
98
136
}
99
137
else {
100
- keys . forEach ( ( key ) => {
101
- result [ key ] = this . m_config . ratio [ key ] ;
102
- } ) ;
138
+ return gacha_result ;
103
139
}
104
- return result ;
105
- } ) ( ) ;
106
- // console.log("availables", availables);
107
-
108
- const available_percentage = this . getPartialPercentage ( availables ) ;
109
- // console.log("available_percentage", available_percentage);
110
-
111
- const gacha_result = ( ( ) : WINS | LOSES | undefined => {
112
- let random = Math . random ( ) ;
113
- let result : WINS | LOSES | undefined = undefined ;
114
- this . m_wins_and_loses . forEach ( ( key ) => {
115
- if ( result !== undefined ) return ;
116
- const value = available_percentage [ key ] ;
117
- if ( value !== undefined ) {
118
- random -= value ;
119
- if ( random <= 0 ) {
120
- result = key ;
121
- }
122
- }
123
- } ) ;
124
- return result ;
125
- } ) ( ) ;
126
-
127
- if ( gacha_result === undefined ) {
128
- throw new Error ( "logic error" ) ;
129
- }
130
- else {
131
- return gacha_result ;
132
- }
140
+ } ) ;
133
141
}
134
142
}
135
143
0 commit comments