main commit
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { Service } from "@flamework/core";
|
||||
import { DataStoreService } from "@rbxts/services";
|
||||
|
||||
/**
|
||||
* A data store, linked to a roblox DataStore and the DataService.
|
||||
*/
|
||||
export class Store {
|
||||
/**
|
||||
* @param service The instance of the data service.
|
||||
* @param name The name of the store.
|
||||
* @param realStore The real roblox dataStore.
|
||||
*/
|
||||
constructor(
|
||||
private service: DataService,
|
||||
private name: string,
|
||||
private realStore: DataStore,
|
||||
) {}
|
||||
/**
|
||||
* Get a data in the database.
|
||||
* @param key The key of the data.
|
||||
* @returns The value of the key.
|
||||
*/
|
||||
get<T>(key: string): T | undefined {
|
||||
const [value] = this.realStore.GetAsync(key);
|
||||
return value as T;
|
||||
}
|
||||
/**
|
||||
* Set a data in the database.
|
||||
* @param key The key of the data.
|
||||
* @param value The value to set.
|
||||
*/
|
||||
set<T>(key: string, value: T) {
|
||||
this.realStore.SetAsync(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias Alastor Service
|
||||
*
|
||||
* Work in Progress
|
||||
*
|
||||
* A flamework service to manage the roblox DataStoreService.
|
||||
*/
|
||||
@Service()
|
||||
export class DataService {
|
||||
private cache = new Map<string, Store>();
|
||||
/**
|
||||
* Get a store with his name.
|
||||
* @param name The name of the store.
|
||||
* @returns A Store class linked the given name.
|
||||
*/
|
||||
getStore(name: string): Store {
|
||||
const store = this.cache.get(name);
|
||||
if (store) return store;
|
||||
const newStore = new Store(this, name, DataStoreService.GetDataStore(name));
|
||||
this.cache.set(name, newStore);
|
||||
return newStore;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { OnPlayerJoined, OnPlayerQuit } from "@common/shared/modding/players";
|
||||
import { Modding, OnStart } from "@flamework/core";
|
||||
import { Players } from "@rbxts/services";
|
||||
|
||||
/**
|
||||
* This Service Manage some player event with modding.
|
||||
*/
|
||||
export class PlayerEventsService implements OnStart {
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
onStart() {
|
||||
const playerJoinListener = new Set<OnPlayerJoined>();
|
||||
const playerQuitListener = new Set<OnPlayerQuit>();
|
||||
|
||||
Modding.onListenerAdded<OnPlayerJoined>((object) => playerJoinListener.add(object));
|
||||
Modding.onListenerRemoved<OnPlayerJoined>((object) => playerJoinListener.delete(object));
|
||||
Modding.onListenerAdded<OnPlayerQuit>((object) => playerQuitListener.add(object));
|
||||
Modding.onListenerRemoved<OnPlayerQuit>((object) => playerQuitListener.delete(object));
|
||||
|
||||
Players.PlayerAdded.Connect((player) => {
|
||||
for (const listener of playerJoinListener) {
|
||||
task.spawn(() => listener.onPlayerJoined(player));
|
||||
}
|
||||
});
|
||||
|
||||
Players.PlayerRemoving.Connect((player) => {
|
||||
for (const listener of playerQuitListener) {
|
||||
task.spawn(() => listener.onPlayerQuit(player));
|
||||
}
|
||||
});
|
||||
|
||||
for (const player of Players.GetPlayers()) {
|
||||
for (const listener of playerJoinListener) {
|
||||
task.spawn(() => listener.onPlayerJoined(player));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { OnPlayerJoined } from "@common/shared/modding/players";
|
||||
import SettingsFile from "@common/shared/settings";
|
||||
import { SetSetting, SettingTypeChecker } from "@common/shared/typings/settings";
|
||||
import { OnStart, Service } from "@flamework/core";
|
||||
|
||||
import { Events, Functions } from "../network";
|
||||
import { DataService, Store } from "./data";
|
||||
|
||||
/**
|
||||
* The Setting Manager service
|
||||
*/
|
||||
@Service()
|
||||
export class SettingsService implements OnPlayerJoined, OnStart {
|
||||
cache = new Map<number, SetSetting>();
|
||||
store: Store;
|
||||
/**
|
||||
* @param dataService The instance of the DataService.
|
||||
*/
|
||||
constructor(private dataService: DataService) {
|
||||
this.store = this.dataService.getStore("UserSettings");
|
||||
}
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
onPlayerJoined(player: Player): void {
|
||||
const store: SetSetting = this.store.get(tostring(player.UserId)) ?? new Map();
|
||||
this.cache.set(player.UserId, store);
|
||||
}
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
onStart(): void {
|
||||
Functions.settings.GetVersion.setCallback(() => SettingsFile.version);
|
||||
|
||||
Functions.settings.GetSettings.setCallback((player) => {
|
||||
return this.cache.get(player.UserId) ?? this.store.get<SetSetting>(tostring(player.UserId)) ?? new Map();
|
||||
});
|
||||
|
||||
Events.settings.SetSettings.connect((player, section, settings) => {
|
||||
settings.forEach((value, key) => {
|
||||
const setting = SettingsFile[section]?.find((sett) => sett.id === key);
|
||||
if (setting === undefined) return;
|
||||
if (typeIs(value, SettingTypeChecker[setting.type])) {
|
||||
// TODO Better checker
|
||||
this.cache.get(player.UserId)?.get(section)?.set(key, value);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user