-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipnick.cs
140 lines (125 loc) · 4.38 KB
/
ipnick.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using MCGalaxy;
using MCGalaxy.Commands;
using MCGalaxy.DB;
using MCGalaxy.Events.PlayerEvents;
using System.Collections.Generic;
namespace Core {
public sealed class IpNickPlugin : Plugin {
public override string name { get { return "ipnick"; } }
public override string creator { get { return "Goodly"; } }
public override string welcome { get { return ""; } }
public override string MCGalaxy_Version { get { return "1.9.3.7"; } }
static OnlineStatPrinter onlineLine;
static OfflineStatPrinter offlineLine;
public override void Load(bool startup) {
onlineLine = (p, who) => DisplayOnlineIpNick(p, who);
offlineLine = (p, who) => DisplayOfflineIpNick(p, who);
OnlineStat.Stats.Add(onlineLine);
OfflineStat.Stats.Add(offlineLine);
OnPlayerFinishConnectingEvent.Register(OnPlayerFinishConnecting, Priority.Low);
}
public override void Unload(bool shutdown) {
OnlineStat.Stats.Remove(onlineLine);
OfflineStat.Stats.Remove(offlineLine);
OnPlayerFinishConnectingEvent.Unregister(OnPlayerFinishConnecting);
}
static void OnPlayerFinishConnecting(Player p) {
Chat.MessageGlobal(p.ColoredName+" logged in from the ip nickname \""+GetIpNick(p.ip)+"\"");
}
static string GetIpNick(string ip) {
Random random = new Random(ip.GetHashCode());
NameGenerator nameGen = new NameGenerator(random);
return nameGen.MakeName();
}
static void DisplayOnlineIpNick(Player p, Player who) {
p.Message(" The IP nickname of \"{0}\"", GetIpNick(who.ip));
}
static void DisplayOfflineIpNick(Player p, PlayerData who) {
p.Message(" Last logged in from the IP nickname \"{0}\"", GetIpNick(who.IP));
}
}
/// <summary>
/// Generates random, made-up names. The names appear to be language neutral (sort of).
/// </summary>
/// <remarks>This is a port of the orginal Javascript written by John Ahlschwede, [email protected]</remarks>
public sealed class NameGenerator
{
private int[] numSyllables = new int[]{1,2,3,4,5};
private int[] numSyllablesChance = new int[]{150,500,80,10,1};
private int[] numConsonants = new int[]{0,1,2,3,4};
private int[] numConsonantsChance = new int[]{80,350,25,5,1};
private int[] numVowels = new int[]{1,2,3};
private int[] numVowelsChance = new int[]{180,25,1};
private char[] vowel = new char[]{'a','e','i','o','u','y'};
private int[] vowelChance = new int[]{10,12,10,10,8,2};
private char[] consonant = new char[]{'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'};
private int[] consonantChance = new int[]{10,10,10,10,10,10,10,10,12,12,12,10,5,12,12,12,8,8,3,4,3};
private Random random;
/// <summary>
/// Create an instance.
/// </summary>
public NameGenerator(Random random)
{
this.random = random;
}
private int IndexSelect( int[] intArray )
{
int totalPossible = 0;
for(int i=0; i < intArray.Length; i++)
{
totalPossible = totalPossible + intArray[i];
}
int chosen = random.Next( totalPossible );
int chancesSoFar = 0;
for(int j=0; j < intArray.Length; j++)
{
chancesSoFar = chancesSoFar + intArray[j];
if( chancesSoFar > chosen )
{
return j;
}
}
return 0;
}
private string MakeSyllable()
{
return MakeConsonantBlock() + MakeVowelBlock() + MakeConsonantBlock();
}
private string MakeConsonantBlock()
{
string newName = "";
int numberConsonants = numConsonants[ IndexSelect(numConsonantsChance)];
for(int i=0; i<numberConsonants; i++)
{
newName += consonant[IndexSelect(consonantChance)];
}
return newName;
}
private string MakeVowelBlock()
{
string newName = "";
int numberVowels = numVowels[ IndexSelect(numVowelsChance) ];
for(int i=0;i<numberVowels;i++)
{
newName += vowel[ IndexSelect(vowelChance) ];
}
return newName;
}
/// <summary>
/// Generates a name randomly using certain construction rules. The name
/// will be different each time it is called.
/// </summary>
/// <returns>A name string.</returns>
public string MakeName()
{
int numberSyllables = numSyllables[ IndexSelect( numSyllablesChance ) ];
string newName = "";
for(int i=0; i < numberSyllables; i++)
{
newName = newName + MakeSyllable();
}
return char.ToUpper(newName[0] ) + newName.Substring(1);
}
}
}