Esempio n. 1
0
        public Task <IPlayerGrain> NewPlayer(IAccountGrain account, string name)
        {
            if (_state == GameState.Unstarted)
            {
                var          accountKey = account.GetPrimaryKey();
                IPlayerGrain playerGrain;
                if (_players.TryGetValue(accountKey, out playerGrain))
                {
                    return(Task.FromResult(playerGrain));
                }

                var playerGuid = Guid.NewGuid();
                playerGrain = PlayerGrainFactory.GetGrain(playerGuid);
                playerGrain.Create(this, account, name);
                _players.Add(accountKey, playerGrain);
                account.JoinGame(this, playerGrain);

                Console.WriteLine(" -- {0,-10} -- New player joined {1}.", "Game", _name);
                return(Task.FromResult(playerGrain));
            }
            else
            {
                Console.WriteLine(" -- {0,-10} -- Cannot join a started game.", "Game");
                return(Task.FromResult <IPlayerGrain>(null));
            }
        }
Esempio n. 2
0
 public Task Transfer(
     IAccountGrain fromAccount,
     IAccountGrain toAccount,
     uint amountToTransfer) =>
 Task.WhenAll(
     fromAccount.Withdraw(amountToTransfer),
     toAccount.Deposit(amountToTransfer));
Esempio n. 3
0
 public static AttachedClient GetAccountFocusedClient(IAccountGrain account)
 {
     lock (addRemoveClientLock)
     {
         var client = Clients.Values.FirstOrDefault(c => c.AttachedAccount != null && c.AttachedAccount.Equals(account));
         return(client);
     }
 }
Esempio n. 4
0
 public Task Create(IGameGrain game, IAccountGrain account, string name)
 {
     _game         = game;
     _accountGrain = account;
     _name         = name;
     _stars        = new List <IStarGrain>();
     Console.WriteLine(" -- {0,-10} -- {1} created.", "Player", _name);
     return(TaskDone.Done);
 }
Esempio n. 5
0
        public void Execute(List <string> extraWords, IAccountGrain executingAccount)
        {
            AttachedClient executingClient = AttachedClients.GetAccountFocusedClient(executingAccount);

            if (executingAccount == null || executingClient == null)
            {
                var errorMsgDto = new DescriptiveTextDto("The use player command is currently unavailable.");
                executingClient?.SendDtoMessage(errorMsgDto);
                return;
            }

            if (extraWords.Count != 1)
            {
                var errorMsgDto = new DescriptiveTextDto("Wrong number of parameters.");
                executingClient?.SendDtoMessage(errorMsgDto);
                return;
            }

            var character = executingAccount.GetCharacter(extraWords[0]).Result;;

            if (character == null)
            {
                var errorMsgDto = new DescriptiveTextDto("Unknown character.");
                executingClient?.SendDtoMessage(errorMsgDto);
                return;
            }

            // TODO: this instantly will switch active players in an account.. there needs to be a cooldown time here to logout then login
            AttachedClients.SetClientFocusedCharacter(executingClient.TrackingId, character.TrackingId);
            if (character.IsNew)
            {
                character.IsNew = false;
                var gameIntroMsgDto = new DescriptiveTextDto(GrainClusterClient.Universe.GetGameIntroductionText().Result);
                executingClient?.SendDtoMessage(gameIntroMsgDto);
            }
            else
            {
                var successMsgDto = new DescriptiveTextDto($"You have entered {character.GetLocation().LocationName}.");
                executingClient?.SendDtoMessage(successMsgDto);
            }
            var othersMsgDto = new DescriptiveTextDto($"{character.Name} has entered the area.");

            character.GetLocation().SendDescriptiveTextDtoMessage($"{character.Name} has entered the area.", character);
        }
