edge-ruedelasoif/edge/server/main.js

153 lines
4.1 KiB
JavaScript
Raw Normal View History

2024-02-23 20:48:46 +01:00
function initConfig(ctx,config) {
var obj = store.get(ctx, "Config",config._id);
if(obj) config.value=obj.value;
var obj = store.upsert(ctx, "Config", config);
return obj;
}
// Fixures
function setFixures() {
var ctx = context.new();
// Migration
var obj = store.get(ctx, "Config", "version");
console.log("Migration");
if(obj) {
console.log(obj);
}
// Config
console.log("Fixures Config");
var obj = initConfig(ctx, {"_id": "version", "default": "0.0.0","visible":false});
}
// La fonction "onInit()" (si déclarée) est automatiquement
// exécutée au démarrage du serveur de votre application.
function onInit() {
setFixures();
rpc.register("getAuthrole");
rpc.register("getAuthid");
rpc.register("getAuthpseudo");
rpc.register("getAuthisuser"),
rpc.register("getConfigs");
rpc.register("getConfig");
rpc.register("upsertConfig");
rpc.register("getPartys");
rpc.register("getPartyswait");
rpc.register("getParty");
rpc.register("upsertParty");
rpc.register("deleteParty");
rpc.register("getPartyplayers");
rpc.register("getPlayer");
rpc.register("initPlayer");
rpc.register("upsertPlayer");
}
// Repository Auth
async function getAuthrole(ctx,id) {
var obj = await auth.getClaim(ctx, auth.CLAIM_ROLE);
return obj;
}
async function getAuthid(ctx,id) {
var obj = await auth.getClaim(ctx, auth.CLAIM_SUBJECT);
if(!obj) {
obj = await context.get(ctx, context.SESSION_ID);
}
return obj;
}
async function getAuthpseudo(ctx,id) {
var obj = await auth.getClaim(ctx, auth.CLAIM_PREFERRED_USERNAME);
if(!obj) {
obj = await context.get(ctx, context.SESSION_ID);
}
return obj;
}
async function getAuthisuser(ctx,id) {
var obj = await auth.getClaim(ctx, auth.CLAIM_ISSUER);
if(!obj) {
obj = false;
}
return obj;
}
// Repository Config
async function getConfigs(ctx) {
var obj = await store.query(ctx, "Config",{"eq":{ "visible": true }},);
return obj;
}
async function getConfig(ctx,id) {
var obj = await store.get(ctx, "Config",id);
if(obj&&!obj.value) obj.value=obj.default;
return obj;
}
async function upsertConfig(ctx,config) {
var obj = await store.upsert(ctx, "Config", config);
return obj;
}
// Repository Party
async function getPartys(ctx) {
var obj = await store.query(ctx, "Party");
return obj;
}
async function getPartyswait(ctx) {
var obj = await store.query(ctx, "Party",{"eq":{ "status": "playerwait" }},);
return obj;
}
async function getParty(ctx,id) {
var obj = await store.get(ctx, "Party",id);
if(obj&&!obj.value) obj.value=obj.default;
return obj;
}
async function upsertParty(ctx,party) {
var obj = await store.upsert(ctx, "Party", party);
return obj;
}
async function deleteParty(ctx,id) {
// Supprimer le media des Partyplayer
var obj = await store.query(ctx, "Player",
{
"eq":{ "partyid": id },
});
obj.forEach(async(player) => {
player.partyid=null;
var obj = await store.upsert(ctx, "Player", player);
});
// Supprimer le media
var obj = await store.delete(ctx, "Party", id);
return obj;
}
async function getPartyplayers(ctx,id) {
var obj = await store.query(ctx, "Player",{"eq":{ "partyid": id }},);
return obj;
}
// Repository Player
async function getPlayer(ctx,id) {
var obj = await store.get(ctx, "Player",id);
if(obj&&!obj.value) obj.value=obj.default;
return obj;
}
async function initPlayer(ctx,player) {
if(!player.isuser) player.name=player.name.replace("Anon ","");
var obj = await store.get(ctx, "Player",player._id);
if(!obj) obj = await store.upsert(ctx, "Player", player);
return obj;
}
async function upsertPlayer(ctx,player) {
var obj = await store.upsert(ctx, "Player", player);
return obj;
}
// La fonction "onClientMessage(ctx, message)" est automatiquement
// exécutée quand le serveur de votre application reçoit un
// message en provenance du client.
async function onClientMessage(ctx, message) {
await net.broadcast(message);
}