Skip to content

Commit 71355c6

Browse files
VitalyVitaly
authored andcommitted
Add points to user
1 parent 7d8dea8 commit 71355c6

File tree

13 files changed

+1110
-74
lines changed

13 files changed

+1110
-74
lines changed

services/app/apps/codebattle/lib/codebattle/application.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ defmodule Codebattle.Application do
2020
{ChromicPDF, chromic_pdf_opts()},
2121
{Codebattle.TasksImporter, []},
2222
{Codebattle.UsersRankUpdateServer, []},
23+
{Codebattle.UsersPointsAndRankUpdateServer, []},
2324
{Codebattle.Bot.GameCreator, []},
2425
{Codebattle.Tournament.UpcomingRunner, []},
2526
{Codebattle.ImageCache, []},

services/app/apps/codebattle/lib/codebattle/pub_sub/events.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ defmodule Codebattle.PubSub.Events do
111111
%Message{
112112
topic: "tournaments",
113113
event: "tournament:finished",
114-
payload: %{id: params.tournament.id}
114+
payload: %{
115+
id: params.tournament.id,
116+
grade: params.tournament.grade
117+
}
115118
},
116119
%Message{
117120
topic: "tournament:#{params.tournament.id}:common",

services/app/apps/codebattle/lib/codebattle/tournament/context.ex

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ defmodule Codebattle.Tournament.Context do
139139
@spec get_upcoming_tournaments(%{
140140
from: DateTime.t(),
141141
to: DateTime.t(),
142-
user_id: non_neg_integer() | nil
142+
user: User.t() | nil
143143
}) :: list(Tournament.t())
144144
def get_upcoming_tournaments(filter) do
145145
%{from: datetime_from, to: datetime_to} = filter
@@ -156,26 +156,53 @@ defmodule Codebattle.Tournament.Context do
156156
)
157157
end
158158

159+
@spec get_one_upcoming_tournament_for_each_grade() :: list(Tournament.t())
160+
def get_one_upcoming_tournament_for_each_grade do
161+
cte_query =
162+
from(t in Tournament,
163+
where: t.state == "upcoming" and t.grade != "open",
164+
group_by: t.grade,
165+
select: %{grade: t.grade, min_id: min(t.id)}
166+
)
167+
168+
Repo.all(
169+
from(t in Tournament,
170+
join: cte in subquery(cte_query),
171+
on: t.grade == cte.grade and t.id == cte.min_id,
172+
where: t.state == "upcoming" and t.grade != "open",
173+
order_by: [t.grade, t.starts_at]
174+
)
175+
)
176+
end
177+
159178
@spec get_user_tournaments(%{
160179
from: DateTime.t(),
161180
to: DateTime.t(),
162-
user_id: non_neg_integer() | nil
181+
user: User.t() | nil
163182
}) :: list(Tournament.t())
164-
def get_user_tournaments(%{user_id: nil}), do: []
183+
def get_user_tournaments(%{user: %{is_guest: true}}), do: []
165184

