Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

86
LINES

< > BotCompany Repo | #1021931 // Gazelle Modules Manager

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1059K of libraries. Click here for Pure Java version (11623L/60K).

!7

concept GModule {
  S moduleID, moduleLibID;
  S userID; // e. g. discord user id
  S userName;
  S shorthand;
}

cmodule2 GModulesManager > DynCRUD<GModule> {
  // API
  
  S startModuleForUser(S moduleLibID, S userID, S userName, S shorthand, O... _) {
    assertNempty("Need user ID", userID);
    assertNempty("Need module shorthand", shorthand);
    Pair<GModule, Bool> p = uniqConcept2(+userID, +shorthand);
    if (!p.b) ret "Module already running";
    cset(p.a, +userName, +moduleLibID);
    print("Starting module: " + moduleLibID);
    S moduleID = dm_makeNewModule(moduleLibID);
    cset(p.a, +moduleID);
    
    O info = dm_getOpt(moduleID, 'gInfo);
    if (info != null) {
      setOpt(info, +userID);
      setOpt(info, +shorthand);
      dm_call(moduleID, 'change);
    }
    
    S answer = cast dm_callOpt(p.a.moduleID, 'initialAnswer, (O) _);
    if (nempty(answer)) ret answer;
    ret print("Module " + moduleLibID + " started for " + userName + " as " + quote(shorthand));
  }
  
  S deleteAllModulesForUser(S userID, O... _) {
    Cl<GModule> l = conceptsWhere GModule(+userID);
    if (empty(l)) ret boolPar silentIfEmpty(_) ? "" : "No modules loaded";
    for (GModule m : l)
      dm_deleteModule(m.moduleID);
    deleteConcepts(l);
    ret n2(l, "module") + " deleted";
  }
  
  // returns module ID
  S findModuleForUser(S userID, S shorthand) {
    if (empty(userID) || empty(shorthand)) null;
    GModule mod = conceptWhere GModule(+userID, +shorthand);
    ret mod == null ? null : mod.moduleID;
  }
  
  S deleteModuleForUser(S userID, S shorthand) {
    GModule mod = conceptWhere GModule(+userID, +shorthand);
    if (mod == null) ret "Module not found";
    dm_deleteModule(mod.moduleID);
    deleteConcept(mod);
    ret "Module deleted";
  }
  
  void removeModule(O mod) {
    deleteConcept(conceptWhere GModule(moduleID := dm_moduleID(mod)));
  }
  
  void removeModuleAndDelete(O mod) {
    removeModule(mod);
    doLater(5.0, r {
      dm_deleteModule(mod)
    });
  }
  
  LS modulesForUser(S userID) {
    ret collect moduleID(conceptsWhere GModule(+userID));
  }
  
  LS activeModulesForUser(S userID) {
    ret filter(modulesForUser(userID), mod -> moduleIsActive(mod));
  }
  
  bool moduleIsActive(S moduleID) {
    O info = dm_getOpt(moduleID, 'gInfo);
    ret info == null || !isFalse(getOpt(info, 'active));
  }
  
  Cl<GModule> modulesInfoForUser(S userID) {
    ret conceptsWhere GModule(+userID);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1021931
Snippet name: Gazelle Modules Manager
Eternal ID of this version: #1021931/33
Text MD5: 26fe262840352f399aaaa1e7ec1fb974
Transpilation MD5: e18d6b5d8c1a4d5983c2b92de9fc2e0e
Author: stefan
Category: javax / gazelle
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-01-26 06:24:35
Source code size: 2545 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 408 / 39586
Version history: 32 change(s)
Referenced in: #1021932 - dm_gazelle_modulesManager