Skip to content

Commit b2ee3d5

Browse files
Cheng20010201angelsl
authored andcommitted
Add CP3108 Game Component
Merges #581 Squashed commit of the following: commit 03fb0c4753b2a10fc34d5861caa9b18ed8e60723 Author: angelsl <angelsl@in04.sg> Date: Sat May 23 01:13:35 2020 +0800 Fix formatting and Credo lints commit 2f346227ea19ef15012fcd39b99f773b4fdbce0b Merge: a46af78 0593ca9 Author: angelsl <angelsl@in04.sg> Date: Sat May 23 01:09:30 2020 +0800 Merge branch 'sa_2021' into rebase commit a46af78 Author: Yuc Sun <sunyuchengsyc@163.com> Date: Wed Apr 29 20:00:49 2020 +0800 modified positioning of test cases commit f2e3b3f Author: Yuc Sun <sunyuchengsyc@163.com> Date: Wed Apr 29 19:46:42 2020 +0800 fix issues proposed commit b71612a Author: Yuc Sun <sunyuchengsyc@163.com> Date: Wed Apr 29 19:44:01 2020 +0800 fix issues proposed by reviewer commit 1525c25 Author: Yuc Sun <sunyuchengsyc@163.com> Date: Thu Apr 23 19:54:21 2020 +0800 Some format change to adhere to backend style commit 9e7f141 Author: Yuc Sun <sunyuchengsyc@163.com> Date: Thu Apr 23 19:15:20 2020 +0800 Modified Module Name commit c34784c Merge: 0d59384 6597a3e Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 23 00:32:58 2020 +0800 Merge pull request #2 from Source-Academy-Game/mergeGroundControl Merge ground control with our Collectibles System. commit 0d59384 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 23 00:29:16 2020 +0800 Added tests on collectible system commit cca29ea Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 23 00:26:32 2020 +0800 Eliminated one compile warning commit 3ff9e52 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Wed Apr 22 18:11:28 2020 +0800 Minor change to user.ex This change is to fit the testing format. commit a47cbab Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Wed Apr 22 18:09:24 2020 +0800 Update accounts_test.exs Modified this to fit our collectible system. commit 45448ba Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Wed Apr 22 18:07:27 2020 +0800 Modified user_controller_test.exs Modified this to fit collectible system. commit 6597a3e Author: travisryte <thuyaoo200@gmail.com> Date: Wed Apr 22 05:21:32 2020 +0800 Ran Mix Format commit 0a4cdb3 Merge: 3c3c780 ae7beac Author: travisryte <thuyaoo200@gmail.com> Date: Tue Apr 21 22:45:12 2020 +0800 Merge branch 'ground_control' into mergeGroundControl commit ae7beac Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Mon Apr 20 18:33:39 2020 +0800 Updated AssessmentOverview schema commit 5d4e6d8 Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Mon Apr 20 14:11:52 2020 +0800 Minor formatting change commit c7f01c2 Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Mon Apr 20 13:48:59 2020 +0800 Changed assessment deletion such that associated submissions are also deleted commit 159a2d3 Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Mon Apr 20 13:39:55 2020 +0800 Updated XML parsers tests commit 7663bf2 Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Mon Apr 20 02:26:25 2020 +0800 Modified AssessmentsController tests to fit with changes to how the is_published parameter is now handled commit 1a07757 Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Sun Apr 19 22:22:55 2020 +0800 Fixed format commit b39ad61 Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Sun Apr 19 22:15:26 2020 +0800 Added swagger paths commit 3c3c780 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 16 11:44:32 2020 +0800 Add function to differentiate students from others commit 3ca9dc6 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 16 11:23:20 2020 +0800 Added status codes information commit 3d5e37d Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 16 11:19:56 2020 +0800 Added status codes information commit efbc305 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 16 11:19:24 2020 +0800 Update game_states.ex commit 0f59ab0 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 16 03:44:25 2020 +0800 Solved no status codes error commit 34c4567 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 16 03:43:00 2020 +0800 Fixed no status codes error Also, satisfied frontend's need to make gameStates an object to input. Maintained consistency with other parts of the backend. commit 8c956cf Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 16 02:55:42 2020 +0800 Update user_view.ex commit 53141ea Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 16 02:52:51 2020 +0800 Add files via upload commit 9d482c1 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 16 02:52:17 2020 +0800 Delete add_game_states commit 72d9744 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 16 02:45:56 2020 +0800 Delete 20200402094115_add_user_game_states.exs commit e3eddd6 Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Wed Apr 15 20:50:49 2020 +0800 Fixed bug when force updating assessments that are not opened. Added requirement that all question types remain the same when force updating commit c0c73c9 Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Wed Apr 15 18:13:17 2020 +0800 Changed variable naming commit 4ca0ffb Merge: fbdf592 8654b13 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Mon Apr 13 21:14:49 2020 +0800 Merge branch 'sa_2021' into master commit fbdf592 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Apr 10 17:15:53 2020 +0800 Update user_controller.ex commit 71f1ac9 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Apr 10 17:07:44 2020 +0800 fixed a consistency problem commit 3f2bcb3 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Apr 10 16:06:57 2020 +0800 Update game_states.ex commit 6b42190 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Apr 10 16:06:07 2020 +0800 Update user_controller.ex commit 39791c3 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Apr 10 16:05:33 2020 +0800 Update router.ex commit ac472d3 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Apr 10 16:05:02 2020 +0800 Create add_game_states commit 2053868 Author: travisryte <thuyaoo200@gmail.com> Date: Thu Apr 2 01:52:01 2020 +0800 Update to allow for xml files commit 26ac37c Author: jiachen <jiachen247@users.noreply.github.com> Date: Wed Jan 29 22:07:49 2020 +0800 Update README.md (#570) commit 5bb40e9 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Apr 10 13:53:39 2020 +0800 Added some functios regarding game states. commit 14f5025 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Apr 10 13:51:41 2020 +0800 Added some routes regarding game states. commit 0218305 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Apr 10 13:50:49 2020 +0800 Added few functions regarding game data commit 5a865d9 Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Tue Apr 7 21:47:10 2020 +0800 Minor edit commit 431a058 Author: ScrubWzz <wongzhengzhi@gmail.com> Date: Mon Apr 6 21:22:00 2020 +0800 Added GroundControl commit 210cff6 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 2 20:54:04 2020 +0800 make changes to model the behavior of game_states changed from the previous collectibles version to game_states version. add a few to-dos for futrue implementation of save_data related functions. Also noted that the error handling part is not implemented yet, which is also a to-do. commit e92b5c0 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 2 20:51:13 2020 +0800 added one line to be consistent with migration commit 9635a2b Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 2 20:49:32 2020 +0800 updated to game_states version Updated to game_states version and added some to-dos for the save_data part commit 18375b5 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 2 20:47:58 2020 +0800 changed some post actions to put actions commit 16323c9 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 2 20:46:32 2020 +0800 added trivial change to maitain consitency commit d052ce4 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 2 20:45:00 2020 +0800 Delete original collectibles. commit 709e02a Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Thu Apr 2 20:44:08 2020 +0800 Add a game_states column to the database. commit d2fc579 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Mon Mar 30 20:40:03 2020 +0800 Update collectibles.ex commit b22dc98 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Sun Mar 29 19:44:32 2020 +0800 Update user_controller.ex commit bcf34a9 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Sun Mar 29 19:42:32 2020 +0800 Update router.ex commit 517dca7 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Sun Mar 29 19:41:21 2020 +0800 Update collectibles.ex commit 495a915 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Mar 27 14:48:37 2020 +0800 Create collectibles.ex commit bed98a9 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Mar 27 14:46:59 2020 +0800 Update user_view.ex commit faca0ca Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Mar 27 14:46:01 2020 +0800 Update router.ex commit 2acbf8a Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Mar 27 14:44:52 2020 +0800 Update user_controller.ex commit 82605a9 Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Mar 27 14:43:53 2020 +0800 Add files via upload commit 9c9702c Author: Cheng20010201 <53768585+Cheng20010201@users.noreply.github.com> Date: Fri Mar 27 14:42:16 2020 +0800 Update user.ex
1 parent 0593ca9 commit b2ee3d5

