/
KokoroBotRPG.cs
220 lines (211 loc) · 7.9 KB
/
KokoroBotRPG.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
using Discord;
namespace KokoroBot
{
class KokoroBotRPG
{
Dictionary<long, KRPG_player> players;
Random rng;
const int CURRENT_VERSION=1;
HashAlgorithm hshfnc = HashAlgorithm.Create("SHA");
public KokoroBotRPG()
{
players = new Dictionary<long, KRPG_player>();
rng = new Random((int)(DateTime.Now.ToFileTimeUtc() % int.MaxValue));
}
public void Load()
{
try {
if (File.Exists("kokororpg"))
{
BinaryReader br = new BinaryReader(File.OpenRead("kokororpg"));
int count = br.ReadInt32();
int vers = br.ReadInt32();
if (vers == 1)
{
for (int i = 0; i < count; i++)
{
long id = br.ReadInt64();
float xp = br.ReadSingle();
int level = br.ReadInt32();
float hp = br.ReadSingle();
var plr = new KRPG_player(id);
plr.xp = xp;
plr.level = level;
plr.hp = hp;
players.Add(id, plr);
}
}
br.Close();
}
}
catch(IOException)
{
Console.WriteLine("Did someone say IOException?");
}
}
public void Save()
{
try {
BinaryWriter bw = new BinaryWriter(File.Open("kokororpg", FileMode.OpenOrCreate));
bw.Write(players.Count);
bw.Write(CURRENT_VERSION);
var player_col = players.ToArray();
for (int i = 0; i < player_col.Length; i++)
{
bw.Write(player_col[i].Value.ID);
bw.Write(player_col[i].Value.xp);
bw.Write(player_col[i].Value.level);
bw.Write(player_col[i].Value.hp);
}
}
catch (Exception)
{
Console.WriteLine("Did someone say IOException?");
}
}
public async Task HandleCommands(MessageEventArgs e, DiscordClient client, Channel currentChannel)
{
try {
string log = "";
string clean_message = e.Message.Text.Substring(1);
if (!players.ContainsKey(e.User.Id))
{
players.Add(e.User.Id, new KRPG_player(e.User.Id));
}
KRPG_player player = players[e.User.Id];
if (clean_message.Contains("@"))
{
string command = clean_message.Split('@')[0];
command = command.Trim(' ');
string target = clean_message.Split('@')[1];
target = target.Trim(' ');
var validUsers = e.Server.Members.Where((User u) =>
{
return u.Name == target;
});
var target_usr = validUsers.First();
if (target_usr != null)
{
if(target_usr.Status == UserStatus.Offline)
{
await client.SendMessage(currentChannel, "But nobody came.." );
return;
}
if (!players.ContainsKey(target_usr.Id))
{
players.Add(target_usr.Id, new KRPG_player(target_usr.Id));
}
KRPG_player target_plr;
if (target_usr.Id == player.ID)
{
target_plr = player;
}
else
{
target_plr = players[target_usr.Id];
}
if(command == "level")
{
log += target + " is level "+target_plr.level+" | "+target_plr.xp+'/'+ (target_plr.level * target_plr.level) + '\n';
await client.SendMessage(currentChannel, log);
return;
}
var bytes = Encoding.UTF8.GetBytes(target_usr + command);
byte hash_b = hshfnc.ComputeHash(bytes)[0];
float variation = 0.4f + 0.01f * rng.Next(60);
float hash = hash_b / 2;
float scale = 0.01f * (hash - 28);
float damagescale = 100.0f - (100.0f * (1.0f / ((player.level + 1))));
float absolutedamage = (damagescale * variation) * scale;
//await client.SendMessage(currentChannel, e.User.Name + ' ' + command + "s " + target + '.');
if (absolutedamage > 0)
{
log += "It deals " + absolutedamage.ToString() + " damage." + '\n';
}
else
{
log += "It heals for " + absolutedamage.ToString() + " damage." + '\n';
}
target_plr.hp -= absolutedamage;
if (target_plr.hp <= 0)
{
target_plr.hp = float.MaxValue;
if (target_plr != player)
{
float xpgain = target_plr.level * 2;
player.xp += xpgain;
log += target + " has been killed and resurrected. +" + xpgain + "XP" + '\n';
}
else
{
log += target + " has killed themselves and resurrected. +1 suicide attempt" + '\n';
}
}
else
{
log += target + " has " + target_plr.hp + " left." + '\n';
}
}
}
else
{
// TODO: Item logic etc.
}
if (player.xp > player.level * player.level)
{
player.xp = player.xp - (player.level * player.level);
player.level++;
log += e.User.Name + " has leveled up to Level " + player.level + '.' + '\n';
}
if (log != "")
{
await client.SendMessage(currentChannel, log);
}
}
catch(Exception)
{
Console.WriteLine("welp.");
}
}
}
class KRPG_player
{
public long ID;
public float xp;
public int level;
public float hp
{
get
{
return _hp;
}
set
{
if(value > 10.0 * Math.Pow(1.4, level))
{
_hp = (float)(10.0 * Math.Pow(1.4, level));
}
else
{
_hp = value;
}
}
}
float _hp = 0;
public KRPG_player(long ID)
{
this.ID = ID;
xp = 0;
level = 1;
hp = level * 10;
}
}
}