public Task <string> UpdatePlayer(string playerSessionID, IPlayerObserver playerObserver, Vec3 position) { int idx = mActivePlayers.FindIndex(item => (item.mID == playerSessionID)); if (idx >= 0) { if ((position.x < mMinLocation.x) || (position.y < mMinLocation.y) || (position.z < mMinLocation.z) || (position.x > mMaxLocation.x) || (position.y > mMaxLocation.y) || (position.z > mMaxLocation.z)) { IChunkGrain transfer = TransferPlayerTo(playerSessionID, playerObserver, ref position, mActivePlayers[idx].mPosition); if (transfer != null) { LeavePlayer(playerSessionID); return(Task.FromResult <string>(transfer.GetPrimaryKeyString())); } } mActivePlayers[idx].mPosition = position; mActivePlayers[idx].mVersion = MinecraftVersion.GetNext(); return(Task.FromResult <string>(this.GetPrimaryKeyString())); } return(Task.FromResult <string>(null)); }
public Task <ResponseToChunkUpdateMessage> InformOfChange(List <IPlayerObserver> initObservers, List <IPlayerObserver> updateObservers, MinecraftVersion lastPlayerVersion, int suggestedMaxPlayerRequests, MinecraftVersion lastBlockVersion, int suggestedMaxBlockRequests) { //gather new changes from the last delta of changes with the max requested being filled out int nBlocksLeft, nPlayersLeft; MinecraftVersion lastBlockUsedVersion; MinecraftVersion lastPlayerUsedVersion; var maxVersion = MinecraftVersion.GetNext(); GatherChangesAndUpdateObservers(updateObservers, lastBlockVersion, maxVersion, suggestedMaxBlockRequests, lastPlayerVersion, maxVersion, suggestedMaxPlayerRequests, out nBlocksLeft, out nPlayersLeft, out lastBlockUsedVersion, out lastPlayerUsedVersion); //information needed to calculate the ask for next request var response = new ResponseToChunkUpdateMessage(); response.mChunkID = this.mID; response.mAvailableBlocks = nBlocksLeft; response.mAvailablePlayers = nPlayersLeft; response.mLastPlayersVersion = lastPlayerUsedVersion; response.mLastBlockVersion = lastBlockUsedVersion; //bring all new observers up to the same place as everyone else, but this might not be everything if (initObservers.Count > 0) { GatherChangesAndUpdateObservers(initObservers, MinecraftVersion.MinValue, response.mLastBlockVersion, int.MaxValue, MinecraftVersion.MinValue, response.mLastPlayersVersion, int.MaxValue, out nBlocksLeft, out nPlayersLeft, out lastBlockUsedVersion, out lastPlayerUsedVersion); } return(Task.FromResult <ResponseToChunkUpdateMessage>(response)); }
public Task <FeedbackMessage> StartPlayer(string playerSessionID, IPlayerObserver playerObserver, Vec3 position, string[] fromGrain) { //Is player already here? int idx = mActivePlayers.FindIndex(item => (item.mID == playerSessionID)); if (idx >= 0) { return(Task.FromResult <FeedbackMessage>(new FeedbackMessage(FeedbackMessage.Responces.Error, "Already Exist"))); } //Add this player to the active list for other to monitors PlayerInfo update = new PlayerInfo(); update.mPosition = position; update.mVersion = MinecraftVersion.GetNext(); update.mID = playerSessionID; mActivePlayers.Add(update); //this player want to listen to all associated blocks mNeedsInitObservers.Add(playerObserver); //each associated grain needs to update this one foreach (var pair in mAssociatedGrains) { bool bFound = false; if (fromGrain != null) { foreach (var id in fromGrain) { if (id == pair.Key) { bFound = true; break; } } } if (bFound == false) { var tracking = pair.Value; tracking.mNeedInitObservers.Add(playerObserver); } } //if no updates are occuring, now we have a player to update if (mTimer == null) { mTimer = RegisterTimer((_) => Fetch(), null, TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(mUpdateRateinMilliSeconds)); } return(Task.FromResult <FeedbackMessage>(new FeedbackMessage(FeedbackMessage.Responces.None))); }
public async Task <GenericResponse> RegisterObserver(ActorId playerAgentID, ActorId[] fromActors) { var players = await this.StateManager.GetStateAsync <Dictionary <ActorId, MinecraftVersion> >("observers"); var version = MinecraftVersion.GetNext(); //Is player already here? if (players.ContainsKey(playerAgentID) == true) { return(new GenericResponse(true, "Already Exist")); } //this player want to listen to all associated blocks var initPlayers = await this.StateManager.GetStateAsync <Dictionary <ActorId, MinecraftVersion> >("initializeObservers"); initPlayers.Add(playerAgentID, version); await this.StateManager.SetStateAsync("initializeObservers", initPlayers); //map to hash for faster lookup var map = new HashSet <ActorId>(); foreach (var actor in fromActors) { map.Add(actor); } var associations = await this.StateManager.GetStateAsync <Dictionary <ActorId, AssociateChunkMetaData> >("associations"); //each associated grain needs to update this one foreach (var pair in associations) { var actorID = pair.Key; if (map.Contains(actorID) == false) { var assocations = pair.Value; assocations.needInitObservers.Add(actorID); } } await this.StateManager.SetStateAsync("associations", associations); //if no updates are occuring, now we have a player to update if (_updateTimer == null) { int updateRate = await this.StateManager.GetStateAsync <int>("updateRateMS"); _updateTimer = RegisterTimer((_) => Fetch(), null, TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(updateRate)); } return(new GenericResponse()); }
public async Task Validate_MinecraftVersion() { MinecraftVersion v1 = MinecraftVersion.GetNext(); Thread.Sleep(TimeSpan.FromMilliseconds(1)); MinecraftVersion v2 = MinecraftVersion.GetNext(); bool bGreaterThan = (v1 < v2); bool bLessThan = (v2 > v1); bool bGreaterEqualThan = (v1 <= v2); bool bLessEqualThan = (v2 >= v1); Assert.Equal(true, bGreaterEqualThan); Assert.Equal(true, bLessEqualThan); Assert.Equal(true, bLessThan); Assert.Equal(true, bGreaterThan); }
public async Task <InformOfChunkChangeResponse> InformOfChange(ActorId[] observer, AssociateChunkMetaData associate, TrackingChunkMetaData tracking) { var maxVersion = MinecraftVersion.GetNext(); //Get the data associated with request var blockPerChunkActor = ActorProxy.Create <BlocksPerChunkActor>(associate.blocksPerChunkActorId, this.ServiceUri); var playerPerChunkActor = ActorProxy.Create <PlayersPerChunkActor>(associate.playersPerChunkActorId, this.ServiceUri); var taskPlayerData = playerPerChunkActor.GetAsync(); var taskBlockData = blockPerChunkActor.GetAsync(); await Task.WhenAll(taskPlayerData, taskBlockData); var blockData = taskBlockData.Result; var playerData = taskPlayerData.Result; //gather new changes from the last delta of changes with the max requested being filled out GatherChangesAndUpdateObservers(observer, tracking.blockVersion, maxVersion, tracking.maxBlocks, tracking.playerVersion, maxVersion, tracking.maxPlayers, out int nBlocksLeft, out int nPlayersLeft, out MinecraftVersion lastBlockUsedVersion, out MinecraftVersion lastPlayerUsedVersion, blockData, playerData); //information needed to calculate the ask for next request var response = new InformOfChunkChangeResponse() { actorID = this.Id, availableBlocks = nBlocksLeft, availablePlayers = nPlayersLeft, lastPlayersVersion = lastPlayerUsedVersion, lastBlockVersion = lastBlockUsedVersion }; //bring all new observers up to the same place as everyone else, but this might not be everything if (associate.needInitObservers.Count > 0) { GatherChangesAndUpdateObservers(associate.needInitObservers.ToArray(), MinecraftVersion.minValue, response.lastBlockVersion, int.MaxValue, MinecraftVersion.minValue, response.lastPlayersVersion, int.MaxValue, out nBlocksLeft, out nPlayersLeft, out lastBlockUsedVersion, out lastPlayerUsedVersion, blockData, playerData); } return(response); }
public async Task <GenericResponse> Update(ActorId playerAgentID, PlayerMetaData playerData) { var player = await this.StateManager.GetStateAsync <Dictionary <ActorId, PlayerMetaData> >(_playerTag); playerData.version = MinecraftVersion.GetNext(); if (player.ContainsKey(playerAgentID)) { player[playerAgentID] = playerData; } else { player.Add(playerAgentID, playerData); } await this.StateManager.SetStateAsync(_playerTag, player); return(new GenericResponse()); }
public async Task <GenericResponse> Update(long playerAgentID, BlockMetaData blockData) { var blocks = await this.StateManager.GetStateAsync <Dictionary <Position, BlockMetaData> >("blocks"); blockData.version = MinecraftVersion.GetNext(); if (blocks.ContainsKey(blockData.position)) { blocks[blockData.position] = blockData; } else { blocks.Add(blockData.position, blockData); } await this.StateManager.SetStateAsync("blocks", blocks); return(new GenericResponse()); }
public Task <FeedbackMessage> UpdateBlock(string playerSessionID, BlockInfo blockUpdate) { //remove previous version int idx = mBlocks.FindIndex(item => ((item.mPosition.x == blockUpdate.mPosition.x) && (item.mPosition.y == blockUpdate.mPosition.y) && (item.mPosition.z == blockUpdate.mPosition.z))); if (idx >= 0) { mBlocks.RemoveAt(idx); } //add new version at end MinecraftVersion version = MinecraftVersion.GetNext(); blockUpdate.mVersion = version; mBlocks.Add(blockUpdate); return(Task.FromResult <FeedbackMessage>(new FeedbackMessage(FeedbackMessage.Responces.None))); }