forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_statistics.cpp
1060 lines (890 loc) · 36.1 KB
/
event_statistics.cpp
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "event_statistics.h"
#include <algorithm>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <unordered_map>
#include <utility>
#include "cata_variant.h"
#include "debug.h"
#include "enum_conversions.h"
#include "enums.h"
#include "event.h"
#include "event_field_transformations.h"
#include "generic_factory.h"
#include "json.h"
#include "stats_tracker.h"
#include "string_formatter.h"
// event_transformation and event_statistic are both objects defined in json
// and managed via generic_factory.
// They both use the pimpl idiom and dynamic polymorphism to support their
// implementation.
// Their values can be calculated directly, or watched for updates.
// To register something that watches for updates, call
// stats_tracker::add_watcher.
//
// The way watching works is as follows:
// - Each event_transformation and event_statistic has a corresponding state
// object, which can be stored in the stats_tracker to hold the current
// state of that transformation or statistic.
// - Also, the stats_tracker stores all the watchers; any object watching any
// event type or the value of any transformation or statistic.
// - Each stat object will also register itself as a watcher for any event
// type, transformation, or statistic it depends on.
// - When an event occurs, watchers of that event_type will be informed.
// - When stats are thus informed, they update their value if appropriate, and
// inform the stats_tracker of the new value.
// - The stats_tracker then notifies watchers again in turn.
//
// In this way, the updates cascade to all relevant objects and ultimately the
// end-users (also in the form of watchers) are informed. For example, an
// achievement might watch in this way so that it can trigger itself when its
// conditions become true.
namespace
{
generic_factory<event_transformation> event_transformation_factory( "event_transformation" );
generic_factory<event_statistic> event_statistic_factory( "event_statistic" );
generic_factory<score> score_factory( "score" );
} // namespace
template<>
const event_transformation &string_id<event_transformation>::obj() const
{
return event_transformation_factory.obj( *this );
}
template<>
bool string_id<event_transformation>::is_valid() const
{
return event_transformation_factory.is_valid( *this );
}
void event_transformation::load_transformation( const JsonObject &jo, const std::string &src )
{
event_transformation_factory.load( jo, src );
}
void event_transformation::check_consistency()
{
event_transformation_factory.check();
}
void event_transformation::reset()
{
event_transformation_factory.reset();
}
template<>
const event_statistic &string_id<event_statistic>::obj() const
{
return event_statistic_factory.obj( *this );
}
template<>
bool string_id<event_statistic>::is_valid() const
{
return event_statistic_factory.is_valid( *this );
}
void event_statistic::load_statistic( const JsonObject &jo, const std::string &src )
{
event_statistic_factory.load( jo, src );
}
void event_statistic::check_consistency()
{
event_statistic_factory.check();
}
void event_statistic::reset()
{
event_statistic_factory.reset();
}
template<>
const score &string_id<score>::obj() const
{
return score_factory.obj( *this );
}
template<>
bool string_id<score>::is_valid() const
{
return score_factory.is_valid( *this );
}
void score::load_score( const JsonObject &jo, const std::string &src )
{
score_factory.load( jo, src );
}
void score::check_consistency()
{
score_factory.check();
}
const std::vector<score> &score::get_all()
{
return score_factory.get_all();
}
void score::reset()
{
score_factory.reset();
}
class event_transformation::impl
{
public:
virtual ~impl() = default;
virtual event_multiset initialize( stats_tracker & ) const = 0;
virtual std::unique_ptr<stats_tracker_state> watch( stats_tracker & ) const = 0;
virtual void check( const std::string &/*name*/ ) const {}
virtual cata::event::fields_type fields() const = 0;
virtual monotonically monotonicity() const = 0;
virtual std::unique_ptr<impl> clone() const = 0;
};
class event_statistic::impl
{
public:
virtual ~impl() = default;
virtual cata_variant value( stats_tracker & ) const = 0;
virtual std::unique_ptr<stats_tracker_state> watch( stats_tracker & ) const = 0;
virtual void check( const std::string &/*name*/ ) const {}
virtual cata_variant_type type() const = 0;
virtual monotonically monotonicity() const = 0;
virtual std::unique_ptr<impl> clone() const = 0;
};
struct value_constraint {
std::optional<cata_variant> equals_;
std::optional<std::string> equals_string_;
std::optional<string_id<event_statistic>> equals_statistic_;
bool permits( const cata_variant &v, stats_tracker &stats ) const {
if( equals_ && *equals_ != v ) {
return false;
}
if( equals_string_ && *equals_string_ != v.get_string() ) {
return false;
}
if( equals_statistic_ && stats.value_of( *equals_statistic_ ) != v ) {
return false;
}
return true;
}
void deserialize( JsonIn &jsin ) {
JsonObject jo = jsin.get_object();
int equals_int;
if( jo.read( "equals", equals_int, false ) ) {
equals_ = cata_variant::make<cata_variant_type::int_>( equals_int );
}
bool equals_bool;
if( jo.read( "equals", equals_bool, false ) ) {
equals_ = cata_variant::make<cata_variant_type::bool_>( equals_bool );
}
std::string equals_string;
if( jo.read( "equals", equals_string, false ) ) {
equals_string_ = equals_string;
}
string_id<event_statistic> stat;
if( jo.read( "equals_statistic", stat ) ) {
equals_statistic_ = stat;
}
if( !equals_ && !equals_string_ && !equals_statistic_ ) {
jo.throw_error( "No valid value constraint found" );
}
}
void check( const std::string &name, const cata_variant_type input_type ) const {
if( equals_statistic_ ) {
if( !equals_statistic_->is_valid() ) {
debugmsg( "constraint for event_transformation %s refers to invalid statistic %s",
name, equals_statistic_->str() );
return;
}
cata_variant_type stat_type = ( *equals_statistic_ )->type();
if( stat_type != input_type ) {
debugmsg( "constraint for event_transformation %s matches statistic %s of "
"different type. Statistic has type %s but value compared with it has "
"type %s",
name, equals_statistic_->str(), io::enum_to_string( stat_type ),
io::enum_to_string( input_type ) );
}
}
if( equals_ ) {
if( input_type != equals_->type() ) {
debugmsg( "constraint for event_transformation %s matches constant of type %s but "
"value compared with it has type %s",
name, io::enum_to_string( equals_->type() ),
io::enum_to_string( input_type ) );
}
}
}
bool is_constant() const {
return !equals_statistic_ ||
( *equals_statistic_ )->monotonicity() == monotonically::constant;
}
};
struct new_field {
event_field_transformation transformation;
std::string input_field;
void deserialize( JsonIn &jsin ) {
JsonObject jo = jsin.get_object();
if( jo.size() != 1 ) {
jo.throw_error( "new field specifications must have exactly one entry" );
}
for( const JsonMember &m : jo ) {
const std::string &transformation_name = m.name();
auto it = event_field_transformations.find( transformation_name );
if( it == event_field_transformations.end() ) {
m.throw_error(
string_format( "unknown event field transformation '%s'", transformation_name )
);
}
transformation = it->second;
input_field = m.get_string();
}
}
void check( const std::string &context_name,
const cata::event::fields_type &input_fields ) const {
auto it = input_fields.find( input_field );
if( it == input_fields.end() ) {
debugmsg( "event_transformation %s specifies transformation on field %s but not such "
"field exists on the input", context_name, input_field );
return;
}
if( transformation.argument_types.size() != 1 ) {
debugmsg( "event_field_transformations of arity not 1 not supported" );
return;
}
if( it->second != transformation.argument_types[0] ) {
debugmsg( "event_transformation %s specifies transformation on field %s of incorrect "
"type. Transformation expects %s; field was %s.", context_name, input_field,
io::enum_to_string( transformation.argument_types[0] ),
io::enum_to_string( it->second ) );
}
}
std::vector<cata::event::data_type> transform( const cata::event::data_type &data,
const std::string &new_field_name ) const {
std::vector<cata::event::data_type> result;
auto it = data.find( input_field );
if( it == data.end() ) {
// Field missing, probably due to stale data. Reporting an error
// would spam, so we rely on the fact that it should already have
// been reported at startup time.
return result;
}
for( cata_variant v : transformation.function( it->second ) ) {
result.push_back( data );
result.back().emplace( new_field_name, v );
}
return result;
}
cata_variant_type type( const cata::event::fields_type &/*input*/ ) const {
return transformation.return_type;
}
};
// Interface to abstract the two possible sources of event_multisets:
// event_types and event_transformations
struct event_source {
static std::unique_ptr<event_source> load( const JsonObject &jo );
virtual ~event_source() = default;
virtual event_multiset get( stats_tracker &stats ) const = 0;
virtual std::string debug_description() const = 0;
virtual bool is_game_start() const = 0;
virtual void add_watcher( stats_tracker &stats, event_multiset_watcher *watcher ) const = 0;
virtual cata::event::fields_type fields() const = 0;
virtual monotonically monotonicity() const = 0;
virtual std::unique_ptr<event_source> clone() const = 0;
};
struct event_type_event_source : event_source {
event_type_event_source( event_type t ) : type( t ) {}
event_type type;
event_multiset get( stats_tracker &stats ) const override {
return stats.get_events( type );
}
std::string debug_description() const override {
return "event type " + io::enum_to_string( type );
}
bool is_game_start() const override {
return type == event_type::game_start;
}
void add_watcher( stats_tracker &stats, event_multiset_watcher *watcher ) const override {
stats.add_watcher( type, watcher );
}
cata::event::fields_type fields() const override {
return cata::event::get_fields( type );
}
monotonically monotonicity() const override {
return monotonically::increasing;
}
std::unique_ptr<event_source> clone() const override {
return std::make_unique<event_type_event_source>( *this );
}
};
struct event_transformation_event_source : event_source {
event_transformation_event_source( const string_id<event_transformation> &t ) :
transformation( t ) {}
string_id<event_transformation> transformation;
event_multiset get( stats_tracker &stats ) const override {
return stats.get_events( transformation );
}
std::string debug_description() const override {
return "event_transformation " + transformation->id.str();
}
bool is_game_start() const override {
return false;
}
void add_watcher( stats_tracker &stats, event_multiset_watcher *watcher ) const override {
stats.add_watcher( transformation, watcher );
}
cata::event::fields_type fields() const override {
return transformation->fields();
}
monotonically monotonicity() const override {
return transformation->monotonicity();
}
std::unique_ptr<event_source> clone() const override {
return std::make_unique<event_transformation_event_source>( *this );
}
};
std::unique_ptr<event_source> event_source::load( const JsonObject &jo )
{
if( jo.has_member( "event_type" ) == jo.has_member( "event_transformation" ) ) {
jo.throw_error( "Must specify exactly one of 'event_type' or 'event_transformation'" );
}
if( jo.has_member( "event_type" ) ) {
event_type type = event_type::num_event_types;
jo.read( "event_type", type, true );
return std::make_unique<event_type_event_source>( type );
} else {
string_id<event_transformation> transformation;
jo.read( "event_transformation", transformation, true );
return std::make_unique<event_transformation_event_source>( transformation );
}
}
struct event_transformation_impl : public event_transformation::impl {
template<typename NewFields, typename Constraints, typename DropFields>
event_transformation_impl( const string_id<event_transformation> &id,
std::unique_ptr<event_source> source,
const NewFields &new_fields, const Constraints &constraints,
const DropFields &drop_fields ) :
id_( id ),
source_( std::move( source ) ),
new_fields_( new_fields.begin(), new_fields.end() ),
constraints_( constraints.begin(), constraints.end() ),
drop_fields_( drop_fields.begin(), drop_fields.end() )
{}
string_id<event_transformation> id_;
cata::clone_ptr<event_source> source_;
std::vector<std::pair<std::string, new_field>> new_fields_;
std::vector<std::pair<std::string, value_constraint>> constraints_;
std::vector<std::string> drop_fields_;
using EventVector = std::vector<cata::event::data_type>;
EventVector match_and_transform( const cata::event::data_type &input_data,
stats_tracker &stats ) const {
EventVector result = { input_data };
for( const std::pair<std::string, new_field> &p : new_fields_ ) {
EventVector before_this_pass = std::move( result );
result.clear();
for( const cata::event::data_type &data : before_this_pass ) {
EventVector from_this_event = p.second.transform( data, p.first );
result.insert( result.end(), from_this_event.begin(), from_this_event.end() );
}
}
auto violates_constraints = [&]( const cata::event::data_type & data ) {
for( const std::pair<std::string, value_constraint> &p : constraints_ ) {
const std::string &field = p.first;
const value_constraint &constraint = p.second;
const auto it = data.find( field );
if( it == data.end() || !constraint.permits( it->second, stats ) ) {
return true;
}
}
return false;
};
result.erase( std::remove_if( result.begin(), result.end(), violates_constraints ),
result.end() );
for( cata::event::data_type &data : result ) {
for( const std::string &drop_field_name : drop_fields_ ) {
data.erase( drop_field_name );
}
}
return result;
}
event_multiset initialize( const event_multiset::counts_type &input,
stats_tracker &stats ) const {
event_multiset result;
for( const std::pair<const cata::event::data_type, int> &p : input ) {
cata::event::data_type event_data = p.first;
EventVector transformed = match_and_transform( event_data, stats );
for( cata::event::data_type &d : transformed ) {
result.add( { d, p.second } );
}
}
return result;
}
event_multiset initialize( stats_tracker &stats ) const override {
return initialize( source_->get( stats ).counts(), stats );
}
void check( const std::string &name ) const override {
const cata::event::fields_type input_fields = source_->fields();
for( const std::pair<std::string, new_field> &p : new_fields_ ) {
if( input_fields.count( p.first ) ) {
debugmsg( "event_transformation %s tries to add field with name %s but a field "
"with that name already exists", name, p.first );
}
p.second.check( name, input_fields );
}
for( const std::pair<std::string, value_constraint> &p : constraints_ ) {
auto it = input_fields.find( p.first );
if( it == input_fields.end() ) {
debugmsg( "event_transformation %s applies constraint to field %s, but no field "
"with that name exists in the input", name, p.first );
} else {
p.second.check( name, it->second );
}
}
for( const std::string &drop_field_name : drop_fields_ ) {
if( input_fields.find( drop_field_name ) == input_fields.end() ) {
debugmsg( "event_transformation %s lists field %s to be dropped, but no field "
"with that name exists in the input", name, drop_field_name );
}
}
}
struct state : stats_tracker_state, event_multiset_watcher, stat_watcher {
state( const event_transformation_impl *trans, stats_tracker &stats ) :
transformation_( trans ),
data_( trans->initialize( stats ) ) {
trans->source_->add_watcher( stats, this );
for( const auto &p : trans->constraints_ ) {
if( p.second.equals_statistic_ ) {
stats.add_watcher( *p.second.equals_statistic_, this );
}
}
}
void new_value( const cata_variant &, stats_tracker &stats ) override {
data_ = transformation_->initialize( stats );
stats.transformed_set_changed( transformation_->id_, data_ );
}
void event_added( const cata::event &e, stats_tracker &stats ) override {
EventVector transformed = transformation_->match_and_transform( e.data(), stats );
for( cata::event::data_type &d : transformed ) {
cata::event new_event( e.type(), e.time(), std::move( d ) );
data_.add( new_event );
stats.transformed_set_changed( transformation_->id_, new_event );
}
}
void events_reset( const event_multiset &new_value, stats_tracker &stats ) override {
data_ = transformation_->initialize( new_value.counts(), stats );
stats.transformed_set_changed( transformation_->id_, data_ );
}
const event_transformation_impl *transformation_;
event_multiset data_;
};
std::unique_ptr<stats_tracker_state> watch( stats_tracker &stats ) const override {
return std::make_unique<state>( this, stats );
}
cata::event::fields_type fields() const override {
cata::event::fields_type result = source_->fields();
for( const std::pair<std::string, new_field> &p : new_fields_ ) {
result.emplace( p.first, p.second.type( result ) );
}
for( const std::string &drop_field_name : drop_fields_ ) {
result.erase( drop_field_name );
}
return result;
}
monotonically monotonicity() const override {
for( const std::pair<std::string, value_constraint> &constraint : constraints_ ) {
if( !constraint.second.is_constant() ) {
return monotonically::unknown;
}
}
return monotonically::increasing;
}
std::unique_ptr<impl> clone() const override {
return std::make_unique<event_transformation_impl>( *this );
}
};
event_multiset event_transformation::value( stats_tracker &stats ) const
{
return impl_->initialize( stats );
}
std::unique_ptr<stats_tracker_state> event_transformation::watch( stats_tracker &stats ) const
{
return impl_->watch( stats );
}
void event_transformation::load( const JsonObject &jo, const std::string & )
{
std::map<std::string, new_field> new_fields;
optional( jo, was_loaded, "new_fields", new_fields );
std::map<std::string, value_constraint> constraints;
optional( jo, was_loaded, "value_constraints", constraints );
std::vector<std::string> drop_fields;
optional( jo, was_loaded, "drop_fields", drop_fields );
if( new_fields.empty() && constraints.empty() && drop_fields.empty() ) {
jo.throw_error( "Event transformation is a no-op." );
}
impl_ = std::make_unique<event_transformation_impl>(
id, event_source::load( jo ), new_fields, constraints, drop_fields );
}
void event_transformation::check() const
{
impl_->check( id.str() );
}
cata::event::fields_type event_transformation::fields() const
{
return impl_->fields();
}
monotonically event_transformation::monotonicity() const
{
return impl_->monotonicity();
}
struct event_statistic_count : event_statistic::impl {
event_statistic_count( const string_id<event_statistic> &i, std::unique_ptr<event_source> s ) :
id( i ),
source( std::move( s ) )
{}
string_id<event_statistic> id;
cata::clone_ptr<event_source> source;
cata_variant value( stats_tracker &stats ) const override {
int count = source->get( stats ).count();
return cata_variant::make<cata_variant_type::int_>( count );
}
struct state : stats_tracker_state, event_multiset_watcher {
state( const event_statistic_count *s, stats_tracker &stats ) :
stat( s ),
value( s->value( stats ).get<int>() ) {
stat->source->add_watcher( stats, this );
}
void event_added( const cata::event &, stats_tracker &stats ) override {
++value;
stats.stat_value_changed( stat->id, cata_variant( value ) );
}
void events_reset( const event_multiset &new_set, stats_tracker &stats ) override {
value = new_set.count();
stats.stat_value_changed( stat->id, cata_variant( value ) );
}
const event_statistic_count *stat;
int value;
};
std::unique_ptr<stats_tracker_state> watch( stats_tracker &stats ) const override {
return std::make_unique<state>( this, stats );
}
cata_variant_type type() const override {
return cata_variant_type::int_;
}
monotonically monotonicity() const override {
return source->monotonicity();
}
std::unique_ptr<impl> clone() const override {
return std::make_unique<event_statistic_count>( *this );
}
};
struct event_statistic_total : event_statistic::impl {
event_statistic_total( const string_id<event_statistic> &i, std::unique_ptr<event_source> s,
const std::string &f ) :
id( i ), source( std::move( s ) ), field( f )
{}
string_id<event_statistic> id;
cata::clone_ptr<event_source> source;
std::string field;
cata_variant value( stats_tracker &stats ) const override {
int total = source->get( stats ).total( field );
return cata_variant::make<cata_variant_type::int_>( total );
}
struct state : stats_tracker_state, event_multiset_watcher {
state( const event_statistic_total *s, stats_tracker &stats ) :
stat( s ),
value( s->value( stats ).get<int>() ) {
stat->source->add_watcher( stats, this );
}
void event_added( const cata::event &e, stats_tracker &stats ) override {
value += e.get<int>( stat->field );
stats.stat_value_changed( stat->id, cata_variant( value ) );
}
void events_reset( const event_multiset &new_set, stats_tracker &stats ) override {
value = new_set.total( stat->field );
stats.stat_value_changed( stat->id, cata_variant( value ) );
}
const event_statistic_total *stat;
int value;
};
std::unique_ptr<stats_tracker_state> watch( stats_tracker &stats ) const override {
return std::make_unique<state>( this, stats );
}
void check( const std::string &name ) const override {
cata::event::fields_type event_fields = source->fields();
auto it = event_fields.find( field );
if( it == event_fields.end() ) {
debugmsg( "event_statistic %s refers to field %s in event source %s, but that source "
"has no such field", name, field, source->debug_description() );
}
}
cata_variant_type type() const override {
return cata_variant_type::int_;
}
monotonically monotonicity() const override {
return source->monotonicity();
}
std::unique_ptr<impl> clone() const override {
return std::make_unique<event_statistic_total>( *this );
}
};
struct event_statistic_maximum : event_statistic::impl {
event_statistic_maximum( const string_id<event_statistic> &i, std::unique_ptr<event_source> s,
const std::string &f ) :
id( i ), source( std::move( s ) ), field( f )
{}
string_id<event_statistic> id;
cata::clone_ptr<event_source> source;
std::string field;
cata_variant value( stats_tracker &stats ) const override {
int maximum = source->get( stats ).maximum( field );
return cata_variant::make<cata_variant_type::int_>( maximum );
}
struct state : stats_tracker_state, event_multiset_watcher {
state( const event_statistic_maximum *s, stats_tracker &stats ) :
stat( s ),
value( s->value( stats ).get<int>() ) {
stat->source->add_watcher( stats, this );
}
void event_added( const cata::event &e, stats_tracker &stats ) override {
const int new_value = std::max( e.get<int>( stat->field ), value );
if( new_value != value ) {
value = new_value;
stats.stat_value_changed( stat->id, cata_variant( value ) );
}
}
void events_reset( const event_multiset &new_set, stats_tracker &stats ) override {
value = new_set.maximum( stat->field );
stats.stat_value_changed( stat->id, cata_variant( value ) );
}
const event_statistic_maximum *stat;
int value;
};
std::unique_ptr<stats_tracker_state> watch( stats_tracker &stats ) const override {
return std::make_unique<state>( this, stats );
}
void check( const std::string &name ) const override {
cata::event::fields_type event_fields = source->fields();
auto it = event_fields.find( field );
if( it == event_fields.end() ) {
debugmsg( "event_statistic %s refers to field %s in event source %s, but that source "
"has no such field", name, field, source->debug_description() );
}
}
cata_variant_type type() const override {
return cata_variant_type::int_;
}
monotonically monotonicity() const override {
return source->monotonicity();
}
std::unique_ptr<impl> clone() const override {
return std::make_unique<event_statistic_maximum>( *this );
}
};
struct event_statistic_minimum : event_statistic::impl {
event_statistic_minimum( const string_id<event_statistic> &i, std::unique_ptr<event_source> s,
const std::string &f ) :
id( i ), source( std::move( s ) ), field( f )
{}
string_id<event_statistic> id;
cata::clone_ptr<event_source> source;
std::string field;
cata_variant value( stats_tracker &stats ) const override {
int minimum = source->get( stats ).minimum( field );
return cata_variant::make<cata_variant_type::int_>( minimum );
}
struct state : stats_tracker_state, event_multiset_watcher {
state( const event_statistic_minimum *s, stats_tracker &stats ) :
stat( s ),
value( s->value( stats ).get<int>() ) {
stat->source->add_watcher( stats, this );
}
void event_added( const cata::event &e, stats_tracker &stats ) override {
const int new_value = std::min( e.get<int>( stat->field ), value );
if( new_value != value ) {
value = new_value;
stats.stat_value_changed( stat->id, cata_variant( value ) );
}
}
void events_reset( const event_multiset &new_set, stats_tracker &stats ) override {
value = new_set.minimum( stat->field );
stats.stat_value_changed( stat->id, cata_variant( value ) );
}
const event_statistic_minimum *stat;
int value;
};
std::unique_ptr<stats_tracker_state> watch( stats_tracker &stats ) const override {
return std::make_unique<state>( this, stats );
}
void check( const std::string &name ) const override {
cata::event::fields_type event_fields = source->fields();
auto it = event_fields.find( field );
if( it == event_fields.end() ) {
debugmsg( "event_statistic %s refers to field %s in event source %s, but that source "
"has no such field", name, field, source->debug_description() );
}
}
cata_variant_type type() const override {
return cata_variant_type::int_;
}
monotonically monotonicity() const override {
// If the source is increasing (adding more events)
if( is_increasing( source->monotonicity() ) ) {
// Then the minimum value will be decreasing
return monotonically::decreasing;
} else {
return monotonically::unknown;
}
}
std::unique_ptr<impl> clone() const override {
return std::make_unique<event_statistic_minimum>( *this );
}
};
struct event_statistic_unique_value : event_statistic::impl {
event_statistic_unique_value( const string_id<event_statistic> &id,
std::unique_ptr<event_source> s,
const std::string &field ) :
id_( id ), source_( std::move( s ) ), field_( field )
{}
string_id<event_statistic> id_;
cata::clone_ptr<event_source> source_;
std::string field_;
cata_variant value( stats_tracker &stats ) const override {
const event_multiset::counts_type counts = source_->get( stats ).counts();
if( counts.size() != 1 ) {
return cata_variant();
}
const cata::event::data_type &d = counts.begin()->first;
auto it = d.find( field_ );
if( it == d.end() ) {
return cata_variant();
}
return it->second;
}
struct state : stats_tracker_state, event_multiset_watcher {
state( const event_statistic_unique_value *s, stats_tracker &stats ) :
stat( s ) {
init( stats );
stat->source_->add_watcher( stats, this );
}
void init( stats_tracker &stats ) {
count = stat->source_->get( stats ).count();
value = stat->value( stats );
}
void event_added( const cata::event &e, stats_tracker &stats ) override {
++count;
if( count == 1 ) {
value = e.get_variant_or_void( stat->field_ );
} else if( count == 2 ) {
value = cata_variant();
} else {
return;
}
stats.stat_value_changed( stat->id_, value );
}
void events_reset( const event_multiset &, stats_tracker &stats ) override {
init( stats );
stats.stat_value_changed( stat->id_, value );
}
const event_statistic_unique_value *stat;
int count;
cata_variant value;
};
std::unique_ptr<stats_tracker_state> watch( stats_tracker &stats ) const override {
return std::make_unique<state>( this, stats );
}
void check( const std::string &name ) const override {
cata::event::fields_type event_fields = source_->fields();
auto it = event_fields.find( field_ );
if( it == event_fields.end() ) {
debugmsg( "event_statistic %s refers to field %s in event source %s, but that source "
"has no such field", name, field_, source_->debug_description() );
}
}
cata_variant_type type() const override {
cata::event::fields_type source_fields = source_->fields();
auto it = source_fields.find( field_ );
if( it == source_fields.end() ) {
return cata_variant_type::void_;
} else {
return it->second;
}
}
monotonically monotonicity() const override {
if( source_->is_game_start() ) {
return monotonically::constant;
} else {
return monotonically::unknown;
}
}
std::unique_ptr<impl> clone() const override {
return std::make_unique<event_statistic_unique_value>( *this );
}
};
cata_variant event_statistic::value( stats_tracker &stats ) const
{
return impl_->value( stats );
}
std::unique_ptr<stats_tracker_state> event_statistic::watch( stats_tracker &stats ) const
{
return impl_->watch( stats );
}
void event_statistic::load( const JsonObject &jo, const std::string & )
{
std::string type;
mandatory( jo, was_loaded, "stat_type", type );
description_.make_plural();
optional( jo, was_loaded, "description", description_ );
if( type == "count" ) {
impl_ = std::make_unique<event_statistic_count>( id, event_source::load( jo ) );
} else if( type == "total" ) {
std::string field;
mandatory( jo, was_loaded, "field", field );
impl_ = std::make_unique<event_statistic_total>( id, event_source::load( jo ), field );
} else if( type == "minimum" ) {
std::string field;
mandatory( jo, was_loaded, "field", field );
impl_ = std::make_unique<event_statistic_minimum>( id, event_source::load( jo ), field );
} else if( type == "maximum" ) {
std::string field;
mandatory( jo, was_loaded, "field", field );
impl_ = std::make_unique<event_statistic_maximum>( id, event_source::load( jo ), field );