-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
179 lines (169 loc) · 7.01 KB
/
Program.cs
File metadata and controls
179 lines (169 loc) · 7.01 KB
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Computing_Giants
{
internal class Program
{
public static List<Entity> entities = new List<Entity>();
public static Random rand = new Random(DateTime.Now.Millisecond);
private static void Main(string[] args)
{
TcpListener listener = new TcpListener(IPAddress.Any, 74);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
Thread thread = new Thread(handleClient);
thread.Start(client);
string output = "";
Console.WriteLine(output);
}
}
public static string genID()
{
string chars = "ABCDEFGHIJKLMPQRSTUVWXYZabcdefghijklmpqrstuvwxyz1234567890";
string output = "";
for(int i = 0;i < 100;i++)
{
output += chars[rand.Next(0, chars.Length - 1)];
}
return output.ToString();
}
public static string stripEnding(string input)
{
string output;
if (input.ToCharArray()[input.Length - 1] == '\n')
{
output = input.Substring(0, input.Length - 2); ;
}
else
{
output = input;
}
return output;
}
public static void handleClient(object data)
{
TcpClient client = (TcpClient)data;
NetworkStream stream = client.GetStream();
string ID = genID();
string pubID = genID();
Entity thisEntity = new Entity(ID, "NOT_SPECIFIED", pubID);
lock(entities) entities.Add(thisEntity);
byte[] bufferTMP = Encoding.ASCII.GetBytes("ID:" + ID.ToString() + "\r\n");
stream.Write(bufferTMP, 0, bufferTMP.Length);
while (client.Connected)
{
byte[] buffer = new byte[1024];
Thread.Sleep(1);
if (stream.DataAvailable)
{
stream.Read(buffer, 0, 1024);
string input;
input = Encoding.ASCII.GetString(buffer);
input = input.Split(Convert.ToChar(0))[0];
input = stripEnding(input);
switch (input.Split(':')[0])
{
case "GodMode":
thisEntity.level = 6;
break;
case "getPlayers":
string playerData = "";
foreach (Entity entity in entities)
{
lock (entities) playerData = playerData + entity.username + "," + entity.level + "," + entity.publicKey + "\r\n";
}
buffer = Encoding.ASCII.GetBytes(playerData);
stream.Write(buffer, 0, playerData.Length);
break;
case "setUsername":
try
{
if (thisEntity.privateKey == input.Split(':')[1])
{
thisEntity.username = input.Split(':')[2];
break;
}
}
catch
{
}
break;
case "attackPlayer":
try
{
if (thisEntity.privateKey == input.Split(':')[1])
{
foreach(Entity enemy in entities)
{
if(enemy.publicKey == input.Split(':')[2] && enemy.level > thisEntity.level)
{
thisEntity.attackingKey = input.Split(':')[2];
}
}
}
}
catch
{
}
break;
case "guessKey":
foreach (Entity entity in entities)
{
try
{
if (entity.privateKey == input.Split(':')[1])
{
foreach(Entity enemy in entities)
{
if(enemy.secretKey == input.Split(':')[2])
{
enemy.level--;
thisEntity.level++;
enemy.secretKey = enemy.newKey();
}
}
}
}
catch
{
}
}
break;
case "getOpponentsMD5":
foreach (Entity entity in entities)
{
try
{
if (entity.privateKey == input.Split(':')[1])
{
foreach (Entity enemy in entities)
{
if(enemy.publicKey == entity.attackingKey)
{
string output;
output = Utils.GetMD5(enemy.secretKey);
buffer = Encoding.ASCII.GetBytes(output);
stream.Write(buffer, 0, output.Length);
}
}
break;
}
}
catch
{
Console.WriteLine("Error");
}
}
break;
}
}
}
}
}
}