public static void CheckAuction(Game g, Player bot) { var p = bot; var cell = g.currAuction.cell; var sum = g.GetPlayerCash(p.Id); var fact = BotBrain.FactorOfBuy(g, p, cell); var aucSum = cell.Cost * fact; if (sum >= aucSum && aucSum > g.currAuction.nextBid) { g.currAuction.currBid = g.currAuction.nextBid; g.Tlog("Auction.Yes", "@p{0} дает {1}", "@p{0} bid {1}", p.Id, g.currAuction.currBid.PrintMoney()); g.currAuction.LastBiddedPlayer = p; } else { g.Tlog("Auction.No", "@p{0} выбывает", "@p{0} is out", p.Id); //g.aucState.allPlayers = g.aucState.allPlayers.Where(x => x.Id != p.Id).ToList(); p.InAuction = false; } }
public static void MakeAuctionNo(Game g, string userName) { var p = g.GetPlayer(userName); g.Tlog("PlayerAction.AuctionNo.PlayerOut", "@p{0} выбывает", "@p{0} is out", p.Id); p.InAuction = false; GameManager.CheckAuctionWinner(g); }
public static string RenderAuction(Game g, string userName) { if (g == null) return "finish game"; var res = UIParts("HtmlAuction"); if (g.currAuction == null) return "auction is null"; var curr = g.currAuction.CurrPlayer; if (curr == null) return "wait..."; if (curr.Name != userName) return "wait..."; if (g.currAuction.IsFinished) { var pl = g.currAuction.LastBiddedPlayer; if (pl != null) return string.Format("{0} is winner, your bid is {1}", pl.htmlName, g.currAuction.currBid); else return string.Format(" bid is {0}, no players", g.currAuction.currBid); } else { var startBid = g.currAuction.cell.Cost; var bid = g.currAuction.currBid; var pls = g.Players.Where(x => x.InAuction).Select(x => x.Name).ToArray(); return string.Format(res, startBid, bid, bid + 50000, g.currAuction.cell.Name, string.Join(":", pls)); } return res; }
public static bool TryDoTrade(Game g) { //var res = MakeExchange(g, g.Curr); var trs = GetValidTrades(g, g.Curr); Trade tr = null; foreach (var item in trs) { var res = g.RejectedTrades.Any(x => x.Equals(item)); if (!res) { tr = item; break; } } if (tr != null) { g.CurrTrade = tr; g.Tlog("CheckTradeJob.FromBotToHuman", "Обмен между {0} и {1}", "Exchange between {0} and {1}", tr.from.Name, tr.to.Name); g.SetState(GameState.Trade); //GameManager.CheckTradeJob(g); return true; } return false; }
protected JsonResult RenderRound(int r, Game g) { var list = g.RoundActions.Where(x => x.round == r).ToList(); var ra = list.Any() ? list.Last() : new GameAction { Cells = g.Cells, Players = g.Players }; var data = new { Map = from x in ra.Cells select new { id = x.Id, text = x.PrintTextOnCell, color = MapHelper.GetPlayerColorRGB(x.Owner) }, Players = from x in MapHelper.GetPlayerState(ra.Players) select new { id = x.Key, images = x.Value }, State = "finished", GameLog = SimHelper.RoundLog(r, list).text, PlayersInfo = SimHelper.GetPlayersInfo(ra), PlayersState = RenderRazorViewToString("Game/PlayersState", g), Round = r, MaxRound = g.RoundNumber }; return base.Json(data); }
public static string RenderTradeProposal(Game g, string userName) { var res = "player {0} want trade with {1} <br />player {0} -> {2} <br /> player {1} -> {3} <br />"; var action = "<input id=\"btnTrY\" type=\"button\" value=\"yes\" onclick=\"action('tr_y');\" />" + "<input id=\"btnTrNo\" type=\"button\" value=\"no\" onclick=\"action('tr_no');\" />"; //request of player var pl = g.GetPlayer(userName); var fr = g.CurrTrade.from.htmlName; var to = g.CurrTrade.to.htmlName; var fromProposal = g.CurrTrade.give_cells.Aggregate("", (acc, i) => acc + "," + g.Cells[i].Name); fromProposal += g.CurrTrade.giveMoney == 0 ? "" : string.Format(", money = {0}K", g.CurrTrade.giveMoney); var toProposal = g.CurrTrade.get_cells.Aggregate("", (acc, i) => acc + "," + g.Cells[i].Name); toProposal += g.CurrTrade.getMoney == 0 ? "" : string.Format(", money = {0}K", g.CurrTrade.getMoney); if (pl != null && pl.Id == g.CurrTrade.to.Id) { return string.Format(res + action, fr, to, fromProposal, toProposal); } else return string.Format(res, fr, to, fromProposal, toProposal); }
public ActionResult Go(string act) { g = GetGame(); if (g == null) return RedirectToAction("Index", "Home"); if (act == "exit") { GameManager.LeaveGame(g, CurrUser); return RedirectToAction("Index", "Home"); } if (act == "InitManTrades") { SiteGameHelper.LoadResources(g, Server.MapPath(ConfigHelper.ResDir)); } if (act == "SetAsBot") { var pl = g.GetPlayer(CurrUser); if (pl != null) pl.IsBot = !pl.IsBot; } GameHandlers.Go(g, act, CurrUser); return RenderGame(g); }
public ActionResult Build(string str) { g = GetGame(); var res = ""; if (Request.IsAuthenticated) { var actRes = GameHandlers.BuildOrSell(g, str, "build", CurrUser); if (actRes) res = string.Format("{0} вы построили :", g.Curr.htmlName); else res = string.Format("{0} не хватает денег:", g.Curr.htmlName); } else { res = "error"; } var jsonData = new { //MapInfo = RenderGame(g), GameLog = string.Join("<br />", g.LogInfo), PlayerCells = RenderRazorViewToString("Game/PlayerCells", g), }; return RenderLogAndPlayerCells(g); }
public static bool CanOutFromPolice(Game g) { var p = g.Curr; bool rollDouble = p.LastRoll[0] == p.LastRoll[1]; if (rollDouble) return true; if (p.IsBot && BotBrain.ShouldGoFromPolice(g)) { g.Tlogp("step.PoliceOut", "заплатил 500 k$, чтобы выйти", "you paid 500 k$ to out"); PlayerAction.Pay(g, 500000); return true; } else { p.Police++; if (p.Police == 4) { g.Tlogp("ProcessPolice.PoliceOut", "заплатите 500 k$, чтобы выйти", "you need pay 500 k$ to out"); g.PayAmount = 500000; g.ToPay(false); return true; } else { g.Tlogp("ProcessPolice.PoliceCatch", "вы можете заплатить и выйти ", "you can pay 500 k$ and out"); return false; } } return false; }
public static void CheckAuctionWinner(Game g) { var count = g.Players.Count(x => x.InAuction); if (count == 0 || (g.currAuction.LastBiddedPlayer != null && count == 1)) g.currAuction.IsFinished = true; if (g.currAuction.IsFinished) { var last = g.currAuction.LastBiddedPlayer; if (last != null) { if (BotBrain.MortgageSell(g, last, g.currAuction.currBid)) { g.Tlog("Game.CheckAuctionWinner.Winner", "@p{0} выиграл", "@p{0} winner", last.Id); //set winner GameManager.SetAuctionWinner(g); } } //g.Tlog("Game.CheckAuctionWinner.FinishStepForBot", "FinishStepForBot", "FinishStepForBot"); g.FinishStep(); } }
public static bool Run(Game g, string comm) { var arr = comm.Split(';'); if (comm.StartsWith("set-pos")) { g.Players[int.Parse(arr[1])].Pos = int.Parse(arr[2]); } if (comm.StartsWith("set-money")) { g.Players[int.Parse(arr[1])].Money = int.Parse(arr[2]); } if (comm.StartsWith("set-cellowner")) { var cell = g.Cells[int.Parse(arr[2])]; cell.Owner = int.Parse(arr[1]); g.Map.UpdateMap(); } if (comm.StartsWith("set-gamestate")) { g.State = GameState.BeginStep; } if (comm.StartsWith("make-trade")) { var p0 = arr[1].Split('-'); int from = int.Parse(p0[0].Replace("p", "")); GameManager.InitTrade(g, arr[1] + ";" + arr[2], from); GameManager.MakeTrade(g); } return true; }
//state - p0-1-2-3-m400 public static bool BuildOrSell(Game g, string state, string action, string userName) { //dont build if user on your land if (action == "build" && (g.State == GameState.NeedPay || g.State == GameState.CantPay)) return false; var pst = state.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); var ss = pst.OrderByDescending(x => x.Length).Take(1).ToArray(); Player from = g.GetPlayer(userName); var q = ss.Length == 1; if (q && from != null) { var from_ids = ss[0].Replace("p" + from.Id, "").Trim('-').Split('-').Select(x => Int32.Parse(x)).ToArray(); if (action == "build") return PlayerAction.Build(g, from.Id, from_ids); else { PlayerAction.SellHouses(g, from.Id, from_ids); return true; } } return false; //g.FinishStep(); }
public static void CheckPayState(Game g) { if (g.Curr.IsBot) { var res = PlayerAction.Pay(g); if (!res) LeaveGame(g, g.Curr.Name); } }
public static void AddPlayers(Game g, int count, string[] names = null) { for (int i = 0; i < count; i++) { var botName = names != null ? names[i] : "bot-" + i; g.Players.Add(new Player { Id = i, Name = botName, OneDirection = true, IsBot = true, Status = "master", Money = 15000000 }); } //g.OnLeavedGame += (s, w) => { GameAction.SaveToDB(s, g, w); }; }
public static void InitWithData(Game g, string path) { g.InitRules(path); //string path2 = Path.Combine(path, "res_23.xml"); //g.Texts = GameHelper.RestoreTexts(File.ReadAllText(path2)); //var ui_xml = File.ReadAllText(Path.Combine(path, "ui.xml")); //g.UIParts = GameHelper.LoadUI(g, ui_xml); }
public void SetGame(Game game) { this.game = game; #region Game events subscription game.sendDiceResult += game_diceResult; game.requestXmlForBibliotheca += game_requestXmlForBibliotheca; #endregion }
public static IEnumerable<string> GetLog(Game g, int countLines) { string filename = Path.Combine(Path.GetTempPath(), "log.htm"); var count = g.Logs.Count(); if (2 * countLines < count) return g.Logs.Take(countLines).Union(new[] { "************************" }).Union(g.Logs.Skip(count - countLines)); else return g.Logs; }
public static List<int[]> GetChartData(Game game) { var res = new List<int[]>(); foreach (var gr in game.RoundActions.GroupBy(x => x.round)) { var ra = gr.First(); res.Add(ra.Players.Select(pl => GameHelper.GetPlayerAssets(pl.Id, pl.Money, ra.Cells, true)).ToArray()); } return res; }
public string LeaveGame() { g = GetGame(); if (g != null) { GameManager.LeaveGame(g, CurrUser); //Monop.Data.DataManager.SaveState(CurrUser, g.LastGameState,false); } return ""; }
public ActionResult Next() { g = GetGame(); if (g != null) { //GameManager.CheckState(g); PlayerStep.MakeStep(g); } return RenderGame(g); ; }
public static string[] GetGameActions(Game g) { if (g.IsFinished) { return new string[] { g.Winner.htmlName, Simulator.ShowTrades(g), Simulator.ShowPlayersCells(g) }; } return null; }
//example //from player: from have 2_russia + another 1_russia //to: to have 2_france + 1_france private static Trade CheckOnPlayersCells(Game g, TradeRule trd, int my) { var pfrom = g.GetPlayer(my); //from_pl get cells var wantedCellsGroupedByUser = g.Map.CellsByGroup(trd.GetLand) .Where(x => x.Owner.HasValue && x.Owner != my) .GroupBy(x => x.Owner.Value); if (!wantedCellsGroupedByUser.Any()) return null; //process for each player foreach (var wantedByUser in wantedCellsGroupedByUser) { if (wantedByUser.Count() != trd.GetCount) continue; var pto = g.GetPlayer(wantedByUser.First().Owner.Value); // i have var _myCells = g.Map.CellsByUserByGroup(my, trd.GetLand).Count() == trd.MyCount; //you have var _yourCells = g.Map.CellsByUserByGroup(pto.Id, trd.GiveLand).Count() == trd.YourCount; //i give to you var giveCells = g.Map.CellsByUserByGroup(my, trd.GiveLand); //money factor var money1 = g.GetPlayerAssets(my, false); var money2 = g.GetPlayerAssets(pto.Id, false); var mfac = (money1 / (double)money2) >= trd.MoneyFactor; if (giveCells.Count() == trd.GiveCount && _myCells && _yourCells && mfac) { //g.trState = new TradeState return new Trade { from = g.GetPlayer(my), give_cells = giveCells.Select(x => x.Id).ToArray(), giveMoney = trd.GiveMoney, //fromMoney = string.IsNullOrEmpty(from_money) ? 0 : Int32.Parse(from_money), to = pto, get_cells = wantedByUser.Select(x => x.Id).ToArray(), getMoney = trd.GetMoney, ExchId = trd.Id, }; } } return null; }
private static void ShowInfo(Game g) { var res = Simulator.GetResult(g); string filename = Path.Combine(Path.GetTempPath(), "log.htm"); File.WriteAllText(filename, DateTime.Now + "<br />"); //show log File.AppendAllText(filename, "winner=" + res[0] + "<br />"); File.AppendAllText(filename, res[1] + "<br />"); File.AppendAllText(filename, res[2] + "<br />"); File.AppendAllLines(filename, Simulator.GetLog(g, 30).Select(x => x + "<br />")); Process.Start(filename); }
public static bool MakeTradeFromPlayer(Game g) { var ptrade = g.CurrTrade; if (ptrade.to.IsBot) { var trs = GetValidTrades(g, g.CurrTrade.to); if (IsGoodTrade(ptrade, trs)) { GameManager.MakeTrade(g); g.FixAction("trade_completed"); return true; } } g.ToBeginRound(); return false; }
public ActionResult Play() { g = GetGame(); if (Request.IsAuthenticated) { ViewData["admin"] = IsAdmin; if (g != null) { ViewData["rollMode"] = g.conf.cnfRollMode; Session["gid"] = g.Id.ToString(); return View(); } else return RedirectToAction("Index", "Home"); } else return RedirectToAction("LogOn", "Account"); }
public static void BuildHouses(Game g, int Sum, int? Group) { //if (!g.Curr.IsBot) return; var p = g.Curr; var groupCells = from x in g.Map.CellsByUserByType(p.Id, 1) where x.IsMonopoly group x by x.Group into gr where gr.All(a => !a.IsMortgage) select new { Key = gr.Key, Vals = gr }; if (Group.HasValue) groupCells = groupCells.Where(x => x.Key == Group); //var amSpent = GetCashForHouses(g, p); string text = ""; foreach (var gr in groupCells. OrderByDescending(x => x.Vals.Max(a => a.HousesCount))) { var cost = gr.Vals.First().HouseCost; while (BotBrain.Mortgage(g, p, cost) && gr.Vals.Any(c => c.HousesCount < 5)) foreach (var cell in gr.Vals.OrderBy(c => c.HousesCount).ThenByDescending(x => x.Id)) { if (p.Money < cell.HouseCost) BotBrain.Mortgage(g, p, cell.HouseCost); if (p.Money >= cell.HouseCost && cell.HousesCount < 5) { cell.HousesCount++; p.Money -= cell.HouseCost; text = text + "_" + cell.Id; } } } if (text != "") { g.FixAction("build" + text); } }
public static void Buy(Game g) { if (g.State != GameState.CanBuy) return; var p = g.Curr; var cell = g.CurrCell; if (cell.IsLand) { if (cell.Owner == null) { //--buy var ff = BotBrain.FactorOfBuy(g, p, cell); bool needBuy = ff >= 1; if (p.IsBot) { if (ff >= 1 && p.Money < cell.Cost) needBuy = BotBrain.MortgageSell(g, cell.Cost); } else { if (p.Money < cell.Cost) { g.ToCantPay(); return; } } if (needBuy) { g.Map.SetOwner(p, cell); g.Tlogp("PlayerAction.Bought", "вы купили {0} за {1}", "You bought {0} for {1}", cell.Name, cell.Cost.PrintMoney()); g.FinishStep(string.Format("bought_{0}_f({1})", cell.Id, ff)); } else { g.ToAuction(); } } } }
public ActionResult Index() { g = GetGame(); if (Request.IsAuthenticated) { ViewData["admin"] = IsAdmin; if (g != null) { ViewData["game"] = g; return View(); } else return RedirectToAction("Index", "Home"); } else return RedirectToAction("LogOn", "Account"); return View(); }
public void Login() { if (inputString != "") { string playerName = inputString; Communicator.init(inputString, "46.101.155.56", 5222); communicator = Communicator.getInstance(); game = new GameLogic.Game(playerName); communicator.SetGame(game); outputString = "Connesso"; GameEventManager_requestXmlForBibliotheca(); } else { outputString = "Inserisci il nome Utente"; } }
public static void MakeStep(Game g, bool needRoll = true) { //g.logp("rolls {0} {1}", g.LastRoll); if (g.State != GameState.BeginStep) return; var p = g.Curr; if (p.IsBot && BotBrain.BeforeRollMakeBotAction(g)) return; g.CleanTimeOut(); if (needRoll) GameManager.MakeRoll(g); bool CanGo = true; if (p.Pos == 10 && p.Police > 0) { CanGo = CanOutFromPolice(g); if (p.Police == 4) return; } if (CanGo) { var NotTrippleRoll = g.Curr.Step(); if (NotTrippleRoll) { ProcessPosition(g); } else { g.Tlogp("step.TripleRoll", "вы выкинули тройной дубль, вас задержала милиция", "you roll triple, go to POLICE"); g.FinishStep(); } } else { g.FinishStep(); } }