Beispiel #1
0
void MainWindow::on_actionReplay_triggered()
{
	QString location = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
	QString last_dir = Config.value("LastReplayDir").toString();
	if(!last_dir.isEmpty())
		location = last_dir;

	QString filename = QFileDialog::getOpenFileName(this,
													tr("Select a reply file"),
													location,
													tr("Pure text replay file (*.txt);; Image replay file (*.png)"));

	if(filename.isEmpty())
		return;

	QFileInfo file_info(filename);
	last_dir = file_info.absoluteDir().path();
	Config.setValue("LastReplayDir", last_dir);

	Client *client = new Client(this, filename);

	connect(client, SIGNAL(server_connected()), SLOT(enterRoom()));

	client->signup();
}
void MainWindow::on_actionReplay_triggered() {
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
    QString location = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
#else
    QString location = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
#endif
    QString last_dir = Config.value("LastReplayDir").toString();
    if (!last_dir.isEmpty())
        location = last_dir;

    QString filename = QFileDialog::getOpenFileName(this,
        tr("Select a reply file"),
        location,
        tr("QSanguosha Replay File(*.qsgs);; Image replay file (*.png)"));

    if (filename.isEmpty())
        return;

    QFileInfo file_info(filename);
    last_dir = file_info.absoluteDir().path();
    Config.setValue("LastReplayDir", last_dir);

    Client *client = new Client(this, filename);
    connect(client, SIGNAL(server_connected()), SLOT(enterRoom()));
    client->signup();
}
Beispiel #3
0
void MainWindow::checkVersion(const QString &server_version, const QString &server_mod){
	QString client_mod = Bang->getMODName();
	if(client_mod != server_mod){
		QMessageBox::warning(this, tr("Warning"), tr("Client MOD name is not same as the server!"));
		return;
	}

	Client *client = qobject_cast<Client *>(sender());
	QString client_version = Bang->getVersionNumber();

	if(server_version == client_version){
		client->signup();
		connect(client, SIGNAL(server_connected()), SLOT(enterRoom()));

		if(qApp->arguments().contains("-hall")){
			HallDialog *dialog = HallDialog::GetInstance(this);
			connect(client, SIGNAL(server_connected()), dialog, SLOT(accept()));
		}

		return;
	}

	client->disconnectFromHost();

	static QString link = "http://code.google.com/p/onepiecebang/downloads/list";
	QString text = tr("Server version is %1, client version is %2 <br/>").arg(server_version).arg(client_version);
	if(server_version > client_version)
		text.append(tr("Your client version is older than the server's, please update it <br/>"));
	else
		text.append(tr("The server version is older than your client version, please ask the server to update<br/>"));

	text.append(tr("Download link : <a href='%1'>%1</a> <br/>").arg(link));
	QMessageBox::warning(this, tr("Warning"), text);
}
Beispiel #4
0
void MainWindow::checkVersion(const QString &server_version, const QString &server_mod)
{
    QString client_mod = Sanguosha->getMODName();
    if (client_mod != server_mod) {
        QMessageBox::warning(this, tr("Warning"), tr("Client MOD name is not same as the server!"));
        return;
    }

    Client *client = qobject_cast<Client *>(sender());
    QString client_version = Sanguosha->getVersionNumber();

    if (server_version == client_version) {
        client->signup();
        connect(client, SIGNAL(server_connected()), SLOT(enterRoom()));
        return;
    }

    client->disconnectFromHost();

    static QString link = "http://github.com/Mogara/QSanguosha-v2";
    QString text = tr("Server version is %1, client version is %2 <br/>").arg(server_version).arg(client_version);
    if (server_version > client_version)
        text.append(tr("Your client version is older than the server's, please update it <br/>"));
    else
        text.append(tr("The server version is older than your client version, please ask the server to update<br/>"));

    text.append(tr("Download link : <a href='%1'>%1</a> <br/>").arg(link));
    QMessageBox::warning(this, tr("Warning"), text);
}
Beispiel #5
0
void MainWindow::checkVersion(const QString &server_version){
    Client *client = qobject_cast<Client *>(sender());
    QString client_version = Sanguosha->getVersion();

    if(server_version == client_version){
        client->signup();
        connect(client, SIGNAL(server_connected()), SLOT(enterRoom()));

        if(qApp->arguments().contains("-hall")){
            HallDialog *dialog = HallDialog::GetInstance(this);
            connect(client, SIGNAL(server_connected()), dialog, SLOT(accept()));
        }

        return;
    }

    client->disconnectFromHost();

    static QString link = "http://github.com/Moligaloo/QSanguosha/downloads";
    QString text = tr("Server version is %1, client version is %2 <br/>").arg(server_version).arg(client_version);
    if(server_version > client_version)
        text.append(tr("Your client version is older than the server's, please update it <br/>"));
    else
        text.append(tr("The server version is older than your client version, please ask the server to update<br/>"));

    text.append(tr("Download link : <a href='%1'>%1</a> <br/>").arg(link));
    QMessageBox::warning(this, tr("Warning"), text);
}
Beispiel #6
0
void MainWindow::startConnection(){
    Client *client = new Client(this);

    connect(client, SIGNAL(error_message(QString)), SLOT(networkError(QString)));
    connect(client, SIGNAL(server_connected()), SLOT(enterRoom()));

    //client->signup();
}
Beispiel #7
0
int
isEmptySharedStack(SharedStack_t* ss)
{
	int empty;
	enterRoom(ss->threeRoom,1);	/* Enter push room */
	/* If empty in push room, stack is really empty at this point */
	empty = internalIsEmptySharedStack(ss);
	exitRoom(ss->threeRoom);
	return empty;
}
Beispiel #8
0
void MessageType::onEnterRoom(netlib::connectionPtr conn,EnterRoomPtr message)
{
    Status status;
    if(!enterRoom(rooms_,conn,message,sql_))
    {
        status.set_enter_room(ROOM::E_ROOM_FAIL);
    }
    else
    {
        conn->rooName_ = message->room_name();
        status.set_enter_room(ROOM::E_ROOM_SUCCEED);
    }
    codec_.fillEmptyBuffer(&conn->outputBuffer_,status);
    conn->send();
}
Beispiel #9
0
void MainWindow::on_actionReplay_triggered()
{
    QString location = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
    QString filename = QFileDialog::getOpenFileName(this,
                                                    tr("Select a reply file"),
                                                    location,
                                                    tr("Replay file (*.txt)"));

    if(filename.isEmpty())
        return;

    Client *client = new Client(this, filename);

    connect(client, SIGNAL(server_connected()), SLOT(enterRoom()));

    client->signup();
}
Beispiel #10
0
bool DrasculaEngine::loadGame(const char *gameName) {
	int l, savedChapter, roomNum = 0;
	Common::InSaveFile *sav;

	previousMusic = roomMusic;
	_menuScreen = false;
	if (currentChapter != 1)
		clearRoom();

	if (!(sav = _saveFileMan->openForLoading(gameName))) {
		error("missing savegame file");
	}

	savedChapter = sav->readSint32LE();
	if (savedChapter != currentChapter) {
		strcpy(saveName, gameName);
		currentChapter = savedChapter - 1;
		loadedDifferentChapter = 1;
		return false;
	}
	sav->read(currentData, 20);
	curX = sav->readSint32LE();
	curY = sav->readSint32LE();
	trackProtagonist = sav->readSint32LE();

	for (l = 1; l < ARRAYSIZE(inventoryObjects); l++) {
		inventoryObjects[l] = sav->readSint32LE();
	}

	for (l = 0; l < NUM_FLAGS; l++) {
		flags[l] = sav->readSint32LE();
	}

	takeObject = sav->readSint32LE();
	pickedObject = sav->readSint32LE();
	loadedDifferentChapter = 0;
	if (!sscanf(currentData, "%d.ald", &roomNum)) {
		error("Bad save format");
	}
	enterRoom(roomNum);
	selectVerb(kVerbNone);

	return true;
}
Beispiel #11
0
int
pushSharedStack(int conditional, SharedStack_t* ss, LocalWork_t* lw)
{
	int empty;
	enterRoom(ss->threeRoom,conditional ? 2 : 1);
	helpPushSharedStack(ss, lw);
	if (!conditional) {
		if (lw->hasShared == 1) {
			FetchAndAdd(&ss->numLocalStack,-1);
			lw->hasShared = 0;
		}
	}
	else 
		assert(lw->hasShared == 0);
	assert(ss->numLocalStack >= 0);
	empty = (exitRoom(ss->threeRoom) == StackEmpty);
	FetchAndAdd(&ss->numPush, 1);
	return empty;
}
Beispiel #12
0
bool tickEmpty(bool _doInit) {
  if (_doInit == true) {
    s_state = 0;
    m_player.m_position = GPoint(0, SIZE*9);
    addCluter(20, 20, 0);
    return false;
  }

  if (s_state == 0) { // start initial move
    enterRoom(&s_state);
  } else if (s_state == 1) { // initial move is done
    setGameState(kAwaitInput);
    ++s_state;
  } else if (s_state == 2) {
    moveToExit(&s_state);
  } else if (s_state == 3) {
    setGameState(kFadeOut);
  }

  return false;
}
Beispiel #13
0
bool tickStart(bool _doInit) {
  if (_doInit == true) {
    s_state = 0;
    m_player.m_position = GPoint(0, SIZE*9);
    addCluter(4, 0, 20); // Only left
    return false;
  }

  static bool _first = true;
  static const char _msgA[] = "SHAKE TO TURN...";
  static const char _msgB[] = "ON BACKLIGHT";

  if (s_state == 0) { // start initial move
    enterRoom(&s_state);
  } else if (s_state == 1) { // initial move is done
    if (_first == true) {
      setDisplayMsg(_msgA);
      setGameState(kDisplayMsg);
      s_state = 2;
      _first = false;
    } else {
      setGameState(kAwaitInput);
      s_state = 4;
    }
  } else if (s_state == 2) {
    setDisplayMsg(_msgB);
    setGameState(kDisplayMsg);
    ++s_state;
  } else if (s_state == 3) {
    setGameState(kAwaitInput);
    ++s_state;
  } else if (s_state == 4) {
    moveToExit(&s_state);
  } else if (s_state == 5) {
    setGameState(kFadeOut);
  }

  return false;
}
Beispiel #14
0
void
popSharedStack(SharedStack_t* ss, LocalWork_t* lw)
{
	int stackletFetched = 0;
	int globalLocFetched = 0;
	int rootLocFetched = 0;
	int grayRegionFetched = 0;
	int objFetched = 0;
	int segFetched = 0;
	int backLocFetched = 0;
	int backObjFetched = 0;
	int stackletRequest = threadFetchSize;
	int globalLocRequest = globalLocFetchSize;
	int rootLocRequest = rootLocFetchSize;
	int grayRegionRequest = grayRegionFetchSize;
	int objRequest = objFetchSize;
	int segRequest = segFetchSize;
	int backLocRequest = backLocFetchSize;
	int backObjRequest = backObjFetchSize;

	int objSize = SetLength(&ss->work.objs);
	objRequest = Min(objFetchSize, 1 + (int) (objSize / (double) NumProc));

	enterRoom(ss->threeRoom,0);

	stackletFetched = getFromSharedStack(&lw->stacklets,
		&ss->work.stacklets, stackletRequest);
	globalLocRequest = MakeFraction(globalLocRequest, stackletRequest,
		stackletFetched);
	if (globalLocRequest == 0)
		goto done;

	globalLocFetched = getFromSharedStack(&lw->globals,
		&ss->work.globals, globalLocRequest);
	rootLocRequest = MakeFraction(rootLocRequest, globalLocRequest,
		globalLocFetched);
	if (rootLocRequest == 0)
		goto done;

	rootLocFetched = getFromSharedStack(&lw->roots,
		&ss->work.roots, rootLocRequest);
	grayRegionRequest = MakeFraction(grayRegionRequest, rootLocRequest,
		rootLocFetched);
	if(grayRegionRequest == 0)
		goto done;

	grayRegionFetched = getFromSharedStack(&lw->grayRegion,
		&ss->work.grayRegion, regionSize * grayRegionRequest) / regionSize;
	objRequest = MakeFraction(objRequest, grayRegionRequest,
		grayRegionFetched);
	if (objRequest == 0)
		goto done;

	objFetched = getFromSharedStack(&lw->objs, &ss->work.objs, objRequest);
	if ((segRequest = MakeFraction(segRequest, objRequest, objFetched)) == 0)
		goto done;

	segFetched = getFromSharedStack(&lw->segments,
		&ss->work.segments, segSize * segRequest) / segSize;
	backObjRequest = MakeFraction(backObjRequest, segRequest, segFetched);
	if (backObjRequest == 0)
		goto done;

	backObjFetched = getFromSharedStack(&lw->backObjs,
		&ss->work.backObjs, backObjRequest);
	backLocRequest = MakeFraction(backLocRequest, backObjRequest,
		backObjFetched);
	if (backLocRequest == 0)
		goto done;

	backLocFetched = getFromSharedStack(&lw->backLocs,
		&ss->work.backLocs, backLocRequest);

done:
	assert(ss->numLocalStack >= 0);
	if (stackletFetched || globalLocFetched || rootLocFetched
	|| grayRegionFetched || objFetched || segFetched || backObjFetched
	|| backLocFetched) {
		assert(lw->hasShared == 0);
		lw->hasShared = 1;
		FetchAndAdd(&ss->numLocalStack,1);
	}
	FetchAndAdd(&ss->numPop, 1);
	exitRoom(ss->threeRoom);
}
Beispiel #15
0
bool DrasculaEngine::runCurrentChapter() {
	int n;

	rightMouseButton = 0;

	previousMusic = -1;

	if (currentChapter != 2) {
		int soc = 0;
		for (n = 0; n < 6; n++) {
			soc = soc + CHARACTER_WIDTH;
			_frameX[n] = soc;
		}
	}

	for (n = 1; n < ARRAYSIZE(inventoryObjects); n++)
		inventoryObjects[n] = 0;

	for (n = 0; n < NUM_FLAGS; n++)
		flags[n] = 0;

	if (currentChapter == 2) {
		flags[16] = 1;
		flags[17] = 1;
		flags[27] = 1;
	}

	for (n = 1; n < 7; n++)
		inventoryObjects[n] = n;

	if (currentChapter == 1) {
		pickObject(28);

		if (loadedDifferentChapter == 0)
			animation_1_1();

		selectVerb(kVerbNone);
		loadPic("2aux62.alg", drawSurface2);
		trackProtagonist = 1;
		objExit = 104;
		if (loadedDifferentChapter != 0) {
			if (!loadGame(saveName)) {
				return true;
			}
		} else {
			enterRoom(62);
			curX = -20;
			curY = 56;
			gotoObject(65, 145);
		}
	} else if (currentChapter == 2) {
		addObject(kItemPhone);
		trackProtagonist = 3;
		objExit = 162;
		if (loadedDifferentChapter == 0)
			enterRoom(14);
		else {
			if (!loadGame(saveName)) {
				return true;
			}
		}
	} else if (currentChapter == 3) {
		addObject(kItemPhone);
		addObject(kItemEarplugs);
		addObject(kItemSickle);
		addObject(kItemHandbag);
		addObject(kItemCross);
		addObject(kItemReefer);
		addObject(kItemOneCoin);
		flags[1] = 1;
		trackProtagonist = 1;
		objExit = 99;
		if (loadedDifferentChapter == 0)
			enterRoom(20);
		else {
			if (!loadGame(saveName)) {
				return true;
			}
		}
	// From here onwards the items have different IDs
	} else if (currentChapter == 4) {
		addObject(kItemPhone2);
		addObject(kItemCross2);
		addObject(kItemReefer2);
		addObject(kItemOneCoin2);
		objExit = 100;
		if (loadedDifferentChapter == 0) {
			enterRoom(21);
			trackProtagonist = 0;
			curX = 235;
			curY = 164;
		} else {
			if (!loadGame(saveName)) {
				return true;
			}
		}
	} else if (currentChapter == 5) {
		addObject(28);
		addObject(7);
		addObject(9);
		addObject(11);
		addObject(13);
		addObject(14);
		addObject(15);
		addObject(17);
		addObject(20);
		trackProtagonist = 1;
		objExit = 100;
		if (loadedDifferentChapter == 0) {
			enterRoom(45);
		} else {
			if (!loadGame(saveName)) {
				return true;
			}
		}
	} else if (currentChapter == 6) {
		addObject(28);
		addObject(9);

		trackProtagonist = 1;
		objExit = 104;
		if (loadedDifferentChapter == 0) {
			enterRoom(58);
			animation_1_6();
		} else {
			if (!loadGame(saveName)) {
				return true;
			}
			loadPic("auxdr.alg", drawSurface2);
		}
	}

	showCursor();

	while (!shouldQuit()) {
		if (characterMoved == 0) {
			stepX = STEP_X;
			stepY = STEP_Y;
		}
		if (characterMoved == 0 && walkToObject == 1) {
			trackProtagonist = trackFinal;
			walkToObject = 0;
		}

		if (currentChapter == 2) {
			// NOTE: the checks for room number 14 below are a hack used in the original
			// game, and move the character to a place where his feet are not drawn above
			// the pianist's head. Originally, walkToObject was not updated properly, which
			// lead to an incorrect setting of the protagonist's tracking flag (above). This
			// made the character start walking off screen, as his actual position was
			// different than the displayed one
			if (roomNumber == 3 && (curX == 279) && (curY + curHeight == 101)) {
				gotoObject(178, 121);
				gotoObject(169, 135);
			} else if (roomNumber == 14 && (curX == 214) && (curY + curHeight == 121)) {
				walkToObject = 1;
				gotoObject(190, 130);
			} else if (roomNumber == 14 && (curX == 246) && (curY + curHeight == 112)) {
				walkToObject = 1;
				gotoObject(190, 130);
			}
		}

		moveCursor();
		updateScreen();

		if (currentChapter == 2) {
			if (musicStatus() == 0 && roomMusic != 0)
				playMusic(roomMusic);
		} else {
			if (musicStatus() == 0)
				playMusic(roomMusic);
		}

		delay(25);
#ifndef _WIN32_WCE
		// FIXME
		// This and the following #ifndefs disable the excess updateEvents() calls *within* the game loop.
		// Events such as keypresses or mouse clicks are dropped on the ground with no processing
		// by these calls. They are properly handled by the implicit call through getScan() below.
		// It is not a good practice to not process events and indeed this created problems with synthesized
		// events in the wince port.
		updateEvents();
#endif

		if (!_menuScreen && takeObject == 1)
			checkObjects();

#ifdef _WIN32_WCE
		if (rightMouseButton)
			if (_menuScreen) {
#else
		if (rightMouseButton == 1 && _menuScreen) {
#endif
			rightMouseButton = 0;
			delay(100);
			if (currentChapter == 2) {
				loadPic(menuBackground, cursorSurface);
				loadPic(menuBackground, backSurface);
			} else {
				loadPic(99, cursorSurface);
				loadPic(99, backSurface);
			}
			setPalette((byte *)&gamePalette);
			_menuScreen = false;
#ifndef _WIN32_WCE
			// FIXME: This call here is in hope that it will catch the rightmouseup event so the
			// next if block won't be executed. This too is not a good coding practice. I've recoded it
			// with a mutual exclusive if block for the menu. I would commit this properly but I cannot test
			// for other (see Desktop) ports right now.
			updateEvents();
#endif
#ifdef _WIN32_WCE
			} else {
#else
		}

		// Do not show the inventory screen in chapter 5, if the right mouse button is clicked
		// while the plug (object 16) is held
		// Fixes bug #2059621 - "DRASCULA: Plug bug"
		if (rightMouseButton == 1 && !_menuScreen &&
			!(currentChapter == 5 && pickedObject == 16)) {
#endif
			rightMouseButton = 0;
			delay(100);
			characterMoved = 0;
			if (trackProtagonist == 2)
				trackProtagonist = 1;
			if (currentChapter == 4) {
				loadPic("icons2.alg", backSurface);
				loadPic("icons2.alg", cursorSurface);
			} else if (currentChapter == 5) {
				loadPic("icons3.alg", backSurface);
				loadPic("icons3.alg", cursorSurface);
			} else if (currentChapter == 6) {
				loadPic("iconsp.alg", backSurface);
				loadPic("iconsp.alg", cursorSurface);
			} else {
				loadPic("icons.alg", backSurface);
				loadPic("icons.alg", cursorSurface);
			}
			_menuScreen = true;
#ifndef _WIN32_WCE
			updateEvents();
#endif
			selectVerb(kVerbNone);
		}

		if (leftMouseButton == 1 && _menuBar) {
			delay(100);
			selectVerbFromBar();
		} else if (leftMouseButton == 1 && takeObject == 0) {
			delay(100);
			if (verify1())
				return true;
		} else if (leftMouseButton == 1 && takeObject == 1) {
			if (verify2())
				return true;
		}

		_menuBar = (mouseY < 24 && !_menuScreen) ? true : false;

		Common::KeyCode key = getScan();
		if (key == Common::KEYCODE_F1 && !_menuScreen) {
			selectVerb(kVerbLook);
		} else if (key == Common::KEYCODE_F2 && !_menuScreen) {
			selectVerb(kVerbPick);
		} else if (key == Common::KEYCODE_F3 && !_menuScreen) {
			selectVerb(kVerbOpen);
		} else if (key == Common::KEYCODE_F4 && !_menuScreen) {
			selectVerb(kVerbClose);
		} else if (key == Common::KEYCODE_F5 && !_menuScreen) {
			selectVerb(kVerbTalk);
		} else if (key == Common::KEYCODE_F6 && !_menuScreen) {
			selectVerb(kVerbMove);
		} else if (key == Common::KEYCODE_F9) {
			volumeControls();
		} else if (key == Common::KEYCODE_F10) {
			if (!saveLoadScreen())
				return true;
		} else if (key == Common::KEYCODE_F8) {
			selectVerb(kVerbNone);
		} else if (key == Common::KEYCODE_v) {
			_subtitlesDisabled = true;
			ConfMan.setBool("subtitles", !_subtitlesDisabled);

			print_abc(_textsys[2], 96, 86);
			updateScreen();
			delay(1410);
		} else if (key == Common::KEYCODE_t) {
			_subtitlesDisabled = false;
			ConfMan.setBool("subtitles", !_subtitlesDisabled);

			print_abc(_textsys[3], 94, 86);
			updateScreen();
			delay(1460);
		} else if (key == Common::KEYCODE_ESCAPE) {
			if (!confirmExit())
				return false;
		} else if (key == Common::KEYCODE_TILDE || key == Common::KEYCODE_BACKQUOTE) {
			_console->attach();
			_console->onFrame();
		} else if (currentChapter == 6 && key == Common::KEYCODE_0 && roomNumber == 61) {
			loadPic("alcbar.alg", bgSurface, 255);
		}

		if (leftMouseButton != 0 || rightMouseButton != 0 || key != 0)
			if (currentChapter != 3)
				framesWithoutAction = 0;

		if (framesWithoutAction == 15000) {
			screenSaver();
			if (currentChapter != 3)
				framesWithoutAction = 0;
		}

		if (currentChapter != 3)
			framesWithoutAction++;
	}

	return false;
}


bool DrasculaEngine::verify1() {
	int l;

	if (_menuScreen)
		removeObject();
	else {
		for (l = 0; l < numRoomObjs; l++) {
			if (mouseX >= x1[l] && mouseY >= y1[l]
					&& mouseX <= x2[l] && mouseY <= y2[l] && doBreak == 0) {
				if (exitRoom(l))
					return true;
				if (doBreak == 1)
					break;
			}
		}

		if (mouseX > curX && mouseY > curY
				&& mouseX < curX + curWidth && mouseY < curY + curHeight)
			doBreak = 1;

		for (l = 0; l < numRoomObjs; l++) {
			if (mouseX > x1[l] && mouseY > y1[l]
					&& mouseX < x2[l] && mouseY < y2[l] && doBreak == 0) {
				roomX = roomObjX[l];
				roomY = roomObjY[l];
				trackFinal = trackObj[l];
				doBreak = 1;
				walkToObject = 1;
				startWalking();
			}
		}

		if (doBreak == 0) {
			roomX = CLIP(mouseX, floorX1, floorX2);
			roomY = CLIP(mouseY, floorY1 + feetHeight, floorY2);
			startWalking();
		}
		doBreak = 0;
	}

	return false;
}
Beispiel #16
0
void CPetRemote::postLoad() {
	reset();
	CRoomItem *room = getRoom();
	if (room)
		enterRoom(room);
}
Beispiel #17
0
int main(int argc, char **argv) {
    char *notice;
    int sockfd, new_fd;
    //int socketNum[MAX_LINK];  //保存客户端连接数
    int user_link[MAX_LINK];

    int userfd[MAX_LINK]; //保存连接客户端的socket描述符号
    char clientName[MAX_LINK][MAX_NAME];

    char line[MAX_BUF];
    char temp[MAX_BUF];

    int userCount, i, j;
    unsigned int cli_len;
    struct sockaddr_in server_addr, client_addr;//网络地址结构体,包括端口号,IP地址
    int port = 9999; //服务器端口号

    int flag;
    char strfd[10]; //将int型的userfd 转化为 字符串

    int length; //标志符,用来判断当前读写是否成功
    fd_set sockset;
    int maxfd = -1; //用来标志当前连接的最大描述符

    char *nam;        //分割字符串得到姓名和密码,临时保存变量
    char *pass;
    char *tokenPtr;
    //char friend[2] ; //保存

    //指定端口号,创建socket
    /*	if (argv[1])
        myport = atoi(argv[1]);
        else
        myport = 9999;
     */
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    /*  if (argv[3])
        server_addr.sin_addr.s_addr = inet_addr(argv[3]);
        else
        server_addr.sin_addr.s_addr = INADDR_ANY;
     */
    //绑定端口号
    if (bind(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr))
            < 0) {
        perror("bind");
        exit(1);
    }
    printf("-------------监听客户的连接------------\n");
    fflush(stdout);
    //监听端口号
    if (listen(sockfd, MAX_LINK) < 0) {
        perror("listen");
        exit(1);
    }
    cli_len = sizeof(client_addr);
    for (i = 0; i < MAX_LINK; ++i) {
        user_link[i] = 0;
        clientName[i][0] = '\0';
    }

    userCount = 0;
    FD_ZERO(&sockset);
    FD_SET(sockfd,&sockset);

    if (maxfd < sockfd + 1) {
        maxfd = sockfd + 1;
    }
    printf("------------启动聊天系统-----------\n");
    fflush(stdout);
    while (1) {

        select(maxfd, &sockset, NULL, NULL, NULL);
        //如果该client已经连接过服务器
        if (FD_ISSET(sockfd,&sockset) && (userCount = clinkNumber(user_link))
                >= 0) {
            new_fd = accept(sockfd, (struct sockaddr*) &client_addr, &cli_len);

            if (new_fd < 0) {
                user_link[userCount] = 0;
                printf("%d连接失败\n", new_fd);
                fflush(stdout);
            } else {

                user_link[userCount] = 1; //标志端口存在
                userfd[userCount] = new_fd;
                FD_SET(new_fd ,&sockset); //加入端口集合
                if (maxfd < (new_fd + 1))
                    maxfd = new_fd + 1;
                printf("\n-------------用户进程描述符号为 %d 连接服务器------------", new_fd);
                fflush(stdout);
            }
        }
        //监听已注册的端口是否有数据发送
        for (i = 0; i < MAX_LINK; ++i) {
            if ((user_link[i] == 1) && (FD_ISSET(userfd[i],&sockset))) {
                length = read(userfd[i], line, MAX_BUF);
                if (length == 0) //client的 socket已经关闭
                {
                    /*注销已经关闭的端口*/
                    printf("\n-------------%s 已经注销-----------\n", clientName[i]);
                    fflush(stdout);
                    user_link[i] = 0;
                    clientName[i][0] = '\0';
                    FD_CLR(userfd[i],&sockset);
                } else if (length > 0) {
                    strcpy(temp, line);
                    line[length] = '\0';
                    temp[length] = '\0';
                    /*根据不同的字符串提示头进行操作*/

                    /*注册*/
                    if (line[0] == '@') {
                        nam = strtok(temp, "@");
                        pass = strtok(NULL, "@");
                        flag = regist(nam, pass);
                        if (flag == 1) {
                            printf("\n注册成功\n ");
                            fflush(stdout);
                            notice = "1";
                            write(userfd[i], notice, strlen(notice));
                        } else {
                            printf("\n注册失败\n ");
                            fflush(stdout);
                            notice = "0";
                            write(userfd[i], notice, strlen(notice));
                        }
                    }

                    /*登录*/
                    else if (line[0] == '/') {
                        nam = strtok(temp, "/");
                        pass = strtok(NULL, "/");

                        /*检测用户名和密码是否正确*/

                        flag = login(nam, pass);

                        if (flag == 1) {
                            printf("\n在文件夹中找到\n ");
                            fflush(stdout);
                            notice = "1";
                            write(userfd[i], notice, strlen(notice));
                        } else {
                            printf("\n用户名或者密码无法在文件中找到\n ");
                            fflush(stdout);
                            notice = "0";
                            write(userfd[i], notice, strlen(notice));
                        }
                        //第一次进入聊天室&& (clientName[i][0] =="\0")
                        if ((line[0] == '/')) {
                            strcpy(clientName[i], nam);
                            enterRoom(line, clientName[i]);
                        }
                        //播放XX进入聊天室,进行广播
                        if(flag==1){
                            printf("%s\n", line);
                            fflush(stdout);
                        }
                        for (j = 0; j < MAX_LINK && (flag == 1); ++j) {
                            if ((j != i) && (user_link[j] == 1)) {
                                write(userfd[j], line, strlen(line));
                            }
                        }
                    }/*私聊*/
                    else if (line[0] == '#') {
                        /*若以2个##开始的字串,表示开始进行私聊*/
                        if (line[1] == '#') {
                            tokenPtr = strtok(temp, "/");
                            tokenPtr = strtok(NULL, "/");
                            strcpy(temp, tokenPtr);
                            printf("%s\n", temp);
                            fflush(stdout);
                            strfd[0] = line[2];
                            strfd[1] = '\0';
                            write(atoi(strfd), temp, strlen(temp));
                        }
                        /*只有一个#开始的字符串,表示第一次连接私聊
                          判断当前活动用户数是否为0
                         */
                        else {
                            bzero(temp, MAX_BUF);
                            if (userCount < 1) {
                                write(userfd[i], "0", strlen("0"));
                            } else {
                                for (j = 0; j < MAX_LINK; ++j) {
                                    if ((j != i) && (user_link[j] == 1)) {
                                        sprintf(strfd, "%d", userfd[j]);
                                        strcat(temp, strfd);
                                        strcat(temp, "-->");
                                        strcat(temp, clientName[j]);
                                        strcat(temp, "\n");
                                    }
                                }
                                write(userfd[i], temp, strlen(temp));
                            }
                        }
                    }
                    /*版面聊天*/
                    else {
                        printf("%s\n",line);
                        fflush(stdout);
                        for (j = 0; j < MAX_LINK; ++j) {
                            if ((j != i) && (user_link[j] == 1)) {
                                write(userfd[j], line, strlen(line));
                            }
                        }
                    }
                }
            }
        }//結束数据传送的监听
        //重置sockset集合
        FD_ZERO(&sockset);
        FD_SET(sockfd,&sockset);
        for (i = 0; i < MAX_LINK; ++i) {
            if (user_link[i] == 1) {
                FD_SET(userfd[i],&sockset);
            }
        }
    }
    close(sockfd);
    return 0;
}