Skip to content

Commit

Permalink
Random Names from Team (YARC-Official#257)
Browse files Browse the repository at this point in the history
* Random Names from Team

[EDITED]
- Added a function to read from Credits.txt and add a random name for the "Empty" Player Name
- Change the "BOT" text to blue color

[CHANGED]
- Changed Location of Credits.txt from '/Assets' to '/Assets/Resources'

* Update PlayerManager.cs
  • Loading branch information
nevespt authored May 7, 2023
1 parent 8b8231d commit 7bda997
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
File renamed without changes.
File renamed without changes.
64 changes: 62 additions & 2 deletions Assets/Script/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,72 @@
using YARG.Input;
using YARG.PlayMode;
using YARG.Settings;
using UnityEngine;
using UnityEngine.AddressableAssets;

namespace YARG {
public static class PlayerManager {

public struct LastScore {
public DiffPercent percentage;
public DiffScore score;
public int notesHit;
public int notesMissed;

}

public struct RandomName {
public string name;
public int size;
}

private static RandomName RandomNameFromFile() {
// load credits.txt
// read each line
// ignore lines starting with << or <u> or empty lines
// return random line

// load Assets/Credits.txt
var creditsPath = Addressables.LoadAssetAsync<TextAsset>("Credits");
creditsPath.WaitForCompletion();
// split credits into lines
var lines = creditsPath.Result.text.Split(new[] { "\r\n", "\r", "\n" }, System.StringSplitOptions.RemoveEmptyEntries);


//var lines = System.IO.File.ReadAllLines(creditsPath);
var names = new List<string>();
foreach (var line in lines) {
if (line.StartsWith("<<") || line.StartsWith("<u>") || line.Length == 0) {
continue;
}

// special conditions
if (line.Contains("EliteAsian (barely)")) {
continue;
}
if (line.Contains("EliteAsian")) {
names.Add("<b>E</b>lite<b>A</b>sian");
continue;
}
if (line.Contains("NevesPT")) {
names.Add("<b>N</b>eves<b>PT</b>");
continue;
}

names.Add(line);
}

return new RandomName() {
name = names[Random.Range(0, names.Count)],
size = names.Count
};
}

public class Player {
private static int nextPlayerName = 1;

public string name;
public string DisplayName => name + (inputStrategy.botMode ? " (BOT)" : "");
public string DisplayName => name + (inputStrategy.botMode ? " <color=#00DBFD>BOT</color>" : "");

public InputStrategy inputStrategy;

Expand All @@ -34,7 +85,16 @@ public class Player {
public AbstractTrack track = null;

public Player() {
name = $"New Player {nextPlayerName++}";
int counter = 0;
// do not use the same name twice, if no available names, use "New Player"
do {
RandomName randomName = RandomNameFromFile();
name = randomName.name;
if (counter++ > randomName.size || name == null) {
name = $"New Player {nextPlayerName++}";
break;
}
} while (players.Any(i => i.name == name));
}
}

Expand Down

0 comments on commit 7bda997

Please sign in to comment.