-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetPlayer.cpp
112 lines (97 loc) · 2.37 KB
/
NetPlayer.cpp
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
#include "NetPlayer.h"
#include "Pick.h"
// VCL headers
#include <ScktComp.hpp>
#include <time.h>
/*TClientNetPlayer::TClientNetPlayer(string aName, bool aLog)
: TDraftPlayer(aName, aLog)
{
}
void TClientNetPlayer::Pick(TCardPile& Booster)
{
if (log != NULL)
{
fprintf(log, "\nBooster:\n");
for (unsigned int i=0; i<Booster.size(); i++)
{
fprintf(log, "%s\n", Booster[i]->Name.c_str());
}
}
int i = GetUserPick(Booster, Picked);
if (log != NULL)
{
fprintf(log, "Picked: %s\n", Booster[i]->Name.c_str());
}
TCardData* c = *(Booster.begin()+i);
Picked.push_back(c);
Booster.erase(Booster.begin()+i);
}*/
TServerNetPlayer::TServerNetPlayer(
string aName,
bool aLog,
Scktcomp::TCustomWinSocket *aSocket)
: TDraftPlayer(aName, aLog), Socket(aSocket), UsedByFactory(false)
{
}
TServerNetPlayer::~TServerNetPlayer()
{
if (Socket != NULL)
{
Socket->SendText("enddraft\n");
Socket->Close();
}
}
void TServerNetPlayer::SetSocket(Scktcomp::TCustomWinSocket* s)
{
Socket = s;
}
void TServerNetPlayer::OnReceiveText(AnsiString Text)
{
string Name = Text.c_str();
std::binder2nd<CompareName> b(CompareName(), Name);
TCardBrowser i = std::find_if(Booster.begin(), Booster.end(), b);
if (i != Booster.end())
{
PickIndex = i - Booster.begin();
}
else
{
PickIndex = 0; // ???
}
//StrToInt(Text);
PickReceived = true;
}
void TServerNetPlayer::Pick(TCardPile& aBooster)
{
Booster = aBooster;
extern bool BreakDraft;
if (BreakDraft) return;
reconnect:
if (Socket == NULL) // disconnected
{
time_t Stamp = time(NULL);
while (time(NULL) - Stamp < TIMEOUT)
{
Application->ProcessMessages();
if (BreakDraft) return;
if (Socket != NULL) break;
}
if (Socket == NULL) throw Exception("User not reconnected");
}
PickReceived = false;
Socket->SendText("booster\n");
for (unsigned int i=0; i<Booster.size(); i++)
{
Socket->SendText((Booster[i]->Name + "\n").c_str());
}
Socket->SendText("endbooster\n");
while (!PickReceived)
{
Application->ProcessMessages();
if (BreakDraft) return;
if (Socket == NULL) goto reconnect;
}
TCardData* c = *(aBooster.begin()+PickIndex);
Picked.push_back(c);
aBooster.erase(aBooster.begin()+PickIndex);
}