@@ -15,6 +15,74 @@ gg.controller('ScoutController', ['$scope', '$element', '$urlFilter',
1515 console . log ( 'refresh!' , params ) ;
1616 }
1717
18+ NUM_CLUSTERS = 10 ;
19+
20+ $scope . compute_groups = function ( ) {
21+ var start1 = Date . now ( ) ;
22+ var start2 = Date . now ( ) ;
23+ var all_gamerecords = dateDim . top ( Infinity ) ;
24+ var armies = _ . map ( all_gamerecords , function ( gr ) {
25+ return gr . player . army_vector ( 8 ) ;
26+ } ) ;
27+ var start3 = Date . now ( ) ;
28+ kmeans_output = clusterfck . kmeans ( armies , NUM_CLUSTERS ) ;
29+ var end = Date . now ( ) ;
30+ var total_time = end - start1 ;
31+ console . log ( "kmeans took " + ( total_time / 1000 ) + " seconds" ) ;
32+
33+ var centroids = kmeans_output [ 0 ] ;
34+ var clusters = kmeans_output [ 1 ] ;
35+ var assignment = kmeans_output [ 2 ] ;
36+
37+ // make groups an array of empty arrays, one for each cluster
38+ var cluster_gamerecords = _ . map ( clusters , function ( ) { return [ ] } ) ;
39+
40+ // put the gamerecords for each cluster into its respective array
41+ _ . each ( assignment , function ( gr_assign , index ) {
42+ cluster_gamerecords [ gr_assign ] . push ( all_gamerecords [ index ] ) ;
43+ } ) ;
44+
45+ // then compute $scope.groups as aggregate functions of arrays of gamerecords
46+ var groupstats = _ . map ( clusters , function ( cluster , cluster_index ) {
47+ var groupstat = { count : cluster_gamerecords [ cluster_index ] . length ,
48+ gamerecords : cluster_gamerecords [ cluster_index ]
49+ } ;
50+ var armySum = _ . map ( _ . range ( 0 , 19 ) , function ( ) { return 0.0 } ) ;
51+ var numWins = 0 ;
52+ _ . each ( cluster_gamerecords [ cluster_index ] , function ( gamerecord ) {
53+ _ . each ( _ . range ( 0 , 19 ) , function ( army_index ) {
54+ armySum [ army_index ] += gamerecord . player . stat ( 8 , 'u' + army_index ) ;
55+ } ) ;
56+ if ( gamerecord . player . win == 'true' ) {
57+ numWins ++ ;
58+ }
59+ } ) ;
60+ armySum = _ . map ( armySum , function ( unit_count ) {
61+ var average_count = unit_count / groupstat . count ;
62+ if ( average_count < 0.1 ) {
63+ return '' ;
64+ }
65+ if ( average_count >= 10.0 ) {
66+ return Math . round ( average_count ) ;
67+ }
68+ return Math . floor ( average_count * 10.0 ) / 10.0 ;
69+
70+ } ) ;
71+ groupstat . unit_counts = armySum ;
72+ groupstat . winPct = Math . floor ( 100.0 * numWins / groupstat . count ) ;
73+ groupstat . click = function ( ) {
74+ // console.log("groupstat clicked!", this);
75+ $scope . groupsub = this . gamerecords ;
76+ } ;
77+
78+ return groupstat ;
79+ } ) ;
80+
81+ $scope . groupstats = _ . sortBy ( groupstats , function ( groupstat ) {
82+ return - 1 * groupstat . winPct ;
83+ } ) ;
84+ }
85+
1886 $scope . $watch ( 'race + vs_race' , function ( v ) {
1987 $scope . filter . params . race = $scope . race ;
2088 $scope . filter . params . vs_race = $scope . vs_race ;
0 commit comments