Commit 5d31f314 authored by Colomban Wendling's avatar Colomban Wendling
Browse files

OVCCServer: better tables handling

Now the server creates tables on demand until it reaches the maximum
open tables count; and cleans up old tables that aren't used anymore.
parent a59bd821
......@@ -30,12 +30,53 @@ public class Server: ThreadedSocketService
{
private List<Client> clients = null;
private List<Game> tables = null;
private TilesDef tiles = new TilesDef ();
private TileSet tileset = new TileSet ();
public string name { get; construct; }
public uint port { get; construct; default = 0xdead; }
public string name { get; construct; }
public uint port { get; construct; default = 0xdead; }
public uint max_tables { get; set; default = 1; }
public signal void stopped ();
/* removes old tables */
private void cleanup_tables ()
{
lock (tables) {
foreach (var game in tables) {
if (game.state != GameState.STOPPED && game.n_players < 1) {
tables.remove (game);
debug ("Cleaned up table %p", game);
}
}
}
}
/* get the most populated open table available, maybe creating one */
private Game? pick_open_table ()
{
Game? candidate = null;
uint n_open_tables = 0;
lock (tables) {
foreach (var game in tables) {
if (game.state == GameState.STOPPED &&
(candidate == null || candidate.n_players < game.n_players)) {
candidate = game;
}
n_open_tables++;
}
/* if there is no open tables but room for more, create a new one */
if (candidate == null && n_open_tables < max_tables) {
candidate = new Game (tileset, null);
tables.prepend (candidate);
debug ("Added table %p", candidate);
}
}
return candidate;
}
private bool send_all (Message message,
Client? exclude = null,
Cancellable? cancel = null)
......@@ -92,7 +133,16 @@ public class Server: ThreadedSocketService
private bool handle_join (JoinMessage msg, Client client)
{
client.join(tables.first().data);
Game? table = pick_open_table ();
if (table == null) {
warning ("A player tried to join, but there is no open table. " +
"We should tell her, but we just do nothing yet. " +
"She's pretty much fucked.");
return false;
}
client.join (table);
debug ("Sending game data");
var data = new GamedataMessage (client.game.get_stack_ids(),
client.game.get_player_nicks(),
......@@ -161,6 +211,7 @@ public class Server: ThreadedSocketService
this.clients.remove (client);
}
try { client.disconnect (); } catch { /* we don't care if disconnection failed */ }
cleanup_tables ();
debug ("Closed connection");
return true;
}
......@@ -181,13 +232,8 @@ public class Server: ThreadedSocketService
{
Object (name: name, port: port);
TilesDef tiles = new TilesDef ();
TileSet tileset = new TileSet ();
tiles.load (File.new_for_path (TILES_FILE));
tileset.load (tiles, File.new_for_path (TILESET_FILE));
Game table = new Game (tileset, null);
this.tables.prepend (table);
}
/* a bit ugly since a direct call to parent method will not trigger the
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment