Commit b129490a authored by Jonathan Michalon's avatar Jonathan Michalon

Add a user-friendly name to Game and be able to join/create a game with it

parent 3efa37a2
......@@ -177,7 +177,7 @@ ovccgtk_client_login_ready (GObject *object,
g_object_unref (task);
} else {
g_debug ("ovccclient_client_join_game()");
ovccclient_client_join_game (OVCCCLIENT_CLIENT (object), -1,
ovccclient_client_join_game (OVCCCLIENT_CLIENT (object), -1, NULL,
g_task_get_cancellable (task),
ovccgtk_client_join_game_ready, task);
}
......
......@@ -25,8 +25,9 @@ public class OVCC.Network.JoinMessage : VariantMessage
{
public override MessageType message_type { get { return MessageType.JOIN; } }
public int game_index { get; set; default = -1; }
public State status { get; set; default = State.QUERY; }
public int game_index { get; set; default = -1; }
public string? name { get; set; default = null; }
public State status { get; set; default = State.QUERY; }
public enum State
{
......@@ -36,21 +37,23 @@ public class OVCC.Network.JoinMessage : VariantMessage
}
public JoinMessage (int idx)
public JoinMessage (int idx, string? name = null)
{
Object (game_index: idx);
Object (game_index: idx, name: name);
}
protected override Variant build_variant ()
{
return new Variant ("(ii)", status, game_index);
return new Variant ("(iims)", status, game_index, name);
}
protected override void parse_variant (uint8[] data)
{
State s;
int idx;
parse_data (data, "(ii)", out s, out idx);
string? n;
parse_data (data, "(iims)", out s, out idx, out n);
status = s;
game_index = idx;
name = n;
}
}
......@@ -108,6 +108,7 @@ namespace OVCC
public Board board { get; construct; }
public unowned SList<Player> players { get { return this._players; } }
public Player current_player { get; private set; }
public string name { get; construct; }
public unowned Tile current_tile {
get { return stack.peek(); }
}
......@@ -124,7 +125,7 @@ namespace OVCC
public signal void turn_finished ();
public Game (TileSet ts, Stack? s = null)
public Game (TileSet ts, Stack? s = null, string? name = null)
{
var our_stack = s;
if (ts.first == null) {
......@@ -133,7 +134,12 @@ namespace OVCC
if (s == null) {
our_stack = new Stack.from_tileset (ts);
}
Object (stack: our_stack, board: new Board (ts.first.dup ()));
if (name == null) {
name = "Unnamed Game";
}
Object (stack: our_stack, board: new Board (ts.first.dup ()), name: name);
stack.item_removed.connect (t => {
this.notify_property ("current-tile");
......@@ -552,8 +558,7 @@ namespace OVCC
*/
public GameDescription describe ()
{
/* FIXME game name */
return new GameDescription ("Unnamed Game", state, get_player_nicks());
return new GameDescription (name, state, get_player_nicks());
}
}
}
......@@ -156,10 +156,11 @@ namespace OVCCClient
* @return The joined Game
*/
public async Game join_game (int index,
string? name = null,
Cancellable? cancellable = null)
throws ServerError, IOError
{
game = yield server.join_game (index, player, cancellable);
game = yield server.join_game (index, player, name, cancellable);
sigqueue.add (game, game.notify["current-player"].connect (() => {
debug ("Current player changed to \"%s\"", game.current_player.nick);
......
......@@ -479,6 +479,7 @@ namespace OVCCClient
*/
public async Game join_game (int index,
Player player,
string? name = null,
Cancellable? cancellable = null)
throws ServerError, IOError
requires (connection != null)
......@@ -487,7 +488,7 @@ namespace OVCCClient
TileSet tileset = new TileSet ();
/* call the server */
yield send_message (new JoinMessage (index), cancellable);
yield send_message (new JoinMessage (index, name), cancellable);
/* wait for answer */
Message msg = yield receive_type (MessageType.JOIN, cancellable);
......
......@@ -55,7 +55,7 @@ public class Server: ThreadedSocketService
}
/* get the most populated open game available, maybe creating one */
private Game? pick_open_game ()
private Game? pick_open_game (string? name = null)
{
Game? candidate = null;
uint n_open_games = 0;
......@@ -63,14 +63,15 @@ public class Server: ThreadedSocketService
lock (games) {
foreach (var game in games) {
if (game.state.is_open () &&
(candidate == null || candidate.players.length() < game.players.length())) {
(candidate == null || candidate.players.length() < game.players.length()) &&
(name == null || game.name == name)) {
candidate = game;
}
n_open_games++;
}
/* if there is no open games but room for more, create a new one */
if (candidate == null && n_open_games < max_games) {
candidate = new Game (tileset, null);
candidate = new Game (tileset, null, name);
games.prepend (candidate);
debug ("Added game %p, which describes as '%s'", candidate, candidate.describe().to_string());
}
......@@ -145,10 +146,10 @@ public class Server: ThreadedSocketService
{
Game? game = null;
debug ("Game Join tentative for game '%d'", msg.game_index);
debug ("Game Join tentative for game '%d/%s'", msg.game_index, msg.name);
if (msg.game_index == -1) {
game = pick_open_game ();
game = pick_open_game (msg.name);
} else {
game = games.nth_data (msg.game_index);
}
......
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