Crossfire Server, Trunk  R20513
cfpython_party.c
Go to the documentation of this file.
1 /*****************************************************************************/
2 /* CFPython - A Python module for Crossfire RPG. */
3 /* Version: 2.0beta8 (also known as "Alexander") */
4 /* Contact: yann.chachkoff@myrealbox.com */
5 /*****************************************************************************/
6 /* That code is placed under the GNU General Public Licence (GPL) */
7 /* (C)2001-2005 by Chachkoff Yann (Feel free to deliver your complaints) */
8 /*****************************************************************************/
9 /* CrossFire, A Multiplayer game for X-windows */
10 /* */
11 /* Copyright (C) 2000 Mark Wedel */
12 /* Copyright (C) 1992 Frank Tore Johansen */
13 /* */
14 /* This program is free software; you can redistribute it and/or modify */
15 /* it under the terms of the GNU General Public License as published by */
16 /* the Free Software Foundation; either version 2 of the License, or */
17 /* (at your option) any later version. */
18 /* */
19 /* This program is distributed in the hope that it will be useful, */
20 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
21 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
22 /* GNU General Public License for more details. */
23 /* */
24 /* You should have received a copy of the GNU General Public License */
25 /* along with this program; if not, write to the Free Software */
26 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 /* */
28 /*****************************************************************************/
29 
30 #include <cfpython.h>
31 
32 static PyObject *Crossfire_Party_GetName(Crossfire_Party *partyptr, void *closure) {
33  return Py_BuildValue("s", cf_party_get_name(partyptr->party));
34 }
35 
36 static PyObject *Crossfire_Party_GetPassword(Crossfire_Party *partyptr, void *closure) {
37  return Py_BuildValue("s", cf_party_get_password(partyptr->party));
38 }
39 
40 static PyObject *Crossfire_Party_GetNext(Crossfire_Party *party, void *closure) {
42 }
43 
44 static PyObject *Crossfire_Party_GetPlayers(Crossfire_Party *party, PyObject *args) {
45  PyObject *list;
46  player *pl;
47 
48  list = PyList_New(0);
49  pl = cf_party_get_first_player(party->party);
50  while (pl) {
51  PyList_Append(list, Crossfire_Object_wrap(pl->ob));
52  pl = cf_party_get_next_player(party->party, pl);
53  }
54  return list;
55 }
56 
57 PyObject *Crossfire_Party_wrap(partylist *what) {
58  Crossfire_Party *wrapper;
59 
60  /* return None if no object was to be wrapped */
61  if (what == NULL) {
62  Py_INCREF(Py_None);
63  return Py_None;
64  }
65 
66  wrapper = PyObject_NEW(Crossfire_Party, &Crossfire_PartyType);
67  if (wrapper != NULL)
68  wrapper->party = what;
69  return (PyObject *)wrapper;
70 }
71 
73  return (left->party < right->party ? -1 : (left->party == right->party ? 0 : 1));
74 }
75 
76 static PyObject *Crossfire_Party_RichCompare(Crossfire_Party *left, Crossfire_Party *right, int op) {
77  int result;
78  if (!left
79  || !right
80  || !PyObject_TypeCheck((PyObject*)left, &Crossfire_PartyType)
81  || !PyObject_TypeCheck((PyObject*)right, &Crossfire_PartyType)) {
82  Py_INCREF(Py_NotImplemented);
83  return Py_NotImplemented;
84  }
85  result = Crossfire_Party_InternalCompare(left, right);
86  /* Based on how Python 3.0 (GPL compatible) implements it for internal types: */
87  switch (op) {
88  case Py_EQ:
89  result = (result == 0);
90  break;
91  case Py_NE:
92  result = (result != 0);
93  break;
94  case Py_LE:
95  result = (result <= 0);
96  break;
97  case Py_GE:
98  result = (result >= 0);
99  break;
100  case Py_LT:
101  result = (result == -1);
102  break;
103  case Py_GT:
104  result = (result == 1);
105  break;
106  }
107  return PyBool_FromLong(result);
108 }
109 
110 static PyGetSetDef Party_getseters[] = {
111  { "Name", (getter)Crossfire_Party_GetName, NULL, NULL, NULL },
112  { "Password", (getter)Crossfire_Party_GetPassword, NULL, NULL, NULL },
113  { "Next", (getter)Crossfire_Party_GetNext, NULL, NULL, NULL },
114  { NULL, NULL, NULL, NULL, NULL }
115 };
116 
117 static PyMethodDef PartyMethods[] = {
118  { "GetPlayers", (PyCFunction)Crossfire_Party_GetPlayers, METH_NOARGS, NULL },
119  { NULL, NULL, 0, NULL }
120 };
121 
122 /* Our actual Python ArchetypeType */
123 PyTypeObject Crossfire_PartyType = {
124 #ifdef IS_PY3K
125  /* See http://bugs.python.org/issue4385 */
126  PyVarObject_HEAD_INIT(NULL, 0)
127 #else
128  PyObject_HEAD_INIT(NULL)
129  0, /* ob_size*/
130 #endif
131  "Crossfire.Party", /* tp_name*/
132  sizeof(Crossfire_Party), /* tp_basicsize*/
133  0, /* tp_itemsize*/
134  NULL, /* tp_dealloc*/
135  NULL, /* tp_print*/
136  NULL, /* tp_getattr*/
137  NULL, /* tp_setattr*/
138 #ifdef IS_PY3K
139  NULL, /* tp_reserved */
140 #else
141  (cmpfunc)Crossfire_Party_InternalCompare, /* tp_compare*/
142 #endif
143  NULL, /* tp_repr*/
144  NULL, /* tp_as_number*/
145  NULL, /* tp_as_sequence*/
146  NULL, /* tp_as_mapping*/
147  PyObject_HashNotImplemented, /* tp_hash */
148  NULL, /* tp_call*/
149  NULL, /* tp_str*/
150  PyObject_GenericGetAttr, /* tp_getattro*/
151  PyObject_GenericSetAttr, /* tp_setattro*/
152  NULL, /* tp_as_buffer*/
153  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags*/
154  "Crossfire parties", /* tp_doc */
155  NULL, /* tp_traverse */
156  NULL, /* tp_clear */
157  (richcmpfunc)Crossfire_Party_RichCompare, /* tp_richcompare */
158  0, /* tp_weaklistoffset */
159  NULL, /* tp_iter */
160  NULL, /* tp_iternext */
161  PartyMethods, /* tp_methods */
162  NULL, /* tp_members */
163  Party_getseters, /* tp_getset */
164  NULL, /* tp_base */
165  NULL, /* tp_dict */
166  NULL, /* tp_descr_get */
167  NULL, /* tp_descr_set */
168  0, /* tp_dictoffset */
169  NULL, /* tp_init */
170  NULL, /* tp_alloc */
171  NULL, /* tp_new */
172  NULL, /* tp_free */
173  NULL, /* tp_is_gc */
174  NULL, /* tp_bases */
175  NULL, /* tp_mro */
176  NULL, /* tp_cache */
177  NULL, /* tp_subclasses */
178  NULL, /* tp_weaklist */
179  NULL, /* tp_del */
180 };
static PyGetSetDef Party_getseters[]
One player.
Definition: player.h:92
PyObject * Crossfire_Party_wrap(partylist *what)
PyObject_HEAD partylist * party
One party.
Definition: party.h:10
static PyObject * Crossfire_Party_GetNext(Crossfire_Party *party, void *closure)
const char * cf_party_get_name(partylist *party)
static PyObject * Crossfire_Party_GetPassword(Crossfire_Party *partyptr, void *closure)
PyTypeObject Crossfire_PartyType
static PyObject * Crossfire_Party_GetPlayers(Crossfire_Party *party, PyObject *args)
partylist * cf_party_get_next(partylist *party)
Get next party in party list.
#define PyObject_HashNotImplemented
Definition: cfpython.h:73
static PyObject * Crossfire_Party_GetName(Crossfire_Party *partyptr, void *closure)
const char * cf_party_get_password(partylist *party)
Get party&#39;s password.
static PyMethodDef PartyMethods[]
PyObject * Crossfire_Object_wrap(object *what)
Python initialized.
static PyObject * Crossfire_Party_RichCompare(Crossfire_Party *left, Crossfire_Party *right, int op)
object * ob
The object representing the player.
Definition: player.h:158
player * cf_party_get_next_player(partylist *party, player *op)
Get next player in party.
player * cf_party_get_first_player(partylist *party)
Get first player in party.
static int Crossfire_Party_InternalCompare(Crossfire_Party *left, Crossfire_Party *right)