Exemplo n.º 1
0
//
// interrupt an event involving the given room, object, or mobile
PyObject *PyEvent_interrupt_event(PyObject *self, PyObject *args) {
  PyObject *PyOwner = NULL; // the python representation of our owner

  // parse our owner
  if(!PyArg_ParseTuple(args, "O", &PyOwner)) {
    PyErr_Format(PyExc_TypeError,
		 "A target must be specified for interrupting events.");
    return NULL;
  }

  // check the possibilities
  if(PyOwner == Py_None)
    interrupt_events_involving(NULL);
  else if(PyChar_Check(PyOwner))
    interrupt_events_involving(PyChar_AsChar(PyOwner));
  else if(PyRoom_Check(PyOwner))
    interrupt_events_involving(PyRoom_AsRoom(PyOwner));
  else if(PyObj_Check(PyOwner))
    interrupt_events_involving(PyObj_AsObj(PyOwner));

  // everything ok
  return Py_BuildValue("i", 1);
}
Exemplo n.º 2
0
//*****************************************************************************
// methods for the Account class
//*****************************************************************************
PyObject *PyAccount_add_char(PyAccount *self, PyObject *args) {
  PyObject    *val = NULL;
  CHAR_DATA    *ch = NULL;
  const char *name = NULL;

  if(!PyArg_ParseTuple(args, "O", &val)) {
    PyErr_Format(PyExc_TypeError,
		 "add_char() must be supplied with a character or name.");
    return NULL;
  }

  // make sure we exist
  ACCOUNT_DATA *acc = PyAccount_AsAccount((PyObject *)self);
  if(acc == NULL) {
    PyErr_Format(PyExc_StandardError,
		 "Tried to add character to a nonexistant account.");
    return NULL;
  }

  if(PyString_Check(val))
    name = PyString_AsString(val);
  else if(PyChar_Check(val)) {
    ch = PyChar_AsChar(val);
    if(ch != NULL)
      name = charGetName(ch);
    else {
      PyErr_Format(PyExc_StandardError,
		   "Tried to add nonexistant character %d to account.",
		   PyChar_AsUid(val));
      return NULL;
    }
  }

  accountPutChar(acc, name);
  listSortWith(accountGetChars(acc), strcasecmp);
  return Py_BuildValue("i", 1);
}
Exemplo n.º 3
0
//
// no point in reinventing the wheel; start_event and start_update both
// need to be handled exactly the same, so here's a function to do just that
PyObject *PyEvent_start(PyObject *self, PyObject *args, void *func) {
  void (* start_func)(void *, int, void *, void *, void *, const char *) = func;
  PyObject *PyOwner = NULL;    // the python representation of our owner
  PyObject   *efunc = NULL;    // the event function
  PyObject   *edata = Py_None; // the event data
  char         *arg = NULL;    // the arg we will be supplying to the function
  double      delay = 0;       // how long the event delay is (in seconds)
  void       *owner = NULL;    // actual owner supplied to the event handler
  char    otype[20];           // is the owner a char, room, obj, or None?

  // try to parse all of our values
  if(!PyArg_ParseTuple(args, "OdO|Os", &PyOwner, &delay, &efunc, &edata, &arg)){
    //PyErr_Format(PyExc_TypeError, 
    //"Invalid arguments provided to event handler");
    return NULL;
  }

  // make sure our event is a function
  if(!PyFunction_Check(efunc)) {
    PyErr_Format(PyExc_TypeError,
		 "The event handler supplied must be a python function");
    return NULL;
  }

  // figure out what type of data our owner is
  if(PyOwner == Py_None) {
    owner = NULL;
    sprintf(otype, "none");
  }
  else if(PyChar_Check(PyOwner)) {
    owner = PyChar_AsChar(PyOwner);
    sprintf(otype, "char");
  }
  else if(PyRoom_Check(PyOwner)) {
    owner = PyRoom_AsRoom(PyOwner);
    sprintf(otype, "room");
  }
  else if(PyObj_Check(PyOwner)) {
    owner = PyObj_AsObj(PyOwner);
    sprintf(otype, "obj");
  }
  // invalid type
  else {
    PyErr_Format(PyExc_TypeError,
		 "An event owner must be a room, char, obj, or none");
    return NULL;
  }

  // make sure the owner exists
  if(PyOwner != Py_None && owner == NULL) {
    PyErr_Format(PyExc_StandardError, "Owner supplied does not exist in game");
    return NULL;
  }
  
  // now, queue up the action
  start_func(owner, (int)(delay SECONDS), PyEvent_on_complete, NULL, 
	     Py_BuildValue("sOO", otype, efunc, edata), arg);

  // everything seems ok. exit normally
  return Py_BuildValue("i", 1);
}
Exemplo n.º 4
0
PyObject *py_gen_do_trigs(PyObject *self, PyObject *args, PyObject *kwds) {
  static char *kwlist[] ={ "type","ch","obj","room","exit","cmd","arg","opts",NULL };
  char       *type = NULL;
  char        *cmd = NULL;
  char        *arg = NULL;
  PyObject   *pych = NULL;
  PyObject  *pyobj = NULL;
  PyObject *pyroom = NULL;
  PyObject *pyexit = NULL;
  PyObject *pyopts = NULL;
  LIST       *opts = NULL;
  CHAR_DATA    *ch = NULL;
  OBJ_DATA    *obj = NULL;
  ROOM_DATA  *room = NULL;
  EXIT_DATA  *exit = NULL;
  void         *me = NULL;
  int      me_type = -1;
  bool        fail = FALSE;

  // first, parse all of our arguments
  if(!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOOssO", kwlist, 
				  &type, &pych, &pyobj, &pyroom, &pyexit,
				  &cmd, &arg, &pyopts)) {
    PyErr_Format(PyExc_TypeError, "do_trigs supplied invalid arguments.");
    return NULL;
  }

  // make sure we exist, and are of a valid type
  if(PyChar_Check(self)) {
    me = PyChar_AsChar(self);
    me_type = TRIGVAR_CHAR;
  }
  else if(PyObj_Check(self)) {
    me = PyObj_AsObj(self);
    me_type = TRIGVAR_OBJ;
  }
  else if(PyRoom_Check(self)) {
    me = PyRoom_AsRoom(self);
    me_type = TRIGVAR_ROOM;
  }

  // did we find a character?
  if(me == NULL) {
    PyErr_Format(PyExc_TypeError,"do_trigs owner does not exist.");
    return NULL;
  }

  // make sure ch is of the specified type
  if(pych!=NULL && (!PyChar_Check(pych) || (ch=PyChar_AsChar(pych)) == NULL)){
    PyErr_Format(PyExc_TypeError,"do_trigs expects ch to be character.");
    return NULL;
  }

  // make sure obj is of the specified type
  if(pyobj!=NULL && (!PyObj_Check(pyobj) || (obj=PyObj_AsObj(pyobj)) == NULL)){
    PyErr_Format(PyExc_TypeError,"do_trigs expects obj to be object.");
    return NULL;
  }

  // make sure room is of the specified type
  if(pyroom!=NULL&&(!PyRoom_Check(pyroom)||(room=PyRoom_AsRoom(pyroom))==NULL)){
    PyErr_Format(PyExc_TypeError,"do_trigs expects room to be room.");
    return NULL;
  }

  // make sure exit is of the specified type
  if(pyexit!=NULL&&(!PyExit_Check(pyexit)||(exit=PyExit_AsExit(pyexit))==NULL)){
    PyErr_Format(PyExc_TypeError,"do_trigs expects exit to be an exit.");
    return NULL;
  }

  // parse opts
  if(pyopts != NULL && !PyDict_Check(pyopts)) {
    PyErr_Format(PyExc_TypeError,"do_trigs expects opts to be a dict.");
    return NULL;
  }
  else if(pyopts != NULL) {
    PyObject  *pairs = PyDict_Items(pyopts);
    int            i = 0;
    opts             = newList();
    // go through each pair and add it to the pyopts list
    for(; i < PyList_Size(pairs); i++) {
      PyObject  *pair = PyList_GetItem(pairs, i);
      PyObject *pykey = PyTuple_GetItem(pair, 0);
      PyObject *pyval = PyTuple_GetItem(pair, 1);
      OPT_VAR    *opt = NULL;
      char       *key = NULL;

      // make sure the key is a string
      if(PyString_Check(pykey))
	key = PyString_AsString(pykey);
      else {
	PyErr_Format(PyExc_TypeError, "do_trigs opt keys must be strings.");
	fail = TRUE;
	break;
      }

      // check to make sure the val is a valid type
      if(PyChar_Check(pyval)) {
	CHAR_DATA *val = PyChar_AsChar(pyval);
	if(val != NULL)
	  opt = newOptVar(key, val, TRIGVAR_CHAR);
	else {
	  PyErr_Format(PyExc_TypeError,"%s opt provided nonexistent char.",key);
	  fail = TRUE;
	  break;
	}
      }
      else if(PyObj_Check(pyval)) {
	OBJ_DATA *val = PyObj_AsObj(pyval);
	if(val != NULL)
	  opt = newOptVar(key, val, TRIGVAR_OBJ);
	else {
	  PyErr_Format(PyExc_TypeError,"%s opt provided nonexistent obj.", key);
	  fail = TRUE;
	  break;
	}
      }
      else if(PyRoom_Check(pyval)) {
	ROOM_DATA *val = PyRoom_AsRoom(pyval);
	if(val != NULL)
	  opt = newOptVar(key, val, TRIGVAR_ROOM);
	else {
	  PyErr_Format(PyExc_TypeError,"%s opt provided nonexistent room.",key);
	  fail = TRUE;
	  break;
	}
      }
      else {
	PyErr_Format(PyExc_TypeError,"%s opt provided invalid value.",key);
	fail = TRUE;
	break;
      }

      // append the opt to the opt list
      listPut(opts, opt);
    }
    Py_DECREF(pairs);
  }

  // did everything succeed?
  if(fail == FALSE)
    gen_do_trigs(me,me_type,type,ch,obj,room,exit,cmd,arg,opts);

  // garbage collection
  if(opts != NULL)
    deleteListWith(opts, deleteOptVar);

  if(fail == TRUE)
    return NULL;
  return Py_BuildValue("");
}