Esempio n. 6
0
        public async Task TestSequence(IAccountGrain account, bool hasLogStored)
        {
            Assert.Equal(0u, await account.Balance());

            var initialdepositguid = Guid.NewGuid();
            await account.Deposit(100, initialdepositguid, "initial deposit");

            Assert.Equal(100u, await account.Balance());

            var firstwithdrawalguid = Guid.NewGuid();
            var success             = await account.Withdraw(70, firstwithdrawalguid, "first withdrawal");

            Assert.True(success);
            Assert.Equal(30u, await account.Balance());

            var secondwithdrawalguid = Guid.NewGuid();

            success = await account.Withdraw(70, secondwithdrawalguid, "second withdrawal");

            Assert.False(success);
            Assert.Equal(30u, await account.Balance());

            if (hasLogStored)
            {
                // check the transaction log
                var log = await account.GetTransactionLog();

                Assert.Equal(2, log.Count());
                Assert.Equal(initialdepositguid, log[0].Guid);
                Assert.Equal("initial deposit", log[0].Description);
                Assert.Equal(firstwithdrawalguid, log[1].Guid);
                Assert.Equal("first withdrawal", log[1].Description);
                Assert.True(log[0].IssueTime < log[1].IssueTime);
            }
            else
            {
                await Assert.ThrowsAsync(typeof(NotSupportedException),
                                         async() => await account.GetTransactionLog());
            }
        }
        public void Execute(List <string> extraWords, IAccountGrain executingAccount)
        {
            AttachedClient executingClient = AttachedClients.GetAccountFocusedClient(executingAccount);

            if (executingAccount == null || executingClient == null || !executingAccount.CanCreateNewPlayer().Result)
            {
                var errorMsgDto = new DescriptiveTextDto("The create new player command is currently unavailable.");
                executingClient?.SendDtoMessage(errorMsgDto);
                return;
            }

            if (extraWords.Count != 1)
            {
                var errorMsgDto = new DescriptiveTextDto("Wrong number of parameters.");
                executingClient?.SendDtoMessage(errorMsgDto);
                return;
            }

            if (GrainClusterClient.Universe.IsPlayerCharacterNameInUse(extraWords[0]).Result)
            {
                var errorMsgDto = new DescriptiveTextDto("This character name is already taken.");
                executingClient?.SendDtoMessage(errorMsgDto);
                return;
            }

            var newPlayerCharacter = EngineInternal.NewPlayerCreator();

            newPlayerCharacter.Name = extraWords[0];

            // Mark all player characters as needing focus to stay in the world
            newPlayerCharacter.NeedsFocus = true;
            executingAccount.AddCharacter(newPlayerCharacter.TrackingId).Wait();

            var successMsgDto = new DescriptiveTextDto("New player created.");

            executingClient?.SendDtoMessage(successMsgDto);
        }
Esempio n. 8
0
        internal static bool TryRunCommandFromAccount(string word, List <string> extraWords, IAccountGrain executingAccount)
        {
            // Built in account commands
            var accountCommandToRun = BuildInAccountCommands
                                      .FirstOrDefault(c => c.ActivatingWords.Any(w => w.Equals(word, StringComparison.OrdinalIgnoreCase)));

            // If the command was not found
            if (accountCommandToRun == null)
            {
                return(false);
            }

            // If the command requires a certain permission to run
            if (accountCommandToRun.PermissionNeeded != null)
            {
                var hasPermission = executingAccount.HasPermission(accountCommandToRun.PermissionNeeded).Result;
                if (!hasPermission)
                {
                    return(false);
                }
            }

            // All checks passed, run the command
            accountCommandToRun.Execute(extraWords, executingAccount);
            return(true);
        }
Esempio n. 9
0
        //public Task Transfer<TBalance>(long fromAccount, long toAccount, ulong amountToTransfer) where TBalance : Balance
        //{
        //    return Task.WhenAll(
        //        this.GrainFactory.GetGrain<IAccount<TBalance>>(fromAccount).Withdraw(amountToTransfer),
        //        this.GrainFactory.GetGrain<IAccount<TBalance>>(toAccount).Deposit(amountToTransfer));
        //}

        public Task Transfer <TBalance>(IAccountGrain <TBalance> fromAccount, IAccountGrain <TBalance> toAccount, ulong amountToTransfer) where TBalance : Balance
        {
            return(Task.WhenAll(
                       fromAccount.Withdraw(amountToTransfer),
                       toAccount.Deposit(amountToTransfer)));
        }
Esempio n. 10
0
 async Task <IEnumerable <IAccountGrain> > IUserGrain.DeleteAccounts(IAccountGrain acct)
 {
     throw new NotImplementedException();
 }