File: player.cpp

package info (click to toggle)
teg 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,036 kB
  • sloc: cpp: 16,819; xml: 1,313; makefile: 268; sh: 195; ansic: 112
file content (111 lines) | stat: -rw-r--r-- 2,260 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2000 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <rquesada@core-sdi.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; only version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/*
 * client side player functions
 */

#include "player.h"

#include <list>
#include <algorithm>
#include <cassert>
#include <string.h>

#include "client.h"

namespace teg::client
{

using ClientPlayerList = std::list<Player>;

ClientPlayerList players;

TEG_STATUS player_whois(int numjug, PCPLAYER *j)
{
	TEG_STATUS result = TEG_STATUS_PLAYERNOTFOUND;
	players_map_int([&result, &j, numjug](Player& player) {
		if((result != TEG_STATUS_SUCCESS) && (player.numjug == numjug)) {
			*j = &player;
			result = TEG_STATUS_SUCCESS;
			return false;
		}
		return true;
	});
	return result;
}

TEG_STATUS player_update(PCPLAYER j)
{
	PCPLAYER i{nullptr};

	if(player_whois(j->numjug, &i) != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_PLAYERNOTFOUND;
	}

	*i = *j;
	return TEG_STATUS_SUCCESS;
}

void player_ins(PCPLAYER j)
{
	players.push_back(*j);
	g_game.playeres = players.size();
}

void player_del(PCPLAYER pJ)
{
	assert(players.size() > 0);

	auto it = std::find_if(players.begin(), players.end(),
	[pJ](Player &p) {
		return &p == pJ;
	});
	assert(it!=players.end());

	players.erase(it);

	g_game.playeres = players.size();
}

void player_flush(void)
{
	players.clear();
	g_game.playeres = players.size();
}

void player_init(void)
{
}

void players_map(PlayersCallback cb)
{
	std::for_each(players.begin(), players.end(), cb);
}

void players_map_int(InterruptablePlayersCallback cb)
{
	for(Player& p: players) {
		if(!cb(p)) {
			return;
		}
	}
}

}