File tree

10 files changed

+376
-28
lines changed

10 files changed

+376
-28
lines changed

lib/cadet/accounts/game_states.ex

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
defmodule Cadet.Accounts.GameStates do
2+
@moduledoc """
3+
An entity that stores user game state.
4+
"""
5+
6+
use Cadet, :context
7+
alias Cadet.Accounts.User
8+
9+
@update_gamestate_roles ~w(student)a
10+
# currently in this module no error handling function
11+
# has been implemented yet
12+
13+
def user_game_states(user) do
14+
user.game_states
15+
end
16+
17+
def user_collectibles(user) do
18+
user.game_states["collectibles"]
19+
end
20+
21+
def user_save_data(user) do
22+
user.game_states["completed_quests"]
23+
end
24+
25+
def update(user = %User{role: role}, new_game_states) do
26+
if role in @update_gamestate_roles do
27+
changeset = cast(user, %{game_states: new_game_states}, [:game_states])
28+
Repo.update!(changeset)
29+
{:ok, nil}
30+
else
31+
{:error, {:forbidden, "Please try again later."}}
32+
end
33+
end
34+
35+
def clear(user = %User{role: role}) do
36+
if role in @update_gamestate_roles do
37+
changeset =
38+
cast(user, %{game_states: %{collectibles: %{}, completed_quests: []}}, [
39+
:game_states
40+
])
41+
42+
Repo.update!(changeset)
43+
{:ok, nil}
44+
else
45+
{:error, {:forbidden, "Please try again later."}}
46+
end
47+
end
48+
end

