public async Task <PixelResult> AddPixel(AddPixelRequest model) { var game = await GameCache.GetGame(model.GameId); if (game == null) { return(new PixelResult(false, "Game not found")); } if (game.Status != GameStatus.Started) { return(new PixelResult(false, "Game is not currently active")); } var pixel = await PixelCache.GetPixel(game.Id, model.X, model.Y); if (pixel != null) { if (pixel.Type == PixelType.Fixed) { return(new PixelResult(false, "Cannot overwrite gameboard pixel")); } if (pixel.Points > model.MaxPoints) { return(new PixelResult(false, "Pixel points are greater than Spend Limit")); } } var userId = Context.GetUserId(); var rateLimitResult = await CheckRateLimits(userId, game); if (!string.IsNullOrEmpty(rateLimitResult)) { return(new PixelResult(false, rateLimitResult)); } var result = await PixelWriter.AddPixel(userId, model); if (!result.Success) { return(new PixelResult(false, result.Message)); } return(new PixelResult(result)); }
public async Task <AddPixelResponse> AddPixel(int userId, AddPixelRequest model) { try { var game = await GameReader.GetGame(model.GameId); if (game == null) { return new AddPixelResponse { Success = false, Message = "Game not found" } } ; if (!Constant.IsValidColor(model.Color)) { return new AddPixelResponse { Success = false, Message = "Failed to add new pixel, Pixel color must be valid hex color code #000000-#FFFFFF" } } ; if (!Constant.IsValidLocation(model.X, model.Y, game.Width, game.Height)) { return new AddPixelResponse { Success = false, Message = "Failed to add new pixel, Pixel X,Y must be within range 0-999" } } ; if (Constant.PixelPoints > model.MaxPoints) { return new AddPixelResponse { Success = false, Message = "Failed to add new pixel, Points are greater than MaxPoints" } } ; var result = await QueueHubClient.SubmitPixel(new SubmitPixelRequest { UserId = userId, X = model.X, Y = model.Y, Color = model.Color, Type = PixelType.User, Points = Constant.PixelPoints, MaxPoints = model.MaxPoints, GameId = game.Id, GameType = game.Type }); return(new AddPixelResponse { Success = result.Success, Message = result.Message, }); } catch (Exception) { return(new AddPixelResponse { Success = false, Message = "Failed to add new pixel, unknown error" }); } }