inprogressmenu (int gameid)
{
int i, menuchoice, len = 20, offset = 0, doresizewin = 0;
+ dg_sortmode sortmode = SORTMODE_NONE;
time_t ctime;
struct dg_game **games;
char ttyrecname[130], *replacestr = NULL, gametype[10];
sigset_t oldmask, toblock;
games = populate_games (gameid, &len);
+ games = sort_games (games, len, sortmode);
while (1)
{
(time (&ctime) - games[i + offset]->idle_time) % 60);
}
+ mvprintw (22, 1, "'s' and 'S' change sort mode (current: %s)", SORTMODE_NAME[sortmode]);
+
if (len > 0)
mvprintw (21, 1, "(%d-%d of %d)", offset + 1, offset + i, len);
mvaddstr (23, 1,
case 'q': case 'Q':
return;
+ case 's':
+ if (sortmode < (NUM_SORTMODES-1)) sortmode++; else sortmode = SORTMODE_NONE;
+ break;
+ case 'S':
+ if (sortmode > SORTMODE_NONE) sortmode--; else sortmode = (NUM_SORTMODES-1);
+ break;
+
case 12: case 18: /* ^L, ^R */
clear ();
break;
}
games = populate_games (gameid, &len);
+ games = sort_games (games, len, sortmode);
}
}
int allow_registration; /* allow registering new nicks */
};
+
+/* username asc and idletime desc are most important */
+typedef enum
+{
+ SORTMODE_NONE = 0,
+ SORTMODE_USERNAME_ASC,
+ SORTMODE_IDLETIME_DESC,
+ SORTMODE_IDLETIME_ASC,
+ SORTMODE_USERNAME_DESC,
+ NUM_SORTMODES
+} dg_sortmode;
+
+static const char *SORTMODE_NAME[] = {
+ "Unsorted",
+ "Username, asc",
+ "Idletime, desc",
+ "Idletime, asc",
+ "Username, desc",
+ "",
+};
+
+
/* Global variables */
extern char* config; /* file path */
extern struct dg_config **myconfig;
extern void drawbanner(unsigned int start_line, unsigned int howmany);
extern char *dgl_format_str(int game, struct dg_user *me, char *str);
extern struct dg_game **populate_games(int game, int *l);
+
+extern struct dg_game **sort_games(struct dg_game **games, int len, dg_sortmode sortmode);
+
extern void inprogressmenu(int gameid);
extern void change_email(void);
extern int changepw(int dowrite);
}
+static int
+sort_game_username_asc(const void *g1, const void *g2)
+{
+ const struct dg_game *game1 = *(const struct dg_game **)g1;
+ const struct dg_game *game2 = *(const struct dg_game **)g2;
+ return strcmp(game1->name, game2->name);
+}
+
+static int
+sort_game_username_desc(const void *g1, const void *g2)
+{
+ const struct dg_game *game1 = *(const struct dg_game **)g1;
+ const struct dg_game *game2 = *(const struct dg_game **)g2;
+ return strcmp(game2->name, game1->name);
+}
+
+static int
+sort_game_idletime_asc(const void *g1, const void *g2)
+{
+ const struct dg_game *game1 = *(const struct dg_game **)g1;
+ const struct dg_game *game2 = *(const struct dg_game **)g2;
+ return difftime(game1->idle_time, game2->idle_time);
+}
+
+static int
+sort_game_idletime_desc(const void *g1, const void *g2)
+{
+ const struct dg_game *game1 = *(const struct dg_game **)g1;
+ const struct dg_game *game2 = *(const struct dg_game **)g2;
+ return difftime(game2->idle_time, game1->idle_time);
+}
+
+
+struct dg_game **
+sort_games (struct dg_game **games, int len, dg_sortmode sortmode)
+{
+ switch (sortmode) {
+ case SORTMODE_USERNAME_ASC: qsort(games, len, sizeof(struct dg_game *), sort_game_username_asc); break;
+ case SORTMODE_USERNAME_DESC: qsort(games, len, sizeof(struct dg_game *), sort_game_username_desc); break;
+ case SORTMODE_IDLETIME_ASC: qsort(games, len, sizeof(struct dg_game *), sort_game_idletime_asc); break;
+ case SORTMODE_IDLETIME_DESC: qsort(games, len, sizeof(struct dg_game *), sort_game_idletime_desc); break;
+ default: ;
+ }
+ return games;
+}
+
struct dg_game **
populate_games (int xgame, int *l)
{