project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/RoomList.java
author Medo <smaxein@googlemail.com>
Thu, 02 Aug 2012 16:36:11 +0200
changeset 7461 38acbfdb484f
parent 7358 57a508884052
child 7476 2fb781bbdd51
permissions -rw-r--r--
Hedgeroid: Started to implement RoomActivity
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
package org.hedgewars.hedgeroid.netplay;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
     3
import java.util.Collections;
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
     4
import java.util.Map;
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
     5
import java.util.TreeMap;
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
     6
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
     7
import android.database.DataSetObservable;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
import android.util.Log;
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
     9
import android.util.Pair;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    11
public class Roomlist extends DataSetObservable {
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
	private long nextId = 1;
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    13
	private Map<String, Pair<Room, Long>> rooms = new TreeMap<String, Pair<Room, Long>>();
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    14
	
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    15
	public void updateList(Room[] newRooms) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    16
		Map<String, Pair<Room, Long>> newMap = new TreeMap<String, Pair<Room, Long>>();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    17
		for(Room room : newRooms) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    18
			Pair<Room, Long> oldEntry = rooms.get(room.name);
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    19
			if(oldEntry == null) {
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    20
				newMap.put(room.name, Pair.create(room, nextId++));
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    21
			} else {
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    22
				newMap.put(room.name, Pair.create(room, oldEntry.second));
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    23
			}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    24
		}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    25
		rooms = newMap;
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    26
		notifyChanged();
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    27
	}
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
	
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    29
	public void addRoomWithNewId(Room room) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    30
		rooms.put(room.name, Pair.create(room, nextId++));
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    31
		notifyChanged();
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
	}
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
	
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    34
	public void updateRoom(String name, Room room) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    35
		Pair<Room, Long> oldEntry = rooms.get(name);
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
		if(oldEntry == null) {
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
			Log.e("RoomList", "Received update for unknown room: "+name);
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    38
			rooms.put(room.name, Pair.create(room, nextId++));
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
		} else {
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    40
			if(!name.equals(room.name)) {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    41
				rooms.remove(name);
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
			}
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    43
			rooms.put(room.name, Pair.create(room, oldEntry.second));
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    44
		}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    45
		notifyChanged();
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    46
	}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    47
	
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    48
	public void removeRoom(String name) {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    49
		if(rooms.remove(name) != null) {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    50
			notifyChanged();
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
		}
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
	}
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    53
	
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    54
	public void clear() {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    55
		if(!rooms.isEmpty()) {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    56
			rooms.clear();
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    57
			notifyChanged();
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    58
		}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    59
	}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    60
	
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    61
	public Map<String, Pair<Room, Long>> getMap() {
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    62
		return Collections.unmodifiableMap(rooms);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    63
	}
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
}