Skip to content

Commit

Permalink
feat(ui): server can run arbitrary ui actions
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Jun 26, 2023
1 parent 8fa6fcf commit f0a6074
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 17 deletions.
4 changes: 4 additions & 0 deletions client/src/app/helpers/data-grabber.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export class DataGrabberInterceptor implements HttpInterceptor {
if (body.notifications) {
this.store.dispatch(new SetNotifications(body.notifications));
}

if (body.actions) {
this.store.dispatch(body.actions);
}
}),
);
}
Expand Down
2 changes: 2 additions & 0 deletions client/src/app/helpers/store-context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { inject } from '@angular/core';
import { IAttachmentHelpers } from '@interfaces';
import { NotifyService } from '@services/notify.service';
import { VisualService } from '@services/visual.service';

export function getStateHelpers(): IAttachmentHelpers {
return {
visual: inject(VisualService),
notify: inject(NotifyService),
};
}
11 changes: 5 additions & 6 deletions client/src/app/services/notify.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { Injectable } from '@angular/core';
import { ToastController } from '@ionic/angular';

@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class NotifyService {

constructor(private toastController: ToastController) {}

private async showToast(message: string, color: string) {
public async showToast(message: string, color: string) {
const toast = await this.toastController.create({
message,
duration: 1500,
Expand All @@ -17,9 +16,9 @@ export class NotifyService {
buttons: [
{
text: 'Close',
role: 'cancel'
}
]
role: 'cancel',
},
],
});

await toast.present();
Expand Down
4 changes: 3 additions & 1 deletion client/src/interfaces/ngxs.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { StateContext } from '@ngxs/store';
import { type NotifyService } from '@services/notify.service';
import { type VisualService } from '@services/visual.service';

export interface IAttachmentHelpers {
visual: VisualService;
notify: NotifyService;
}

export interface IAttachment {
action: any;
handler: (
ctx: StateContext<any>,
action?: any,
helpers?: IAttachmentHelpers
helpers?: IAttachmentHelpers,
) => void;
}
5 changes: 5 additions & 0 deletions client/src/stores/inventory/inventory.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export class ApplyInventoryPatches {
static type = '[Inventory] Apply Patches';
constructor(public patches: jsonpatch.Operation[]) {}
}

export class RemoveItemFromInventory {
static type = '[Inventory] Remove Item';
constructor(public instanceId: string) {}
}
5 changes: 5 additions & 0 deletions client/src/stores/notifications/notifications.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ export class ClearNotificationActions {
static type = '[Notifications] Clear Actions';
constructor(public id: string) {}
}

export class Notify {
static type = 'Notify';
constructor(public messageType: string, public message: string) {}
}
3 changes: 3 additions & 0 deletions client/src/stores/notifications/notifications.attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {
ApplyNotificationsPatches,
ClearNotificationActions,
MarkNotificationRead,
Notify,
SetNotifications,
} from './notifications.actions';
import {
applyNotificationsPatches,
clearActions,
markRead,
notify,
setNotifications,
} from './notifications.functions';

Expand All @@ -17,4 +19,5 @@ export const attachments: IAttachment[] = [
{ action: ApplyNotificationsPatches, handler: applyNotificationsPatches },
{ action: MarkNotificationRead, handler: markRead },
{ action: ClearNotificationActions, handler: clearActions },
{ action: Notify, handler: notify },
];
15 changes: 14 additions & 1 deletion client/src/stores/notifications/notifications.functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { INotification, INotificationsStore } from '@interfaces';
import {
IAttachmentHelpers,
INotification,
INotificationsStore,
} from '@interfaces';
import { StateContext } from '@ngxs/store';
import { patch, updateItem } from '@ngxs/store/operators';
import { applyPatch } from 'fast-json-patch';
Expand All @@ -7,6 +11,7 @@ import {
ApplyNotificationsPatches,
ClearNotificationActions,
MarkNotificationRead,
Notify,
SetNotifications,
} from './notifications.actions';

Expand Down Expand Up @@ -65,3 +70,11 @@ export function clearActions(
}),
);
}

export function notify(
ctx: StateContext<INotificationsStore>,
{ message, messageType }: Notify,
helpers: IAttachmentHelpers | undefined,
) {
helpers?.notify.showToast(message, messageType);
}
12 changes: 6 additions & 6 deletions client/src/styles/theme/ateoat.theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
--ion-color-tertiary-shade: #3d7173;
--ion-color-tertiary-tint: #588d8f;

--ion-color-success: #2dd36f;
--ion-color-success-rgb: 45,211,111;
--ion-color-success-contrast: #000000;
--ion-color-success-contrast-rgb: 0,0,0;
--ion-color-success-shade: #28ba62;
--ion-color-success-tint: #42d77d;
--ion-color-success: #27b05d;
--ion-color-success-rgb: 39,176,93;
--ion-color-success-contrast: #ffffff;
--ion-color-success-contrast-rgb: 255,255,255;
--ion-color-success-shade: #229b52;
--ion-color-success-tint: #3db86d;

--ion-color-warning: #ffc409;
--ion-color-warning-rgb: 255,196,9;
Expand Down
2 changes: 2 additions & 0 deletions server/src/interfaces/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface IFullUser {
achievements: Achievements;
inventory: Inventory;
items: InventoryItem[];

actions: Array<{ type: string } & any>;
}

export interface IPatchUser {
Expand Down
22 changes: 20 additions & 2 deletions server/src/modules/discoveries/discoveries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ export class DiscoveriesService {
},
);

return { discoveries: discoveryPatches };
return {
discoveries: discoveryPatches,
actions: [
{
type: 'Notify',
messageType: 'success',
message: `You collected ${itemDefinition.name}!`,
},
],
};
}

async discoverEquipment(
Expand Down Expand Up @@ -127,6 +136,15 @@ export class DiscoveriesService {
},
);

return { discoveries: discoveryPatches };
return {
discoveries: discoveryPatches,
actions: [
{
type: 'Notify',
messageType: 'success',
message: `You collected ${itemDefinition.name}!`,
},
],
};
}
}
13 changes: 12 additions & 1 deletion server/src/modules/player/gameplay.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,17 @@ export class GameplayService {
},
);

return { player: playerPatches };
return {
player: playerPatches,
actions: [
{
type: 'Notify',
messageType: 'success',
message: `You sold ${
item.name
} for ${coinsGained.toLocaleString()} coins!`,
},
],
};
}
}

0 comments on commit f0a6074

Please sign in to comment.