如何提高飞镖功能的速度?

时间:2018-11-15 15:48:26

标签: performance dart flutter

我刚刚开始编写游戏,但是我有一个问题,即该方法的使用速度非常慢,对于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。如果您需要更多详细信息,请发表评论。

0 个答案:

没有答案