-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat_engine.as
257 lines (219 loc) · 8.91 KB
/
stat_engine.as
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
class stat_engine{
static function test(){
var o:Object = new Object();
var en:Object = new Object();
en.name = 'ðûöà ñîñà';
o.name = 'unit';
unit_stat.setName(o, undefined, undefined, '÷åëîâåê', 'Ì', 25);
A(o, 'ñèëà', 12);
A(o, 'ïðîâîðíîñòü', new Array(4, 5,6));
A(o, 'ðàíà', new Array(2, 4, 8), 'ñîñòîÿíèå-áîëåçíü', undefined, en);
A(o, 'ðàíà', new Array(3, 1, 16), 'ñîñòîÿíèå-áîëåçíü', undefined, statWithName(o, 'ðàíà', 0));
trace(watchUniqueStatValueParam(o, 'ïðîâîðíîñòü', 2));
listStats(o)
//òåëîñëîæåíèå
//trace(getStatValue(o, 'òåëîñëîæåíèå'));
changeUniqueStatValue(o, 'ñèëà', getStatValue(o, 'òåëîñëîæåíèå')[0]+2);
//changeUniqueStatValue(o, 'ïðîâîðíîñòü', 10,1);
//changeStatValue(o, 'ïðîâîðíîñòü', undefined, 12,1);
//changeStatValue(o, 'ðàíà', undefined, 3);
//changeStatValue(o, 'ðàíà', 3, 2);
}
static function createStat(
name:String,
value,
category:String,
dateTime:Number,
source:Object
):Object{
var nStat:Object = new Object();
nStat.name = name;
if (value == undefined) nStat.value = new Array(); else {
var isArray = (typeof(value) +'')== 'object'; // string + 5 == isNaN
if (isArray)
nStat.value = value;
else{
nStat.value = new Array(); nStat.value.push(value);// add a {value1}, non valu1
}
}
if (category == undefined) nStat.category = 'ñòàò'; else nStat.category = category;
nStat.dateTime = dateTime;
nStat.source = source;
nStat.isStat = true;
return nStat;
}
static function statToString(stat:Object):String{
var res:String = "";
if (stat.category != undefined) res += stat.category+"-";
res += stat.name + ": {" + stat.value + "}";
if (stat.source != undefined) res += " îò " + ((stat.source.isStat == true)? (stat.source.host.name+":"):"") +(stat.source.name);
if (stat.dateTime != undefined) res += " ñ " + stat.dateTime;
return res;
}
static function traceStat(stat:Object){
ut.Trace(" -" + statToString(stat));
}
static function A(who, name:String, value, category:String, dateTime:Number, source:Object):Object{
var newStat = createStat(name, value, category, dateTime, source);
addStat(who, newStat);
return newStat;
}
static function S(X):Array{
var res:Array = new Array();
res.push(X);
return res;
}
static function addStatHolder(who){
who.stats = new Array();
who.hasStats = true;
return who;
}
static function addStat(who, stat:Object){
if (who.hasStats != true)
addStatHolder(who);
// stat0 == stat's name
if (statExistWithName(who, stat.name)){
// ò.å. åñëè ýòî îáû÷íûé ñòàò, òî íèçÿ
if (stat.category.indexOf ('ñòàò') >= 0){
ut.Trace(who.name + ' óæå èìååò óíèêàëüíûé ñòàò ñ èìåíåì "' + stat.name);
return;
}
}
// check for incomplete stat!
if (stat.name != undefined){
var isEmpty = (stat.value.length == 0);
if (isEmpty){
ut.TraceDebug('+ äîáàâëåíî "' + stat.name + '", íî, òê çíà÷åíèå íå ïåðåäàíî, åìó áóäåò ïðèñâîåíî äåôîëòíîå çíà÷åíèå.');
stat = stat_default.getDefaultStat(who, stat.name);
}
}
stat.host = who;
who.stats.push(stat);
ut.TraceDebug('+ äîáàâëåí ñòàò "' + stat.name+'" y ' + who.name);
ut.Trace(who.name + ' òåïåðü èìååò "' + stat.name+"\"=" + stat.value);
traceStat(stat);
}
static function listStats(who){
ut.Trace(who.name);
for (var i = 0; i < who.stats.length; ++i)
traceStat(who.stats[i]);
}
static function statsWithName(who, statName:String):Array{
ut.TraceDebug('? íàéòè âñå ñòàòû "' + statName + '", êîòîðûìè âëàäååò ' + who.name);
if (who.hasStats != true) return new Array();
var arr = new Array();
for (var i = 0; i < who.stats.length; ++i)
if (who.stats[i].name == statName)
arr.push(who.stats[i]);
ut.TraceDebug('+ íàéäåíî '+arr.length+' ñòàò(îâ) ñ èìåíåì "' + statName);
return arr;
}
static function statExistWithName(who, statName):Boolean{
ut.TraceDebug('? åñòü ëè ñòàò ñ èìåíåì "' + statName + '" ó ' + who.name);
if (who.hasStats != true) return false;
var arr = new Array();
for (var i = 0; i < who.stats.length; ++i)
if (who.stats[i].name == statName)
return true;
ut.TraceDebug('- íåò ñòàòà ñ èìåíåì "' + statName + '"');
return false;
}
static function statCountWithName(who, statName:String):Number{
return statsWithName(who, statName).length;
}
static function statWithName(who, statName:String, indexMult):Object{
var arr = statsWithName(who, statName);
if (arr.length == 0) {ut.TraceDebug('! ' + who.name + ' íå èìååò ñòàòà ñ èìåíåì "' + statName); return undefined;}
if (arr.length == 1) {ut.TraceDebug('+ íàéäåí óíèêàëüíûé ñòàò "' + statName); return arr[0];}
if (indexMult != undefined) return arr[indexMult];
ut.Trace('~ ' + who.name + ' èìååò ' + arr.length + ' ñòàòîâ ñ èìåíåì "' + statName + '", íî ò.ê. íå ñêàçàíî, êàêîé íóæåí, âûäàí ïåðâûé íàéäåííûé!');
return arr[0];
}
static function watchStatValue(who, statName:String, indexMult):Array{
ut.TraceDebug('? íàéòè ìàññèâ çíà÷åíèé ñòàòà ñ èìåíåì "' + statName + '"');
return (statWithName(who, statName, indexMult)).value;
}
static function watchUniqueStatValueParam(who, statName:String, paramIndex):Number{
if (paramIndex == undefined) paramIndex = 0;
ut.TraceDebug('? íàéòè '+paramIndex+'-îå çíà÷åíèå èç ìàññèâà çíà÷åíèé ñòàòà ñ èìåíåì "' + statName + '"');
var res:Array = watchStatValue(who, statName, undefined);
ut.TraceDebug('+ íàéäåí ñòàò ñ èìåíåì "' + statName+'" è çíà÷åíèåì ' + res);
ut.TraceDebug('+ âçÿòî çíà÷åíèå "' + statName+'"('+paramIndex+') :: ' + res[paramIndex]);
return res[paramIndex];
}
static function statIndexWithName (who, statName:String, indexMult):Number{
var indices = new Array();
for (var i = 0; i < who.stats.length; ++i)if (who.stats[i].name == statName)indices.push(i);
if (indices.length == 0) return -1;
if (indices.length == 1) return indices[0];
if (indexMult != undefined) return indices[indexMult];
return indices[0];
}
static function changeStatValue(who, statName:String, indexMult, newValue, paramNumber):Object{
var statIndex = statIndexWithName(who, statName, indexMult);
if (paramNumber == undefined)
paramNumber = 0;
if (newValue != undefined){
ut.TraceDebug('+ èçìåíåí ñòàò '+who.stats[statIndex].value[paramNumber] + " -> " + newValue + ' ' + who.name+':'+ who.stats[statIndex].name + ((indexMult != undefined)? (' (' +indexMult+ ')'): ('')));
who.stats[statIndex].value[paramNumber] = newValue;
ut.Trace(who.name + ':'+statName + ' ìåíåÿåòñÿ. '+ "Òåïåðü \"" +statName + "\" = "+ who.stats[statIndex].value);
}
return who.stats[statIndex];
}
static function changeUniqueStatValue(who, statName:String, newValue, paramNumber):Object{
return changeStatValue(who, statName, undefined, newValue, paramNumber);
}
static function getStatValue(who, statName:String, indexMult):Array{
ut.TraceDebug('? âçÿòü çíà÷åíèå ñòàòà "' + statName + '" ó ' + who.name);
if (statExistWithName(who, statName))
return (statWithName(who, statName, indexMult)).value;
ut.TraceDebug('! â òàêîì ñëó÷àå äîáàâèòü ñòàò "' + statName + '" ñ äåôîëòíûì çíà÷åíèåì äëÿ ' + who.name);
A(who, statName);
// recursive! kidding
return watchStatValue(who, statName, indexMult);
}
// static function statIndex(who, statName:String):Number{
// if (who.hasStats != true)
// return -1;
// for (var i = 0; i < who.stats.length; ++i)
// if (who.stats[i].name == statName)
// return i;
// return -1;
// }
// static function watchStat(who, statName):Array{
// var statInd = statIndex(who, statName);
// if (statInd < 0) return null;
// return who.stats[statInd];
// }
// //!!!! get is like watch, but if none, then add default and return it!
// static function getStat(who, statName):Array{
// var statInd = statIndex(who, statName);
// if (statInd < 0){
// ut.Trace('requested ' + statName + ' from ' + who.name+', but none found. Adding default ' + statName);
// addStat(who, statName);
// statInd = who.stats.length - 1;
// }
// return who.stats[statInd];
// }
//static function getStatParameter(who, statName, paramIndex){
// return -1;//who.stats[][paramIndex];
//}
// static function changeStat(who, statName, statParamInd, newValue){
// var statInd = statIndex(who, statName);
// if (who.hasStats != true || who.stats.length <= statInd)
// return;
// statParamInd -= 1;
// var vas:String = ' >>> ' + who.name + ": " + who.stats[statInd][0] + ": " + who.stats[statInd][statParamInd] + " -> ";
// who.stats[statInd][statParamInd] = newValue;
// ut.Trace(vas + who.stats[statInd][statParamInd]);
// }
// static function incrementStat(who, statName, statParamInd, addCount){
// var statInd = statIndex(who, statName);
// if (who.hasStats != true || who.stats.length <= statInd)
// return;
// statParamInd -= 1;
// var vas:String = ' >>> ' + who.name + ": " + who.stats[statInd][0] + ": " + who.stats[statInd][statParamInd] + " -> ";
// who.stats[statInd][statParamInd] += addCount;
// ut.Trace(vas + who.stats[statInd][statParamInd]);
// }
}