Skip to content

Commit 9f62b87

Browse files
authored
Merge pull request #134 from fmasa/db-cleanup
Migrate default values to old Character/Party fields
2 parents 2c82880 + 93c290a commit 9f62b87

File tree

12 files changed

+176
-43
lines changed

12 files changed

+176
-43
lines changed

common/firebase/src/androidMain/kotlin/cz/frantisekmasa/wfrp_master/common/firebase/firestore/Query.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ actual open class Query(
4141
return Query(query.whereEqualTo(field, value))
4242
}
4343

44+
actual fun whereIn(field: String, values: Iterable<Any?>): Query {
45+
return Query(query.whereIn(field, values.toList()))
46+
}
47+
4448
actual fun whereArrayContains(field: String, value: Any): Query {
4549
return Query(query.whereArrayContains(field, value))
4650
}

common/firebase/src/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/firebase/firestore/Query.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ expect open class Query {
1616

1717
fun whereEqualTo(field: String, value: Any?): Query
1818

19+
fun whereIn(field: String, values: Iterable<Any?>): Query
20+
1921
fun whereArrayContains(field: String, value: Any): Query
2022
}

common/firebase/src/jvmMain/kotlin/cz/frantisekmasa/wfrp_master/common/firebase/firestore/Query.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ actual open class Query(
4040
return Query(query.whereEqualTo(field, value))
4141
}
4242

43+
actual fun whereIn(field: String, values: Iterable<Any?>): Query {
44+
return Query(query.whereIn(field, values.toList()))
45+
}
46+
4347
actual fun whereArrayContains(field: String, value: Any): Query {
4448
return Query(query.whereArrayContains(field, value))
4549
}

