From 54f785639580ee672b765ef20b6b07af448a2b46 Mon Sep 17 00:00:00 2001 From: klikini Date: Sun, 13 Sep 2015 14:58:57 -0500 Subject: [PATCH] letter performance tweaks (commit 3/3) --- lib/src/display/loop.dart | 33 --------------------------------- lib/src/display/view.dart | 1 + lib/src/game/game.dart | 34 ++++++++++++++++++++++++++++++++-- lib/src/game/street.dart | 8 ++++++++ 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/lib/src/display/loop.dart b/lib/src/display/loop.dart index 6dec160d..3ad31ddf 100644 --- a/lib/src/display/loop.dart +++ b/lib/src/display/loop.dart @@ -63,37 +63,4 @@ update(double dt) } Wormhole.updateAll(); -} - -// Not run as part of the loop, only every 5 seconds -Future updatePlayerLetters() async { - Map players = {}; - players.addAll(otherPlayers); - players.addAll(({game.username: CurrentPlayer})); - - players.forEach((String username, Player player) async { - Element parentE = player.playerParentElement; - - String username = parentE.id.replaceFirst("player-", ""); - if (username.startsWith("pc-")) { - username = username.replaceFirst("pc-", ""); - } - - if ( - mapData.hubData[currentStreet.hub_id] != null && - mapData.hubData[currentStreet.hub_id]["players_have_letters"] != null && - mapData.hubData[currentStreet.hub_id]["players_have_letters"] == true - ) { - String letter = await HttpRequest.getString("http://${Configs.utilServerAddress}/letters/getPlayerLetter?username=$username"); - - DivElement letterDisplay = new DivElement() - ..classes.addAll(["letter", "letter-$letter"]); - - parentE - ..children.removeWhere((Element e) => e.classes.contains("letter")) - ..append(letterDisplay); - } else { - parentE.children.removeWhere((Element e) => e.classes.contains("letter")); - } - }); } \ No newline at end of file diff --git a/lib/src/display/view.dart b/lib/src/display/view.dart index 20f4f23a..450790a4 100644 --- a/lib/src/display/view.dart +++ b/lib/src/display/view.dart @@ -127,6 +127,7 @@ class UserInterface { // The 'you won' splash window.onBeforeUnload.listen((_) { youWon.hidden = false; + transmit("gameUnloading"); }); diff --git a/lib/src/game/game.dart b/lib/src/game/game.dart index 1dedaee9..b76a4232 100644 --- a/lib/src/game/game.dart +++ b/lib/src/game/game.dart @@ -92,8 +92,10 @@ class Game { logmessage("Game loaded!"); new Timer.periodic(new Duration(seconds: 1), (_) => updatePlayerLetters()); - new Service(["streetLoaded"], (_) { - HttpRequest.getString("http://${Configs.utilServerAddress}/letters/newPlayerLetter?username=${game.username}"); + new Service(["streetLoaded", "gameUnloading"], (_) { + if (currentStreet.useLetters) { + HttpRequest.getString("http://${Configs.utilServerAddress}/letters/newPlayerLetter?username=${game.username}"); + } }); // Load previous GPS state @@ -144,4 +146,32 @@ class Game { //previousTag.makeCurrent(); } + + Future updatePlayerLetters() async { + Map players = {}; + players.addAll(otherPlayers); + players.addAll(({game.username: CurrentPlayer})); + + players.forEach((String username, Player player) async { + Element parentE = player.playerParentElement; + + String username = parentE.id.replaceFirst("player-", ""); + if (username.startsWith("pc-")) { + username = username.replaceFirst("pc-", ""); + } + + if (currentStreet.useLetters) { + String letter = await HttpRequest.getString("http://${Configs.utilServerAddress}/letters/getPlayerLetter?username=$username"); + + DivElement letterDisplay = new DivElement() + ..classes.addAll(["letter", "letter-$letter"]); + + parentE + ..children.removeWhere((Element e) => e.classes.contains("letter")) + ..append(letterDisplay); + } else { + parentE.children.removeWhere((Element e) => e.classes.contains("letter")); + } + }); + } } diff --git a/lib/src/game/street.dart b/lib/src/game/street.dart index 348bf9a5..f9078fd8 100644 --- a/lib/src/game/street.dart +++ b/lib/src/game/street.dart @@ -43,6 +43,14 @@ class Street { return currentStreetName; } + bool get useLetters { + return ( + mapData.hubData[hub_id] != null && + mapData.hubData[hub_id]["players_have_letters"] != null && + mapData.hubData[hub_id]["players_have_letters"] == true + ); + } + Stopwatch loadTime; Rectangle bounds;