blob: c6ef13e1b4c98ad4909933e3b2d47969b426a318 [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
part of hearts;
class HeartsLog extends GameLog {
LogWriter logWriter;
Set<String> seenKeys; // the seen ones can be ignored.
HeartsLog() {
// TODO(alexfandrianto): The Game ID needs to be part of this constructor.
seenKeys = new Set<String>();
}
@override
void setGame(Game g) {
this.game = g;
logWriter = new LogWriter(handleSyncUpdate, [0, 1, 2, 3]);
logWriter.associatedUser = this.game.playerNumber;
logWriter.logPrefix = "${game.gameID}/log";
}
void handleSyncUpdate(String key, String cmd) {
// In this game, we can execute commands in any order.
// However, we must avoid repeated keys.
if (!seenKeys.contains(key)) {
HeartsCommand hc = new HeartsCommand.fromCommand(cmd);
this.update(hc);
seenKeys.add(key);
} else {
print("The log is ignoring repeated key: ${key}");
}
}
@override
void addToLogCb(List<GameCommand> log, GameCommand newCommand) {
logWriter.write(newCommand.simultaneity, newCommand.command);
}
@override
List<GameCommand> updateLogCb(
List<GameCommand> current, List<GameCommand> other, int mismatchIndex) {
// Note: The Hearts schema avoids all conflict, so this should never be called.
assert(false);
return current;
}
@override
void close() {
logWriter.close();
}
}