Skip to content

Commit 7d4815e

Browse files
committed
feat: create RoomConfig, add functions to change score, custom guards for community and affinity
1 parent a77152c commit 7d4815e

File tree

9 files changed

+193
-39
lines changed

9 files changed

+193
-39
lines changed

lib/viral_spiral/game/player.ex

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
defmodule ViralSpiral.Game.Player do
2+
alias ViralSpiral.Game.Player
3+
alias ViralSpiral.Game.RoomConfig
4+
25
defstruct id: "",
36
name: "",
4-
biases: [],
5-
affinities: [],
6-
score: 0,
7-
hand: nil
7+
identity: nil,
8+
hand: []
89

910
def new() do
10-
%__MODULE__{
11+
%Player{
1112
id: UXID.generate!(prefix: "player", size: :small)
1213
}
1314
end
1415

15-
def set_name(%__MODULE__{} = player, name) do
16+
def new(%RoomConfig{} = room_config) do
17+
%Player{
18+
id: UXID.generate!(prefix: "player", size: :small),
19+
identity: Enum.shuffle(room_config.communities) |> Enum.at(0)
20+
}
21+
end
22+
23+
def set_name(%Player{} = player, name) do
1624
%{player | name: name}
1725
end
26+
27+
def set_identity(%Player{} = player, identity) do
28+
%{player | identity: identity}
29+
end
1830
end

lib/viral_spiral/game/room_config.ex

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule ViralSpiral.Game.RoomConfig do
2+
defstruct affinities: [:cat, :sock],
3+
communities: [:red, :yellow, :blue],
4+
chaos_counter: 10,
5+
volatility: :medium
6+
end
7+
8+
defmodule ViralSpiral.Game.RoomConfig.Guards do
9+
@affinities [:cat, :sock, :highfive, :houseboat, :skub]
10+
@communities [:red, :yellow, :blue]
11+
12+
defguard is_affinity(value) when value in @affinities
13+
14+
defguard is_community(value) when value in @communities
15+
end

lib/viral_spiral/game/score.ex

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
defmodule ViralSpiral.Game.Score.Room do
2+
alias ViralSpiral.Game.Score.Room
3+
defstruct chaos_countdown: 10
4+
5+
def new() do
6+
%Room{}
7+
end
8+
9+
def countdown(%Room{} = room) do
10+
%{room | chaos_countdown: room.chaos_countdown - 1}
11+
end
12+
end
13+
14+
defmodule ViralSpiral.Game.Score.Player do
15+
alias ViralSpiral.Game.Score.Player
16+
alias ViralSpiral.Game.RoomConfig
17+
alias ViralSpiral.Game.Player, as: PlayerData
18+
import ViralSpiral.Game.RoomConfig.Guards
19+
20+
defstruct biases: %{}, affinities: %{}, clout: 0
21+
22+
def new(%PlayerData{} = player, %RoomConfig{} = room_config) do
23+
bias_list = Enum.filter(room_config.communities, &(&1 != player.identity))
24+
bias_map = Enum.reduce(bias_list, %{}, fn x, acc -> Map.put(acc, x, 0) end)
25+
26+
affinity_list = room_config.affinities
27+
affinity_map = Enum.reduce(affinity_list, %{}, fn x, acc -> Map.put(acc, x, 0) end)
28+
29+
%Player{
30+
biases: bias_map,
31+
affinities: affinity_map
32+
}
33+
end
34+
35+
def change(%Player{} = player, :bias, target_bias, count)
36+
when is_community(target_bias) and is_integer(count) do
37+
new_biases = Map.put(player.biases, target_bias, player.biases[target_bias] + count)
38+
%{player | biases: new_biases}
39+
end
40+
41+
def change(%Player{} = player, :affinity, target, count)
42+
when is_affinity(target) and is_integer(count) do
43+
new_affinities = Map.put(player.affinities, target, player.affinities[target] + count)
44+
%{player | affinities: new_affinities}
45+
end
46+
47+
def change(%Player{} = player, :clout, count) when is_integer(count) do
48+
new_clout = player.clout + count
49+
%{player | clout: new_clout}
50+
end
51+
end

lib/viral_spiral/game/state.ex

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
defmodule ViralSpiral.Game.State do
2-
defstruct room: nil,
2+
defstruct room_config: nil,
3+
room: nil,
34
player_list: nil,
45
player_map: nil,
5-
scores: nil,
6+
room_score: nil,
7+
player_scores: nil,
68
round: nil,
79
turn: nil
810

test/support/fixtures.ex

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
defmodule Fixtures do
2-
alias ViralSpiral.Game.Player
2+
alias ViralSpiral.Game.Score.Player
3+
alias ViralSpiral.Game.Turn
4+
alias ViralSpiral.Game.Round
35
alias ViralSpiral.Game.Room
6+
alias ViralSpiral.Game.RoomConfig
7+
alias ViralSpiral.Game.Score.Player, as: PlayerScore
8+
# alias ViralSpiral.Game.Score.Room, as: RoomScore
9+
alias ViralSpiral.Game.Player
410
alias ViralSpiral.Game.State
511

612
def initialized_game() do
13+
room_config = %RoomConfig{}
14+
715
player_list = [
8-
Player.new() |> Player.set_name("adhiraj"),
9-
Player.new() |> Player.set_name("aman"),
10-
Player.new() |> Player.set_name("krys"),
11-
Player.new() |> Player.set_name("farah")
16+
Player.new(room_config) |> Player.set_name("adhiraj"),
17+
Player.new(room_config) |> Player.set_name("aman"),
18+
Player.new(room_config) |> Player.set_name("krys"),
19+
Player.new(room_config) |> Player.set_name("farah")
1220
]
1321

1422
players = Enum.reduce(player_list, %{}, fn player, acc -> Map.put(acc, player.id, player) end)
1523

24+
round = Round.new(player_list)
25+
turn = Turn.new(round)
26+
1627
%State{
28+
room_config: room_config,
1729
room: Room.new(),
1830
player_map: players,
19-
player_list: player_list
31+
player_list: player_list,
32+
round: round,
33+
turn: turn,
34+
# room_score: RoomScore.new(),
35+
player_scores: Enum.map(player_list, &PlayerScore.new(&1, room_config))
2036
}
2137
end
2238
end
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule ViralSpiral.Game.PlayerTest do
2+
alias ViralSpiral.Game.Player
3+
alias ViralSpiral.Game.RoomConfig
4+
use ExUnit.Case
5+
6+
test "create player from room config" do
7+
room_config = %RoomConfig{}
8+
9+
player =
10+
Player.new(room_config)
11+
|> Player.set_name("adhiraj")
12+
13+
assert player.name == "adhiraj"
14+
end
15+
end

test/viral_spiral/game/score_test.exs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
defmodule ViralSpiral.Game.ScoreTest do
2+
alias ViralSpiral.Game.Score.Player, as: PlayerScore
3+
alias ViralSpiral.Game.RoomConfig
4+
alias ViralSpiral.Game.Player
5+
use ExUnit.Case
6+
7+
setup_all do
8+
room_config = %RoomConfig{}
9+
player = Player.new(room_config) |> Player.set_identity("yellow")
10+
player_score = PlayerScore.new(player, room_config)
11+
12+
%{player: player, player_score: player_score}
13+
end
14+
15+
test "player should not have a bias against their own identity", state do
16+
player_score = state.player_score
17+
18+
assert Enum.find(player_score.biases, &(&1 == "yellow")) == nil
19+
end
20+
21+
test "change player bias", state do
22+
player_score = state.player_score
23+
24+
player_score = PlayerScore.change(player_score, :bias, :yellow, 3)
25+
assert player_score.biases.yellow == 3
26+
27+
player_score = PlayerScore.change(player_score, :bias, :yellow, -2)
28+
assert player_score.biases.yellow == 1
29+
end
30+
31+
test "change player affinity", state do
32+
player_score = state.player_score
33+
34+
player_score = PlayerScore.change(player_score, :affinity, :cat, 5)
35+
assert player_score.affinities.cat == 5
36+
37+
player_score = PlayerScore.change(player_score, :affinity, :cat, -2)
38+
assert player_score.affinities.cat == 3
39+
end
40+
41+
test "change player clout", state do
42+
player_score = state.player_score
43+
44+
player_score = PlayerScore.change(player_score, :clout, 3)
45+
assert player_score.clout == 3
46+
47+
player_score = PlayerScore.change(player_score, :clout, -2)
48+
assert player_score.clout == 1
49+
end
50+
end

test/viral_spiral/game_test.exs

-25
This file was deleted.

test/viral_spiral/gameplay_test.exs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule ViralSpiral.GameTest do
2+
use ExUnit.Case
3+
4+
describe "card actions" do
5+
setup do
6+
game_state = Fixtures.initialized_game()
7+
%{state: game_state}
8+
end
9+
10+
test "passing an affinity card", %{state: game_state} do
11+
players = game_state.player_map
12+
round = game_state.round
13+
turn = game_state.turn
14+
room_score = game_state.room_score
15+
player_scores = game_state.player_scores
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)