private IEnumerable <bool> ParseHideEntity() { if (HideEntityRegex.IsMatch(this.parsing_log)) { var match = HideEntityRegex.Match(this.parsing_log); string entity_str; int entityId = ParserUtilities.GetEntityIdFromRawString(this.game_state, match.Groups["entity"].Value, out entity_str); if (entityId < 0) { var prev_entity = game_state.Entities.Items[entityId].Clone(); game_state.ChangeTag(entity_str, match.Groups["tag"].Value, match.Groups["value"].Value); EntityTagChanged(this, new EntityTagChangedEventArgs() { prev = prev_entity, current = game_state.Entities.Items[entityId] }); } else { game_state.ChangeTag(entityId, match.Groups["tag"].Value, match.Groups["value"].Value); } yield return(true); } }
private void PatchPlayerName(String entity_raw, State.GameTag tag, int tag_value) { if (tag == State.GameTag.PLAYSTATE && tag_value == (int)State.TAG_PLAYSTATE.PLAYING) { // the player's name can only be known from debug print power logs // so we parse the name out, and write back to our entity object string entity_str; if (ParserUtilities.GetEntityIdFromRawString(this.game_state, entity_raw, out entity_str) >= 0) { return; // already has id } foreach (var entity in this.game_state.Entities.Items) { foreach (var entity_tag in entity.Value.Tags) { if (entity_tag.Key == State.GameTag.PLAYSTATE && entity_tag.Value == (int)State.TAG_PLAYSTATE.PLAYING) { if (entity.Value.Name == String.Empty) { game_state.ChangeEntityName(entity.Key, entity_str); return; } } } } logger_.Info("Failed to patch player name."); } }
private IEnumerable <bool> ParseTagChange() { var match = TagChangeRegex.Match(this.parsing_log); if (!match.Success) { yield break; } var entity_raw = match.Groups["entity"].Value; string entity_str; int entityId = ParserUtilities.GetEntityIdFromRawString(this.game_state, entity_raw, out entity_str); if (entityId >= 0) { State.ReadOnlyEntity prev_entity = game_state.Entities.Items[entityId].Clone(); this.game_state.ChangeTag(entityId, match.Groups["tag"].Value, match.Groups["value"].Value); EntityTagChanged(this, new EntityTagChangedEventArgs() { prev = prev_entity, current = game_state.Entities.Items[entityId] }); } else { game_state.ChangeTag(entity_str, match.Groups["tag"].Value, match.Groups["value"].Value); } yield return(true); }
private IEnumerable <bool> ParseEntityChoiceCreate() { var match = EntityChoicesCreate.Match(this.parsing_log); if (!match.Success) { yield break; } int entity_choice_id = -1; if (int.TryParse(match.Groups["id"].Value.Trim(), out entity_choice_id) == false) { logger_.Info("[INFO] Cannot get entity choice id (ignoring): " + this.parsing_log); } var player_entity_raw = match.Groups["player_entity"].Value.Trim(); string player_entity_str; int player_entity_id = ParserUtilities.GetEntityIdFromRawString(this.game_state, player_entity_raw, out player_entity_str); var choice_type = match.Groups["choice_type"].Value.Trim(); if (this.game_state.EntityChoices.ContainsKey(entity_choice_id)) { logger_.Info("[ERROR] Entity choice index overlapped (overwritting)"); this.game_state.EntityChoices.Remove(entity_choice_id); } this.game_state.EntityChoices[entity_choice_id] = new State.EntityChoice(); this.game_state.EntityChoices[entity_choice_id].id = entity_choice_id; this.game_state.EntityChoices[entity_choice_id].choice_type = choice_type; this.game_state.EntityChoices[entity_choice_id].player_entity_id = player_entity_id; this.game_state.EntityChoices[entity_choice_id].player_entity_str = player_entity_str; this.game_state.EntityChoices[entity_choice_id].choices_has_sent = false; yield return(true); while (true) { if (!ParseEntityChoiceSource(entity_choice_id)) { break; } yield return(true); } while (true) { if (ParseEntityChoiceEntity(entity_choice_id)) { yield return(true); } else { break; } } }
// return for each consumed line // break if error and the line is not consumed private IEnumerable <bool> ParseShowEntities() { var match = ShowEntityRegex.Match(this.parsing_log); if (!match.Success) { yield break; } var cardId = match.Groups["cardId"].Value; int entityId = ParserUtilities.GetEntityIdFromRawString(this.game_state, match.Groups["entity"].Value); if (entityId < 0) { logger_.Info("[ERROR] Parse error: cannot find entity id. '" + this.parsing_log + "'"); yield return(false); } if (!this.game_state.Entities.Items.ContainsKey(entityId)) { this.game_state.CreateEntity(entityId); } var prev_entity = game_state.Entities.Items[entityId].Clone(); this.game_state.ChangeEntityCardId(entityId, cardId); yield return(true); while (true) { if (this.ParseCreatingTag(entityId)) { yield return(true); } else { break; } } EntityTagChanged(this, new EntityTagChangedEventArgs() { prev = prev_entity, current = game_state.Entities.Items[entityId] }); }
private IEnumerable <bool> ParseOneItem() { int entity_choice_id = -1; while (true) { if (!SendChoices.IsMatch(this.parsing_log)) { logger_.Info("[INFO] Waiting for send-choice log, but got " + this.parsing_log + " (ignoring)"); continue; } var match = SendChoices.Match(this.parsing_log); entity_choice_id = Int32.Parse(match.Groups["id"].Value.Trim()); this.game_state.EntityChoices[entity_choice_id].choices_has_sent = true; yield return(true); break; } while (true) { if (SendChoicesEntities.IsMatch(this.parsing_log)) { var match_chosen_entities = SendChoicesEntities.Match(this.parsing_log); var chosen_entity = match_chosen_entities.Groups["entity"].Value.Trim(); var chosen_entity_id = ParserUtilities.GetEntityIdFromRawString(this.game_state, chosen_entity); if (chosen_entity_id < 0) { logger_.Info("[ERROR] Failed to get entity id for a choice: " + this.parsing_log + " (ignoring)"); yield return(true); continue; } this.game_state.EntityChoices[entity_choice_id].sent_choices.Add(chosen_entity_id); yield return(true); } else { break; } } }
private bool ParseEntityChoiceEntity(int entity_choice_id) { if (!EntityChoicesEntities.IsMatch(this.parsing_log)) { return(false); } var match = EntityChoicesEntities.Match(this.parsing_log); if (this.game_state.EntityChoices.ContainsKey(entity_choice_id) == false) { logger_.Info("[ERROR] missing current entity choice id"); return(false); } int idx; if (int.TryParse(match.Groups["idx"].Value.Trim(), out idx) == false) { logger_.Info("[ERROR] Parse failed: " + this.parsing_log); return(false); } var entity_raw = match.Groups["entity"].Value.Trim(); int entity_id = ParserUtilities.GetEntityIdFromRawString(this.game_state, entity_raw); if (entity_id < 0) { logger_.Info("[ERROR] Failed to get entity id: " + this.parsing_log); return(false); } this.game_state.EntityChoices[entity_choice_id].choices[idx] = entity_id; // Trigger this.EntityChoiceAdded(entity_choice_id); return(true); }
private IEnumerable <bool> ParseBlock() { var match = BlockStartRegex.Match(this.parsing_log); if (!match.Success) { yield break; } var indent = match.Groups["indent"].Value; var entity_raw = match.Groups["entity"].Value.Trim(); var block_type = match.Groups["block_type"].Value.Trim(); var target_raw = match.Groups["target"].Value.Trim(); var entity_id = ParserUtilities.GetEntityIdFromRawString(this.game_state, entity_raw); if (entity_id < 0) { logger_.Info(String.Format("[INFO] Cannot get entity id '{0}'.", entity_raw)); } var target_id = ParserUtilities.GetEntityIdFromRawString(this.game_state, target_raw); if (target_id < 0) { logger_.Info(String.Format("[INFO] Cannot get target id '{0}'.", target_raw)); } if (this.BlockStart != null) { this.BlockStart(this, new BlockStartEventArgs(entity_id, block_type)); } yield return(true); while (true) { if (BlockEndRegex.IsMatch(this.parsing_log)) { break; } bool matched = false; foreach (var ret in ParserUtilities.TrySubParsers(new List <Func <IEnumerable <bool> > > { () => this.ParseFullEntity(), () => this.ParseShowEntities(), () => this.ParseTagChange(), () => this.ParseHideEntity(), () => this.ParseBlock(), () => this.ParseMetadata() })) { matched = true; yield return(ret); } if (!matched) { yield break; } } if (this.BlockEnd != null) { this.BlockEnd(this, new BlockEndEventArgs(entity_id, block_type)); } yield return(true); }
private IEnumerable <bool> ParseCreateGame() { if (!CreateGameRegex.IsMatch(this.parsing_log)) { yield break; } if (this.CreateGameEvent != null) { this.CreateGameEvent(this, new CreateGameEventArgs()); } yield return(true); { bool matched = false; foreach (var ret in this.ParseGameEntityCreation()) { matched = true; yield return(ret); } if (!matched) { yield break; } } for (int i = 0; i < 2; i++) // two players { bool matched = false; foreach (var ret in this.ParsePlayerEntityCreation()) { matched = true; yield return(ret); } if (!matched) { yield break; } } while (true) { bool matched = false; foreach (var ret in ParserUtilities.TrySubParsers(new List <Func <IEnumerable <bool> > > { () => { return(this.ParseFullEntity()); }, () => { return(this.ParseShowEntities()); }, () => { return(this.ParseTagChange()); }, () => { return(this.ParseHideEntity()); }, () => { return(this.ParseBlock()); } })) { matched = true; yield return(ret); } if (!matched) { yield break; } } }