lib/cadet/accounts/user.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule Cadet.Accounts.User do
1414
field(:name, :string)
1515
field(:role, Role)
1616
field(:nusnet_id, :string)
17+
field(:game_states, :map, default: %{"collectibles" => %{}, "completed_quests" => []})
1718
belongs_to(:group, Group)
1819
timestamps()
1920
end

lib/cadet/course/material_upload.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule Cadet.Course.MaterialUpload do
66
use Arc.Ecto.Definition
77

88
@env Mix.env()
9-
@extension_whitelist ~w(.doc .docx .jpg .pdf .png .ppt .pptx .txt .xls .xlsx)
9+
@extension_whitelist ~w(.doc .docx .jpg .pdf .png .ppt .pptx .txt .xls .xlsx .xml)
1010
@versions [:original]
1111

1212
def bucket, do: :cadet |> Application.fetch_env!(:uploader) |> Keyword.get(:materials_bucket)

lib/cadet_web/controllers/user_controller.ex

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ defmodule CadetWeb.UserController do
55

66
use CadetWeb, :controller
77
use PhoenixSwagger
8-
98
import Cadet.Assessments
9+
import Cadet.Accounts.GameStates
1010

1111
def index(conn, _) do
1212
user = user_with_group(conn.assigns.current_user)
1313
grade = user_total_grade(user)
1414
max_grade = user_max_grade(user)
1515
story = user_current_story(user)
1616
xp = user_total_xp(user)
17+
game_states = user_game_states(user)
1718

1819
render(
1920
conn,
@@ -22,23 +23,72 @@ defmodule CadetWeb.UserController do
2223
grade: grade,
2324
max_grade: max_grade,
2425
story: story,
25-
xp: xp
26+
xp: xp,
27+
game_states: game_states
2628
)
2729
end
2830

31+
def update_game_states(conn, %{"gameStates" => new_game_states}) do
32+
user = conn.assigns[:current_user]
33+
34+
case update(user, new_game_states) do
35+
{:ok, nil} ->
36+
text(conn, "OK")
37+
38+
{:error, {status, message}} ->
39+
conn
40+
|> put_status(status)
41+
|> text(message)
42+
end
43+
end
44+
45+
def clear_up_game_states(conn, _) do
46+
user = conn.assigns[:current_user]
47+
48+
case clear(user) do
49+
{:ok, nil} ->
50+
text(conn, "OK")
51+
52+
{:error, {status, message}} ->
53+
conn
54+
|> put_status(status)
55+
|> text(message)
56+
end
57+
end
58+
2959
swagger_path :index do
3060
get("/user")
3161

3262
summary("Get the name, role and group of a user")
3363

3464
security([%{JWT: []}])
35-
3665
produces("application/json")
37-
3866
response(200, "OK", Schema.ref(:UserInfo))
3967
response(401, "Unauthorised")
4068
end
4169

70+
swagger_path :update_game_states do
71+
put("/user/game_states/save")
72+
summary("update user's game states")
73+
security([%{JWT: []}])
74+
consumes("application/json")
75+
76+
parameters do
77+
new_game_states(:body, :map, "new game states", required: true)
78+
end
79+
80+
response(200, "OK")
81+
response(403, "Please try again later.")
82+
end
83+
84+
swagger_path :clear_up_game_states do
85+
put("/user/game_states/clear")
86+
summary("clear up users' game data saved")
87+
security([%{JWT: []}])
88+
response(200, "OK")
89+
response(403, "Please try again later.")
90+
end
91+
4292
def swagger_definitions do
4393
%{
4494
UserInfo:
@@ -79,6 +129,13 @@ defmodule CadetWeb.UserController do
79129
:integer,
80130
"Amount of xp. Only provided for 'Student'. " <> "Value will be 0 for non-students."
81131
)
132+
133+
game_states(
134+
:map,
135+
"States for user's game, including users' collectibles and completed quests.\n" <>
136+
"Collectibles is a map.\n" <>
137+
"Completed quests is an array of strings"
138+
)
82139
end
83140
end,
84141
UserStory:

lib/cadet_web/router.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ defmodule CadetWeb.Router do
5757
post("/notification/acknowledge", NotificationController, :acknowledge)
5858

5959
get("/user", UserController, :index)
60+
put("/user/game_states/clear", UserController, :clear_up_game_states)
61+
put("/user/game_states/save", UserController, :update_game_states)
6062

6163
post("/chat/token", ChatController, :index)
6264
post("/chat/notify", ChatController, :notify)

lib/cadet_web/views/user_view.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
defmodule CadetWeb.UserView do
22
use CadetWeb, :view
33

4-
def render("index.json", %{user: user, grade: grade, max_grade: max_grade, xp: xp, story: story}) do
4+
def render("index.json", %{
5+
user: user,
6+
grade: grade,
7+
max_grade: max_grade,
8+
xp: xp,
9+
story: story,
10+
game_states: game_states
11+
}) do
512
%{
613
name: user.name,
714
role: user.role,
@@ -17,7 +24,8 @@ defmodule CadetWeb.UserView do
1724
transform_map_for_view(story, %{
1825
story: :story,
1926
playStory: :play_story?
20-
})
27+
}),
28+
gameStates: game_states
2129
}
2230
end
2331
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule Cadet.Repo.Migrations.AddGameStates do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:users) do
6+
add(:game_states, :map, default: %{collectibles: %{}, completed_quests: []})
7+
end
8+
end
9+
end

test/cadet/accounts/accounts_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ defmodule Cadet.AccountsTest do
106106
describe "sign in using nusnet_id" do
107107
test "unregistered user" do
108108
use_cassette "accounts/sign_in#1" do
109-
{:ok, _} = Accounts.sign_in("e012345", "TOM", @token)
109+
{:ok, _user} = Accounts.sign_in("e012345", "TOM", @token)
110110
assert Repo.one(Query.nusnet_id("e012345")).uid == "e012345"
111111
end
112112
end

test/cadet/updater/xml_parser_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Cadet.Updater.XMLParserTest do
99
import ExUnit.CaptureLog
1010

1111
@local_name "test/fixtures/local_repo"
12+
1213
# @locations %{mission: "missions", sidequest: "quests", path: "paths", contest: "contests"}
1314

1415
setup do

0 commit comments

Comments
 (0)