-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae9203d
commit a669e9f
Showing
10 changed files
with
199 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
probability,multiplier | ||
0.00005,10.0 | ||
0.0001,7.0 | ||
0.003,3.0 | ||
0.009,2.0 | ||
0.05,1.0 | ||
0.3,1.0 | ||
0.08,2.0 | ||
0.04,3.0 | ||
0.025,5.0 | ||
0.00499,10.0 | ||
0.00001,100.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule Safira.SlotsFactory do | ||
@moduledoc """ | ||
A factory to build all slots related structs | ||
""" | ||
defmacro __using__(_opts) do | ||
quote do | ||
def attendee_payout_factory do | ||
payout = build(:payout) | ||
bet = Enum.random(1..100) | ||
tokens = (payout.multiplier * bet) |> round() | ||
|
||
%Safira.Slots.AttendeePayout{ | ||
attendee: build(:attendee), | ||
payout: payout, | ||
bet: bet, | ||
tokens: tokens | ||
} | ||
end | ||
|
||
def payout_factory do | ||
%Safira.Slots.Payout{ | ||
probability: 0.5, | ||
multiplier: Enum.random(1..10) / 1 | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule Safira.SlotsTest do | ||
use Safira.DataCase | ||
alias Safira.Slots | ||
|
||
describe "payouts" do | ||
alias Safira.Slots.Payout | ||
|
||
@valid_attrs %{ | ||
multiplier: 10.0, | ||
probability: 0.42 | ||
} | ||
|
||
@invalid_attrs1 %{ | ||
multiplier: -1.0, | ||
probability: 0.42 | ||
} | ||
|
||
@invalid_attrs2 %{ | ||
multiplier: 10.0, | ||
probability: 2.0 | ||
} | ||
|
||
@invalid_attrs3 %{ | ||
multiplier: 10.0, | ||
probability: -2.0 | ||
} | ||
|
||
test "create_payout/1 with valid data creates a payout" do | ||
assert {:ok, %Payout{} = payout} = Slots.create_payout(@valid_attrs) | ||
assert payout.multiplier == 10.0 | ||
assert payout.probability == 0.42 | ||
end | ||
|
||
test "create_payout/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = Slots.create_payout(@invalid_attrs1) | ||
assert {:error, %Ecto.Changeset{}} = Slots.create_payout(@invalid_attrs2) | ||
assert {:error, %Ecto.Changeset{}} = Slots.create_payout(@invalid_attrs3) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
defmodule SafiraWeb.SlotsControllerTest do | ||
use SafiraWeb.ConnCase | ||
|
||
describe "spin" do | ||
test "with valid bet (attendee with enough tokens)" do | ||
user = create_user_strategy(:user) | ||
insert(:attendee, user: user, token_balance: 1000) | ||
payout = create_payout_strategy(:payout, probability: 1.0) | ||
bet = Enum.random(1..100) | ||
%{conn: conn, user: _user} = api_authenticate(user) | ||
|
||
conn = | ||
conn | ||
|> post(Routes.slots_path(conn, :spin, bet: bet)) | ||
|> doc() | ||
|
||
assert json_response(conn, 200) == %{ | ||
"multiplier" => payout.multiplier, | ||
"tokens" => (bet * payout.multiplier) |> round() | ||
} | ||
end | ||
|
||
test "with valid bet (attendee without enough tokens)" do | ||
user = create_user_strategy(:user) | ||
insert(:attendee, user: user, token_balance: 0) | ||
create_payout_strategy(:payout, probability: 1.0) | ||
%{conn: conn, user: _user} = api_authenticate(user) | ||
|
||
conn = | ||
conn | ||
|> post(Routes.slots_path(conn, :spin, bet: 100)) | ||
|
||
assert json_response(conn, 401) == %{ | ||
"error" => "Insufficient token balance" | ||
} | ||
end | ||
|
||
test "with invalid bet (float value)" do | ||
user = create_user_strategy(:user) | ||
insert(:attendee, user: user) | ||
create_payout_strategy(:payout, probability: 1.0) | ||
%{conn: conn, user: _user} = api_authenticate(user) | ||
|
||
conn = | ||
conn | ||
|> post(Routes.slots_path(conn, :spin, bet: 1.2)) | ||
|
||
assert json_response(conn, 400) == %{ | ||
"error" => "Bet should be an integer" | ||
} | ||
end | ||
|
||
test "with invalid bet (negative value)" do | ||
user = create_user_strategy(:user) | ||
insert(:attendee, user: user) | ||
create_payout_strategy(:payout, probability: 1.0) | ||
%{conn: conn, user: _user} = api_authenticate(user) | ||
|
||
conn = | ||
conn | ||
|> post(Routes.slots_path(conn, :spin, bet: -1)) | ||
|
||
assert json_response(conn, 400) == %{ | ||
"error" => "Bet should be a positive integer" | ||
} | ||
end | ||
|
||
test "when user is not an attendee" do | ||
user = create_user_strategy(:user) | ||
insert(:staff, user: user) | ||
create_payout_strategy(:payout, probability: 1.0) | ||
%{conn: conn, user: _user} = api_authenticate(user) | ||
|
||
conn = | ||
conn | ||
|> post(Routes.slots_path(conn, :spin, bet: 100)) | ||
|
||
assert json_response(conn, 401)["error"] == "Only attendees can play the slots" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule Safira.PayoutStrategy do | ||
@moduledoc """ | ||
ExMachina strategy for creating payouts | ||
""" | ||
use ExMachina.Strategy, function_name: :create_payout_strategy | ||
|
||
def handle_create_payout_strategy(record, _opts) do | ||
{:ok, payout} = | ||
Safira.Slots.create_payout(%{ | ||
probability: record.probability, | ||
multiplier: record.multiplier | ||
}) | ||
|
||
payout | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters