1 |
douglas |
136 |
// Renegade Map Selector |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// MapSelector.cpp |
6 |
|
|
|
7 |
|
|
#include "MapSelector.h" |
8 |
douglas |
137 |
#include <unistd.h> |
9 |
|
|
#include <sys/types.h> |
10 |
|
|
#include <sys/stat.h> |
11 |
|
|
#include <sys/param.h> |
12 |
|
|
#include <dirent.h> |
13 |
douglas |
144 |
#include <fcntl.h> |
14 |
douglas |
136 |
|
15 |
douglas |
137 |
MapSelector::MapSelector() |
16 |
|
|
{ |
17 |
|
|
session = 0; |
18 |
|
|
|
19 |
|
|
vector<FormEntry> result; |
20 |
|
|
if (cgi.getElement("session", result)) |
21 |
|
|
{ |
22 |
|
|
session = result[result.size() - 1].getIntegerValue(); |
23 |
|
|
result.clear(); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
session = load(session); |
27 |
|
|
if (debug) |
28 |
|
|
{ |
29 |
|
|
cerr << "session = " << session << "\navailable = {\n"; |
30 |
|
|
|
31 |
|
|
for (unsigned index = 0; index < available.size(); index++) |
32 |
|
|
{ |
33 |
|
|
cerr << " [" << index << "] = " << available[index] << "\n"; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
cerr << "}\nselected = {\n"; |
37 |
|
|
|
38 |
|
|
for (unsigned index = 0; index < selected.size(); index++) |
39 |
|
|
{ |
40 |
|
|
cerr << " [" << index << "] = " << selected[index] << "\n"; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
cerr << "}\n"; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
string button; |
47 |
|
|
if (cgi.getElement("button", result)) |
48 |
|
|
{ |
49 |
|
|
button = result[result.size() - 1].getValue(); |
50 |
|
|
result.clear(); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
select(button); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
long MapSelector::load(long session) |
57 |
|
|
{ |
58 |
|
|
if (session == 0) |
59 |
|
|
{ |
60 |
|
|
ifstream fin("mapselector.cfg"); |
61 |
|
|
|
62 |
|
|
while (fin.good()) |
63 |
|
|
{ |
64 |
|
|
string name; |
65 |
|
|
string value; |
66 |
|
|
|
67 |
|
|
getline(fin, name, '='); |
68 |
|
|
getline(fin, value); |
69 |
|
|
|
70 |
|
|
if (name == "server") |
71 |
|
|
{ |
72 |
|
|
server = value; |
73 |
|
|
} |
74 |
|
|
else if (name == "config") |
75 |
|
|
{ |
76 |
|
|
ini = value; |
77 |
|
|
} |
78 |
|
|
else if (name == "template") |
79 |
|
|
{ |
80 |
|
|
html = value; |
81 |
|
|
} |
82 |
|
|
else if (name == "redirect") |
83 |
|
|
{ |
84 |
|
|
redirect = value; |
85 |
|
|
} |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
fin.close(); |
89 |
|
|
|
90 |
douglas |
144 |
long seed; |
91 |
|
|
|
92 |
|
|
int devrandom = open("/dev/urandom", O_RDONLY); |
93 |
|
|
read(devrandom, &seed, sizeof (seed)); |
94 |
|
|
close(devrandom); |
95 |
|
|
|
96 |
|
|
if (debug) cerr << "seed = " << seed << "\n"; |
97 |
|
|
|
98 |
|
|
srandom(seed); |
99 |
douglas |
141 |
// srandomdev(); |
100 |
douglas |
137 |
session = random(); |
101 |
|
|
|
102 |
|
|
DIR* dir = opendir((server + "/data").c_str()); |
103 |
|
|
|
104 |
|
|
set<string> files; |
105 |
|
|
dirent* file; |
106 |
|
|
while ((file = readdir(dir)) != NULL) |
107 |
|
|
{ |
108 |
|
|
string name = file->d_name; |
109 |
|
|
|
110 |
|
|
if (name.rfind(".mix") == name.length() - 4) |
111 |
|
|
{ |
112 |
|
|
files.insert(name); |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
closedir(dir); |
117 |
|
|
|
118 |
|
|
for (set<string>::iterator itor = files.begin(); itor != files.end(); |
119 |
|
|
itor++) |
120 |
|
|
{ |
121 |
|
|
available.push_back(*itor); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
char* dot = new char[MAXPATHLEN]; |
125 |
|
|
getcwd(dot, MAXPATHLEN); |
126 |
|
|
|
127 |
|
|
chdir(server.c_str()); |
128 |
|
|
|
129 |
|
|
config = new RenegadeConfig(ini); |
130 |
|
|
config->load(); |
131 |
|
|
|
132 |
|
|
selected = config->getMaps(); |
133 |
|
|
|
134 |
|
|
chdir(dot); |
135 |
|
|
delete [] dot; |
136 |
|
|
} |
137 |
|
|
else |
138 |
|
|
{ |
139 |
|
|
// |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
return session; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
void MapSelector::select(const string& button) |
146 |
|
|
{ |
147 |
|
|
// |
148 |
|
|
} |