public void JoinQueue(string playerId, Action <string> onGameFound, Action <AggregateException> fallback) => DatabaseAPI.PostObject($"matchmaking/{playerId}", "placeholder", () => queueListener = DatabaseAPI.ListenForValueChanged($"matchmaking/{playerId}", args => { var gameId = StringSerializationAPI.Deserialize(typeof(string), args.Snapshot.GetRawJsonValue()) as string; if (gameId == "placeholder") { return; } LeaveQueue(playerId, () => onGameFound( gameId), fallback); }, fallback), fallback);
public void ListenForMoves(Action <Move> onNewMove) { moveListener = DatabaseAPI.ListenForValueChanged( $"games/{currentGameInfo.gameId}/move", args => { if (!args.Snapshot.Exists) { return; } var move = StringSerializationAPI.Deserialize(typeof(Move), args.Snapshot.GetRawJsonValue()) as Move; if (move.userId == MainManager.Instance.currentUserId) { return; } onNewMove(move); }, exc => Debug.Log("!!!Fallback subscribe for move!!!")); }
// private readonly Dictionary<string, KeyValuePair<DatabaseReference, EventHandler<ChildChangedEventArgs>>> // moveListeners = // new Dictionary<string, KeyValuePair<DatabaseReference, EventHandler<ChildChangedEventArgs>>>(); public void GetCurrentGameInfo(string gameId, string localPlayerId, Action <GameInfo> callback, Action <AggregateException> fallback) { currentGameInfoListener = DatabaseAPI.ListenForValueChanged($"games/{gameId}/gameInfo", args => { if (!args.Snapshot.Exists) { return; } var gameInfo = StringSerializationAPI.Deserialize(typeof(GameInfo), args.Snapshot.GetRawJsonValue()) as GameInfo; currentGameInfo = gameInfo; currentGameInfo.localPlayerId = localPlayerId; DatabaseAPI.StopListeningForValueChanged(currentGameInfoListener); callback(currentGameInfo); }, fallback); }