Exemplo n.º 1
0
        /// <summary>
        /// This method is called whenever an actor is activated.
        /// An actor is activated the first time any of its methods are invoked.
        /// </summary>
        protected override async Task OnActivateAsync()
        {
            ActorEventSource.Current.ActorMessage(this, "Actor activated.");

            // The StateManager is this actor's private state store.
            // Data stored in the StateManager will be replicated for high-availability for actors that use volatile or persisted state storage.
            // Any serializable object can be saved in the StateManager.
            // For more information, see https://aka.ms/servicefabricactorsstateserialization

            var state = await StateManager.TryGetStateAsync <ActorState>(GameState);

            if (!state.HasValue)
            {
                var newState = new ActorState()
                {
                    Board           = new int[9],
                    Winner          = "",
                    Players         = new List <Tuple <long, string> >(),
                    NextPlayerIndex = 0,
                    NumberOfMoves   = 0
                };

                await StateManager.SetStateAsync <ActorState>(GameState, newState);
            }
        }
Exemplo n.º 2
0
        public async Task <bool> JoinGameAsync(long playerId, string playerName)
        {
            var state = (await StateManager.TryGetStateAsync <ActorState>(GameState)).Value;

            if (state.Players.Count >= 2 || state.Players.Any(p => p.Item2 == playerName))
            {
                return(false);
            }

            state.Players.Add(new Tuple <long, string>(playerId, playerName));

            await StateManager.AddOrUpdateStateAsync <ActorState>(GameState, state, (k, v) => state);

            return(true);
        }
Exemplo n.º 3
0
        public async Task <bool> MakeMoveAsync(long playerId, int x, int y)
        {
            var state = (await StateManager.TryGetStateAsync <ActorState>(GameState)).Value;

            if (x < 0 || x > 2 || y < 0 || y > 2 ||
                state.Players.Count != 2 ||
                state.NumberOfMoves >= 9 ||
                state.Winner != "")
            {
                return(false);
            }

            var index = state.Players.FindIndex(p => p.Item1 == playerId);

            if (index != state.NextPlayerIndex)
            {
                return(false);
            }

            if (state.Board[y * 3 + x] != 0)
            {
                return(false);
            }

            var piece = index * 2 - 1;

            state.Board[y * 3 + x] = piece;
            state.NumberOfMoves++;

            if (HasWon(state, piece * 3))
            {
                state.Winner = state.Players[index].Item2 + " (" + (piece == -1 ? "X" : "O") + ")";
            }
            else if (state.Winner == "" && state.NumberOfMoves >= 9)
            {
                state.Winner = "TIE";
            }

            state.NextPlayerIndex = (state.NextPlayerIndex + 1) % 2;

            await StateManager.AddOrUpdateStateAsync <ActorState>(GameState, state, (k, v) => state);

            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// This method is called whenever an actor is activated.
        /// An actor is activated the first time any of its methods are invoked.
        /// </summary>
        protected override async Task OnActivateAsync()
        {
            ActorEventSource.Current.ActorMessage(this, "Actor activated.");

            ConditionalValue <ActorState> state = await StateManager.TryGetStateAsync <ActorState>(STATE_KEY);

            if (!state.HasValue)
            {
                var newState = new ActorState()
                {
                    Board           = new int[9],
                    Winner          = "",
                    Players         = new List <Tuple <long, string> >(),
                    NextPlayerIndex = 0,
                    NumberOfMoves   = 0
                };
                await StateManager.SetStateAsync <ActorState>(STATE_KEY, newState);
            }
        }
Exemplo n.º 5
0
        public async Task <string> GetWinnerAsync()
        {
            var state = await StateManager.TryGetStateAsync <ActorState>(GameState);

            return(state.Value.Winner);
        }
Exemplo n.º 6
0
        public async Task <int[]> GetGameBoardAsync()
        {
            var state = await StateManager.TryGetStateAsync <ActorState>(GameState);

            return(state.Value.Board);
        }
Exemplo n.º 7
0
        private async Task <ActorState> GetActorState()
        {
            ConditionalValue <ActorState> stateValue = await StateManager.TryGetStateAsync <ActorState>(STATE_KEY);

            return(await Task.FromResult <ActorState>(stateValue.Value));
        }