我刚刚开始编写游戏,但是我有一个问题,即该方法的使用速度非常慢,对于4个玩家来说大约需要6秒。我想改善它。
我到目前为止所做的:
好的方法是一秒钟内运行一次。但是我不知道该如何提高速度了。
那是代码:
Future nextRound(
ChatMessageRound chatMessageRound, String gamePlayId, Chat chat) async {
if (chatMessageRound.getWinner().length <= 0)
throw NoWinnerMarkedException();
var futures = List<Future>();
var gamePlay =
await _chatRepository.getGamePlayRepository(chat.id).get(gamePlayId);
var winnerIds = ListBuilder<String>(
gamePlay.winnerIds.toList(growable: true)
..addAll(chatMessageRound.getWinner()));
var chatMessageGameRoundIds = ListBuilder<String>(
gamePlay.chatMessageGameRoundIds.toList(growable: true)
..add(chatMessageRound.id));
gamePlay = gamePlay.rebuild((builder) {
builder.winnerIds = winnerIds;
builder.chatMessageGameRoundIds = chatMessageGameRoundIds;
});
var newChatMessageRound =
chatMessageRound.rebuild((builder) => builder.disabled = true);
futures.add(_chatRepository
.getChatMessagesRepository(chat.id)
.update(newChatMessageRound));
if (gamePlay.chatMessageGameRoundIds.length >=
_remoteConfigRepository.gamesToPlayCount()) {
var player = getPlayer(chat, gamePlay.playerIds);
var scoreMembers = player.map((member) {
return ChatMessageScoreUserInfo((builder) {
builder.name = member.name;
builder.profilePictureUrl = member.profilePictureDownloadUrl;
builder.userId = member.userId;
builder.score = gamePlay.winnerIds
.where((winnerId) => winnerId == member.userId)
.length;
});
});
for (var player in scoreMembers) {
futures.add(Future<dynamic>(() async {
var user = await getUser(player.userId);
user = user.rebuild((builder) {
builder.points = builder.points + player.score;
});
await updateUser(user);
}));
}
var chatMessageScore = ChatMessageScore((builder) {
builder.scorePossible = _remoteConfigRepository.gamesToPlayCount();
builder.members = ListBuilder(scoreMembers);
});
futures.add(_chatRepository
.getChatMessagesRepository(chat.id)
.add(chatMessageScore));
} else {
futures.add(
startRound(chat, gamePlay.playerIds, previousGamePlay: gamePlay));
}
futures
.add(_chatRepository.getGamePlayRepository(chat.id).update(gamePlay));
await Future.wait<dynamic>(futures);
}
我的数据库是Firestore。如果您需要更多详细信息,请发表评论。