166185
def get_user_tournaments(filter) do
167-
%{from: datetime_from, to: datetime_to, user_id: user_id} = filter
168-
169-
Repo.all(
170-
from(t in Tournament,
171-
order_by: t.id,
172-
where:
173-
t.starts_at >= ^datetime_from and
174-
t.starts_at <= ^datetime_to and
175-
t.grade == "open" and
176-
(t.creator_id == ^user_id or fragment("? = ANY(?)", ^user_id, t.winner_ids))
186+
%{from: datetime_from, to: datetime_to, user: %{id: user_id} = user} = filter
187+
188+
if User.admin?(user) do
189+
Repo.all(
190+
from(t in Tournament,
191+
order_by: t.id,
192+
where: t.starts_at >= ^datetime_from and t.starts_at <= ^datetime_to
193+
)
177194
)
178-
)
195+
else
196+
Repo.all(
197+
from(t in Tournament,
198+
order_by: t.id,
199+
where:
200+
t.starts_at >= ^datetime_from and
201+
t.starts_at <= ^datetime_to and
202+
(t.creator_id == ^user_id or fragment("? = ANY(?)", ^user_id, t.winner_ids))
203+
)
204+
)
205+
end
179206
end
180207

181208
@spec get_live_tournaments_for_user(User.t()) :: list(Tournament.t())

services/app/apps/codebattle/lib/codebattle/tournament/tournament.ex

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -65,50 +65,6 @@ defmodule Codebattle.Tournament do
6565
@task_strategies ~w(random sequential)
6666
@types ~w(swiss)
6767

68-
@grade_points %{
69-
# Open tournaments
70-
# All tasks are existing
71-
# Casual/unranked mode, no points awarded
72-
# Tasks: free play, any level, not ranked, created by user
73-
"open" => [],
74-
75-
# Rookie — every 1 hours
76-
# All tasks are existing
77-
# Tasks: 5 existing easy tasks
78-
# Designed for frequent play and grinding
79-
"rookie" => [8, 4, 2],
80-
81-
# Challenger — daily
82-
# All tasks are existing
83-
# Tasks: 3 existing easy tasks + 1 existing medium task
84-
# Daily backbone tournaments for steady point growth
85-
"challenger" => [64, 32, 16, 8, 4, 2],
86-
87-
# Pro — weekly
88-
# All tasks are existing
89-
# Tasks: 4 existing easy tasks + 2 existing medium tasks
90-
# Mid-level weekly tournaments with more challenges
91-
"pro" => [128, 64, 32, 16, 8, 4, 2],
92-
93-
# Elite — every two weeks
94-
# All tasks are existing
95-
# Tasks: 5 existing easy tasks + 3 existing medium tasks
96-
# Advanced difficulty and higher prestige
97-
"elite" => [256, 128, 64, 32, 16, 8, 4, 2],
98-
99-
# Masters — once per month on the 21st (evening, two per day)
100-
# All tasks are new
101-
# Tasks: 5 easy tasks + 2 medium tasks
102-
# Monthly major tournaments with fresh content
103-
"masters" => [1024, 512, 256, 128, 64, 32, 16, 8, 4, 2],
104-
105-
# Grand Slam — four times per year (21 Mar, 21 Jun, 21 Sep, 21 Dec)
106-
# All tasks are new
107-
# Tasks: 5 easy tasks + 3 medium tasks + 1 hard task
108-
# Seasonal finals, always ends the season
109-
"grand_slam" => [2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2]
110-
}
111-
11268
@default_match_timeout Application.compile_env(:codebattle, :tournament_match_timeout)
11369

11470
schema "tournaments" do
@@ -257,5 +213,4 @@ defmodule Codebattle.Tournament do
257213
def task_providers, do: @task_providers
258214
def task_strategies, do: @task_strategies
259215
def types, do: @types
260-
def grade_points, do: @grade_points
261216
end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
defmodule Codebattle.Tournament do
2+
@moduledoc """
3+
THis is description of tourmanets based on that we want to generate tournaments_schedule for the current season.
4+
5+
I want to generate upcoming graded tournaments for
6+
rookie, challenge, pro, elite, masters, grand_slam
7+
We have 4 seasons
8+
9+
Season 0: Sep 21 - Dec 21
10+
Season 1: Dec 21 - Mar 21
11+
Season 2: Mar 21 - Jun 21
12+
Season 3: Jun 21 - Sep 21
13+
14+
15+
For each tournament i need insert into database
16+
It should looks like, for each tournament
17+
Prettify description with markdown
18+
19+
For tournaments that are only once in the day pic 16 UTC
20+
and i don't need other tournaments that day.
21+
GrandSlam should be the day when we finish season.
22+
23+
Repo.insert(%{
24+
25+
starts_at: {UTC generated datetime}
26+
grade: "rookie",
27+
name: "Rookie, Season:0, #1",
28+
description: "
29+
Codebattle Season contest
30+
Points : {get from @grade_points based on grade}
31+
32+
33+
Play in season tournaments and earn points.
34+
Win the season and get achievement!
35+
36+
4 seasons:application
37+
38+
"
39+
40+
})
41+
42+
"""
43+
44+
@grade_points %{
45+
# Open tournaments
46+
# All tasks are existing
47+
# Casual/unranked mode, no points awarded
48+
# Tasks: free play, any level, not ranked, created by user
49+
"open" => [],
50+
51+
# Rookie — every 1 hours
52+
# All tasks are existing
53+
# Tasks: 5 existing easy tasks
54+
# Designed for frequent play and grinding
55+
"rookie" => [8, 4, 2],
56+
57+
# Challenger — daily
58+
# All tasks are existing
59+
# Tasks: 3 existing easy tasks + 1 existing medium task
60+
# Daily backbone tournaments for steady point growth
61+
"challenger" => [64, 32, 16, 8, 4, 2],
62+
63+
# Pro — weekly
64+
# All tasks are existing
65+
# Tasks: 4 existing easy tasks + 2 existing medium tasks
66+
# Mid-level weekly tournaments with more challenges
67+
"pro" => [128, 64, 32, 16, 8, 4, 2],
68+
69+
# Elite — every two weeks
70+
# All tasks are existing
71+
# Tasks: 5 existing easy tasks + 3 existing medium tasks
72+
# Advanced difficulty and higher prestige
73+
"elite" => [256, 128, 64, 32, 16, 8, 4, 2],
74+
75+
# Masters — once per month on the 21st (evening, two per day)
76+
# All tasks are new
77+
# Tasks: 5 easy tasks + 2 medium tasks
78+
# Monthly major tournaments with fresh content
79+
"masters" => [1024, 512, 256, 128, 64, 32, 16, 8, 4, 2],
80+
81+
# Grand Slam — four times per year (21 Mar, 21 Jun, 21 Sep, 21 Dec)
82+
# All tasks are new
83+
# Tasks: 5 easy tasks + 3 medium tasks + 1 hard task
84+
# Seasonal finals, always ends the season
85+
"grand_slam" => [2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2]
86+
}
87+
end

services/app/apps/codebattle/lib/codebattle/user.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,11 @@ defmodule Codebattle.User do
262262
# nil for new token users, clan will be managed by admin
263263
defp assign_clan(changeset, params, nil), do: assign_clan(changeset, params, 1)
264264

265-
defp assign_clan(changeset, %{clan: clan_name}, user_id), do: find_or_create_by_clan(changeset, clan_name, user_id)
265+
defp assign_clan(changeset, %{clan: clan_name}, user_id),
266+
do: find_or_create_by_clan(changeset, clan_name, user_id)
266267

267-
defp assign_clan(changeset, %{"clan" => clan_name}, user_id), do: find_or_create_by_clan(changeset, clan_name, user_id)
268+
defp assign_clan(changeset, %{"clan" => clan_name}, user_id),
269+
do: find_or_create_by_clan(changeset, clan_name, user_id)
268270

269271
defp assign_clan(changeset, _params, _user_id), do: changeset
270272

0 commit comments

Comments
 (0)