Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/proto/ipc/omgwords.proto
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ message GameInfoResponse {
int32 tournament_game_index = 22;
}

message GameInfoResponses { repeated GameInfoResponse game_info = 1; }
message GameInfoResponses {
repeated GameInfoResponse game_info = 1;
int64 count = 2; // int64 for future-proofing
}


// InstantiateGame is an internal message passed to gamesvc in order to
Expand Down
5 changes: 4 additions & 1 deletion api/proto/tournament_service/tournament_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ message RecentGamesRequest {
int32 offset = 3;
}

message RecentGamesResponse { repeated ipc.TournamentGameEndedEvent games = 1; }
message RecentGamesResponse {
repeated ipc.TournamentGameEndedEvent games = 1;
int64 count = 2; // int64 for future-proofing
}

message UnstartTournamentRequest { string id = 1; }

Expand Down
4 changes: 4 additions & 0 deletions liwords-ui/src/gen/api/proto/ipc/omgwords_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ export class GameInfoResponses extends jspb.Message {
setGameInfoList(value: Array<GameInfoResponse>): void;
addGameInfo(value?: GameInfoResponse, index?: number): GameInfoResponse;

getCount(): number;
setCount(value: number): void;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): GameInfoResponses.AsObject;
static toObject(includeInstance: boolean, msg: GameInfoResponses): GameInfoResponses.AsObject;
Expand All @@ -417,6 +420,7 @@ export class GameInfoResponses extends jspb.Message {
export namespace GameInfoResponses {
export type AsObject = {
gameInfoList: Array<GameInfoResponse.AsObject>,
count: number,
}
}

Expand Down
32 changes: 31 additions & 1 deletion liwords-ui/src/gen/api/proto/ipc/omgwords_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3181,7 +3181,8 @@ proto.ipc.GameInfoResponses.prototype.toObject = function(opt_includeInstance) {
proto.ipc.GameInfoResponses.toObject = function(includeInstance, msg) {
var f, obj = {
gameInfoList: jspb.Message.toObjectList(msg.getGameInfoList(),
proto.ipc.GameInfoResponse.toObject, includeInstance)
proto.ipc.GameInfoResponse.toObject, includeInstance),
count: jspb.Message.getFieldWithDefault(msg, 2, 0)
};

if (includeInstance) {
Expand Down Expand Up @@ -3223,6 +3224,10 @@ proto.ipc.GameInfoResponses.deserializeBinaryFromReader = function(msg, reader)
reader.readMessage(value,proto.ipc.GameInfoResponse.deserializeBinaryFromReader);
msg.addGameInfo(value);
break;
case 2:
var value = /** @type {number} */ (reader.readInt64());
msg.setCount(value);
break;
default:
reader.skipField();
break;
Expand Down Expand Up @@ -3260,6 +3265,13 @@ proto.ipc.GameInfoResponses.serializeBinaryToWriter = function(message, writer)
proto.ipc.GameInfoResponse.serializeBinaryToWriter
);
}
f = message.getCount();
if (f !== 0) {
writer.writeInt64(
2,
f
);
}
};


Expand Down Expand Up @@ -3301,6 +3313,24 @@ proto.ipc.GameInfoResponses.prototype.clearGameInfoList = function() {
};


/**
* optional int64 count = 2;
* @return {number}
*/
proto.ipc.GameInfoResponses.prototype.getCount = function() {
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 2, 0));
};


/**
* @param {number} value
* @return {!proto.ipc.GameInfoResponses} returns this
*/
proto.ipc.GameInfoResponses.prototype.setCount = function(value) {
return jspb.Message.setProto3IntField(this, 2, value);
};



