1
1
import { Injectable } from '@nestjs/common' ;
2
2
import { InjectRepository } from '@nestjs/typeorm' ;
3
- import { Connection , In , Repository } from 'typeorm' ;
3
+ import { Repository } from 'typeorm' ;
4
4
import { HeroItemDto , HeroSummaryDto } from 'rest/dto/meta.dto' ;
5
5
import { MatchmakingMode } from 'gateway/shared-types/matchmaking-mode' ;
6
- import { Page } from 'rest/dto/page' ;
7
- import { cached } from 'util/method-cache' ;
8
6
import FinishedMatchEntity from 'gameserver/model/finished-match.entity' ;
9
7
import PlayerInMatchEntity from 'gameserver/model/player-in-match.entity' ;
10
8
import { ItemHeroView } from 'gameserver/model/item-hero.view' ;
@@ -17,50 +15,14 @@ export class MetaService {
17
15
private readonly playerInMatchRepository : Repository < PlayerInMatchEntity > ,
18
16
@InjectRepository ( FinishedMatchEntity )
19
17
private readonly matchRepository : Repository < FinishedMatchEntity > ,
20
- private readonly connection : Connection ,
21
18
@InjectRepository ( ItemHeroView )
22
19
private readonly itemHeroViewRepository : Repository < ItemHeroView > ,
23
20
@InjectRepository ( ItemView )
24
21
private readonly itemViewRepository : Repository < ItemView > ,
25
22
) { }
26
23
27
- @cached ( 60 * 24 , 'meta_heroMatches' )
28
- public async heroMatches (
29
- page : number ,
30
- perPage : number ,
31
- hero : string ,
32
- ) : Promise < Page < FinishedMatchEntity > > {
33
- const [ ids , count ] = await this . matchRepository
34
- . createQueryBuilder ( 'm' )
35
- . select ( [ 'm.id' , 'm.timestamp' ] )
36
- . addOrderBy ( 'm.timestamp' , 'DESC' )
37
- . leftJoin ( 'm.players' , 'pims' )
38
- . where ( 'pims.hero = :hero' , { hero } )
39
- . andWhere ( 'm.matchmaking_mode in (:...modes)' , {
40
- modes : [ MatchmakingMode . RANKED , MatchmakingMode . UNRANKED ] ,
41
- } )
42
- . take ( perPage )
43
- . skip ( perPage * page )
44
- . getManyAndCount ( ) ;
45
-
46
- const mapped = await this . matchRepository . find ( {
47
- where : {
48
- id : In ( ids . map ( t => t . id ) ) ,
49
- } ,
50
- } ) ;
51
-
52
- return {
53
- data : mapped ,
54
- page,
55
- perPage : perPage ,
56
- pages : Math . ceil ( count / perPage ) ,
57
- } ;
58
- }
59
-
60
- // 24 hours
61
- @cached ( 60 * 24 , 'meta_heroesSummary' )
62
24
public async heroesSummary ( ) : Promise < HeroSummaryDto [ ] > {
63
- return this . connection . query (
25
+ return this . playerInMatchRepository . query (
64
26
`with picks as (select count(*) as cnt from player_in_match p)
65
27
SELECT "pim"."hero" AS "hero",
66
28
(count(pim.hero)::float / greatest(1, s.cnt))::float as pickrate,
@@ -87,7 +49,8 @@ GROUP BY "pim"."hero", s.cnt`,
87
49
* This is a heavy method that should be cached
88
50
* @param hero
89
51
*/
90
- public async heroMeta ( hero : string ) {
52
+
53
+ public async heroMeta ( hero : string ) : Promise < HeroItemDto [ ] > {
91
54
// This query finds all unique items bought on hero, games that played on this hero, and then counts winrates
92
55
const query = `with games as (select pim.item0,
93
56
pim.item1,
@@ -107,7 +70,7 @@ GROUP BY "pim"."hero", s.cnt`,
107
70
group by i.item_id) select w.item, w.wins::int, w.game_count::int, w.winrate::float from winrates w where w.game_count > 10 and w.item != 0
108
71
order by w.game_count desc` ;
109
72
110
- return this . connection . query < HeroItemDto [ ] > ( query , [ hero ] ) ;
73
+ return this . playerInMatchRepository . query ( query , [ hero ] ) ;
111
74
}
112
75
113
76
public async itemHeroes ( item : number ) : Promise < ItemHeroView [ ] > {
0 commit comments