public HSBoard(player pl1, player pl2) { //set player random p1 = pl1; p2 = pl2; if (getrandom(0, 1) == 1)//other player begins { player p = p1; p1 = p2; p2 = p; } p1.HEntity = 3; p2.HEntity = 4; nextentity = 10; this.nextentity = p1.initDeck(this.nextentity); this.nextentity = p2.initDeck(this.nextentity); replay(); }
public void updateStuffAndSend() { this.board.printBoard(); if (this.board.ownHero.Hp <= 0 || this.board.enemyHero.Hp <= 0) { Console.WriteLine("SOME ONE WINS in " + this.board.turnCounter + " turns"); player p1w = p2; player p2w = p1; if (this.currentplayer) { p1w = p1; p2w = p2; } if (this.board.ownHero.Hp <= 0) { this.winner = p2w.folder + p2w.socket; p2w.wins++; Console.WriteLine(p1w.socket + "looses"); if (this.board.enemyHero.Hp <= 0) { p1w.wins++;// split :D } } else { this.winner = p1w.folder + p1w.socket; p1w.wins++; Console.WriteLine(p2w.socket + "looses"); } return; } sendCurrentBoardToClient(); }
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"); }
public void updateHrtprozisAtStart() { int controler = 2; player p = p2; player opponent = p1; if (this.currentplayer) { controler = 1; p = p1; opponent = p2; } Hrtprozis.Instance.setOwnPlayer(controler); Hrtprozis.Instance.updatePlayer(p.maxMana, p.curMana, p.cardsPlayedThisTurn, p.numMinionsPlayedThisTurn, p.numOptionPlayedThisTurn, p.overdrive, p.HEntity, opponent.HEntity, p.numberMinionsDiedThisturn, p.owncurrentRecall, opponent.owncurrentRecall, 0, 0); //new values a zero at start Hrtprozis.Instance.setPlayereffects(p.ownDragonConsort, opponent.ownDragonConsort, p.ownLoathebs, opponent.ownLoathebs, p.ownMillhouse, opponent.ownMillhouse, p.ownKirintor, p.ownPrep, 0, 0, 0); //new values a zero at start //TODO //Hrtprozis.Instance.updateSecretStuff(this.ownsecretlist, enemySecretAmount); bool herowindfury = false; if (p.ownHeroWeapon == "doomhammer") { herowindfury = true; } Minion ownHero = new Minion(); Minion enemyHero = new Minion(); ownHero.isHero = true; enemyHero.isHero = true; ownHero.own = true; enemyHero.own = false; ownHero.maxHp = p.ownheromaxhp; enemyHero.maxHp = opponent.ownheromaxhp; ownHero.entitiyID = p.HEntity; enemyHero.entitiyID = opponent.HEntity; ownHero.Angr = p.ownHeroAttack; ownHero.Hp = p.ownherohp; ownHero.armor = p.ownherodefence; ownHero.frozen = p.ownHeroFrozen; ownHero.immuneWhileAttacking = p.ownHeroimmunewhileattacking; ownHero.immune = p.heroImmune; ownHero.numAttacksThisTurn = p.ownheroattacksThisRound; ownHero.windfury = herowindfury; enemyHero.Angr = opponent.ownHeroWeaponAttack; enemyHero.Hp = opponent.ownherohp; enemyHero.frozen = opponent.ownHeroFrozen; enemyHero.armor = opponent.ownherodefence; enemyHero.immune = opponent.heroImmune; enemyHero.Ready = false; ownHero.updateReadyness(); Console.WriteLine("herodata:" + p.heroname + " " + ownHero.Hp + " " + ownHero.maxHp); Hrtprozis.Instance.updateOwnHero(p.ownHeroWeapon, p.ownHeroWeaponAttack, p.ownHeroWeaponDurability, p.heroname, p.heroAbility, p.heroAbilityReady, ownHero, 0); //at start heropower uses this game = 0 Hrtprozis.Instance.updateEnemyHero(opponent.ownHeroWeapon, opponent.ownHeroWeaponAttack, opponent.ownHeroWeaponDurability, opponent.heroname, opponent.maxMana, opponent.heroAbility, enemyHero, 0); //at start heropower uses this game = 0 Hrtprozis.Instance.updateMinions(p.ownminions, opponent.ownminions); Hrtprozis.Instance.updateFatigueStats(p.deck.Count, p.ownFatigue, opponent.deck.Count, opponent.ownFatigue); //TODO /* * Probabilitymaker.Instance.setEnemySecretData(enemySecrets); * * Probabilitymaker.Instance.setTurnGraveYard(this.turnGraveYard); * Probabilitymaker.Instance.stalaggDead = this.stalaggdead; * Probabilitymaker.Instance.feugenDead = this.feugendead; * * if (og != "") Probabilitymaker.Instance.readGraveyards(og, eg); * if (omd != "") Probabilitymaker.Instance.readTurnGraveYard(omd, emd);*/ }