private static void ProcessDeltas(XmlDocument doc, CardLoader cl, bool verbose = false) { foreach (XmlElement e in doc.GetElementsByTagName("delta")) { Delta d = Delta.FromXml(e, cl); if (verbose) { Console.WriteLine("Processing delta: '{0}'", e.OuterXml); } d.Apply(); } }
private void ProcessActionResults(XmlDocument document) { List <XmlElement> deltaElements = GetDeltaElements(document.DocumentElement); Debug.Log($"Processing {deltaElements.Count} action deltas"); foreach (XmlElement element in deltaElements) { Delta delta = Delta.FromXml(element, cardLoader); delta.Apply(); } uiManager.RenderUnits(); uiManager.LockUnits(); uiManager.UpdateDiscardDisplay(0); uiManager.UpdateDiscardDisplay(1); uiManager.UpdateResourceDisplay(0); uiManager.UpdateResourceDisplay(1); }
private IEnumerator ProcessTurnStart(XmlDocument document) { List <XmlElement> inputRequestElements = GetInputRequestElements(document.DocumentElement); if (inputRequestElements.Count > 0) { // Input request time! List <InputRequest> requests = inputRequestElements.Select(InputRequest.FromXml).ToList(); Debug.Log($"Processing {inputRequestElements.Count} input requests"); foreach (InputRequest request in requests) { yield return(StartCoroutine(ProcessInputRequest(request))); } client.SendInputRequestResponse(requests.ToArray()); Task <XmlDocument> confirmInput = client.ReceiveDocument(type => type == "turnStart"); yield return(new WaitUntil(() => confirmInput.IsCompleted)); yield return(StartCoroutine(ProcessTurnStart(confirmInput.Result))); } List <TurnPhase> phases = document .DocumentElement .GetElementsByTagName("phase") .OfType <XmlElement>() .Select(element => new TurnPhase(element, cardLoader)) .ToList(); foreach (TurnPhase phase in phases) { List <Delta> deltas = phase.Deltas; if (deltas.Count == 0) { continue; } string phaseDisplayName = GetPhaseName(phase.Name); if (phaseDisplayName != null) { yield return(uiManager.ShowPhaseName(phaseDisplayName)); } Debug.Log($"Processing {deltas.Count} deltas of phase {phase.Name}"); foreach (Delta delta in deltas) { delta.Apply(); yield return(StartCoroutine(AnimateDelta(delta))); } // Special case for draw phase. I don't really like this, but I don't see a way around it. if (phase.Name == "startDeploy") { yield return(uiManager.OpponentDrawCards()); } } List <Delta> unphasedDeltas = document .DocumentElement .ChildNodes .OfType <XmlElement>() .Where(element => element.Name == "delta") .Select(element => Delta.FromXml(element, CardLoader.instance)) .ToList(); Debug.Log($"Processing {unphasedDeltas.Count} deltas outside phases"); foreach (Delta delta in unphasedDeltas) { delta.Apply(); yield return(StartCoroutine(AnimateDelta(delta))); } }
public static void Main() { //find local IP IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipAddr = ipEntry.AddressList[0]; //consts string HostName = ipAddr.ToString(); //by default, is using same local IP addr as server; assuming above process is deterministic const int Port = 4011; //objs TcpClient client; NetworkStream stream; // used for testing delta code List <Deck> decks = new List <Deck>(); Console.WriteLine("Connecting to server at {0}:{1}...", HostName, Port); try{ //actual work client = new TcpClient(HostName, Port); Console.WriteLine("Connected!"); CardLoader cl = new CardLoader(); Console.WriteLine("Testing Unkown Card == Card: {0}", new UnknownCard() == cl.LoadCard("Exostellar Marine Squad")? "Success" : "Fail"); Console.WriteLine("Testing Card == Unkown Card: {0}", cl.LoadCard("Exostellar Marine Squad") == new UnknownCard() ? "Success" : "Fail"); stream = client.GetStream(); byte[] data; /* * //send a message * Console.WriteLine("Press Enter to start a message..."); * Console.Read(); * data = System.Text.Encoding.UTF8.GetBytes("<file type='HI!!!'>"); * stream.Write(data, 0, data.Length); * * //test if will wait for EOF * Console.WriteLine("Press Enter to finish the message..."); * Console.Read(); * data = System.Text.Encoding.UTF8.GetBytes("</file>GARBAGE"); * stream.Write(data, 0, data.Length); * * //recieve * data = new byte[256]; * stream.Read(data, 0, data.Length); * Console.WriteLine("Response from server: {0}",System.Text.Encoding.UTF8.GetString(data)); * // */ //join a game Console.WriteLine("Press Enter to join a game..."); Console.Read(); data = System.Text.Encoding.UTF8.GetBytes("<file type='joinMatch'><deck id='testing'/></file>"); stream.Write(data, 0, data.Length); //* //get matchstart message data = new byte[256]; stream.Read(data, 0, data.Length); string startResp = System.Text.Encoding.UTF8.GetString(data); Console.WriteLine("Response from server: {0}", startResp); XmlDocument doc = new XmlDocument(); doc.LoadXml(startResp); foreach (XmlElement e in doc.GetElementsByTagName("playerIds")) { Deck deck = new Deck(e.Attributes["deck"].Value); DeckList list = new DeckList(); list.AddCard(new UnknownCard(), 20); // the dummy placeholder deck deck.LoadCards(list); decks.Add(deck); } // */ //send a gameaction message Console.WriteLine("Press Enter to make a move..."); Console.Read(); data = System.Text.Encoding.UTF8.GetBytes("<file type='gameAction'><action></action></file>"); stream.Write(data, 0, data.Length); // get and parse XML deltas data = new byte[256]; stream.Read(data, 0, data.Length); string resp = System.Text.Encoding.UTF8.GetString(data); Console.WriteLine("Response from server: {0}", resp); doc = new XmlDocument(); doc.LoadXml(resp); foreach (XmlElement e in doc.GetElementsByTagName("delta")) { Delta d = Delta.FromXml(e, cl); Console.WriteLine("Delta of type {0} parsed.", d.GetType()); } //get and parse A LOT of Xml, for analysis purposes int tests = 1000; long avMillis = 0; Stopwatch watch = new Stopwatch(); Stopwatch totalWatch = new Stopwatch(); totalWatch.Start(); for (int i = 0; i < tests; i++) { watch.Start(); // send request data = System.Text.Encoding.UTF8.GetBytes("<file type='gameAction'><action></action></file>"); stream.Write(data, 0, data.Length); // get and parse XML deltas data = new byte[256]; stream.Read(data, 0, data.Length); resp = System.Text.Encoding.UTF8.GetString(data); doc = new XmlDocument(); doc.LoadXml(resp); foreach (XmlElement e in doc.GetElementsByTagName("delta")) { Delta d = Delta.FromXml(e, cl); } watch.Stop(); avMillis += watch.ElapsedMilliseconds / ((long)tests); watch.Reset(); } totalWatch.Stop(); Console.WriteLine("{0} tests parsing Xml run; avergage time was {1} millis; total time was {2} millis.", tests, avMillis, totalWatch.ElapsedMilliseconds); //send a end turn message Console.WriteLine("Press Enter to end turn..."); Console.Read(); data = System.Text.Encoding.UTF8.GetBytes("<file type='lockInTurn'></file>"); stream.Write(data, 0, data.Length); data = new byte[256]; stream.Read(data, 0, data.Length); Console.WriteLine("Response from server: {0}", System.Text.Encoding.UTF8.GetString(data)); //send a gameaction message Console.WriteLine("Press Enter to make a move..."); Console.Read(); data = System.Text.Encoding.UTF8.GetBytes("<file type='gameAction'><action></action></file>"); stream.Write(data, 0, data.Length); data = new byte[256]; stream.Read(data, 0, data.Length); Console.WriteLine("Response from server: {0}", System.Text.Encoding.UTF8.GetString(data)); //send a gameaction message Console.WriteLine("Press Enter to make a move..."); Console.Read(); data = System.Text.Encoding.UTF8.GetBytes("<file type='gameAction'><action></action></file>"); stream.Write(data, 0, data.Length); data = new byte[256]; stream.Read(data, 0, data.Length); Console.WriteLine("Response from server: {0}", System.Text.Encoding.UTF8.GetString(data)); //send a end turn message Console.WriteLine("Press Enter to end turn..."); Console.Read(); data = System.Text.Encoding.UTF8.GetBytes("<file type='lockInTurn'></file>"); stream.Write(data, 0, data.Length); //wait for user then close Console.WriteLine("Presse Enter to disconnect..."); Console.Read(); stream.Close(); client.Close(); }catch (SocketException e) { Console.WriteLine("SocketEsception: {0}", e); } }