Skip to content

Commit

Permalink
Added Users for Easy switching between Players.
Browse files Browse the repository at this point in the history
  • Loading branch information
DrRetro2033 committed Oct 7, 2024
1 parent 312d69b commit 7a9ccdd
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 70 deletions.
91 changes: 46 additions & 45 deletions bin/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:io';
import 'dart:convert';
// import 'package:ansix/ansix.dart';
import "package:args/command_runner.dart";
import 'package:cli_spin/cli_spin.dart';
import 'file_pattern.dart';
Expand All @@ -21,29 +20,28 @@ Future<dynamic> main(List<String> arguments) async {
abbr: "a",
defaultsTo: Directory.current.path,
);
runner.addCommand(ConstellationCommands());
runner.addCommand(PatternCommands());
currentPath = runner.argParser.parse(arguments)["app-path"];
currentPath = Directory.current.path;
if (arguments.contains("--app-path") || arguments.contains("-a")) {
currentPath = arguments[
arguments.indexWhere((e) => e == "--app-path" || e == "-a") + 1];
}
if (!Constellation.checkForConstellation(currentPath)) {
runner.addCommand(CreateConstellationCommand());
} else {
runner.addCommand(ShowMapConstellationCommand());
runner.addCommand(CheckForDifferencesCommand());
runner.addCommand(ConstellationJumpToCommand());
runner.addCommand(ConstellationGrowCommand());
runner.addCommand(ConstellationDeleteCommand());
}
runner.addCommand(ReadPatternCommand());
runner.addCommand(WritePatternCommand());

if (arguments.isNotEmpty) {
return await runner.run(arguments);
}
}

class ConstellationCommands extends Command {
@override
String get name => "const";
@override
String get description => "Commands for Constellations.";

ConstellationCommands() {
addSubcommand(CreateConstellationCommand());
addSubcommand(ShowMapConstellationCommand());
addSubcommand(CheckForDifferencesCommand());
addSubcommand(ConstellationJumpToCommand());
addSubcommand(ConstellationBranchCommand());
}
}

class CreateConstellationCommand extends Command {
@override
String get description =>
Expand All @@ -53,49 +51,53 @@ class CreateConstellationCommand extends Command {
String get name => "create";

CreateConstellationCommand() {
argParser.addOption("path", abbr: "p", mandatory: true);
argParser.addOption("name", abbr: "n", mandatory: true);
argParser.addMultiOption("user", abbr: "u", defaultsTo: Iterable.empty());
}

@override
void run() {
Constellation(argResults?["path"], name: argResults?["name"]);
List<String>? users = argResults?["user"];
if (users?.isEmpty ?? true) {
Constellation(currentPath, name: argResults?["name"]);
} else {
Constellation(currentPath, name: argResults?["name"], users: users!);
}
}
}

class ShowMapConstellationCommand extends Command {
@override
String get description => "Shows the map of a constellation.";
String get description => "Get the map of a constellation.";

@override
String get name => "map";

ShowMapConstellationCommand() {
argParser.addOption("path", abbr: "p", mandatory: true);
}
ShowMapConstellationCommand();

@override
void run() {
Constellation(argResults?["path"]).starmap.showMap();
dynamic run() {
Constellation constellation = Constellation(currentPath);
constellation.starmap?.showMap();
return jsonEncode(constellation.starmap?.toJson());
}
}

class CheckForDifferencesCommand extends Command {
@override
String get description =>
"Checks for differences between a star and the current file.";
"Checks for differences between the current star and what is currently in the directory.";

@override
String get name => "check";

CheckForDifferencesCommand() {
argParser.addOption("path", abbr: "p", mandatory: true);
argParser.addOption("star", abbr: "s");
}

@override
Future<bool> run() async {
Constellation constellation = Constellation(argResults?["path"]);
Constellation constellation = Constellation(currentPath);
final spinner = CliSpin(
text:
"Checking for differences between current directory and provided star...")
Expand All @@ -112,49 +114,48 @@ class CheckForDifferencesCommand extends Command {

class ConstellationJumpToCommand extends Command {
@override
String get description =>
"Jumps to a star in the constellation. Must call extract if you want the star to be extracted.";
String get description => "Jumps to a different star in the constellation.";

@override
String get name => "jump";

ConstellationJumpToCommand() {
argParser.addOption("path", abbr: "p", mandatory: true);
argParser.addOption("star", abbr: "s", mandatory: true);
}

@override
void run() {
Constellation(argResults?["path"]).starmap[argResults?["star"]];
Constellation(currentPath).starmap?[argResults?["star"]];
}
}

class ConstellationBranchCommand extends Command {
class ConstellationGrowCommand extends Command {
@override
String get description =>
"Creates a new branch in the constellation from the current star.";
"Continues the from the current star to a new star in the constellation. Will branch if necessary.";
@override
String get name => "branch";
String get name => "grow";

ConstellationBranchCommand() {
argParser.addOption("path", abbr: "p", mandatory: true);
ConstellationGrowCommand() {
argParser.addOption("name", abbr: "n", mandatory: true);
}

@override
void run() {
Constellation(argResults?["path"]).branch(argResults?["name"]);
Constellation(currentPath).grow(argResults?["name"]);
}
}

class PatternCommands extends Command {
class ConstellationDeleteCommand extends Command {
@override
String get name => "pattern";
String get description => "Deletes the constellation. Be CAREFUL!";

@override
String get description => "Commands for patterns.";
String get name => "delete";

PatternCommands() {
addSubcommand(ReadPatternCommand());
@override
void run() {
Constellation(currentPath).delete();
}
}

Expand Down
46 changes: 31 additions & 15 deletions bin/version_control/constellation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ import '../extensions.dart';

import 'star.dart';
import 'dossier.dart';
import 'users.dart';

/// # `class` Constellation
/// ## Represents a constellation.
/// Is similar to how a normal Git repository works, with an initial commit acting as the root star.
/// However, the plan is for Arceus to be able to do more with stars in the future,
/// so just using Git as a backbone would limit the scope of this project.
class Constellation {
String? name; // The name of the constellation.
String path; // The path to the folder this constellation is in.

late Starmap starmap;
Starmap? starmap;
UserIndex? userIndex;

Directory get directory => Directory(
path); // Fetches a directory object from the path this constellation is in.
Expand All @@ -30,17 +29,20 @@ class Constellation {
Directory get constellationDirectory => Directory(
constellationPath); // Fetches a directory object from the path the constellation stores its data in.

Constellation(this.path, {this.name}) {
Constellation(this.path,
{this.name, Iterable<String> users = const ["host"]}) {
path = path.fixPath();
if (constellationDirectory.existsSync()) {
load();
if (starmap.currentStarHash == null) {
starmap.currentStarHash = starmap.rootHash;
if (starmap?.currentStarHash == null) {
starmap?.currentStarHash = starmap?.rootHash;
save();
}
return;
} else if (name != null) {
_createConstellationDirectory();
userIndex = UserIndex(constellationPath);
userIndex?.createUsers(users);
starmap = Starmap(this);
_createRootStar();
save();
Expand All @@ -60,18 +62,21 @@ class Constellation {
}

void _createRootStar() {
starmap.root = Star(this, name: "Initial Star");
starmap.currentStar = starmap.root;
starmap?.root =
Star(this, name: "Initial Star", user: userIndex?.getHostUser());
starmap?.currentStar = starmap?.root;
save();
}

String generateUniqueStarHash() {
while (true) {
for (int i = 0; i < 100; i++) {
String hash = generateUUID();
if (!(doesStarExist(hash))) {
return hash;
}
}
throw Exception(
"Unable to generate a unique star hash. Either you are extremely unlucky or there are zero unique hashes left.");
}

/// # `String` getStarPath(`String` hash)
Expand Down Expand Up @@ -102,20 +107,31 @@ class Constellation {
starmap = Starmap(this, map: json["map"]);
}

Map<String, dynamic> toJson() => {"name": name, "map": starmap.toJson()};
Map<String, dynamic> toJson() => {"name": name, "map": starmap?.toJson()};
// ============================================================================

String? branch(String name) {
return starmap.currentStar?.createChild(name);
// # `String?` grow(`String` name)
// ## Creates a new star with the given name and returns the hash of the new star at the current star.
String? grow(String name) {
return starmap?.currentStar?.createChild(name);
}

// List<String> listChildren(String hash) {}

void delete() {
constellationDirectory.deleteSync(recursive: true);
starmap = null;
}

bool checkForDifferences(String? hash) {
hash ??= starmap.currentStarHash;
hash ??= starmap?.currentStarHash;
Star star = Star(this, hash: hash);
return Dossier(star).checkForDifferences();
}

static bool checkForConstellation(String path) {
return Directory("$path/.constellation").existsSync();
}
}

/// # `class` Starmap
Expand Down Expand Up @@ -235,7 +251,7 @@ class Starmap {
/// ## Returns a tree view of the constellation.
Map<String, dynamic> getReadableTree(String curHash) {
Map<String, dynamic> list = {};
String displayName = "Star $curHash";
String displayName = curHash;
if (currentStarHash == curHash) {
displayName += "✨";
}
Expand Down
30 changes: 20 additions & 10 deletions bin/version_control/star.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:archive/archive_io.dart';
import 'dart:convert';
import 'constellation.dart';
import 'users.dart';
import 'dart:io';

/// # `class` Star
Expand All @@ -14,6 +15,8 @@ class Star {
String? hash; // The hash of the star.
DateTime? createdAt; // The time the star was created.
String? parentHash; // The hash of the parent star.
String? _userHash; // The hash of the user who this star belongs to.
User? get user => constellation.userIndex?.getUser(_userHash!);
Star? get parent {
if (parentHash == null) return null;
return Star(constellation, hash: parentHash);
Expand All @@ -28,12 +31,20 @@ class Star {

Archive get archive => getArchive();

Star(this.constellation, {this.name, this.hash}) {
constellation.starmap.initEntry(hash ?? "");
if (name != null && hash == null) {
Star(
this.constellation, {
this.name,
this.hash,
User? user,
}) {
constellation.starmap?.initEntry(hash ?? "");
if (name != null && user != null) {
_userHash = user.hash;
_create();
} else if (name == null && hash != null) {
} else if (hash != null) {
load();
} else {
throw Exception("Star must have either a name and a user, or a hash.");
}
}

Expand Down Expand Up @@ -66,8 +77,8 @@ class Star {
"Cannot create a child star when there are no differences. Please make changes and try again.");
}
Star star = Star(constellation, name: name);
constellation.starmap.addRelationship(this, star);
constellation.starmap.currentStar = star;
constellation.starmap?.addRelationship(this, star);
constellation.starmap?.currentStar = star;
constellation.save();
return star.hash!;
}
Expand Down Expand Up @@ -113,12 +124,11 @@ class Star {
void fromJson(Map<String, dynamic> json) {
name = json["name"];
createdAt = DateTime.tryParse(json["createdAt"]);
_userHash = json["user"];
}

/// # `Map<String, dynamic>` toJson()
/// ## Converts the `Star` object into a JSON object.
Map<String, dynamic> toJson() => {
"name": name,
"createdAt": createdAt.toString(),
};
Map<String, dynamic> toJson() =>
{"name": name, "createdAt": createdAt.toString(), "user": _userHash};
}
Loading

0 comments on commit 7a9ccdd

Please sign in to comment.