/**
* List of repeated fields within this message type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,9 @@ export class RecentGamesResponse extends jspb.Message {
setGamesList(value: Array<api_proto_ipc_tournament_pb.TournamentGameEndedEvent>): void;
addGames(value?: api_proto_ipc_tournament_pb.TournamentGameEndedEvent, index?: number): api_proto_ipc_tournament_pb.TournamentGameEndedEvent;

getCount(): number;
setCount(value: number): void;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): RecentGamesResponse.AsObject;
static toObject(includeInstance: boolean, msg: RecentGamesResponse): RecentGamesResponse.AsObject;
Expand All @@ -597,6 +600,7 @@ export class RecentGamesResponse extends jspb.Message {
export namespace RecentGamesResponse {
export type AsObject = {
gamesList: Array<api_proto_ipc_tournament_pb.TournamentGameEndedEvent.AsObject>,
count: number,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4637,7 +4637,8 @@ proto.tournament_service.RecentGamesResponse.prototype.toObject = function(opt_i
proto.tournament_service.RecentGamesResponse.toObject = function(includeInstance, msg) {
var f, obj = {
gamesList: jspb.Message.toObjectList(msg.getGamesList(),
api_proto_ipc_tournament_pb.TournamentGameEndedEvent.toObject, includeInstance)
api_proto_ipc_tournament_pb.TournamentGameEndedEvent.toObject, includeInstance),
count: jspb.Message.getFieldWithDefault(msg, 2, 0)
};

if (includeInstance) {
Expand Down Expand Up @@ -4679,6 +4680,10 @@ proto.tournament_service.RecentGamesResponse.deserializeBinaryFromReader = funct
reader.readMessage(value,api_proto_ipc_tournament_pb.TournamentGameEndedEvent.deserializeBinaryFromReader);
msg.addGames(value);
break;
case 2:
var value = /** @type {number} */ (reader.readInt64());
msg.setCount(value);
break;
default:
reader.skipField();
break;
Expand Down Expand Up @@ -4716,6 +4721,13 @@ proto.tournament_service.RecentGamesResponse.serializeBinaryToWriter = function(
api_proto_ipc_tournament_pb.TournamentGameEndedEvent.serializeBinaryToWriter
);
}
f = message.getCount();
if (f !== 0) {
writer.writeInt64(
2,
f
);
}
};


Expand Down Expand Up @@ -4757,6 +4769,24 @@ proto.tournament_service.RecentGamesResponse.prototype.clearGamesList = function
};


/**
* optional int64 count = 2;
* @return {number}
*/
proto.tournament_service.RecentGamesResponse.prototype.getCount = function() {
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 2, 0));
};


/**
* @param {number} value
* @return {!proto.tournament_service.RecentGamesResponse} returns this
*/
proto.tournament_service.RecentGamesResponse.prototype.setCount = function(value) {
return jspb.Message.setProto3IntField(this, 2, value);
};





Expand Down
15 changes: 12 additions & 3 deletions pkg/stores/game/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type game struct {
// most games.
TournamentID string `gorm:"index"`
TournamentData datatypes.JSON

// only for querying
Count int64 `gorm:"migration"`
}

// NewDBStore creates a new DB store for games.
Expand Down Expand Up @@ -228,7 +231,7 @@ func (s *DBStore) GetRecentGames(ctx context.Context, username string, numGames
var games []*game
// gorm does not intend to support with clause. https://github.com/go-gorm/gorm/issues/3955#issuecomment-761939460
if results := ctxDB.Raw(`with u as (select id from users where lower(username) = lower(?))
select games.* from games inner join u on (player0_id = u.id or player1_id = u.id)
select games.*, count(*) over () count from games inner join u on (player0_id = u.id or player1_id = u.id)
where game_end_reason not in (?, ?, ?) order by created_at desc limit ? offset ?`,
username, pb.GameEndReason_NONE, pb.GameEndReason_ABORTED, pb.GameEndReason_CANCELLED, numGames, offset).
Find(&games); results.Error != nil {
Expand All @@ -249,6 +252,7 @@ func (s *DBStore) GetRecentTourneyGames(ctx context.Context, tourneyID string, n
Where("tournament_id = ? AND game_end_reason NOT IN (?, ?, ?)", tourneyID,
pb.GameEndReason_NONE, pb.GameEndReason_ABORTED, pb.GameEndReason_CANCELLED).
Order("updated_at desc").
Select("games.*, count(*) over () count"). // that last "count" is the (default) column name
Find(&games); results.Error != nil {
return nil, results.Error
}
Expand All @@ -264,7 +268,12 @@ func convertGamesToInfoResponses(games []*game) (*pb.GameInfoResponses, error) {
}
responses = append(responses, info)
}
return &pb.GameInfoResponses{GameInfo: responses}, nil
count := int64(0)
if len(games) > 0 {
// all rows have the same count
count = games[0].Count
}
return &pb.GameInfoResponses{GameInfo: responses, Count: count}, nil
}

func convertGameToInfoResponse(g *game) (*pb.GameInfoResponse, error) {
Expand Down Expand Up @@ -453,7 +462,7 @@ func (s *DBStore) ListActive(ctx context.Context, tourneyID string) (*pb.GameInf
var games []*game

ctxDB := s.db.WithContext(ctx)
query := ctxDB.Table("games").Select("quickdata, request, uuid, started, tournament_data").
query := ctxDB.Table("games").Select("quickdata, request, uuid, started, tournament_data, count(*) over () count").
Where("games.game_end_reason = ?", 0 /* ongoing games only*/)

if tourneyID != "" {
Expand Down
1 change: 1 addition & 0 deletions pkg/stores/tournament/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func (s *DBStore) GetRecentGames(ctx context.Context, tourneyID string, numGames
}
return &pb.RecentGamesResponse{
Games: evts,
Count: infos.Count,
}, nil
}

Expand Down
Loading