//todo (later) // ogre brut, dunemaul shaman, ogre ninja, mogor the ogre, == game-tag forgetfull //TODO secrets // resurrect, // blingtron 3000 // minions to do: //http://hearthstone.gamepedia.com/Random_effect static void Main(string[] args) { //Bot b = new Bot(); CardDB carddb = CardDB.Instance; HSServer hsc = new HSServer(); hsc.start(100); /* * bool readed =false; * while(!readed) * { * try * { * string data = System.IO.File.ReadAllText("crrntbrd.txt"); * if (data != "" && data != "<EoF>") * { * data = data.Replace("<EoF>", ""); * Helpfunctions.Instance.resetBuffer(); * Helpfunctions.Instance.writeBufferToFile(); * //readed = true; * b.doData(data); * } * } * catch * { * * } * System.Threading.Thread.Sleep(10); * }*/ Console.ReadLine(); }
public void writeBufferToFile(Socket client) { bool writed = true; this.sendbuffer += "<EoF>"; while (writed) { try { HSServer.sendToClient(client, this.sendbuffer); //System.IO.File.WriteAllText(Settings.Instance.path + "crrntbrd.txt", this.sendbuffer); writed = false; } catch { writed = true; } } this.sendbuffer = ""; }
public void writeBufferToActionFile(Socket client) { bool writed = true; this.sendbuffer += "<EoF>"; this.ErrorLog("write to action file: " + sendbuffer); while (writed) { try { HSServer.sendToClient(client, this.sendbuffer); //System.IO.File.WriteAllText(Settings.Instance.path + "actionstodo.txt", this.sendbuffer); writed = false; } catch { writed = true; } } this.sendbuffer = ""; }
public void start(int maxgames) { //read ip + port String ip_port = "127.0.0.1:11111"; try { ip_port = System.IO.File.ReadAllText("ip.txt").Replace("\r\n", "").Replace(" ", ""); } catch { } try { string ipa = ip_port.Split(':')[0]; serverListenPort = Convert.ToInt32(ip_port.Split(':')[1]); ipAddress = IPAddress.Parse(ipa); } catch { Console.WriteLine("Error: cant read ip + port, legal tupel is 127.0.0.1:11111"); } //ip + port readed.................................. DateTime start = DateTime.Now; int games = 0; ArrayList sockList = new ArrayList(2); ArrayList copyList = new ArrayList(2); Socket main = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint iep = new IPEndPoint(ipAddress, serverListenPort); byte[] data = new byte[1024]; string stringData; int recv; main.Bind(iep); main.Listen(2); Console.WriteLine("Waiting for 2 clients..."); Socket client1 = main.Accept(); IPEndPoint iep1 = (IPEndPoint)client1.RemoteEndPoint; client1.Send(Encoding.ASCII.GetBytes("connected with HSServer")); Console.WriteLine("Connected to {0}", iep1.ToString()); sockList.Add(client1); //TODO remove comment! Console.WriteLine("Waiting for 1 more client..."); Socket client2 = main.Accept(); IPEndPoint iep2 = (IPEndPoint)client2.RemoteEndPoint; Console.WriteLine("Connected to {0}", iep2.ToString()); client2.Send(Encoding.ASCII.GetBytes("connected with HSServer")); sockList.Add(client2); main.Close(); Boolean running = true; string oldinstructions = ""; int instructionCounter = 0; int roundcounter = 0; while (running) { copyList = new ArrayList(sockList); string win = ""; if (this.board != null) { win = this.board.getWinstring(); } Console.WriteLine("Monitoring {0} sockets..." + win, copyList.Count); Socket.Select(copyList, null, null, 100000000);//10000000 foreach (Socket client in copyList) { try { data = new byte[1024]; recv = client.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine("##############################Received: {0}", stringData); if (recv == 0) { iep = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Client {0} disconnected.", iep.ToString()); client.Close(); sockList.Remove(client); if (sockList.Count == 0) { Console.WriteLine("Last client disconnected, bye"); return; } } else { if (stringData.StartsWith("hello:")) { int port = (client.RemoteEndPoint as IPEndPoint).Port; //load player if (this.p1 == null) { p1 = new player(port, Directory.GetCurrentDirectory(), stringData.Split(':')[1]); p1.client = client; //TODO delte this: only for testing //p2 = new player(port, Directory.GetCurrentDirectory(), stringData.Split(':')[1]); //p2.client = client; //ready++; ready++; } else { p2 = new player(port, Directory.GetCurrentDirectory(), stringData.Split(':')[1]); p2.client = client; ready++; } HSServer.sendToClient(client, "ok " + port); Console.WriteLine("ok :" + ready); if (ready == 2) { //create board board = new HSBoard(p1, p2); } } if (stringData.EndsWith("<EoF>")) { //read instructions List <string> instructions = new List <string>((stringData.Replace("<EoF>", "")).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)); //we only need the first one! string boardl = instructions[0]; string boardnumm = "0"; float value = 0f; //foreach (string s in instructions) { if (boardl.StartsWith("board ")) { boardnumm = (boardl.Split(' ')[1].Split(' ')[0]); instructions.RemoveAt(0); /*if (boardnumm != Ai.Instance.currentCalculatedBoard) * { * if (passiveWaiting) * { * System.Threading.Thread.Sleep(10); * return; * } * continue; * }*/ } string first = instructions[0]; if (first.StartsWith("value ")) { value = float.Parse((first.Split(' ')[1].Split(' ')[0])); instructions.RemoveAt(0); } } Playfield p = new Playfield(this.board.board); List <Action> aclist = new List <Action>(); string instr = "";; foreach (string a in instructions) { aclist.Add(new Action(a, p)); instr += a; } if (instr == oldinstructions) { instructionCounter++; } else { instructionCounter = 0; } oldinstructions = instr; if (instructionCounter >= 2) { Console.WriteLine("ERROR#####################"); running = false; } if (aclist.Count == 0) { //end phase Console.WriteLine("END TURN#####################"); this.board.endturn(); instructionCounter = 0; oldinstructions = "1"; } else { roundcounter++; if (roundcounter >= 20) { running = false; } Helpfunctions.Instance.ErrorLog("DO ACTION:"); aclist[0].print(); //perform first action! if (aclist[0].card != null) { int id = aclist[0].card.entity; Handmanager.Handcard hc = this.board.getcardFromcurrentPlayer(id); if (hc.card.Secret) { this.board.playedSecret(hc.card.cardIDenum, hc.entity); } } this.board.doAction(aclist[0]); } if (this.board.winner != "") { String winsss = this.board.getWinstring(); Console.WriteLine(winsss); games++; if (games < maxgames) { this.board.replay(); } else { running = false; } } } } } catch (ArgumentNullException ane) { Console.WriteLine("ArgumentNullException : {0}", ane.ToString()); } catch (SocketException se) { Console.WriteLine("SocketException : {0}", se.ToString()); sockList.Remove(client); copyList = new ArrayList(sockList); if (copyList.Count == 0) { running = false; } } //catch (Exception e) //{ // Console.WriteLine("Unexpected exception : {0}", e.ToString()); //} } } Console.WriteLine("server closed"); Console.WriteLine("time: " + (DateTime.Now - start).TotalSeconds + " seconds OR " + (DateTime.Now - start).TotalMinutes + " minutes"); }