Trust ai (#13)

* add 'Robot' to Player::State

* add definition to Room::addRobot

* addRobot

* modify room abandon

* robot will wait for 1 second(not 1000)

* tell lua and js someone run

Co-authored-by: Notify-ctrl <notify-ctrl@qq.com>
This commit is contained in:
Notify-ctrl 2022-04-03 06:41:55 +08:00 committed by GitHub
parent 927c5f479b
commit 4c93c897b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 106 additions and 8 deletions

View File

@ -256,6 +256,31 @@ fk.room_callback["QuitRoom"] = function(jsonData)
end end
end end
fk.room_callback["AddRobot"] = function(jsonData)
-- jsonData: [ int uid ]
local data = json.decode(jsonData)
local player = fk.ServerInstance:findPlayer(tonumber(data[1]))
local room = player:getRoom()
if not room:isLobby() then
room:addRobot(player)
end
end
fk.room_callback["PlayerRunned"] = function(jsonData)
-- jsonData: [ int runner_id, int robot_id ]
-- note: this function is not called by Router.
-- note: when this function is called, the room must be started
local data = json.decode(jsonData)
local runner = data[1]
local robot = data[2]
for _, p in ipairs(RoomInstance.players) do
if p:getId() == runner then
p.serverplayer = RoomInstance:findPlayerById(robot)
end
end
end
fk.room_callback["PlayerStateChanged"] = function(jsonData) fk.room_callback["PlayerStateChanged"] = function(jsonData)
-- jsonData: [ int uid, string stateString ] -- jsonData: [ int uid, string stateString ]
-- note: this function is not called by Router. -- note: this function is not called by Router.

View File

@ -27,9 +27,12 @@ Item {
} }
} }
Button { Button {
text: "start game" text: "add robot"
visible: dashboardModel.isOwner && !isStarted visible: dashboardModel.isOwner && !isStarted
anchors.centerIn: parent anchors.centerIn: parent
onClicked: {
ClientInstance.notifyServer("AddRobot", "[]");
}
} }
states: [ states: [

View File

@ -187,6 +187,21 @@ callbacks["MoveFocus"] = function(jsonData) {
} }
} }
callbacks["PlayerRunned"] = function(jsonData) {
// jsonData: int runner, int robot
let data = JSON.parse(jsonData);
let runner = data[0];
let robot = data[1];
let model;
for (let i = 0; i < playerNum - 1; i++) {
model = photoModel.get(i);
if (model.id === runner) {
model.id = robot;
}
}
}
callbacks["AskForGeneral"] = function(jsonData) { callbacks["AskForGeneral"] = function(jsonData) {
// jsonData: string[] Generals // jsonData: string[] Generals
// TODO: choose multiple generals // TODO: choose multiple generals

View File

@ -56,6 +56,8 @@ QString Player::getStateString() const
return QStringLiteral("online"); return QStringLiteral("online");
case Trust: case Trust:
return QStringLiteral("trust"); return QStringLiteral("trust");
case Robot:
return QStringLiteral("robot");
case Offline: case Offline:
return QStringLiteral("offline"); return QStringLiteral("offline");
default: default:
@ -75,6 +77,8 @@ void Player::setStateString(const QString &state)
setState(Online); setState(Online);
else if (state == QStringLiteral("trust")) else if (state == QStringLiteral("trust"))
setState(Trust); setState(Trust);
else if (state == QStringLiteral("robot"))
setState(Robot);
else if (state == QStringLiteral("offline")) else if (state == QStringLiteral("offline"))
setState(Offline); setState(Offline);
else else

View File

@ -10,7 +10,8 @@ public:
enum State{ enum State{
Invalid, Invalid,
Online, Online,
Trust, Trust, // Trust or run
Robot, // only for real robot
Offline Offline
}; };

View File

@ -11,6 +11,7 @@ Room::Room(Server* server)
setParent(server); setParent(server);
owner = nullptr; owner = nullptr;
gameStarted = false; gameStarted = false;
robot_id = -1;
timeout = 15; timeout = 15;
if (!isLobby()) { if (!isLobby()) {
connect(this, &Room::playerAdded, server->lobby(), &Room::removePlayer); connect(this, &Room::playerAdded, server->lobby(), &Room::removePlayer);
@ -80,7 +81,14 @@ bool Room::isFull() const
bool Room::isAbandoned() const bool Room::isAbandoned() const
{ {
return players.isEmpty(); if (players.isEmpty())
return true;
foreach (ServerPlayer *p, players) {
if (p->getState() == Player::Online)
return false;
}
return true;
} }
ServerPlayer *Room::getOwner() const ServerPlayer *Room::getOwner() const
@ -149,6 +157,20 @@ void Room::addPlayer(ServerPlayer *player)
emit playerAdded(player); emit playerAdded(player);
} }
void Room::addRobot(ServerPlayer *player)
{
if (player != owner || isFull()) return;
ServerPlayer *robot = new ServerPlayer(this);
robot->setState(Player::Robot);
robot->setId(robot_id);
robot->setAvatar("guanyu");
robot->setScreenName(QString("COMP-%1").arg(robot_id));
robot_id--;
addPlayer(robot);
}
void Room::removePlayer(ServerPlayer *player) void Room::removePlayer(ServerPlayer *player)
{ {
players.removeOne(player); players.removeOne(player);
@ -157,12 +179,22 @@ void Room::removePlayer(ServerPlayer *player)
if (isLobby()) return; if (isLobby()) return;
if (gameStarted) { if (gameStarted) {
// TODO: if the player is died..
// create robot first
ServerPlayer *robot = new ServerPlayer(this);
robot->setState(Player::Robot);
robot->setId(robot_id);
robot->setAvatar(player->getAvatar());
robot->setScreenName(QString("COMP-%1").arg(robot_id));
robot_id--;
// tell lua & clients
QJsonArray jsonData; QJsonArray jsonData;
jsonData << player->getId(); jsonData << player->getId();
jsonData << robot->getId();
callLua("PlayerRunned", QJsonDocument(jsonData).toJson());
doBroadcastNotify(getPlayers(), "PlayerRunned", QJsonDocument(jsonData).toJson()); doBroadcastNotify(getPlayers(), "PlayerRunned", QJsonDocument(jsonData).toJson());
runned_players << player->getId(); runned_players << player->getId();
// TODO: create a robot for runned player.
} else { } else {
QJsonArray jsonData; QJsonArray jsonData;
jsonData << player->getId(); jsonData << player->getId();
@ -190,7 +222,11 @@ QList<ServerPlayer *> Room::getOtherPlayers(ServerPlayer* expect) const
ServerPlayer *Room::findPlayer(int id) const ServerPlayer *Room::findPlayer(int id) const
{ {
return server->findPlayer(id); foreach (ServerPlayer *p, players) {
if (p->getId() == id)
return p;
}
return nullptr;
} }
int Room::getTimeout() const int Room::getTimeout() const
@ -230,9 +266,9 @@ void Room::gameOver()
{ {
gameStarted = false; gameStarted = false;
runned_players.clear(); runned_players.clear();
// clean offline players // clean not online players
foreach (ServerPlayer *p, players) { foreach (ServerPlayer *p, players) {
if (p->getState() == Player::Offline) { if (p->getState() != Player::Online) {
p->deleteLater(); p->deleteLater();
} }
} }

View File

@ -26,6 +26,7 @@ public:
void setOwner(ServerPlayer *owner); void setOwner(ServerPlayer *owner);
void addPlayer(ServerPlayer *player); void addPlayer(ServerPlayer *player);
void addRobot(ServerPlayer *player);
void removePlayer(ServerPlayer *player); void removePlayer(ServerPlayer *player);
QList<ServerPlayer*> getPlayers() const; QList<ServerPlayer*> getPlayers() const;
QList<ServerPlayer *> getOtherPlayers(ServerPlayer *expect) const; QList<ServerPlayer *> getOtherPlayers(ServerPlayer *expect) const;
@ -76,6 +77,7 @@ private:
ServerPlayer *owner; // who created this room? ServerPlayer *owner; // who created this room?
QList<ServerPlayer *> players; QList<ServerPlayer *> players;
QList<int> runned_players; QList<int> runned_players;
int robot_id;
bool gameStarted; bool gameStarted;
int timeout; int timeout;

View File

@ -232,6 +232,7 @@ void Server::handleNameAndPassword(ClientSocket *client, const QString& name, co
void Server::onRoomAbandoned() void Server::onRoomAbandoned()
{ {
Room *room = qobject_cast<Room *>(sender()); Room *room = qobject_cast<Room *>(sender());
room->gameOver();
rooms.remove(room->getId()); rooms.remove(room->getId());
updateRoomList(); updateRoomList();
room->deleteLater(); room->deleteLater();

View File

@ -64,22 +64,32 @@ void ServerPlayer::speak(const QString& message)
void ServerPlayer::doRequest(const QString& command, const QString& jsonData, int timeout) void ServerPlayer::doRequest(const QString& command, const QString& jsonData, int timeout)
{ {
if (getState() != Player::Online) return;
int type = Router::TYPE_REQUEST | Router::SRC_SERVER | Router::DEST_CLIENT; int type = Router::TYPE_REQUEST | Router::SRC_SERVER | Router::DEST_CLIENT;
router->request(type, command, jsonData, timeout); router->request(type, command, jsonData, timeout);
} }
QString ServerPlayer::waitForReply() QString ServerPlayer::waitForReply()
{ {
if (getState() != Player::Online) {
QThread::sleep(1);
return "";
}
return router->waitForReply(); return router->waitForReply();
} }
QString ServerPlayer::waitForReply(int timeout) QString ServerPlayer::waitForReply(int timeout)
{ {
if (getState() != Player::Online) {
QThread::sleep(1);
return "";
}
return router->waitForReply(timeout); return router->waitForReply(timeout);
} }
void ServerPlayer::doNotify(const QString& command, const QString& jsonData) void ServerPlayer::doNotify(const QString& command, const QString& jsonData)
{ {
if (getState() != Player::Online) return;
int type = Router::TYPE_NOTIFICATION | Router::SRC_SERVER | Router::DEST_CLIENT; int type = Router::TYPE_NOTIFICATION | Router::SRC_SERVER | Router::DEST_CLIENT;
router->notify(type, command, jsonData); router->notify(type, command, jsonData);
} }

View File

@ -32,6 +32,7 @@ public:
void setOwner(ServerPlayer *owner); void setOwner(ServerPlayer *owner);
void addPlayer(ServerPlayer *player); void addPlayer(ServerPlayer *player);
void addRobot(ServerPlayer *player);
void removePlayer(ServerPlayer *player); void removePlayer(ServerPlayer *player);
QList<ServerPlayer *> getPlayers() const; QList<ServerPlayer *> getPlayers() const;
ServerPlayer *findPlayer(int id) const; ServerPlayer *findPlayer(int id) const;