Skip to content

Commit 3630c8b

Browse files
PhilippMDoernerPhilippMDoerner
PhilippMDoerner
authored andcommitted
Make marker types and playerclasses capable of being campaign specific
1 parent d56bfb6 commit 3630c8b

10 files changed

+143
-12
lines changed

sql/V1.12__add_link_types.sql

+71-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- WIKIENTRIES_RELATIONSHIP_TYPE
12
CREATE TABLE "wikientries_relationship_type" (
23
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
34
"name" varchar(50) NOT NULL,
@@ -25,6 +26,7 @@ VALUES (
2526
'2024-12-13 12:00:00',
2627
'2024-12-13 12:00:00'
2728
);
29+
-- WIKIENTRIES_RELATIONSHIP - ADD LINK_TYPE_ID AND CAMPAIGN_ID
2830
-- Migrate wikientries_relationships to a table that has a link_type_id fk column
2931
CREATE TABLE wikientries_relationships_new (
3032
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
@@ -34,7 +36,7 @@ CREATE TABLE wikientries_relationships_new (
3436
"weight" integer,
3537
"creation_datetime" datetime NOT NULL,
3638
"update_datetime" datetime NOT NULL,
37-
"campaign_id" integer,
39+
"campaign_id" integer REFERENCES "wikientries_campaign" ("id") DEFERRABLE INITIALLY DEFERRED,
3840
"link_type_id" integer NOT NULL DEFAULT 1,
3941
FOREIGN KEY (link_type_id) REFERENCES wikientries_relationship_type (id)
4042
);
@@ -64,4 +66,71 @@ FROM wikientries_relationships;
6466
DROP TABLE wikientries_relationships;
6567
-- Rename the new table to the old table's name
6668
ALTER TABLE wikientries_relationships_new
67-
RENAME TO wikientries_relationships;
69+
RENAME TO wikientries_relationships;
70+
-- MAP_MARKERTYPE ADD CAMPAIGN_ID COLUMN
71+
-- Extend map marker type to have an optional campaign_id column
72+
CREATE TABLE map_markertype_new (
73+
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
74+
"name" varchar(50) NOT NULL,
75+
"icon" varchar(20) NOT NULL,
76+
"is_text_marker" bool NOT NULL,
77+
"creation_datetime" datetime NOT NULL,
78+
"update_datetime" datetime NOT NULL,
79+
"fontawesome_type" varchar(4) NOT NULL,
80+
"color" varchar(20) NOT NULL,
81+
"campaign_id" integer REFERENCES "wikientries_campaign" ("id") DEFERRABLE INITIALLY DEFERRED
82+
);
83+
-- Copy the data from the old table to the new table
84+
INSERT INTO map_markertype_new (
85+
"id",
86+
"name",
87+
"icon",
88+
"is_text_marker",
89+
"creation_datetime",
90+
"update_datetime",
91+
"fontawesome_type",
92+
"color",
93+
"campaign_id"
94+
)
95+
SELECT "id",
96+
"name",
97+
"icon",
98+
"is_text_marker",
99+
"creation_datetime",
100+
"update_datetime",
101+
"fontawesome_type",
102+
"color",
103+
NULL AS "campaign_id"
104+
FROM map_markertype;
105+
-- Drop the old table
106+
DROP TABLE map_markertype;
107+
-- Rename the new table to the old table's name
108+
ALTER TABLE map_markertype_new
109+
RENAME TO map_markertype;
110+
-- WIKIENTRIES_PLAYERCLASS ADD CAMPAIGN_ID COLUMN
111+
CREATE TABLE "wikientries_playerclass_new" (
112+
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
113+
"name" varchar(200) NOT NULL UNIQUE,
114+
"campaign_id" integer REFERENCES "wikientries_campaign" ("id") DEFERRABLE INITIALLY DEFERRED,
115+
"creation_datetime" datetime NOT NULL,
116+
"update_datetime" datetime NOT NULL
117+
);
118+
-- Copy the data from the old table to the new table
119+
INSERT INTO wikientries_playerclass_new (
120+
"id",
121+
"name",
122+
"campaign_id",
123+
"creation_datetime",
124+
"update_datetime"
125+
)
126+
SELECT "id",
127+
"name",
128+
NULL,
129+
"creation_datetime",
130+
"update_datetime"
131+
FROM wikientries_playerclass;
132+
-- Drop the old table
133+
DROP TABLE wikientries_playerclass;
134+
-- Rename the new table to the old table's name
135+
ALTER TABLE wikientries_playerclass_new
136+
RENAME TO wikientries_playerclass;

src/applications/mapMarkerType/markerTypeModel.nim

+3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import norm/[model, pragmas]
22
import lowdb/sqlite
33
import std/[strutils, options]
44
import constructor/defaults
5+
import ../campaign/campaignModel
56
import ../../utils/djangoDateTime/[djangoDateTimeType]
67
import ../../applicationSettings
8+
import ../../applicationConstants
79

810
type FontAwesomeType* = enum
911
##Determines whehter an icon is supposed to come from 4.7 or 5
@@ -30,6 +32,7 @@ type MarkerType* {.defaults, tableName: MARKERTYPE_TABLE.} = ref object of Mode
3032
update_datetime*: DjangoDateTime = djangoDateTimeType.now()
3133
fontawesome_type*: FontAwesomeType = FontAwesomeType.VERSION5
3234
color*: string = ""
35+
campaign_id* {.fk: Campaign.}: int64 = MODEL_INIT_ID
3336

3437
implDefaults(MarkerType, {DefaultFlag.defExported, DefaultFlag.defTypeConstr})
3538

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import std/[strformat]
2+
import norm/[sqlite]
3+
import ./markerTypeModel
4+
import ../genericRawRepository
5+
import ../../applicationSettings
6+
7+
proc getMarkerTypes*(con: DbConn, campaignName: string): seq[MarkerType] =
8+
const getMarkerTypesSQL: string = fmt """
9+
SELECT
10+
typ.name,
11+
typ.icon,
12+
typ.is_text_marker,
13+
typ.creation_datetime,
14+
typ.update_datetime,
15+
typ.fontawesome_type,
16+
typ.color,
17+
typ.campaign_id,
18+
typ.id
19+
FROM {MARKERTYPE_TABLE} AS typ
20+
LEFT JOIN wikientries_campaign AS camp ON camp.id = typ.campaign_id
21+
WHERE
22+
camp.name = ? OR typ.campaign_id IS NULL
23+
"""
24+
25+
let queryParams: array[1, DbValue] = [campaignName.dbValue()]
26+
return con.rawSelectRows(getMarkerTypesSQL, MarkerType, queryParams)

src/applications/mapMarkerType/markerTypeRoutes.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ proc addMarkerTypeRoutes*(app: Prologue) =
6262
app.addRoute(
6363
re fmt"/markertype/{CAMPAIGN_NAME_PATTERN}/overview",
6464
handler = createReadListHandler(
65-
readListProc = getMarkerTypes,
65+
readListProc = getCampaignMarkerTypes,
6666
checkPermission = checkReadMarkerTypeListPermission,
6767
serialize = serializeMarkerTypes
6868
),
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import ../genericArticleRepository
2-
import markerTypeModel
31
import norm/sqlite
4-
export markerTypeModel
2+
import markerTypeModel
3+
import markerTypeRepository
4+
import ../genericArticleRepository
55
import ../allUrlParams
66

7+
export markerTypeModel
8+
79
proc getMarkerTypes*(connection: DbConn, requestParams: ReadWithoutParams): seq[MarkerType] =
810
## lists all campaign entries using a limited but performant representation of a MarkerType
911
result = connection.getList(MarkerType)
12+
13+
proc getCampaignMarkerTypes*(con: DbConn, params: ReadListParams): seq[MarkerType] =
14+
return con.getMarkerTypes(params.campaignName)

src/applications/nodeMap/nodeMapRepository.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ proc getLinkTypes*(con: DbConn, campaignName: string): seq[CustomLinkType] =
177177
typ.creation_datetime,
178178
typ.update_datetime,
179179
typ.id
180-
FROM wikientries_relationship_type AS typ
180+
FROM {RELATIONSHIP_KIND_TABLE} AS typ
181181
LEFT JOIN wikientries_campaign AS camp ON camp.id = typ.campaign_id
182182
WHERE
183183
camp.name = ? OR typ.campaign_id IS NULL

src/applications/playerclass/playerClassModel.nim

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import norm/[model, pragmas]
2+
import ../character/characterModel
3+
import ../campaign/campaignModel
24
import ../../applicationSettings
35
import ../../applicationConstants
4-
import ../character/characterModel
56
import ../../utils/djangoDateTime/djangoDateTimeType
67
import constructor/defaults
78

89
type PlayerClass* {.defaults, tableName: PLAYERCLASS_TABLE.} = ref object of Model
910
name*: string = ""
11+
campaign_id* {.fk: Campaign.}: int64 = MODEL_INIT_ID
1012
update_datetime*: DjangoDateTime = djangoDateTimeType.now()
1113
creation_datetime*: DjangoDateTime = djangoDateTimeType.now()
1214
implDefaults(PlayerClass, {DefaultFlag.defExported, DefaultFlag.defTypeConstr})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import std/[strformat]
2+
import norm/[sqlite]
3+
import ./playerClassModel
4+
import ../genericRawRepository
5+
import ../../applicationSettings
6+
7+
proc getPlayerClasses*(con: DbConn, campaignName: string): seq[PlayerClass] =
8+
const getPlayerClassesSQL: string = fmt """
9+
SELECT
10+
typ.name,
11+
typ.campaign_id,
12+
typ.update_datetime,
13+
ty.creation_datetime,
14+
typ.id
15+
FROM {PLAYERCLASS_TABLE} AS typ
16+
LEFT JOIN wikientries_campaign AS camp ON camp.id = typ.campaign_id
17+
WHERE
18+
camp.name = ? OR typ.campaign_id IS NULL
19+
"""
20+
21+
let queryParams: array[1, DbValue] = [campaignName.dbValue()]
22+
return con.rawSelectRows(getPlayerClassesSQL, PlayerClass, queryParams)

src/applications/playerclass/playerClassRoutes.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ proc addPlayerClassRoutes*(app: Prologue) =
4646
app.addRoute(
4747
re fmt"/player_class/{CAMPAIGN_NAME_PATTERN}/overview/",
4848
handler = createReadListHandler[ReadListParams, PlayerClass, PlayerClassSerializable](
49-
getPlayerClasses,
49+
getCampaignPlayerClasses,
5050
checkPlayerClassPermission,
5151
serializePlayerClasses
5252
),
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import norm/sqlite
2+
import playerClassModel
3+
import playerClassRepository
14
import ../allUrlParams
25
import ../genericArticleRepository
3-
import playerClassModel
4-
import norm/sqlite
56

67
proc getPlayerClasses*(connection: DbConn, params: ReadListParams): seq[PlayerClass] =
78
result = connection.getList(PlayerClass)
89

910
proc createPlayerClassConnection*(connection: DbConn, params: CreateParams, newEntry: var PlayerClassConnection): PlayerClassConnection =
10-
result = connection.createEntryInTransaction(newEntry)
11+
result = connection.createEntryInTransaction(newEntry)
12+
13+
proc getCampaignPlayerClasses*(connection: DbConn, params: ReadListParams): seq[PlayerClass] =
14+
result = connection.getPlayerClasses(params.campaignName)

0 commit comments

Comments
 (0)