Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Donald Haase
Cockatrice
Commits
792a400a
Commit
792a400a
authored
Apr 08, 2009
by
Max-Wilhelm Bruker
Browse files
minor improvements
parent
a429a4a0
Changes
28
Hide whitespace changes
Inline
Side-by-side
cockatrice/src/carditem.cpp
View file @
792a400a
...
...
@@ -148,7 +148,7 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if
((
event
->
screenPos
()
-
event
->
buttonDownScreenPos
(
Qt
::
LeftButton
)).
manhattanLength
()
<
QApplication
::
startDragDistance
())
return
;
bool
faceDown
=
event
->
modifiers
().
testFlag
(
Qt
::
ShiftModifier
);
bool
faceDown
=
event
->
modifiers
().
testFlag
(
Qt
::
ShiftModifier
)
||
facedown
;
createDragItem
((
CardZone
*
)
parentItem
(),
id
,
event
->
pos
(),
event
->
scenePos
(),
faceDown
);
dragItem
->
grabMouse
();
...
...
cockatrice/src/client.cpp
View file @
792a400a
...
...
@@ -155,7 +155,7 @@ void Client::readLine()
else
if
(
!
prefix
.
compare
(
"welcome"
))
{
emit
welcomeMsgReceived
(
welcomemsg
);
setStatus
(
StatusConnected
);
setName
(
PlayerName
);
login
(
PlayerName
,
password
);
}
msgbuf
.
clear
();
}
else
...
...
@@ -189,9 +189,10 @@ int Client::cmd(const QString &s)
return
MsgId
;
}
void
Client
::
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
playername
)
void
Client
::
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
playername
,
const
QString
&
_password
)
{
PlayerName
=
playername
;
password
=
_password
;
socket
->
connectToHost
(
hostname
,
port
);
setStatus
(
StatusConnecting
);
}
...
...
@@ -229,9 +230,9 @@ int Client::leaveGame()
return
cmd
(
"leave_game"
);
}
int
Client
::
setN
ame
(
const
QString
&
name
)
int
Client
::
login
(
const
QString
&
n
ame
,
const
QString
&
pass
)
{
return
cmd
(
QString
(
"
set_name|%1
"
).
arg
(
name
));
return
cmd
(
QString
(
"
login|%1|%2
"
).
arg
(
name
)
.
arg
(
pass
)
);
}
int
Client
::
say
(
const
QString
&
s
)
...
...
cockatrice/src/client.h
View file @
792a400a
...
...
@@ -48,6 +48,7 @@ private:
ProtocolStatus
status
;
QList
<
QStringList
>
msgbuf
;
QString
PlayerName
;
QString
password
;
unsigned
int
MsgId
;
void
msg
(
const
QString
&
s
);
int
cmd
(
const
QString
&
s
);
...
...
@@ -58,14 +59,14 @@ public:
ProtocolStatus
getStatus
()
{
return
status
;
}
QString
peerName
()
const
{
return
socket
->
peerName
();
}
void
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
playername
);
void
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
playername
,
const
QString
&
password
);
void
disconnectFromServer
();
int
listGames
();
int
listPlayers
();
int
createGame
(
const
QString
&
name
,
const
QString
&
description
,
const
QString
&
password
,
unsigned
int
maxPlayers
);
int
joinGame
(
const
QString
&
name
,
const
QString
&
password
);
int
leaveGame
();
int
setN
ame
(
const
QString
&
name
);
int
login
(
const
QString
&
n
ame
,
const
QString
&
pass
);
int
say
(
const
QString
&
s
);
int
shuffle
();
int
rollDice
(
unsigned
int
sides
);
...
...
cockatrice/src/dlg_connect.cpp
View file @
792a400a
...
...
@@ -16,6 +16,11 @@ DlgConnect::DlgConnect(QWidget *parent)
playernameEdit
=
new
QLineEdit
(
"Player"
);
playernameLabel
->
setBuddy
(
playernameEdit
);
passwordLabel
=
new
QLabel
(
tr
(
"P&assword:"
));
passwordEdit
=
new
QLineEdit
;
passwordLabel
->
setBuddy
(
passwordEdit
);
passwordEdit
->
setEchoMode
(
QLineEdit
::
Password
);
okButton
=
new
QPushButton
(
tr
(
"&OK"
));
okButton
->
setDefault
(
true
);
cancelButton
=
new
QPushButton
(
tr
(
"&Cancel"
));
...
...
@@ -27,6 +32,8 @@ DlgConnect::DlgConnect(QWidget *parent)
grid
->
addWidget
(
portEdit
,
1
,
1
);
grid
->
addWidget
(
playernameLabel
,
2
,
0
);
grid
->
addWidget
(
playernameEdit
,
2
,
1
);
grid
->
addWidget
(
passwordLabel
,
3
,
0
);
grid
->
addWidget
(
passwordEdit
,
3
,
1
);
QHBoxLayout
*
buttonLayout
=
new
QHBoxLayout
;
buttonLayout
->
addStretch
();
...
...
@@ -59,3 +66,8 @@ QString DlgConnect::getPlayerName()
{
return
playernameEdit
->
text
();
}
QString
DlgConnect
::
getPassword
()
{
return
passwordEdit
->
text
();
}
cockatrice/src/dlg_connect.h
View file @
792a400a
...
...
@@ -14,9 +14,10 @@ public:
QString
getHost
();
int
getPort
();
QString
getPlayerName
();
QString
getPassword
();
private:
QLabel
*
hostLabel
,
*
portLabel
,
*
playernameLabel
;
QLineEdit
*
hostEdit
,
*
portEdit
,
*
playernameEdit
;
QLabel
*
hostLabel
,
*
portLabel
,
*
playernameLabel
,
*
passwordLabel
;
QLineEdit
*
hostEdit
,
*
portEdit
,
*
playernameEdit
,
*
passwordEdit
;
QPushButton
*
okButton
,
*
cancelButton
;
};
...
...
cockatrice/src/game.cpp
View file @
792a400a
...
...
@@ -68,6 +68,8 @@ Game::Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_a
connect
(
aUntap
,
SIGNAL
(
triggered
()),
this
,
SLOT
(
actUntap
()));
aDoesntUntap
=
new
QAction
(
tr
(
"Toggle &normal untapping"
),
this
);
connect
(
aDoesntUntap
,
SIGNAL
(
triggered
()),
this
,
SLOT
(
actDoesntUntap
()));
aFlip
=
new
QAction
(
tr
(
"&Flip"
),
this
);
connect
(
aFlip
,
SIGNAL
(
triggered
()),
this
,
SLOT
(
actFlip
()));
aAddCounter
=
new
QAction
(
tr
(
"&Add counter"
),
this
);
connect
(
aAddCounter
,
SIGNAL
(
triggered
()),
this
,
SLOT
(
actAddCounter
()));
aRemoveCounter
=
new
QAction
(
tr
(
"&Remove counter"
),
this
);
...
...
@@ -81,6 +83,8 @@ Game::Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_a
cardMenu
->
addAction
(
aUntap
);
cardMenu
->
addAction
(
aDoesntUntap
);
cardMenu
->
addSeparator
();
cardMenu
->
addAction
(
aFlip
);
cardMenu
->
addSeparator
();
cardMenu
->
addAction
(
aAddCounter
);
cardMenu
->
addAction
(
aRemoveCounter
);
cardMenu
->
addAction
(
aSetCounters
);
...
...
@@ -321,6 +325,16 @@ void Game::actDoesntUntap()
}
}
void
Game
::
actFlip
()
{
QListIterator
<
QGraphicsItem
*>
i
(
scene
->
selectedItems
());
while
(
i
.
hasNext
())
{
CardItem
*
temp
=
(
CardItem
*
)
i
.
next
();
QString
zone
=
qgraphicsitem_cast
<
CardZone
*>
(
temp
->
parentItem
())
->
getName
();
client
->
moveCard
(
temp
->
getId
(),
zone
,
zone
,
temp
->
pos
().
x
(),
temp
->
pos
().
y
(),
!
temp
->
getFaceDown
());
}
}
void
Game
::
actAddCounter
()
{
QListIterator
<
QGraphicsItem
*>
i
(
scene
->
selectedItems
());
...
...
cockatrice/src/game.h
View file @
792a400a
...
...
@@ -15,7 +15,7 @@ class Game : public QObject {
Q_OBJECT
private:
QMenu
*
actionsMenu
,
*
cardMenu
;
QAction
*
aTap
,
*
aUntap
,
*
aDoesntUntap
,
*
aAddCounter
,
*
aRemoveCounter
,
*
aSetCounters
,
*
aRearrange
,
QAction
*
aTap
,
*
aUntap
,
*
aDoesntUntap
,
*
aFlip
,
*
aAddCounter
,
*
aRemoveCounter
,
*
aSetCounters
,
*
aRearrange
,
*
aUntapAll
,
*
aDecLife
,
*
aIncLife
,
*
aSetLife
,
*
aShuffle
,
*
aDraw
,
*
aDrawCards
,
*
aRollDice
,
*
aCreateToken
;
DlgStartGame
*
dlgStartGame
;
...
...
@@ -41,6 +41,7 @@ private slots:
void
actTap
();
void
actUntap
();
void
actDoesntUntap
();
void
actFlip
();
void
actAddCounter
();
void
actRemoveCounter
();
void
actSetCounters
();
...
...
cockatrice/src/window_main.cpp
View file @
792a400a
...
...
@@ -82,7 +82,7 @@ void MainWindow::actConnect()
{
DlgConnect
dlg
(
this
);
if
(
dlg
.
exec
())
client
->
connectToServer
(
dlg
.
getHost
(),
dlg
.
getPort
(),
dlg
.
getPlayerName
());
client
->
connectToServer
(
dlg
.
getHost
(),
dlg
.
getPort
(),
dlg
.
getPlayerName
()
,
dlg
.
getPassword
()
);
}
void
MainWindow
::
actDisconnect
()
...
...
servatrice/serv
ertest
.pro
→
servatrice/serv
atrice
.pro
View file @
792a400a
...
...
@@ -8,25 +8,25 @@ DEPENDPATH += . src
INCLUDEPATH
+=
.
src
CONFIG
+=
qt
thread
QT
+=
network
QT
+=
network
sql
QT
-=
gui
#
Input
HEADERS
+=
src
/
test
server
.
h
src
/
test
servergame
.
h
src
/
test
serversocket
.
h
\
HEADERS
+=
src
/
server
.
h
src
/
servergame
.
h
src
/
serversocket
.
h
\
src
/
playerzone
.
h
\
src
/
test
card
.
h
\
src
/
card
.
h
\
src
/
version
.
h
\
src
/
counter
.
h
\
src
/
test
random
.
h
\
src
/
test
servergamethread
.
h
\
src
/
random
.
h
\
src
/
servergamethread
.
h
\
src
/
returnmessage
.
h
SOURCES
+=
src
/
main
.
cpp
\
src
/
test
server
.
cpp
\
src
/
test
servergame
.
cpp
\
src
/
test
serversocket
.
cpp
\
src
/
server
.
cpp
\
src
/
servergame
.
cpp
\
src
/
serversocket
.
cpp
\
src
/
playerzone
.
cpp
\
src
/
test
card
.
cpp
\
src
/
card
.
cpp
\
src
/
counter
.
cpp
\
src
/
test
random
.
cpp
\
src
/
test
servergamethread
.
cpp
\
src
/
random
.
cpp
\
src
/
servergamethread
.
cpp
\
src
/
returnmessage
.
cpp
servatrice/src/
test
card.cpp
→
servatrice/src/card.cpp
View file @
792a400a
...
...
@@ -17,19 +17,19 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include
"
test
card.h"
#include
"card.h"
Test
Card
::
Test
Card
(
QString
_name
,
int
_id
,
int
_coord_x
,
int
_coord_y
)
Card
::
Card
(
QString
_name
,
int
_id
,
int
_coord_x
,
int
_coord_y
)
:
id
(
_id
),
coord_x
(
_coord_x
),
coord_y
(
_coord_y
),
name
(
_name
),
counters
(
0
),
tapped
(
false
),
attacking
(
false
),
facedown
(
false
),
annotation
(
QString
()),
doesntUntap
(
false
)
{
}
Test
Card
::~
Test
Card
()
Card
::~
Card
()
{
}
void
Test
Card
::
resetState
()
void
Card
::
resetState
()
{
setCoords
(
0
,
0
);
setCounters
(
0
);
...
...
@@ -40,7 +40,7 @@ void TestCard::resetState()
setDoesntUntap
(
false
);
}
bool
Test
Card
::
setAttribute
(
const
QString
&
aname
,
const
QString
&
avalue
,
bool
allCards
)
bool
Card
::
setAttribute
(
const
QString
&
aname
,
const
QString
&
avalue
,
bool
allCards
)
{
if
(
!
aname
.
compare
(
"counters"
))
{
bool
ok
;
...
...
servatrice/src/
test
card.h
→
servatrice/src/card.h
View file @
792a400a
...
...
@@ -17,12 +17,12 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef
TEST
CARD_H
#define
TEST
CARD_H
#ifndef CARD_H
#define CARD_H
#include
<QString>
class
Test
Card
{
class
Card
{
private:
int
id
;
int
coord_x
,
coord_y
;
...
...
@@ -34,8 +34,8 @@ private:
QString
annotation
;
bool
doesntUntap
;
public:
Test
Card
(
QString
_name
,
int
_id
,
int
_coord_x
,
int
_coord_y
);
~
Test
Card
();
Card
(
QString
_name
,
int
_id
,
int
_coord_x
,
int
_coord_y
);
~
Card
();
int
getId
()
{
return
id
;
}
int
getX
()
{
return
coord_x
;
}
...
...
servatrice/src/main.cpp
View file @
792a400a
...
...
@@ -20,14 +20,18 @@
#include
<QCoreApplication>
#include
"
test
server.h"
#include
"server.h"
int
main
(
int
argc
,
char
*
argv
[])
{
QCoreApplication
app
(
argc
,
argv
);
TestServer
server
;
server
.
listen
(
QHostAddress
::
Any
,
4747
);
QCoreApplication
app
(
argc
,
argv
);
return
app
.
exec
();
Server
server
;
if
(
!
server
.
openDatabase
())
{
qCritical
(
"Database error"
);
return
-
1
;
}
server
.
listen
(
QHostAddress
::
Any
,
4747
);
return
app
.
exec
();
}
servatrice/src/playerzone.cpp
View file @
792a400a
...
...
@@ -18,6 +18,8 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include
"playerzone.h"
#include
"random.h"
#include
"card.h"
PlayerZone
::
PlayerZone
(
QString
_name
,
bool
_has_coords
,
bool
_is_public
,
bool
_is_private
,
bool
_id_access
)
:
name
(
_name
),
has_coords
(
_has_coords
),
is_public
(
_is_public
),
is_private
(
_is_private
),
id_access
(
_id_access
)
...
...
@@ -30,21 +32,21 @@ PlayerZone::~PlayerZone()
clear
();
}
void
PlayerZone
::
shuffle
(
Test
Random
*
rnd
)
void
PlayerZone
::
shuffle
(
Random
*
rnd
)
{
QList
<
Test
Card
*>
temp
;
QList
<
Card
*>
temp
;
for
(
int
i
=
cards
.
size
();
i
;
i
--
)
temp
.
append
(
cards
.
takeAt
(
rnd
->
getNumber
(
0
,
i
-
1
)));
cards
=
temp
;
}
Test
Card
*
PlayerZone
::
getCard
(
int
id
,
bool
remove
,
int
*
position
)
Card
*
PlayerZone
::
getCard
(
int
id
,
bool
remove
,
int
*
position
)
{
if
(
hasIdAccess
())
{
QListIterator
<
Test
Card
*>
CardIterator
(
cards
);
QListIterator
<
Card
*>
CardIterator
(
cards
);
int
i
=
0
;
while
(
CardIterator
.
hasNext
())
{
Test
Card
*
tmp
=
CardIterator
.
next
();
Card
*
tmp
=
CardIterator
.
next
();
if
(
tmp
->
getId
()
==
id
)
{
if
(
remove
)
cards
.
removeAt
(
i
);
...
...
@@ -58,7 +60,7 @@ TestCard *PlayerZone::getCard(int id, bool remove, int *position)
}
else
{
if
(
id
>=
cards
.
size
())
return
NULL
;
Test
Card
*
tmp
=
cards
[
id
];
Card
*
tmp
=
cards
[
id
];
if
(
remove
)
cards
.
removeAt
(
id
);
if
(
position
)
...
...
@@ -67,7 +69,7 @@ TestCard *PlayerZone::getCard(int id, bool remove, int *position)
}
}
void
PlayerZone
::
insertCard
(
Test
Card
*
card
,
int
x
,
int
y
)
void
PlayerZone
::
insertCard
(
Card
*
card
,
int
x
,
int
y
)
{
if
(
hasCoords
())
{
card
->
setCoords
(
x
,
y
);
...
...
servatrice/src/playerzone.h
View file @
792a400a
...
...
@@ -21,8 +21,10 @@
#define PLAYERZONE_H
#include
<QList>
#include
"testcard.h"
#include
"testrandom.h"
#include
<QString>
class
Card
;
class
Random
;
class
PlayerZone
{
private:
...
...
@@ -40,7 +42,7 @@ public:
PlayerZone
(
QString
_name
,
bool
_has_coords
,
bool
_is_public
,
bool
_is_private
,
bool
_id_access
);
~
PlayerZone
();
Test
Card
*
getCard
(
int
id
,
bool
remove
,
int
*
position
=
NULL
);
Card
*
getCard
(
int
id
,
bool
remove
,
int
*
position
=
NULL
);
bool
isPublic
()
{
return
is_public
;
}
bool
isPrivate
()
{
return
is_private
;
}
...
...
@@ -48,9 +50,9 @@ public:
bool
hasIdAccess
()
{
return
id_access
;
}
QString
getName
()
{
return
name
;
}
QList
<
Test
Card
*>
cards
;
void
insertCard
(
Test
Card
*
card
,
int
x
,
int
y
);
void
shuffle
(
Test
Random
*
rnd
);
QList
<
Card
*>
cards
;
void
insertCard
(
Card
*
card
,
int
x
,
int
y
);
void
shuffle
(
Random
*
rnd
);
void
clear
();
};
...
...
servatrice/src/
test
random.cpp
→
servatrice/src/random.cpp
View file @
792a400a
#include
"
test
random.h"
#include
"random.h"
#include
<QThread>
void
Test
Random
::
init
()
void
Random
::
init
()
{
if
(
initialized
)
return
;
...
...
@@ -11,7 +11,7 @@ void TestRandom::init()
initialized
=
true
;
}
unsigned
int
Test
Random
::
getNumber
(
unsigned
int
min
,
unsigned
int
max
)
unsigned
int
Random
::
getNumber
(
unsigned
int
min
,
unsigned
int
max
)
{
int
r
=
qrand
();
return
min
+
(
unsigned
int
)
(((
double
)
(
max
+
1
-
min
))
*
r
/
(
RAND_MAX
+
1.0
));
...
...
servatrice/src/
test
random.h
→
servatrice/src/random.h
View file @
792a400a
#ifndef
TEST
RANDOM_H
#define
TEST
RANDOM_H
#ifndef RANDOM_H
#define RANDOM_H
#include
<QObject>
#include
<QDateTime>
#include
<stdlib.h>
class
Test
Random
:
public
QObject
{
class
Random
:
public
QObject
{
Q_OBJECT
private:
bool
initialized
;
public:
Test
Random
(
QObject
*
parent
)
:
QObject
(
parent
),
initialized
(
false
)
{
}
Random
(
QObject
*
parent
)
:
QObject
(
parent
),
initialized
(
false
)
{
}
void
init
();
unsigned
int
getNumber
(
unsigned
int
min
,
unsigned
int
max
);
};
...
...
servatrice/src/returnmessage.cpp
View file @
792a400a
#include
"returnmessage.h"
#include
"
test
serversocket.h"
#include
"serversocket.h"
void
ReturnMessage
::
setMsgId
(
unsigned
int
_msg_id
)
{
msg_id
=
_msg_id
;
}
bool
ReturnMessage
::
send
(
const
QString
&
args
,
bool
success
)
bool
ReturnMessage
::
send
(
ReturnCode
code
)
{
Test
ServerSocket
*
s
=
qobject_cast
<
Test
ServerSocket
*>
(
parent
());
ServerSocket
*
s
=
qobject_cast
<
ServerSocket
*>
(
parent
());
if
(
!
s
)
return
false
;
bool
success
=
(
code
==
ReturnOk
);
QString
returnCodeString
;
switch
(
code
)
{
case
ReturnNothing
:
return
true
;
case
ReturnOk
:
break
;
case
ReturnLoginNeeded
:
returnCodeString
=
"login_needed"
;
break
;
case
ReturnSyntaxError
:
returnCodeString
=
"syntax"
;
break
;
case
ReturnContextError
:
returnCodeString
=
"context"
;
break
;
case
ReturnPasswordWrong
:
returnCodeString
=
"password"
;
break
;
case
ReturnNameNotFound
:
returnCodeString
=
"name_not_found"
;
break
;
}
s
->
msg
(
QString
(
"resp|%1|%2|%3"
).
arg
(
msg_id
)
.
arg
(
success
?
"ok"
:
"err"
)
.
arg
(
args
));
.
arg
(
returnCodeString
));
return
success
;
}
bool
ReturnMessage
::
sendList
(
const
QStringList
&
args
)
{
Test
ServerSocket
*
s
=
qobject_cast
<
Test
ServerSocket
*>
(
parent
());
ServerSocket
*
s
=
qobject_cast
<
ServerSocket
*>
(
parent
());
if
(
!
s
)
return
false
;
...
...
servatrice/src/returnmessage.h
View file @
792a400a
#ifndef RETURNMESSAGE_H
#define RETURNMESSAGE_H
#include
<QString>
#include
<QObject>
#include
<QStringList>
class
ReturnMessage
:
public
QObject
{
Q_OBJECT
...
...
@@ -10,11 +9,12 @@ private:
unsigned
int
msg_id
;
QString
cmd
;
public:
enum
ReturnCode
{
ReturnNothing
,
ReturnOk
,
ReturnLoginNeeded
,
ReturnSyntaxError
,
ReturnContextError
,
ReturnPasswordWrong
,
ReturnNameNotFound
};
ReturnMessage
(
QObject
*
parent
=
0
)
:
QObject
(
parent
),
msg_id
(
0
)
{
}
unsigned
int
getMsgId
()
const
{
return
msg_id
;
}
void
setMsgId
(
unsigned
int
_msg_id
);
void
setCmd
(
const
QString
&
_cmd
)
{
cmd
=
_cmd
;
}
bool
send
(
const
QString
&
args
=
QString
(),
bool
success
=
tru
e
);
bool
send
(
ReturnCode
cod
e
);
bool
sendList
(
const
QStringList
&
args
);
};
...
...
servatrice/src/
test
server.cpp
→
servatrice/src/server.cpp
View file @
792a400a
...
...
@@ -17,58 +17,91 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include
"testserver.h"
#include
"server.h"
#include
"servergamethread.h"
#include
"servergame.h"
#include
"serversocket.h"
#include
"counter.h"
#include
<QtSql>
Test
Server
::
Test
Server
(
QObject
*
parent
)
Server
::
Server
(
QObject
*
parent
)
:
QTcpServer
(
parent
)
{
}
Test
Server
::~
Test
Server
()
Server
::~
Server
()
{
}
void
TestServer
::
gameCreated
(
TestServerGame
*
_game
,
TestServerSocket
*
_creator
)
bool
Server
::
openDatabase
()
{
QSqlDatabase
sqldb
=
QSqlDatabase
::
addDatabase
(
"QMYSQL"
);
sqldb
.
setHostName
(
"localhost"
);
sqldb
.
setDatabaseName
(
"cockatrice"
);
sqldb
.
setUserName
(
"cockatrice"
);
sqldb
.
setPassword
(
"45CdX6rmd"
);
return
sqldb
.
open
();
}
void
Server
::
gameCreated
(
ServerGame
*
_game
,
ServerSocket
*
_creator
)
{
games
<<
_game
;
_creator
->
moveToThread
(
_game
->
thread
());
_game
->
addPlayer
(
_creator
);
}
void
Test
Server
::
addGame
(
const
QString
name
,
const
QString
description
,
const
QString
password
,
const
int
maxPlayers
,
Test
ServerSocket
*
creator
)
void
Server
::
addGame
(
const
QString
name
,
const
QString
description
,
const
QString
password
,
const
int
maxPlayers
,
ServerSocket
*
creator
)
{
Test
ServerGameThread
*
newThread
=
new
Test
ServerGameThread
(
name
,
description
,
password
,
maxPlayers
,
creator
);
connect
(
newThread
,
SIGNAL
(
gameCreated
(
Test
ServerGame
*
,
Test
ServerSocket
*
)),
this
,
SLOT
(
gameCreated
(
Test
ServerGame
*
,
Test
ServerSocket
*
)));
ServerGameThread
*
newThread
=
new
ServerGameThread
(
name
,
description
,
password
,
maxPlayers
,
creator
);
connect
(
newThread
,
SIGNAL
(
gameCreated
(
ServerGame
*
,
ServerSocket
*
)),
this
,
SLOT
(
gameCreated
(
ServerGame
*
,
ServerSocket
*
)));
connect
(
newThread
,
SIGNAL
(
finished
()),
this
,
SLOT
(
gameClosed
()));
newThread
->
start
();
}
void
Test
Server
::
incomingConnection
(
int
socketId
)
void
Server
::
incomingConnection
(
int
socketId
)
{
Test
ServerSocket
*
socket
=
new
Test
ServerSocket
(
this
);
ServerSocket
*
socket
=
new
ServerSocket
(
this
);
socket
->
setSocketDescriptor
(
socketId
);
connect
(
socket
,
SIGNAL
(
createGame
(
const
QString
,
const
QString
,
const
QString
,
const
int
,
Test
ServerSocket
*
)),
this
,
SLOT
(
addGame
(
const
QString
,
const
QString
,
const
QString
,
const
int
,
Test
ServerSocket
*
)));
connect
(
socket
,
SIGNAL
(
joinGame
(
const
QString
,
Test
ServerSocket
*
)),
this
,
SLOT
(
addClientToGame
(
const
QString
,
Test
ServerSocket
*
)));
connect
(
socket
,
SIGNAL
(
createGame
(
const
QString
,
const
QString
,
const
QString
,
const
int
,
ServerSocket
*
)),
this
,
SLOT
(
addGame
(
const
QString
,
const
QString
,
const
QString
,
const
int
,
ServerSocket
*
)));
connect
(
socket
,
SIGNAL
(
joinGame
(
const
QString
,
ServerSocket
*
)),
this
,
SLOT
(
addClientToGame
(
const
QString
,
ServerSocket
*
)));
socket
->
initConnection
();
}
TestServerGame
*
TestServer
::
getGame
(
const
QString
&
name
)
AuthenticationResult
Server
::
checkUserPassword
(
const
QString
&
user
,
const
QString
&
password
)
{
QSqlQuery
query
;
query
.
prepare
(
"select password from users where name = :name"
);
query
.
bindValue
(
":name"
,
user
);
if
(
!
query
.
exec
())
{
qCritical
(
QString
(
"Database error: %1"
).
arg
(
query
.
lastError
().
text
()).
toLatin1
());
exit
(
-
1
);
}
if
(
query
.
next
())
{
if
(
query
.
value
(
0
).
toString
()
==
password
)
return
PasswordRight
;
else
return
PasswordWrong
;
}
else
return
UnknownUser
;
}
ServerGame
*
Server
::
getGame
(
const
QString
&
name
)
{
QListIterator
<
Test
ServerGame
*>
i
(
games
);
QListIterator
<
ServerGame
*>
i
(
games
);
while
(
i
.
hasNext
())
{
Test
ServerGame
*
tmp
=
i
.
next
();
ServerGame
*
tmp
=
i
.
next
();
if
((
!
tmp
->
name
.
compare
(
name
,
Qt
::
CaseSensitive
))
&&
!
tmp
->
getGameStarted
())
return
tmp
;
}
return
NULL
;
}
QList
<
Test
ServerGame
*>
Test
Server
::
listOpenGames
()
QList
<
ServerGame
*>
Server
::
listOpenGames
()
{
QList
<
Test
ServerGame
*>
result
;
QListIterator
<
Test
ServerGame
*>
i
(
games
);
QList
<
ServerGame
*>
result
;
QListIterator
<
ServerGame
*>
i
(
games
);
while
(
i
.
hasNext
())
{
Test
ServerGame
*
tmp
=
i
.
next
();
ServerGame
*
tmp
=
i
.
next
();
tmp
->
mutex
->
lock
();
if
((
!
tmp
->
getGameStarted
())
&&
(
tmp
->
getPlayerCount
()
<
tmp
->
maxPlayers
))
...
...
@@ -78,9 +111,9 @@ QList<TestServerGame *> TestServer::listOpenGames()
return
result
;
}
bool
Test
Server
::
checkGamePassword
(
const
QString
&
name
,
const
QString
&
password
)
bool
Server
::
checkGamePassword
(
const
QString
&
name
,
const
QString
&
password
)
{
Test
ServerGame
*
tmp
;
ServerGame
*
tmp
;
if
((
tmp
=
getGame
(
name
)))
{
QMutexLocker
locker
(
tmp
->
mutex
);
if
((
!
tmp
->
getGameStarted
())
...
...
@@ -91,17 +124,17 @@ bool TestServer::checkGamePassword(const QString &name, const QString &password)
return
false
;
}
void
Test
Server
::
addClientToGame
(
const
QString
name
,
Test
ServerSocket
*
client
)
void
Server
::
addClientToGame
(
const
QString
name
,
ServerSocket
*
client
)
{
Test
ServerGame
*
tmp
=
getGame
(
name
);
ServerGame
*
tmp
=
getGame
(
name
);
client
->
moveToThread
(
tmp
->
thread
());
tmp
->
addPlayer
(
client
);
}
void
Test
Server
::
gameClosed
()
void
Server
::
gameClosed
()
{
qDebug
(
"
Test
Server::gameClosed"
);
Test
ServerGameThread
*
t
=
qobject_cast
<
Test
ServerGameThread
*>
(
sender
());
qDebug
(
"Server::gameClosed"
);
ServerGameThread
*
t
=
qobject_cast
<
ServerGameThread
*>
(
sender
());
games
.
removeAt
(
games
.
indexOf
(
t
->
getGame
()));
delete
t
;
}
servatrice/src/
test
server.h
→
servatrice/src/server.h
View file @
792a400a
...
...
@@ -17,33 +17,36 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef
TEST
SERVER_H
#define
TEST
SERVER_H
#ifndef SERVER_H
#define SERVER_H
#include
<QTcpServer>
#include
"testservergamethread.h"
#include
"testservergame.h"
class
TestServerGame
;
class
TestServerSocket
;
class
ServerGame
;
class
ServerSocket
;
class
QSqlDatabase
;
class
TestServer
:
public
QTcpServer
enum
AuthenticationResult
{
PasswordWrong
=
0
,
PasswordRight
=
1
,
UnknownUser
=
2
};
class
Server
:
public
QTcpServer
{
Q_OBJECT
private
slots
:
void
addGame
(
const
QString
name
,
const
QString
description
,
const
QString
password
,
const
int
maxPlayers
,
Test
ServerSocket
*
creator
);
void
addClientToGame
(
const
QString
name
,
Test
ServerSocket
*
client
);
void
gameCreated
(
Test
ServerGame
*
_game
,
Test
ServerSocket
*
_creator
);
void
addGame
(
const
QString
name
,
const
QString
description
,
const
QString
password
,
const
int
maxPlayers
,
ServerSocket
*
creator
);
void
addClientToGame
(
const
QString
name
,
ServerSocket
*
client
);
void
gameCreated
(
ServerGame
*
_game
,
ServerSocket
*
_creator
);
void
gameClosed
();
public:
TestServer
(
QObject
*
parent
=
0
);
~
TestServer
();
Server
(
QObject
*
parent
=
0
);
~
Server
();
bool
openDatabase
();
bool
checkGamePassword
(
const
QString
&
name
,
const
QString
&
password
);
QList
<
TestServerGame
*>
listOpenGames
();
TestServerGame
*
getGame
(
const
QString
&
name
);
AuthenticationResult
checkUserPassword
(
const
QString
&
user
,
const
QString
&
password
);
QList
<
ServerGame
*>
listOpenGames
();
ServerGame
*
getGame
(
const
QString
&
name
);
private:
void
incomingConnection
(
int
SocketId
);
QList
<
Test
ServerGame
*>
games
;
QList
<
ServerGame
*>
games
;
};
#endif
Prev
1
2
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment