Skip to content

Commit 2949a65

Browse files
committed
starting char sync; missing: connecting handshake
1 parent 16a71df commit 2949a65

File tree

8 files changed

+206
-47
lines changed

8 files changed

+206
-47
lines changed

src/components/progress/tracker/Damage.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default class Damage extends Vue {
7171
types[nr - 1] = this.getNext(types[nr - 1]);
7272
this.$forceUpdate();
7373
74-
CharacterStorage.saveCharacter(this.editingCharacter);
74+
CharacterStorage.saveCharacter(this.editingCharacter, true);
7575
}
7676
7777
private getNext(type: DamageType): DamageType {

src/components/progress/tracker/Humanity.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default class Humanity extends Vue {
4343
this.editingCharacter.humanity = nr === this.editingCharacter.humanity ? 0 : nr;
4444
}
4545
46-
CharacterStorage.saveCharacter(this.editingCharacter);
46+
CharacterStorage.saveCharacter(this.editingCharacter, true);
4747
}
4848
}
4949
</script>

src/libs/io/character-storage.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {ICharacter, ICharacterDirectory} from "@/types/models";
22
//@ts-ignore
33
import {v4 as uuidv4} from 'uuid';
44
import {Storage} from "@/libs/io/storage";
5+
import {VicarSync} from "@/libs/io/vicar-sync";
56

67
export default class CharacterStorage {
78

@@ -64,10 +65,14 @@ export default class CharacterStorage {
6465
}
6566
}
6667

