public int openRoom(gameRoom gr) { try { deleteOldHostRoomsIfAny(gr.host_ID); } catch (Exception) { } string table = "game_room"; string columns = "name, level, numOfPlays, max_players, host_id, host_ip, game_state"; string values = "'" + gr.name + "', '" + gr.level.ToString() + "', '" + gr.numOfPlays.ToString() + "', '" + gr.maxPlayers.ToString() + "', '" + gr.host_ID.ToString() + "', '" + gr.host_IP + "', '" + ((int)gr.gameState).ToString() + "'"; if (dbc.add(table, columns, values)) { //get room id string query = "SELECT id FROM game_room WHERE host_id='" + gr.host_ID.ToString() + "'"; var reader = dbc.retreiveDate(query); if (reader != null) { reader.Read(); int roomID = reader.GetInt32("id"); reader.Close(); _gameRoom = new gameRoom(roomID); return(roomID); } } return(0); }
public List <gameRoom> listAllRooms() { List <gameRoom> allGameRooms = new List <gameRoom>(); string query = "SELECT `id`, `roomName`, `level`, `numOfPlays`, `max_players`, `host_id`, `host_ip`, `game_state`, `hostName` FROM (SELECT `id`, `name` AS 'roomName', `level`, `numOfPlays`, `max_players`, `host_id`, `host_ip`, `game_state` FROM game_room) AS t1 JOIN (SELECT id AS 'hostId', name AS 'hostName' FROM user) AS t2 ON t1.host_id = t2.hostId"; if (dbc == null) { dbc = DBController.Instance; } var roomsReader = dbc.retreiveDate(query); if (roomsReader != null) { while (roomsReader.Read()) { gameRoom gr = new gameRoom(); gr.id = roomsReader.GetInt32("id"); gr.name = roomsReader.GetString("roomName"); gr.level = roomsReader.GetInt32("level"); gr.maxPlayers = roomsReader.GetInt32("max_players"); gr.host_ID = roomsReader.GetInt32("host_id"); gr.host_IP = roomsReader.GetString("host_ip"); gr.gameState = (GAME_STATE)roomsReader["game_state"]; gr.hostName = roomsReader.GetString("hostName"); gr.numOfPlays = roomsReader.GetInt32("numOfPlays"); gr.numOfPlayers = dbc.countRows("room_players", "player_id", "room_id", gr.id.ToString()); allGameRooms.Add(gr); } roomsReader.Close(); } return(allGameRooms); }
public bool joinRoom(int roomID, int playerID, string playerIP) { //if state wait or ready and maxplayers not reached lock (ISurrender_syncLock) { lock (joinRoom_syncLock) { _gameRoom = new gameRoom(roomID); if (_gameRoom.numOfPlayers < _gameRoom.maxPlayers && (_gameRoom.gameState == GAME_STATE.Wait || _gameRoom.gameState == GAME_STATE.Ready)) { if (dbc.add("room_players", "player_id, player_ip, room_id", "'" + playerID.ToString() + "', '" + playerIP + "', '" + roomID.ToString() + "'")) { _gameRoom.numOfPlayers++; return(true); } } return(false); } } }
public int openRoom(gameRoom gr) { return(gc.openRoom(gr)); }