public async Task ChunklerClient_CanHandle_SubscribeUpdates() { var taskCompletionSource = new TaskCompletionSource <object>(); ChunkUpdate update = null; await ChunklerClient.SubscribeOnChunkUpdateAsync(ActiveBattleChunkKey, chunkUpdate => { update = chunkUpdate; taskCompletionSource.SetResult(new object()); return(Task.CompletedTask); }); var action = new ChunkAction() { Color = 0, XIndex = 0, YIndex = 0 }; var changeIndex = await ChunklerClient.ProcessChunkActionAsync(ActiveBattleChunkKey, action); await Task.WhenAny(taskCompletionSource.Task, Task.Delay(10000)); Assert.NotNull(update); Assert.Equal(changeIndex, update.ChangeIndex); Assert.Equal(action.XIndex, update.XIndex); Assert.Equal(action.YIndex, update.YIndex); Assert.Equal(action.Color, update.Color); }
public List <MenuAction> getRiceTerrainActions(RiceTerrainTile riceTerrain) { //Debug.Log ("Actions for Terrain=" + riceTerrain.getChunkNumber ()); List <MenuAction> actionsAvailable = new List <MenuAction>(); if (!isActionInProgress(riceTerrain.getChunkNumber())) { List <int> actionsCurrentPhase = GameObject.FindGameObjectWithTag("Logic").GetComponent <PhaseManager>().getActionsInCurrentPhase(); for (uint i = 0; i < actionsCurrentPhase.Count; ++i) { int currentAction = actionsCurrentPhase[(int)i]; bool areDependenciesOk = riceTerrain.checkDependencies(_action[currentAction]); bool hasBeenInvestigated = InvestigationManager.GetInstance().areInvestigated(_action[currentAction].getInvestigationRequired()); bool hasWater = WorldTerrain.GetInstance().riceChunkHasWater(riceTerrain.getChunkNumber()) || !_action[currentAction].needCanal; bool hasActionDone = riceTerrain.isActionDone(currentAction); if (!hasActionDone && hasBeenInvestigated && areDependenciesOk && hasWater) { ChunkAction newAction = _action[currentAction]; PerformChunkAction callback = this.addActionInProgress; ChunkAction t = newAction.copyWithCallback(callback, riceTerrain.getChunkNumber()); actionsAvailable.Add((MenuAction)t); } //Debug.Log(" Action "+currentAction+" Dep="+areDependenciesOk+" Inv="+hasBeenInvestigated+" Water="+hasWater+" !Done="+!hasActionDone); } } actionsAvailable.Sort((x, y) => x.priority.CompareTo(y.priority)); return(actionsAvailable); }
public async Task ChunklerClient_CanHandle_UnsubscribeAfterSeveralSubscribes() { var firstTaskCompletionSource = new TaskCompletionSource <object>(); var secondTaskCompletionSource = new TaskCompletionSource <object>(); var subscription = await ChunklerClient.SubscribeOnChunkUpdateAsync(ActiveBattleChunkKey, chunkUpdate => { firstTaskCompletionSource.SetResult(new object()); return(Task.CompletedTask); }); await ChunklerClient.SubscribeOnChunkUpdateAsync(ActiveBattleChunkKey, chunkUpdate => { secondTaskCompletionSource.SetResult(new object()); return(Task.CompletedTask); }); await subscription.CloseAsync(); var action = new ChunkAction() { Color = 0, XIndex = 0, YIndex = 0 }; var changeIndex = await ChunklerClient.ProcessChunkActionAsync(ActiveBattleChunkKey, action); await Task.WhenAny(Task.WhenAll(firstTaskCompletionSource.Task, secondTaskCompletionSource.Task), Task.Delay(1000)); Assert.False(firstTaskCompletionSource.Task.IsCompleted); Assert.True(secondTaskCompletionSource.Task.IsCompleted); }
public static Dictionary <int, ChunkAction> readActions() { Dictionary <int, ChunkAction> actions = new Dictionary <int, ChunkAction> (); TextAsset file = Resources.Load("json/terrain_actions", typeof(TextAsset)) as TextAsset; if (file != null) { JSONNode node = JSON.Parse(file.text); for (int j = 0; j < node["ACTIONS"].Count; ++j) { JSONNode nodeDeeper = node["ACTIONS"][j]; int id = nodeDeeper ["ID"].AsInt; bool isRequired = nodeDeeper["IS_REQUIRED"].Value.Equals("TRUE"); bool hasPenalization = nodeDeeper["PENALIZATION"].Value.Equals("TRUE"); string name = Dictionary.getString(nodeDeeper["NAME"].Value); string desc = Dictionary.getString(nodeDeeper["DESCRIPTION"].Value); int duration = nodeDeeper ["DURATION"].AsInt; bool addAsDone = nodeDeeper ["ADD_AS_DONE"].Value.Equals("TRUE"); bool needCanal = nodeDeeper["NEED_CANAL"].Value.Equals("TRUE"); int workersNeeded = nodeDeeper["WORKERS_NEEDED"].AsInt; int priority = nodeDeeper["PRIORITY"].AsInt; ChunkAction chunkAction = new ChunkAction(id, isRequired, hasPenalization, name, desc, duration, addAsDone, needCanal, workersNeeded, priority); //TMP should be info and title instead of name and name for (int i = 0; i < nodeDeeper["DEPENDENCIES"].Count; ++i) { chunkAction.addDependency(nodeDeeper["DEPENDENCIES"][i]["TYPE"].Value, nodeDeeper["DEPENDENCIES"][i]["VALUE"].Value); } for (int i = 0; i < nodeDeeper["OBJECTS_ID"].Count; ++i) { chunkAction.addObjectRequired(nodeDeeper["OBJECTS_ID"][i].AsInt); } for (int i = 0; i < nodeDeeper["ANIMATION"].Count; ++i) { chunkAction.addAnimation(nodeDeeper["ANIMATION"][i].Value); } for (int i = 0; i < nodeDeeper["INVESTIGATION_REQUIRED"].Count; ++i) { chunkAction.addInvestigationRequired(nodeDeeper["INVESTIGATION_REQUIRED"][i].AsInt); } for (int i = 0; i < nodeDeeper["ACTION_PARTNERS"].Count; ++i) { chunkAction.addActionPartner(nodeDeeper["ACTION_PARTNERS"][i].AsInt); } actions.Add(id, chunkAction); } } else { Debug.Log("File json/terrain_actions not loaded"); } return(actions); }
public void addActionInProgress(ChunkAction action) { _actionInProgress.Add(action); //actions that have consequences per chunk not per tile if (action.id == 18 || action.id == 25) { Building_Planter planter = (Building_Planter)BuildingsManager.GetInstance().getBuilding(BUILDINGS.PLANTER); planter.agafarPlanta(); } }
public async Task <int> ProcessChunkAction(ChunkKey key, ChunkAction action) { if (Subscriptions.TryGetValue(key, out var chunkHandlerSubscription)) { return(await chunkHandlerSubscription.ChunkHandler.ProcessAsync(action)); } else { throw new InvalidOperationException("Can't access chunk that not subscribed."); } }
public ChunkAction copyWithCallback(PerformChunkAction callback, int chunk) { ChunkAction newAction = new ChunkAction(this.id, this.isRequired, this.hasPenalization, this.title, this.info, this.duration, this.addAsDone, this.needCanal, this.workersNeeded, this.priority); newAction.performChunkAction = callback; newAction._objectRequired = this._objectRequired; newAction._animation = this._animation; newAction._investigationRequired = this._investigationRequired; newAction._actionPartners = this._actionPartners; newAction.chunk = chunk; newAction.priority = priority; return(newAction); }
public void stopActionChunk(int chunk) { for (int i = 0; i < _actionInProgress.Count; ++i) { ChunkAction action = _actionInProgress[i]; bool isActionInProgressNow = action.chunk == chunk; if (isActionInProgressNow) { action.returnObjects(); WorkerManager.GetInstance().BusyWorkers -= action.workersNeeded; _actionInProgress.RemoveAt(i); break; } } }
public void load(ActionManagerData actionManagerData) { //public ChunkAction(int aID, bool aIsRequired, bool aHasPenalization, string aTitle, string aInfo, int aDuration, bool hasToAddAsDone, bool isCanalNeeded, int neededWorkers) foreach (ActionInProgressData data in actionManagerData.ActionsInProgress) { ChunkAction newAction = _action[data.ID]; PerformChunkAction callback = this.addActionInProgress; ChunkAction t = newAction.copyWithCallback(callback, data.ChunkNumber); t.hoursElapsed = data.HoursElapsed; if (t.hasAnimation()) { t.starAnimationWithRemainingDistance(data.AnimationRemainingDistance); } t.performChunkAction(t); } }
public async Task OnNextAsync(ChunkAction action, StreamSequenceToken token = null) { State.ChangeIndex++; _pixelsCache[action.XIndex + action.YIndex * _chunkWidth].Rgba = action.Color; State.Image = _imageProcessor.GetBytesFromPixels(_pixelsCache, _chunkHeight, _chunkWidth); await WriteStateAsync(); await _chunkUpdateEventStream.OnNextAsync(new ChunkUpdate { ChangeIndex = State.ChangeIndex, Color = action.Color, XIndex = action.XIndex, YIndex = action.YIndex }); }
public bool checkYearDependency(ChunkAction action) { Dictionary <string, ArrayList> dependencies = action.getDependencies(); bool dependenciesOK = true; foreach (KeyValuePair <string, ArrayList> kvp in dependencies) { if (kvp.Key.Equals(DEPENDENCY_TYPE.START_YEAR.ToString(), System.StringComparison.Ordinal)) { //kvp.Value[0] dependenciesOK = dependenciesOK && (Convert.ToUInt32(kvp.Value[0]) <= GameObject.FindGameObjectWithTag("Logic").GetComponent <TimeManager> ().getCurrentYear()); } else if (kvp.Key.Equals(DEPENDENCY_TYPE.END_YEAR.ToString(), System.StringComparison.Ordinal)) { //kvp.Value[0] dependenciesOK = dependenciesOK && (Convert.ToUInt32(kvp.Value[0]) > GameObject.FindGameObjectWithTag("Logic").GetComponent <TimeManager> ().getCurrentYear()); } } return(dependenciesOK); }
public bool checkDependencies(ChunkAction action) { Dictionary <string, ArrayList> dependencies = action.getDependencies(); bool dependenciesOK = true; foreach (KeyValuePair <string, ArrayList> kvp in dependencies) { if (kvp.Key.Equals(DEPENDENCY_TYPE.ACTION.ToString(), System.StringComparison.Ordinal)) { for (uint i = 0; i < kvp.Value.Count; ++i) { //if (checkYearDependency (_action [(string)kvp.Value [(int)i]])) { dependenciesOK = dependenciesOK && this.isActionDone((string)kvp.Value [(int)i]); //} } } else if (kvp.Key.Equals(DEPENDENCY_TYPE.START_YEAR.ToString(), System.StringComparison.Ordinal)) { //kvp.Value[0] uint currentYear = GameObject.FindGameObjectWithTag("Logic").GetComponent <TimeManager> ().getCurrentYear(); uint dependencyYear = Convert.ToUInt32(kvp.Value [0]); dependenciesOK = dependenciesOK && (dependencyYear <= currentYear); } else if (kvp.Key.Equals(DEPENDENCY_TYPE.END_YEAR.ToString(), System.StringComparison.Ordinal)) { //kvp.Value[0] dependenciesOK = dependenciesOK && (Convert.ToUInt32(kvp.Value [0]) > GameObject.FindGameObjectWithTag("Logic").GetComponent <TimeManager> ().getCurrentYear()); } else if (kvp.Key.Equals(DEPENDENCY_TYPE.STATUS.ToString(), System.StringComparison.Ordinal)) { for (int i = 0; i < kvp.Value.Count; ++i) { if (kvp.Value [i].Equals(STATUS_TYPE.PLANTED.ToString())) { dependenciesOK = dependenciesOK && this.isPlanted(); } else if (kvp.Value [i].Equals(STATUS_TYPE.WORKED_ONCE.ToString())) { dependenciesOK = dependenciesOK && this.hasTileAlreadyBeenWorked(); } else if (kvp.Value [i].Equals(STATUS_TYPE.WEED.ToString())) { dependenciesOK = dependenciesOK && WorldTerrain.GetInstance().hasChunkWeed(_chunkID); } else if (kvp.Value [i].Equals(STATUS_TYPE.WILD_RICE.ToString())) { dependenciesOK = dependenciesOK && WorldTerrain.GetInstance().hasChunkWildRice(_chunkID); } else if (kvp.Value [i].Equals(STATUS_TYPE.PLAGUE.ToString())) { dependenciesOK = dependenciesOK && WorldTerrain.GetInstance().hasChunkPlague(_chunkID); } else if (kvp.Value[i].Equals(STATUS_TYPE.PLANTER_PLANTED.ToString())) { Building_Planter planter = (Building_Planter)BuildingsManager.GetInstance().getBuilding(BUILDINGS.PLANTER); dependenciesOK = dependenciesOK && planter.hasPlantsForAChunk(); } } } else if (kvp.Key.Equals(DEPENDENCY_TYPE.ACTION_NOT_DONE.ToString(), System.StringComparison.Ordinal)) { for (uint i = 0; i < kvp.Value.Count; ++i) { dependenciesOK = dependenciesOK && !this.isActionDone((string)kvp.Value [(int)i]); } } } return(dependenciesOK); }
public async Task EnqueueChunkActionAsync(ChunkKey key, ChunkAction action) { var stream = _streamProvider.GetStream <ChunkAction>(FormatChunkKey(key), ChunklerConstants.ChunkIncomingAction); await stream.OnNextAsync(action); }
public Task <int> ProcessChunkActionAsync(ChunkKey key, ChunkAction action) { var chunk = _clusterClient.GetGrain <IChunkGrain>(FormatChunkKey(key)); return(chunk.ProcessActionAsync(action)); }