common/src/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/core/firebase/repositories/FirestoreCharacterItemRepository.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ open class FirestoreCharacterItemRepository<T : CharacterItem<T, *>>(
6868
firestore.collection(Schema.Parties)
6969
.document(partyId.toString())
7070
.collection(Schema.Characters)
71+
.whereEqualTo("archived", false)
7172
.get()
7273
.documents
7374
.map { character ->

common/src/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/core/firebase/repositories/FirestoreCharacterRepository.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ class FirestoreCharacterRepository(
103103

104104
override fun inParty(partyId: PartyId, types: Set<CharacterType>): Flow<List<Character>> {
105105
return characters(partyId)
106+
.whereEqualTo("archived", false)
107+
.whereIn("type", types.map { it.name })
106108
.orderBy("name")
107109
.documents(mapper)
108-
.map { characters -> characters.filter { it.type in types && !it.isArchived } }
109110
}
110111

111112
private fun characters(partyId: PartyId) =

firebase/api.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ export interface Stats {
6262
export interface Character {
6363
id: string,
6464
name: string,
65+
type: string,
66+
publicName: string | null,
67+
size: string | null,
68+
status: {
69+
tier: string,
70+
standing: number,
71+
}
6572
userId: string | null,
6673
career: string,
6774
socialClass: string,
@@ -80,6 +87,14 @@ export interface Character {
8087
sin: number,
8188
hardyWoundsBonus: number
8289
},
90+
woundsModifiers: {
91+
afterMultiplier: number,
92+
extraToughnessBonusMultiplier: number,
93+
isConstruct: boolean
94+
},
95+
encumbranceBonus: number,
96+
avatarUrl: string | null,
97+
hiddenTabs: string[],
8398
ambitions: {
8499
shortTerm: string,
85100
longTerm: string,
@@ -93,6 +108,8 @@ export interface Character {
93108
hardyTalent: boolean,
94109
note: string,
95110
conditions: Conditions,
111+
archived: boolean,
112+
compendiumCareer: { careerId: string, levelId: string } | null,
96113
}
97114

98115
export interface Conditions {

firebase/firestore.indexes.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
{
2-
"indexes": [],
2+
"indexes": [
3+
{
4+
"collectionGroup": "characters",
5+
"queryScope": "COLLECTION",
6+
"fields": [
7+
{ "fieldPath": "archived", "order": "ASCENDING" },
8+
{ "fieldPath": "type", "order": "ASCENDING" },
9+
{ "fieldPath": "name", "order": "ASCENDING" },
10+
{ "fieldPath": "__name__", "order": "ASCENDING" }
11+
]
12+
}
13+
],
314
"fieldOverrides": []
415
}

firebase/firestore.rules

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,9 @@ service cloud.firestore {
525525
(character.userId != null && character.userId == request.auth.uid) ||
526526
(isGameMaster() && (character.userId == null || character.userId in get(/databases/$(database)/documents/parties/$(partyId)).data.users))
527527
)
528-
&& (! ("type" in character) || character.type in ["PLAYER_CHARACTER", "NPC"])
528+
&& character.type in ["PLAYER_CHARACTER", "NPC"]
529529
&& character.name is string && isNotBlank(character.name) && character.name.size() <= 50
530-
&& (! ("publicName" in character) || character.publicName == null || (character.publicName is string && isNotBlank(character.publicName) && character.publicName.size() <= 50))
530+
&& (character.publicName == null || (character.publicName is string && isNotBlank(character.publicName) && character.publicName.size() <= 50))
531531
&& character.career is string && character.career.size() <= 50
532532
&& character.psychology is string && character.psychology.size() <= 200
533533
&& character.motivation is string && character.motivation.size() <= 200
@@ -541,20 +541,21 @@ service cloud.firestore {
541541
&& areAmbitionsValid(character.ambitions)
542542
&& character.note is string && character.note.size() <= 400
543543
&& character.hardyTalent is bool
544-
&& (! ("hiddenTabs" in character) ||
545-
(character.hiddenTabs is list &&
546-
character.hiddenTabs.toSet().size() == character.hiddenTabs.size() &&
547-
[
544+
&& (
545+
character.hiddenTabs is list &&
546+
character.hiddenTabs.toSet().size() == character.hiddenTabs.size() &&
547+
[
548548
"ATTRIBUTES",
549+
"COMBAT",
549550
"CONDITIONS",
550551
"SKILLS_AND_TALENTS",
551552
"SPELLS",
552553
"RELIGION",
553-
"TRAPPINGS"
554+
"TRAPPINGS",
555+
"NOTES"
554556
].hasAll(character.hiddenTabs)
555-
)
556557
)
557-
&& (! ("size" in character) || character.size == null || (
558+
&& (character.size == null || (
558559
character.size is string
559560
&& character.size in [
560561
"TINY",
@@ -567,15 +568,15 @@ service cloud.firestore {
567568
]
568569
)
569570
)
570-
&& (! ("avatarUrl" in character) || character.avatarUrl is string || character.avatarUrl == null)
571-
&& (! ("archived" in character) || (character.archived is bool && (!character.archived || character.userId == null)))
572-
&& (! ("conditions" in character) || areValidConditions(character.conditions))
573-
&& ! ("status" in character) || (
571+
&& (character.avatarUrl is string || character.avatarUrl == null)
572+
&& (character.archived is bool && (!character.archived || character.userId == null))
573+
&& (areValidConditions(character.conditions))
574+
&& (
574575
character.status is map && character.status.keys().toSet() == ["tier", "standing"].toSet()
575576
&& character.status.tier in ["BRASS", "SILVER", "GOLD"]
576577
&& character.status.standing is int && character.status.standing >= 0
577578
)
578-
&& ! ("woundsModifiers" in character) || (
579+
&& (
579580
character.woundsModifiers is map &&
580581
[
581582
"afterMultiplier",
@@ -586,15 +587,13 @@ service cloud.firestore {
586587
character.woundsModifiers.extraToughnessBonusMultiplier is int &&
587588
(! ("isConstruct" in character.woundsModifiers) || character.woundsModifiers.isConstruct is bool)
588589
)
589-
&& (! ("compendiumCareer" in character) || character.compendiumCareer == null || (
590+
&& (character.compendiumCareer == null || (
590591
character.compendiumCareer is map &&
591592
character.compendiumCareer.keys().toSet() == ["careerId", "levelId"].toSet() &&
592593
character.compendiumCareer.careerId is string && isValidUuid(character.compendiumCareer.careerId) &&
593594
character.compendiumCareer.levelId is string && isValidUuid(character.compendiumCareer.levelId)
594595
))
595-
&& (! ("encumbranceBonus" in character) ||
596-
(character.encumbranceBonus is number && character.encumbranceBonus >= 0)
597-
);
596+
&& character.encumbranceBonus is number && character.encumbranceBonus >= 0;
598597
}
599598
}
600599

@@ -704,12 +703,12 @@ service cloud.firestore {
704703
&& isValidPartyName(party.name)
705704
&& party.users == [uid]
706705
&& party.archived is bool
707-
&& (! ("activeCombat" in party) || party.activeCombat == null)
706+
&& party.activeCombat == null
708707
&& (party.gameMasterId == uid || party.gameMasterId == null)
709708
&& party.accessCode is string && party.accessCode != ""
710-
&& (! ("time" in party) || isValidImperialTime(party.time))
709+
&& isValidImperialTime(party.time)
711710
&& areAmbitionsValid(party.ambitions)
712-
&& (! ("settings" in party) || areValidSettings(party.settings))
711+
&& areValidSettings(party.settings)
713712
}
714713

715714
function isValidPartyEdit() {
@@ -720,12 +719,9 @@ service cloud.firestore {
720719
&& areAmbitionsValid(party.ambitions)
721720
&& isValidPartyName(party.name)
722721
&& party.users.toSet() == resource.data.users.toSet()
723-
&& (! ("time" in party) || isValidImperialTime(party.time))
724-
&& (! ("activeCombat" in party)
725-
|| party.activeCombat == null
726-
|| isValidCombat(party.activeCombat)
727-
)
728-
&& (! ("settings" in party) || areValidSettings(party.settings))
722+
&& isValidImperialTime(party.time)
723+
&& (party.activeCombat == null || isValidCombat(party.activeCombat))
724+
&& areValidSettings(party.settings)
729725
&& party.archived is bool;
730726
}
731727

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export async function migrate({firestore}: { firestore: firebase.firestore.Firestore }): Promise<void> {
2+
const characters = await firestore.collectionGroup("characters").get();
3+
4+
for (const document of characters.docs) {
5+
if (!("archived" in document.data())) {
6+
await document.ref.set({archived: false}, {merge: true});
7+
}
8+
9+
if (!("type" in document.data())) {
10+
await document.ref.set({type: "PLAYER_CHARACTER"}, {merge: true});
11+
}
12+
13+
if (!("publicName" in document.data())) {
14+
await document.ref.set({publicName: null}, {merge: true});
15+
}
16+
17+
if (!("encumbranceBonus" in document.data())) {
18+
await document.ref.set({encumbranceBonus: 0}, {merge: true});
19+
}
20+
21+
if (!("compendiumCareer" in document.data())) {
22+
await document.ref.set({compendiumCareer: null}, {merge: true});
23+
}
24+
25+
if (!("avatarUrl" in document.data())) {
26+
await document.ref.set({avatarUrl: null}, {merge: true});
27+
}
28+
29+
if (!("conditions" in document.data())) {
30+
await document.ref.set({conditions: {conditions: {}}}, {merge: true});
31+
}
32+
33+
if (!("status" in document.data())) {
34+
await document.ref.set({status: {tier: "BRASS", standing: 0}}, {merge: true});
35+
}
36+
37+
if (!("hiddenTabs" in document.data())) {
38+
await document.ref.set({hiddenTabs: []}, {merge: true});
39+
}
40+
41+
if (!("size" in document.data())) {
42+
await document.ref.set({size: null}, {merge: true});
43+
}
44+
45+
if (!("woundsModifiers" in document.data())) {
46+
await document.ref.set(
47+
{
48+
woundsModifiers: {
49+
afterMultiplier: 1,
50+
extraToughnessBonusMultiplier: 0,
51+
isConstruct: false,
52+
}
53+
},
54+
{merge: true}
55+
);
56+
}
57+
}
58+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export async function migrate({firestore}: { firestore: firebase.firestore.Firestore }): Promise<void> {
2+
const parties = await firestore.collection("parties").get();
3+
4+
for (const document of parties.docs) {
5+
if (!("activeCombat" in document.data())) {
6+
await document.ref.set({activeCombat: null}, {merge: true});
7+
}
8+
9+
if (!("settings" in document.data())) {
10+
await document.ref.set(
11+
{
12+
settings: {
13+
initiativeStrategy: "INITIATIVE_CHARACTERISTIC",
14+
advantageSystem: "INITIATIVE_CHARACTERISTIC",
15+
advantageCap: ""
16+
}
17+
},
18+
{merge: true},
19+
);
20+
}
21+
22+
if (!("time" in document.data())) {
23+
await document.ref.set(
24+
{
25+
time: {
26+
imperialDay: 1004400,
27+
minutes: 720,
28+
}
29+
},
30+
{merge: true},
31+
);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)