public async void SaveHand(DiceHand handIn, long userIn, long gameIn, int rollNumber) { foreach (var die in handIn.Hand) { var savetask = await this.SaveRoll(die, userIn, gameIn, rollNumber); } }
public async Task <GameWinType> EvaluateGame(DiceHand handIn) { var dice = handIn.Hand; var shakevalue = await _shake.GetShakeOfDay(DateTime.Now.Year, DateTime.Now.DayOfYear); var pairs = dice.Count(x => x.dieValue == shakevalue); //future use? var sumNonRolls = dice.Where(x => x.dieValue != shakevalue).Sum(x => x.dieValue); switch (pairs) { case 3: return(GameWinType.three); case 4: return(GameWinType.four); case 5: return(GameWinType.five); default: return(GameWinType.loss); } }
public IActionResult Get() { var testHand = new DiceHand(new List <Dice>() { new Dice(1), new Dice(1), new Dice(2), new Dice(2), new Dice(2) }, 3); return(Ok(_engine.EvaluateGame(testHand).Result)); }
public IActionResult RollDice(long gameid, long userId, [FromBody] DiceHand handIn) { //get user id from login ? if (!ModelState.IsValid || handIn.Hand.Count != 5) { throw new Exception("invalid model state"); } //if(handIn.) var newHand = _engine.PerformRoll(userId, gameid, handIn).Result; //if the roll comes back as nothing if (newHand.Hand.Count == 0) { return(Ok(new ShakeException(ShakeError.NoMoreRollsAllowed, $"You are out of rolls for game {gameid}"))); } //TODO: move this into it's own route?? var type = _gameRepo.GetGameType(GameTypeEnum.ShakeOfTheDay).Result; if (newHand.RollNumber == type.RollsPerGame) { var wintype = _engine.EvaluateGame(handIn).Result; var t = _gameRepo.CloseGame(gameid, wintype).Result; if (wintype != GameWinType.loss) { bool result; switch (wintype) { case GameWinType.three: result = _wallets.TransferAmountFromJackpot(userId, 1).Result; break; case GameWinType.four: result = _wallets.TransferAmountFromJackpot(userId, 5).Result; break; case GameWinType.five: result = _wallets.TransferJackpot(userId).Result; break; default: break; } } } return(Ok(newHand)); }
/// <summary> /// /// </summary> /// <param name="userId"></param> /// <param name="gameId"></param> /// <param name="diceQty"></param> /// <returns></returns> public async Task <DiceHand> PerformRoll(long userId, long gameId, DiceHand handIn) { var thisGame = await _gameRepo.GetGameById(gameId); var gType = await _gameRepo.GetGameType(thisGame.TypeId); if (thisGame.RollsTaken < gType.RollsPerGame) { handIn.RollNonHeldDice(); var updt = _gameRepo.UpdateGameRollsTaken(thisGame.Id, (thisGame.RollsTaken + 1)); _diceRepo.SaveHand(handIn, userId, gameId, (thisGame.RollsTaken + 1)); //TODO: check results of update, handle if error? handIn.RollNumber = (thisGame.RollsTaken + 1); return(handIn); } else { return(new DiceHand()); } }
/// <summary>Displays the next frame.</summary> public override void Render(IGraphics graphics, long currentTimeInMicroseconds) { if (model.CurrentGameBox.Reference == model.GameLibrary.DefaultGameBox) { // display "dice" logo IRotation rotation1 = model.ComputeRotationYawPitchRoll( 0.750000000f + 0.1f * (float)Math.Cos((float)Math.PI * 2.0f * (currentTimeInMicroseconds % (long)7000000) / 7000000.0f), 0.521750554f + 0.1f * (float)Math.Cos((float)Math.PI * 2.0f * (currentTimeInMicroseconds % (long)5000000) / 5000000.0f), 0.339836909f + 0.1f * (float)Math.Cos((float)Math.PI * 2.0f * (currentTimeInMicroseconds % (long)9000000) / 9000000.0f)); IRotation rotation2 = model.ComputeRotationYawPitchRoll( 0.750000000f + 0.1f * (float)Math.Cos((float)Math.PI * 2.0f * (currentTimeInMicroseconds % 9000000) / 9000000.0f), 2.356194490f + 0.1f * (float)Math.Cos((float)Math.PI * 2.0f * (currentTimeInMicroseconds % 7000000) / 7000000.0f), 3.141592654f + 0.1f * (float)Math.Cos((float)Math.PI * 2.0f * (currentTimeInMicroseconds % 5000000) / 5000000.0f)); Rectangle gameDisplayArea = view.GameDisplayAreaInPixels; logoMesh.Render( new PointF(gameDisplayArea.X + gameDisplayArea.Width * 0.43f, gameDisplayArea.Y + gameDisplayArea.Height * 0.35f), gameDisplayArea.Height * 0.1f, rotation1.ToRotationMatrix(), (uint)Color.LightGreen.ToArgb(), 0xff000000); logoMesh.Render( new PointF(gameDisplayArea.X + gameDisplayArea.Width * 0.57f, gameDisplayArea.Y + gameDisplayArea.Height * 0.45f), gameDisplayArea.Height * 0.1f, rotation2.ToRotationMatrix(), (uint)Color.Salmon.ToArgb(), 0xff000000); } else { DiceHand[] diceHands = model.CurrentGameBox.CurrentGame.DiceHands; if (diceHands.Length > 0) { // check for mouse roll over int rolledOverDiceHandIndex = 0; int rolledOverDiceCount = 0; if (model.ThisPlayer.CursorLocation is IDiceBagCursorLocation) { IDiceBagCursorLocation diceBagMouseLocation = (IDiceBagCursorLocation)model.ThisPlayer.CursorLocation; rolledOverDiceHandIndex = diceBagMouseLocation.DiceHandIndex; rolledOverDiceCount = diceBagMouseLocation.DiceCount; } // render background //IImage image = graphics.CreateMonochromaticImage(0xFFFFFFFF); float scale = view.GameDisplayAreaInPixels.Height / 1200.0f; //image.Render(new RectangleF(area.X + 7.0f * scale, area.Y + 6.0f * scale, area.Width - 14.0f * scale, area.Height - 12.0f * scale), 0xb0f0c67d); frameImageElements[0].Render(new RectangleF(area.X, area.Y, 11.0f * scale, 10.0f * scale)); frameImageElements[1].Render(new RectangleF(area.X + 11.0f * scale, area.Y, area.Width - 22.0f * scale, 10.0f * scale)); frameImageElements[2].Render(new RectangleF(area.Right - 11.0f * scale, area.Y, 11.0f * scale, 10.0f * scale)); frameImageElements[3].Render(new RectangleF(area.X, area.Y + 10.0f * scale, 11.0f * scale, area.Height - 20.0f * scale)); frameImageElements[4].Render(new RectangleF(area.X + 11.0f * scale, area.Y + 10.0f * scale, area.Width - 22.0f * scale, area.Height - 20.0f * scale)); frameImageElements[5].Render(new RectangleF(area.Right - 11.0f * scale, area.Y + 10.0f * scale, 11.0f * scale, area.Height - 20.0f * scale)); frameImageElements[6].Render(new RectangleF(area.X, area.Bottom - 10.0f * scale, 11.0f * scale, 10.0f * scale)); frameImageElements[7].Render(new RectangleF(area.X + 11.0f * scale, area.Bottom - 10.0f * scale, area.Width - 22.0f * scale, 10.0f * scale)); frameImageElements[8].Render(new RectangleF(area.Right - 11.0f * scale, area.Bottom - 10.0f * scale, 11.0f * scale, 10.0f * scale)); float singleLineHeight = area.Width * 0.2f; // idle dice for (int diceHandIndex = 0; diceHandIndex < diceHands.Length; ++diceHandIndex) { DiceHand diceHand = diceHands[diceHandIndex]; for (int dieIndex = 0; dieIndex < diceHand.Dice.Length; ++dieIndex) { if (!diceHand.BeingCast || dieIndex >= diceHand.DiceCountBeingCast) { Die die = diceHand.Dice[dieIndex]; IDieMesh dieMesh = die.DieMesh; float size = die.Size * (!diceHand.BeingCast && (model.ThisPlayer.PieceBeingDragged == null && model.ThisPlayer.StackBeingDragged == null) && (rolledOverDiceHandIndex == diceHandIndex && rolledOverDiceCount > dieIndex) ? 1.40f : 1.0f); dieMesh.Render( die.Position, size, die.Orientation.ToRotationMatrix(), die.Color, die.Pips); } } } // shadows of dice being cast for (int diceHandIndex = 0; diceHandIndex < diceHands.Length; ++diceHandIndex) { DiceHand diceHand = diceHands[diceHandIndex]; for (int dieIndex = 0; dieIndex < diceHand.Dice.Length; ++dieIndex) { if (diceHand.BeingCast && dieIndex < diceHand.DiceCountBeingCast) { Die die = diceHand.Dice[dieIndex]; IDieMesh dieMesh = die.DieMesh; float size = die.Size; dieMesh.RenderShadow( die.Position, size, die.Orientation.ToRotationMatrix(), 0x40000000); } } } // dice being cast for (int diceHandIndex = 0; diceHandIndex < diceHands.Length; ++diceHandIndex) { DiceHand diceHand = diceHands[diceHandIndex]; for (int dieIndex = 0; dieIndex < diceHand.Dice.Length; ++dieIndex) { if (diceHand.BeingCast && dieIndex < diceHand.DiceCountBeingCast) { Die die = diceHand.Dice[dieIndex]; IDieMesh dieMesh = die.DieMesh; float size = die.Size; dieMesh.Render( die.Position, size, die.Orientation.ToRotationMatrix(), die.Color, die.Pips); } } } } } }
internal Game(GameBox gameBox, ScenarioType scenarioType, string fileName, Stream stream) { this.gameBox = gameBox; this.scenarioType = scenarioType; this.fileName = fileName; XmlDocument xml = new XmlDocument(); xml.Load(stream); // Dice diceHands = new DiceHand[gameBox.DiceHands.Length]; for (int i = 0; i < diceHands.Length; ++i) { diceHands[i].DiceType = gameBox.DiceHands[i].DiceType; diceHands[i].BeingCast = false; int diceCount = gameBox.DiceHands[i].Count; diceHands[i].Dice = new Die[diceCount]; for (int j = 0; j < diceCount; ++j) { diceHands[i].Dice[j] = (gameBox.DiceHands[i].TextureFileName[j] != null ? new Die(gameBox.DiceHands[i].TextureFileName[j]) : new Die(gameBox.DiceHands[i].Colors[j], gameBox.DiceHands[i].Pips[j])); } } XmlElement gameNode = xml.DocumentElement; if (gameNode.SelectSingleNode("game-box") != null) { // old format // Scenario data scenarioName = xml.SelectSingleNode("/game/scenario/name").InnerText; XmlElement descriptionNode = (XmlElement)xml.SelectSingleNode("/game/scenario/description"); scenarioDescription = (descriptionNode != null ? descriptionNode.InnerText : null); XmlElement copyrightNode = (XmlElement)xml.SelectSingleNode("/game/scenario/copyright"); scenarioCopyright = (copyrightNode != null ? copyrightNode.InnerText : null); // Game data XmlNodeList layoutNodeList = gameNode.SelectNodes("layout"); boards = new Board[layoutNodeList.Count]; List <Piece> pieceList = new List <Piece>(); for (int i = 0; i < boards.Length; ++i) { string boardId = ((XmlElement)layoutNodeList[i]).GetAttribute("board"); string boardName = ((XmlElement)xml.SelectSingleNode("/game/scenario/map[@id='" + boardId + "']|/game/scenario/counter-sheet[@id='" + boardId + "']")).InnerText; // retrieve board properties foreach (MapProperties properties in gameBox.Maps) { if (properties.Name == boardName) { boards[i] = new Map(i, properties); break; } } if (boards[i] == null) { foreach (CounterSheetProperties properties in gameBox.CounterSheets) { if (properties.Name == boardName) { CounterSheet counterSheet = new CounterSheet(i, properties, pieceList); boards[i] = counterSheet; // register each piece's stack as belonging to the counter sheet foreach (ICounterSection counterSection in counterSheet.CounterSections) { foreach (Piece piece in counterSection.Pieces) { Stack stack = piece.Stack; counterSheet.MoveStackToBack(stack); stack.AttachedToCounterSection = true; } } break; } } } } allPieces = pieceList.ToArray(); visibleBoard = boards[0]; for (int i = 0; i < layoutNodeList.Count; ++i) { XmlElement layoutNode = (XmlElement)layoutNodeList[i]; Board board = boards[i]; board.VisibleArea = RectangleF.FromLTRB( XmlConvert.ToSingle(layoutNode.GetAttribute("left")), XmlConvert.ToSingle(layoutNode.GetAttribute("top")), XmlConvert.ToSingle(layoutNode.GetAttribute("right")), XmlConvert.ToSingle(layoutNode.GetAttribute("bottom"))); if (board is CounterSheet) { if (layoutNode.HasAttribute("side")) { ((CounterSheet)board).Side = (Side)Enum.Parse(typeof(Side), layoutNode.GetAttribute("side")); } else { ((CounterSheet)board).Side = Side.Front; } } if (layoutNode.HasAttribute("visible") && layoutNode.GetAttribute("visible") == "true") { visibleBoard = board; } foreach (XmlElement pieceNode in layoutNode.SelectNodes("counter")) { Piece piece = (Piece)GetPieceById(XmlConvert.ToInt32(pieceNode.GetAttribute("id"))); Stack stack = (Stack)piece.Stack; stack.AttachedToCounterSection = false; if (pieceNode.HasAttribute("rot")) { piece.RotationAngle = XmlConvert.ToSingle(pieceNode.GetAttribute("rot")) * (float)Math.PI / 180.0f; } if (pieceNode.HasAttribute("side")) { piece.Side = (Side)Enum.Parse(typeof(Side), pieceNode.GetAttribute("side")); } else { piece.Side = Side.Front; } board.MoveStackToFront(stack); stack.Position = new PointF( XmlConvert.ToSingle(pieceNode.GetAttribute("x")), XmlConvert.ToSingle(pieceNode.GetAttribute("y"))); } foreach (XmlElement stackNode in layoutNode.SelectNodes("stack")) { Stack stack = null; foreach (XmlElement pieceNode in stackNode.SelectNodes("counter")) { Piece piece = (Piece)GetPieceById(XmlConvert.ToInt32(pieceNode.GetAttribute("id"))); if (stack == null) { stack = (Stack)piece.Stack; stack.AttachedToCounterSection = false; board.MoveStackToFront(stack); stack.Position = new PointF( XmlConvert.ToSingle(stackNode.GetAttribute("x")), XmlConvert.ToSingle(stackNode.GetAttribute("y"))); } else { Stack stackToMerge = (Stack)piece.Stack; ((Board)stackToMerge.Board).RemoveStack(stackToMerge); stack.MergeToPosition(stackToMerge, stackToMerge.Pieces, stack.Pieces.Length); } if (pieceNode.HasAttribute("rot")) { piece.RotationAngle = XmlConvert.ToSingle(pieceNode.GetAttribute("rot")) * ((float)Math.PI / 180.0f); } if (pieceNode.HasAttribute("side")) { piece.Side = (Side)Enum.Parse(typeof(Side), pieceNode.GetAttribute("side")); } else { piece.Side = Side.Front; } } } } } else { // new format // Scenario data scenarioName = gameNode.GetAttribute("scenario-name"); scenarioDescription = gameNode.GetAttribute("scenario-description"); scenarioCopyright = gameNode.GetAttribute("scenario-copyright"); // Operating mode mode = (gameNode.HasAttribute("mode") ? (Mode)Enum.Parse(typeof(Mode), gameNode.GetAttribute("mode")) : Mode.Default); // Stacking stackingEnabled = (!gameNode.HasAttribute("stacking") || gameNode.GetAttribute("stacking") != "disabled"); // Game data XmlNodeList layoutNodeList = gameNode.SelectNodes("layout"); boards = new Board[layoutNodeList.Count]; List <Piece> pieceList = new List <Piece>(); for (int i = 0; i < boards.Length; ++i) { XmlElement layoutNode = (XmlElement)layoutNodeList[i]; string boardName = layoutNode.GetAttribute("board"); // retrieve board properties foreach (MapProperties properties in gameBox.Maps) { if (properties.Name == boardName) { boards[i] = new Map(i, properties); break; } } if (boards[i] == null) { foreach (CounterSheetProperties properties in gameBox.CounterSheets) { if (properties.Name == boardName) { CounterSheet counterSheet = new CounterSheet(i, properties, pieceList); boards[i] = counterSheet; // register each piece's stack as belonging to the counter sheet foreach (ICounterSection counterSection in counterSheet.CounterSections) { foreach (Piece piece in counterSection.Pieces) { Stack stack = piece.Stack; counterSheet.MoveStackToBack(stack); stack.AttachedToCounterSection = true; } } break; } } } if (boards[i] == null) { boards[i] = new Map(i, null); } if (layoutNode.HasAttribute("tab")) { boards[i].Name = layoutNode.GetAttribute("tab"); } } allPieces = pieceList.ToArray(); visibleBoard = boards[0]; for (int i = 0; i < layoutNodeList.Count; ++i) { XmlElement layoutNode = (XmlElement)layoutNodeList[i]; Board board = boards[i]; board.VisibleArea = RectangleF.FromLTRB( (layoutNode.HasAttribute("left") ? XmlConvert.ToSingle(layoutNode.GetAttribute("left")) : 0.0f), (layoutNode.HasAttribute("top") ? XmlConvert.ToSingle(layoutNode.GetAttribute("top")) : 0.0f), (layoutNode.HasAttribute("right") ? XmlConvert.ToSingle(layoutNode.GetAttribute("right")) : 1024.0f * 4), (layoutNode.HasAttribute("bottom") ? XmlConvert.ToSingle(layoutNode.GetAttribute("bottom")) : 768.0f * 4)); if (board is CounterSheet) { if (layoutNode.HasAttribute("side")) { ((CounterSheet)board).Side = (Side)Enum.Parse(typeof(Side), layoutNode.GetAttribute("side")); } else { ((CounterSheet)board).Side = Side.Front; } } if (layoutNode.HasAttribute("visible") && layoutNode.GetAttribute("visible") == "true") { visibleBoard = board; } if (layoutNode.HasAttribute("owner")) { board.Owner = new Guid(layoutNode.GetAttribute("owner")); } foreach (XmlElement pieceNode in layoutNode.SelectNodes("counter")) { Piece piece = (Piece)GetPieceById(XmlConvert.ToInt32(pieceNode.GetAttribute("id"))); if (piece is TerrainPrototype) { piece = new TerrainClone((TerrainPrototype)piece); } Stack stack = (Stack)piece.Stack; stack.AttachedToCounterSection = false; if (pieceNode.HasAttribute("rot")) { piece.RotationAngle = XmlConvert.ToSingle(pieceNode.GetAttribute("rot")) * (float)Math.PI / 180.0f; } if (pieceNode.HasAttribute("side")) { piece.Side = (Side)Enum.Parse(typeof(Side), pieceNode.GetAttribute("side")); } else { piece.Side = Side.Front; } board.MoveStackToFront(stack); stack.Position = new PointF( XmlConvert.ToSingle(pieceNode.GetAttribute("x")), XmlConvert.ToSingle(pieceNode.GetAttribute("y"))); } foreach (XmlElement stackNode in layoutNode.SelectNodes("stack")) { Stack stack = null; foreach (XmlElement pieceNode in stackNode.SelectNodes("counter")) { Piece piece = (Piece)GetPieceById(XmlConvert.ToInt32(pieceNode.GetAttribute("id"))); if (piece is TerrainPrototype) { piece = new TerrainClone((TerrainPrototype)piece); } if (stack == null) { stack = (Stack)piece.Stack; stack.AttachedToCounterSection = false; board.MoveStackToFront(stack); stack.Position = new PointF( XmlConvert.ToSingle(stackNode.GetAttribute("x")), XmlConvert.ToSingle(stackNode.GetAttribute("y"))); } else { Stack stackToMerge = (Stack)piece.Stack; ((Board)stackToMerge.Board).RemoveStack(stackToMerge); stack.MergeToPosition(stackToMerge, stackToMerge.Pieces, stack.Pieces.Length); } if (pieceNode.HasAttribute("rot")) { piece.RotationAngle = XmlConvert.ToSingle(pieceNode.GetAttribute("rot")) * ((float)Math.PI / 180.0f); } if (pieceNode.HasAttribute("side")) { piece.Side = (Side)Enum.Parse(typeof(Side), pieceNode.GetAttribute("side")); } else { piece.Side = Side.Front; } } } } foreach (XmlElement handNode in gameNode.SelectNodes("hand")) { Guid guid = new Guid(handNode.GetAttribute("owner")); PlayerHand hand = new PlayerHand(); Stack stack = null; foreach (XmlElement pieceNode in handNode.SelectNodes("counter")) { Piece piece = (Piece)GetPieceById(XmlConvert.ToInt32(pieceNode.GetAttribute("id"))); if (piece is TerrainPrototype) { piece = new TerrainClone((TerrainPrototype)piece); } if (stack == null) { stack = (Stack)piece.Stack; if (stack.Board != null) { ((Board)stack.Board).RemoveStack(stack); } stack.AttachedToCounterSection = false; } else { Stack stackToMerge = (Stack)piece.Stack; if (stackToMerge.Board != null) { ((Board)stackToMerge.Board).RemoveStack(stackToMerge); } stack.MergeToPosition(stackToMerge, stackToMerge.Pieces, stack.Pieces.Length); } if (pieceNode.HasAttribute("rot")) { piece.RotationAngle = XmlConvert.ToSingle(pieceNode.GetAttribute("rot")) * ((float)Math.PI / 180.0f); } if (pieceNode.HasAttribute("side")) { piece.Side = (Side)Enum.Parse(typeof(Side), pieceNode.GetAttribute("side")); } else { piece.Side = Side.Front; } } hand.Stack = stack; playerHands.Add(guid, hand); } } }
public GameHand(Game gameIn, DiceHand handIn, int ShakeIn) { CurrentGame = gameIn; CurrentHand = handIn; ShakeOfTheDay = ShakeIn; }