67-
public static saveCharacter(character: ICharacter) {
68+
public static saveCharacter(character: ICharacter, triggerSync: boolean = false) {
6869
Storage.writeStorage("character-" + character.id, JSON.stringify(character)).catch(e => {
6970
console.error(e);
7071
});
72+
73+
if (triggerSync) {
74+
VicarSync.triggerCharacterSync(character);
75+
}
7176
}
7277

7378
public static addCharacter(character: ICharacter): string {

src/libs/io/vicar-net.ts

+76-32
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import {get, patch, post, put, del} from "@/libs/io/rest";
22
import {IHomebrewClan, IHomebrewDiscipline} from "@/types/data";
3+
import {ICharacter} from "@/types/models";
34

45
export interface VicarNetAccount {
56
id: number;
67
email: string;
78
alias: string;
89
}
910

10-
export type ClanResponse = {clan: IHomebrewClan, neededHomebrewDisciplines: IHomebrewDiscipline[]};
11-
type FromInviteResponse = {type: "clan"|"discipline", content: IHomebrewDiscipline|ClanResponse};
11+
export type ClanResponse = { clan: IHomebrewClan, neededHomebrewDisciplines: IHomebrewDiscipline[] };
12+
type FromInviteResponse = { type: "clan" | "discipline", content: IHomebrewDiscipline | ClanResponse };
1213

1314
export class VicarNet {
1415

@@ -27,7 +28,7 @@ export class VicarNet {
2728

2829
public static async beginRegister(email: string, alias: string): Promise<boolean> {
2930
try {
30-
const [status, data] = await post<{message: string}>("/auth/register", {email, alias});
31+
const [status, data] = await post<{ message: string }>("/auth/register", {email, alias});
3132
return status === 202 && data.message === "await activation";
3233
} catch (e) {
3334
console.error(e);
@@ -37,7 +38,10 @@ export class VicarNet {
3738

3839
public static async finishRegister(activationCode: string): Promise<boolean> {
3940
try {
40-
const [status, data] = await put<{message: string, user: VicarNetAccount & {passkey: string}}>("/auth/activate/" + activationCode);
41+
const [status, data] = await put<{
42+
message: string,
43+
user: VicarNetAccount & { passkey: string }
44+
}>("/auth/activate/" + activationCode);
4145
if (status === 201 && data.message === "activated") {
4246
const {passkey, ...user} = data.user;
4347
localStorage.setItem("vicar-net:account", JSON.stringify(user));
@@ -54,7 +58,7 @@ export class VicarNet {
5458

5559
public static async checkLogin(): Promise<boolean> {
5660
try {
57-
const [status, data] = await post<{message: string}>("/auth/login", undefined, this.buildHeaders());
61+
const [status, data] = await post<{ message: string }>("/auth/login", undefined, this.buildHeaders());
5862
return status === 200 && data.message === "logged in";
5963
} catch (e) {
6064
console.error(e);
@@ -64,7 +68,7 @@ export class VicarNet {
6468

6569
public static async beginRecover(email: string): Promise<boolean> {
6670
try {
67-
const [status, data] = await patch<{message: string}>(`/auth/recover/${email}/begin`);
71+
const [status, data] = await patch<{ message: string }>(`/auth/recover/${email}/begin`);
6872
return status === 202 && data.message === "await recover";
6973
} catch (e) {
7074
console.error(e);
@@ -74,7 +78,10 @@ export class VicarNet {
7478

7579
public static async finishRecover(recoveryCode: string): Promise<boolean> {
7680
try {
77-
const [status, data] = await put<{message: string, user: VicarNetAccount & {passkey: string}}>(`/auth/recover/${recoveryCode}/finish`);
81+
const [status, data] = await put<{
82+
message: string,
83+
user: VicarNetAccount & { passkey: string }
84+
}>(`/auth/recover/${recoveryCode}/finish`);
7885
if (status === 200 && data.message === "recovered") {
7986
const {passkey, ...user} = data.user;
8087
localStorage.setItem("vicar-net:account", JSON.stringify(user));
@@ -90,7 +97,7 @@ export class VicarNet {
9097

9198
public static async bindVicarShareIdToAlias(vicarShareId: string): Promise<boolean> {
9299
try {
93-
const [status, data] = await post<{message: string}>(`/share/id`, {
100+
const [status, data] = await post<{ message: string }>(`/share/id`, {
94101
shareId: vicarShareId
95102
}, this.buildHeaders());
96103
return status === 200 && data.message === "ok";
@@ -102,7 +109,7 @@ export class VicarNet {
102109

103110
public static async unbindVicarShareIdFromAlias(): Promise<boolean> {
104111
try {
105-
const [status, data] = await del<{message: string}>(`/share/id`, this.buildHeaders());
112+
const [status, data] = await del<{ message: string }>(`/share/id`, this.buildHeaders());
106113
return status === 200 && data.message === "ok";
107114
} catch (e) {
108115
console.error(e);
@@ -117,7 +124,7 @@ export class VicarNet {
117124
}
118125

119126
try {
120-
const [status, data] = await get<{id: string}>(`/share/${idOrAlias}/id`);
127+
const [status, data] = await get<{ id: string }>(`/share/${idOrAlias}/id`);
121128
if (status === 200) {
122129
return data.id;
123130
}
@@ -130,15 +137,15 @@ export class VicarNet {
130137

131138
private static async ping(): Promise<boolean> {
132139
try {
133-
const [status, data] = await get<{message: string}>("/ping");
140+
const [status, data] = await get<{ message: string }>("/ping");
134141
return status === 200 && data.message === "pong";
135142
} catch (e) {
136143
console.error(e);
137144
return false;
138145
}
139146
}
140147

141-
private static buildHeaders(): {[key: string]: string} {
148+
private static buildHeaders(): { [key: string]: string } {
142149
if (!this.isLoggedIn) {
143150
return {};
144151
}
@@ -147,9 +154,15 @@ export class VicarNet {
147154
return {"X-User-ID": account.id.toString(), "X-User-Passkey": localStorage.getItem("vicar-net:passkey")!};
148155
}
149156

150-
public static async getClans(search: string = "", page: number = 1, limit: number = 20): Promise<{total: number, items: IHomebrewClan[]}> {
157+
public static async getClans(search: string = "", page: number = 1, limit: number = 20): Promise<{
158+
total: number,
159+
items: IHomebrewClan[]
160+
}> {
151161
try {
152-
const [status, data] = await get<{total: number, items: IHomebrewClan[]}>(`/homebrew/clans?search=${search}&page=${page}&limit=${limit}`);
162+
const [status, data] = await get<{
163+
total: number,
164+
items: IHomebrewClan[]
165+
}>(`/homebrew/clans?search=${search}&page=${page}&limit=${limit}`);
153166
if (status === 200) {
154167
return data;
155168
}
@@ -160,9 +173,15 @@ export class VicarNet {
160173
}
161174
}
162175

163-
public static async getClan(id: number): Promise<{clan: IHomebrewClan, neededHomebrewDisciplines: IHomebrewDiscipline[]}|null> {
176+
public static async getClan(id: number): Promise<{
177+
clan: IHomebrewClan,
178+
neededHomebrewDisciplines: IHomebrewDiscipline[]
179+
} | null> {
164180
try {
165-
const [status, data] = await get<{clan: IHomebrewClan, neededHomebrewDisciplines: IHomebrewDiscipline[]}>(`/homebrew/clans/${id}`, this.buildHeaders());
181+
const [status, data] = await get<{
182+
clan: IHomebrewClan,
183+
neededHomebrewDisciplines: IHomebrewDiscipline[]
184+
}>(`/homebrew/clans/${id}`, this.buildHeaders());
166185
if (status === 200) {
167186
return data;
168187
}
@@ -173,9 +192,15 @@ export class VicarNet {
173192
}
174193
}
175194

176-
public static async getDisciplines(search: string = "", page: number = 1, limit: number = 20): Promise<{total: number, items: IHomebrewDiscipline[]}> {
195+
public static async getDisciplines(search: string = "", page: number = 1, limit: number = 20): Promise<{
196+
total: number,
197+
items: IHomebrewDiscipline[]
198+
}> {
177199
try {
178-
const [status, data] = await get<{total: number, items: IHomebrewDiscipline[]}>(`/homebrew/disciplines?search=${search}&page=${page}&limit=${limit}`);
200+
const [status, data] = await get<{
201+
total: number,
202+
items: IHomebrewDiscipline[]
203+
}>(`/homebrew/disciplines?search=${search}&page=${page}&limit=${limit}`);
179204
if (status === 200) {
180205
return data;
181206
}
@@ -186,7 +211,7 @@ export class VicarNet {
186211
}
187212
}
188213

189-
public static async getDiscipline(id: number): Promise<IHomebrewDiscipline|null> {
214+
public static async getDiscipline(id: number): Promise<IHomebrewDiscipline | null> {
190215
try {
191216
const [status, data] = await get<IHomebrewDiscipline>(`/homebrew/disciplines/${id}`, this.buildHeaders());
192217
if (status === 200) {
@@ -199,35 +224,39 @@ export class VicarNet {
199224
}
200225
}
201226

202-
public static async generateAccessCodeForClan(clan: IHomebrewClan): Promise<string|null> {
227+
public static async generateAccessCodeForClan(clan: IHomebrewClan): Promise<string | null> {
203228
if (!this.isLoggedIn || clan.creator !== this.account.alias) {
204229
return null;
205230
}
206231

207232
try {
208-
const [_, data] = await post<{inviteCode: string}>(`/homebrew/clans/${clan.id}/invite`, undefined, this.buildHeaders());
233+
const [_, data] = await post<{
234+
inviteCode: string
235+
}>(`/homebrew/clans/${clan.id}/invite`, undefined, this.buildHeaders());
209236
return data.inviteCode;
210237
} catch (e) {
211238
console.error(e);
212239
return null;
213240
}
214241
}
215242

216-
public static async generateAccessCodeForDiscipline(discipline: IHomebrewDiscipline): Promise<string|null> {
243+
public static async generateAccessCodeForDiscipline(discipline: IHomebrewDiscipline): Promise<string | null> {
217244
if (!this.isLoggedIn || discipline.creator !== this.account.alias) {
218245
return null;
219246
}
220247

221248
try {
222-
const [_, data] = await post<{inviteCode: string}>(`/homebrew/disciplines/${discipline.id}/invite`, undefined, this.buildHeaders());
249+
const [_, data] = await post<{
250+
inviteCode: string
251+
}>(`/homebrew/disciplines/${discipline.id}/invite`, undefined, this.buildHeaders());
223252
return data.inviteCode;
224253
} catch (e) {
225254
console.error(e);
226255
return null;
227256
}
228257
}
229258

230-
public static async createClan(clan: IHomebrewClan): Promise<IHomebrewClan|undefined> {
259+
public static async createClan(clan: IHomebrewClan): Promise<IHomebrewClan | undefined> {
231260
if (!this.isLoggedIn) {
232261
return undefined;
233262
}
@@ -244,7 +273,7 @@ export class VicarNet {
244273
}
245274
}
246275

247-
public static async updateClan(clan: IHomebrewClan): Promise<IHomebrewClan|undefined> {
276+
public static async updateClan(clan: IHomebrewClan): Promise<IHomebrewClan | undefined> {
248277
if (!this.isLoggedIn || clan.creator !== this.account.alias) {
249278
return undefined;
250279
}
@@ -267,15 +296,15 @@ export class VicarNet {
267296
}
268297

269298
try {
270-
const [status, data] = await del<{message: string}>(`/homebrew/clans/${clan.id}`, this.buildHeaders());
299+
const [status, data] = await del<{ message: string }>(`/homebrew/clans/${clan.id}`, this.buildHeaders());
271300
return status === 200;
272301
} catch (e) {
273302
console.error(e);
274303
return false;
275304
}
276305
}
277306

278-
public static async createDiscipline(discipline: IHomebrewDiscipline): Promise<IHomebrewDiscipline|undefined> {
307+
public static async createDiscipline(discipline: IHomebrewDiscipline): Promise<IHomebrewDiscipline | undefined> {
279308
if (!this.isLoggedIn) {
280309
return undefined;
281310
}
@@ -292,7 +321,7 @@ export class VicarNet {
292321
}
293322
}
294323

295-
public static async updateDiscipline(discipline: IHomebrewDiscipline): Promise<IHomebrewDiscipline|undefined> {
324+
public static async updateDiscipline(discipline: IHomebrewDiscipline): Promise<IHomebrewDiscipline | undefined> {
296325
if (!this.isLoggedIn || discipline.creator !== this.account.alias) {
297326
return undefined;
298327
}
@@ -315,21 +344,26 @@ export class VicarNet {
315344
}
316345

317346
try {
318-
const [status, data] = await del<{message: string}>(`/homebrew/disciplines/${discipline.id}`, this.buildHeaders());
347+
const [status, data] = await del<{
348+
message: string
349+
}>(`/homebrew/disciplines/${discipline.id}`, this.buildHeaders());
319350
return status === 200;
320351
} catch (e) {
321352
console.error(e);
322353
return false;
323354
}
324355
}
325356

326-
public static async getMyContent(): Promise<{clans: IHomebrewClan[], disciplines: IHomebrewDiscipline[]}> {
357+
public static async getMyContent(): Promise<{ clans: IHomebrewClan[], disciplines: IHomebrewDiscipline[] }> {
327358
if (!this.isLoggedIn) {
328359
return {clans: [], disciplines: []};
329360
}
330361

331362
try {
332-
const [status, data] = await get<{clans: IHomebrewClan[], disciplines: IHomebrewDiscipline[]}>(`/homebrew/my-content`, this.buildHeaders());
363+
const [status, data] = await get<{
364+
clans: IHomebrewClan[],
365+
disciplines: IHomebrewDiscipline[]
366+
}>(`/homebrew/my-content`, this.buildHeaders());
333367
if (status === 200) {
334368
return data;
335369
}
@@ -340,7 +374,7 @@ export class VicarNet {
340374
}
341375
}
342376

343-
public static async getContentFromInvite(inviteCode: string): Promise<FromInviteResponse|undefined> {
377+
public static async getContentFromInvite(inviteCode: string): Promise<FromInviteResponse | undefined> {
344378
try {
345379
const [_, data] = await get<FromInviteResponse>(`/homebrew/from-invite/${inviteCode}`);
346380
return data;
@@ -349,4 +383,14 @@ export class VicarNet {
349383
return undefined;
350384
}
351385
}
386+
387+
public static async postCharSync(roomId: string, charData: string): Promise<void> {
388+
try {
389+
await post(`/sync/characters/${roomId}`, {
390+
data: charData
391+
}, this.buildHeaders());
392+
} catch (e) {
393+
console.error(e);
394+
}
395+
}
352396
}

0 commit comments

Comments
 (0)