-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmud.cc
155 lines (132 loc) · 4.2 KB
/
mud.cc
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
#include <iostream>
#include <unordered_map>
#include <node.h>
#include <v8.h>
#include "src/player.h"
#include "src/room.h"
#include "src/commands.h"
#include "src/sockets.h"
#include "src/trie.h"
#include "src/scrypt.h"
#include "src/chess.h"
using namespace std;
using namespace v8;
static unordered_map<string, string> ActiveNames;
static unordered_map<string, string> UnconfirmedPasswords;
string processCommand(string sockuid, string str)
{
size_t cmdEnd = str.find(' ');
const unsigned char* cmdKey = (const unsigned char*)str.substr(0, cmdEnd).c_str();
CommandFunc cmdFunc = (CommandFunc)valueForKey(Commands(), cmdKey);
string res;
if (cmdFunc){
res = cmdFunc(sockuid, str.substr(cmdEnd+1));
} else {
res = string(1, (char)INGAME) + "Eh?\n";
}
return res;
}
string processConnection(string sockuid, int state, string msg)
{
string res;
// ActiveNames[sockuid] = msg;
// state = INGAME;
if (state == UNNAMED){
msg[0] = toupper(msg[0]);
ActiveNames[sockuid] = msg;
if (checkPlayerExists(msg)){
state = UNVERIFIED;
res = "Password:\n";
} else {
state = NAME_CHOSEN;
res = msg + "? Is that your name?\n";
}
} else if (state == UNVERIFIED){
string name = ActiveNames[sockuid];
if (verifyPlayer(name, msg)){
connectSocketToPlayer(sockuid, name);
state = INGAME;
res = "Very well.\n";
} else {
res = "Wrong. Try again:\n";
}
} else if (state == NAME_CHOSEN){
if (tolower(msg[0]) == 'y'){
state = NAME_CONFIRMED;
res = "Choose a password:\n";
} else if (tolower(msg[0]) == 'n'){
ActiveNames.erase(sockuid);
state = UNNAMED;
res = "Who are you then?\n";
} else {
res = "Answer yes or no. Is your name " + ActiveNames[sockuid] + "?\n";
}
} else if (state == NAME_CONFIRMED){
string passwordHash = HashPassword(msg);
UnconfirmedPasswords[sockuid] = passwordHash;
state = PASSWORD_CHOSEN;
res = "Confirm your password:\n";
} else if (state == PASSWORD_CHOSEN){
if (VerifyPassword(UnconfirmedPasswords[sockuid], msg)){
connectSocketToNewPlayer(sockuid, Player(ActiveNames[sockuid], UnconfirmedPasswords[sockuid]));
state = PASSWORD_CONFIRMED;
res = "Very well.\n";
} else {
state = NAME_CONFIRMED;
res = "Passwords didn't match. Choose a password:\n";
UnconfirmedPasswords.erase(sockuid);
}
} else if (state == PASSWORD_CONFIRMED){
// char creation?
state = INGAME;
}
res.insert(0, 1, (char)state);
return res;
}
Handle<Value> ProcessInput(const Arguments& args)
{
HandleScope scope;
if (args.Length() < 1) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsString()) {
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
return scope.Close(Undefined());
}
string str = string(*String::Utf8Value(args[0]->ToString()));
string sockuid = str.substr(0, 25);
int state = str.at(25);
string msg = str.substr(26);
string res;
if (state == INGAME){
res = processCommand(sockuid, msg);
} else {
res = processConnection(sockuid, state, msg);
}
return scope.Close(String::New(res.c_str()));
}
Handle<Value> Save(const Arguments& args)
{
savePlayers();
saveRooms();
return Null();
}
Handle<Value> RunBenchmarks(const Arguments& args)
{
return Null();
}
void Init(Handle<Object> exports, Handle<Object> module)
{
exports->Set(String::NewSymbol("process"), FunctionTemplate::New(ProcessInput)->GetFunction());
exports->Set(String::NewSymbol("setScrypt"), FunctionTemplate::New(SetScrypt)->GetFunction());
exports->Set(String::NewSymbol("setBroadcast"), FunctionTemplate::New(SetBroadcast)->GetFunction());
exports->Set(String::NewSymbol("setWrite"), FunctionTemplate::New(SetWrite)->GetFunction());
exports->Set(String::NewSymbol("save"), FunctionTemplate::New(Save)->GetFunction());
exports->Set(String::NewSymbol("setDisconnect"), FunctionTemplate::New(SetDisconnect)->GetFunction());
exports->Set(String::NewSymbol("removePlayerOnSocket"), FunctionTemplate::New(RemovePlayerOnSocket)->GetFunction());
exports->Set(String::NewSymbol("runBenchmarks"), FunctionTemplate::New(RunBenchmarks)->GetFunction());
loadRooms(); // must come before players
loadPlayers();
}
NODE_MODULE(mud, Init)