public MainWindow() { InitializeComponent(); this.viewModel = new GameModel(); //this.DataContext = this.viewModel; this.synchronizer = new ServerStateSynchronizer(this.viewModel); this.PlayArea.Visibility = Visibility.Hidden; this.ServerLobby.Visibility = Visibility.Hidden; this.GameSelect.Visibility = Visibility.Visible; this.AddressText.Text = Dns.GetHostName(); }
public static void Main(string[] args) { int port = 4502; string host = Dns.GetHostName(); Socket s = ConnectSocket(host, port); Console.Write("Enter id:"); string id = Console.ReadLine(); Send("CONNECT " + id, s); string result = GetNextMessage(s); Console.WriteLine(result); string authentication = result.Substring(13); string command; DateTime lastChat = DateTime.Now; GameModel gameModel = new GameModel(); ServerStateSynchronizer synchronizer = new ServerStateSynchronizer(gameModel); do { Console.WriteLine("Command(sendchat, viewchat, users, game, quit, accept, decline, check):"); command = Console.ReadLine(); if (command.StartsWith("sendchat")) { string chat = command.Substring(9); Send("SYSTEM|SENDCHAT " + chat, s); } else if (command.StartsWith("viewchat")) { Send("SYSTEM|GETCHAT " + lastChat.ToString(), s); lastChat = DateTime.Now; string chat = GetNextMessage(s); // starts with GETCHAT if (chat != "NOCHAT") { Console.WriteLine(chat.Substring(8)); } } else if (command.StartsWith("users")) { Send("SYSTEM|GETUSERS", s); string users = GetNextMessage(s); Console.WriteLine(users); } else if (command.StartsWith("game")) { Send("SYSTEM|REQUESTGAME" + command.Substring(4), s); } else if(command.StartsWith("check")) { string cmd = GetNextMessage(s); Console.WriteLine(cmd); } else if(command.StartsWith("decline")) { Send("SYSTEM|DECLINEGAME", s); } else if (command == "accept") { Send("SYSTEM|ACCEPTGAME", s); //string conn = GetNextMessage(s); string conn = ""; //Console.WriteLine("GOT: " + conn); while (conn != GameMessages.GamePrefix + GameMessages.GameEnded) { if (conn.StartsWith("GAME|")) { string msg = conn.Substring(5); if (msg == GameMessages.RequestAction) { PrintClientState(gameModel, id); Console.WriteLine("waiting for action"); string req = Console.ReadLine(); Send("GAME|" + req, s); } else if (msg.StartsWith(GameMessages.RequestChoice)) { int q1 = msg.IndexOf('\"') + 1; int q2 = msg.IndexOf('\"', q1); string choiceText = msg.Substring(q1, q2 - q1); int s1 = q2 + 2; int s2 = msg.IndexOf(' ', s1+1); string choiceType = msg.Substring(s1, s2 - s1); if (choiceType == "CARDPILE") { string[] c = msg.Substring(s2 + 1).Split(' '); string choiceSource = c[0]; int minChoices = int.Parse(c[1]); int maxChoices = int.Parse(c[2]); int nChoices = int.Parse(c[3]); Debug.Assert(nChoices == c.Length - 4); Console.WriteLine(choiceText); Console.WriteLine("Choose between " + minChoices + " and " + maxChoices); for (int i = 4; i < c.Length; i++) { Console.Write(c[i] + " "); } Console.WriteLine(); string input = Console.ReadLine(); Send("GAME|" + input, s); } else { Debug.Assert(choiceType == "EFFECT"); s1 = s2 + 1; s2 = msg.IndexOf(' ', s1); int minChoices = int.Parse(msg.Substring(s1, s2 - s1)); s1 = s2 + 1; s2 = msg.IndexOf(' ', s1); int maxChoices = int.Parse(msg.Substring(s1, s2 - s1)); s1 = s2 + 1; s2 = msg.IndexOf(' ', s1); int numChoices = int.Parse(msg.Substring(s1, s2 - s1)); Console.WriteLine(choiceText); Console.WriteLine("Choose between " + minChoices + " and " + maxChoices); for (int i = 0; i < numChoices; i++) { s1 = s2 + 1; s2 = msg.IndexOf(' ', s1 + 1); string ci = msg.Substring(s1, s2 - s1); q1 = msg.IndexOf('\"', s2) + 1; q2 = msg.IndexOf('\"', q1); string cdi = msg.Substring(q1, q2 - q1); s2 = q2; Console.WriteLine(cdi + " " + ci); } Console.WriteLine(); string input = Console.ReadLine(); Send("GAME|" + input, s); } } else if (msg.StartsWith(GameMessages.SupplyPileInfo)) { Console.WriteLine("The available cards are " + msg.Substring(GameMessages.SupplyPileInfo.Length)); // synchronizer.HandleClientStateMessage(msg); } else if (msg.StartsWith(GameMessages.PlayerInfo)) { Console.WriteLine("The players are " + msg.Substring(GameMessages.PlayerInfo.Length)); // synchronizer.HandleClientStateMessage(msg); } else if (msg.StartsWith(GameMessages.PlayerState) || msg.StartsWith(GameMessages.PileState) || msg.StartsWith(GameMessages.CardPileState)) { // synchronizer.HandleClientStateMessage(msg); } else if (msg.StartsWith(GameMessages.Log)) { Console.WriteLine(msg.Substring(GameMessages.Log.Length)); } } conn = GetNextMessage(s); } } } while (command != "quit"); Send("SYSTEM|DISCONNECT", s); s.Close(); }