⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 avatar.c

📁 chord 源码 http://pdos.csail.mit.edu/chord/
💻 C
字号:
#include <iostream.h>#include <dhashclient.h>#include "avatar.h"avatar::avatar (str n, str p, ref<dhashclient> d, ptr<room> l=NULL) :   mud_obj (n), dhash (d), passwd (p), buf (NULL), location (l){  if (!location)    location = New refcounted <room> (str("First room"), dhash);}avatar::avatar (char *bytes, uint size, ref<dhashclient> d) :   dhash (d), buf (NULL){  uint offst = 0;  uint slen;  bcopy (bytes, &slen, USZ);  offst += USZ;  set_name (bytes + offst, slen);  offst += slen;  bcopy (bytes + offst, &slen, USZ);  offst += USZ;  set_desc (bytes + offst, slen);  offst += slen;  bcopy (bytes + offst, &slen, USZ);  offst += USZ;  passwd.setbuf (bytes + offst, slen);  offst += slen;  uint num_inv;  bcopy (bytes + offst, &num_inv, USZ);  offst += USZ;  uint64 ver;  chordID writer;  for (uint i=0; i<num_inv; i++) {    bcopy (bytes + offst, &ver, USZ);    offst += USZ;    bcopy (bytes + offst, &writer, ID_SIZE);    offst += ID_SIZE;    bcopy (bytes + offst, &slen, USZ);    offst += USZ;    str n (bytes + offst, slen);    ptr<thing> inv = New refcounted<thing> (n, writer, ver);    inventory.push_back (inv);    offst += slen;  }  bcopy (bytes + offst, &slen, USZ);  offst += USZ;  str n (bytes + offst, slen);  location = New refcounted<room> (n, dhash);}char *avatar::bytes (){  if (buf) free (buf);  uint offst = 0;  uint slen;  buf = (char *) malloc (size ());  slen = get_name ().len ();  bcopy (&slen , buf + offst, USZ);  offst += USZ;  bcopy (get_name ().cstr (), buf + offst, slen);  offst += slen;  slen = describe ().len ();  bcopy (&slen, buf + offst, USZ);  offst += USZ;  bcopy (describe ().cstr (), buf + offst, slen);  offst += slen;  slen = passwd.len ();  bcopy (&slen, buf + offst, USZ);  offst += USZ;  bcopy (passwd.cstr (), buf + offst, slen);  offst += slen;  slen = inventory.size ();  bcopy (&slen, buf + offst, USZ);  offst += USZ;  for (uint i=0; i<inventory.size (); i++) {    bcopy (&inventory[i]->ctag.ver, buf + offst, USZ);    offst += USZ;    bcopy (&inventory[i]->ctag.writer, buf + offst, ID_SIZE);    offst += ID_SIZE;    slen = inventory[i]->get_name ().len ();    bcopy (&slen, buf + offst, USZ);    offst += USZ;    bcopy (inventory[i]->get_name ().cstr (), buf + offst, slen);    offst += slen;  }    slen = location->get_name ().len ();  bcopy (&slen, buf + offst, USZ);  offst += USZ;  bcopy (location->get_name ().cstr (), buf + offst, slen);    return buf;}uint avatar::inv_size (){  uint sz = 0;  for (uint i=0; i<inventory.size (); i++)    sz += USZ + inventory[i]->get_name ().len ();  return sz;}uintavatar::size () {  return ( 5*USZ + get_name ().len () + describe ().len () + passwd.len () + 	   inv_size () + location->get_name ().len () );}stravatar::to_str (){  strbuf ret;  ret << "\n"      << "Avatar Name: " << get_name () << "\n"      << "         ID: " << ID () << "\n"      << "Description: " << describe () << "\n"      << "   Password: " << passwd << "\n"      << "  Inventory: " << inventory.size () << " items.\n";  for (uint i=0; i<inventory.size (); i++) {    ret << "      item " << i << ": " << inventory[i]->get_name () << "\n";  }  ret << "   Location: " << location->get_name () << "\n";  return str (ret);}voidavatar::enter (ref<room> r){  location = r;}void avatar::play (){  //TODO: Check status if there are any incomplete ops,  //      like moving rooms, taking things, ...  str command = read_input ();  if (!strncasecmp (command.cstr (), "LOOK", 4))    dhash->retrieve (location->ID (), DHASH_NOAUTH, 		     wrap (this, &avatar::look_cb));  else     if (!strncasecmp (command.cstr (), "GET", 3))      get (command);    else       if (!strncasecmp (command.cstr (), "WEST", 4) || 	  !strncasecmp (command.cstr (), "EAST", 4) || 	  !strncasecmp (command.cstr (), "NORTH", 5) || 	  !strncasecmp (command.cstr (), "SOUTH", 5)) 	; //move (command);      else {	cout << "Huh??\n";	play ();      }}voidavatar::look_cb (dhash_stat stat, ptr<dhash_block> blk, vec<chordID> path){  //Later should change so that gets location info from cache,  //and cache is refreshed often.  if (stat == DHASH_OK) {    location = New refcounted<room> (blk->data, blk->len, dhash);    cout << "You are in the " << location->get_name () << ".\n";    if (location->north.get_name ().len () > 0)      cout << "To the north, is the " << location->north.get_name () << "\n";        if (location->south.get_name ().len () > 0)      cout << "To the south, is the " << location->south.get_name () << "\n";        if (location->east.get_name ().len () > 0)      cout << "To the east, is the " << location->east.get_name () << "\n";        if (location->west.get_name ().len () > 0)      cout << "To the west, is the " << location->west.get_name () << "\n";    for (uint i=0; i<location->avatars ().size (); i++)       cout << location->avatars ()[i]->get_name () << " is here.\n";    for (uint i=0; i<location->things ().size (); i++)      cout << "There is a " << location->things ()[i]->get_name () << ".\n";    play ();  } else {  }}void avatar::move (str command, av_cb_t cb){  ref<room> next = New refcounted<room> (str(""), dhash);  if (!strncasecmp (command.cstr (), "WEST", 4) &&       location->west.get_name ().len ())    next->set_name (location->west.get_name ().cstr (), 		    location->west.get_name ().len ());      else     if (!strncasecmp (command.cstr (), "EAST", 4) && 	location->east.get_name ().len ())      next->set_name (location->east.get_name ().cstr (), 		      location->east.get_name ().len ());        else       if (!strncasecmp (command.cstr (), "NORTH", 5) && 	  location->north.get_name ().len ())	next->set_name (location->north.get_name ().cstr (), 			location->north.get_name ().len ());          else 	if (!strncasecmp (command.cstr (), "SOUTH", 5) && 	    location->south.get_name ().len ())	  next->set_name (location->south.get_name ().cstr (),			  location->south.get_name ().len ());      if (next->get_name ().len ()) {    //TODO: change state to limbo    dhash->retrieve (location->ID (), DHASH_NOAUTH, 		     wrap (this, &avatar::done_move_lookup, next, cb));  } else {    cout << "You can't go that way!\n";    (*cb) (0);    //play ();  }}voidavatar::done_move_lookup (ref<room> next, av_cb_t cb, dhash_stat stat, 			  ptr<dhash_block> blk, vec<chordID> path){  if (stat == DHASH_OK) {    location = New refcounted<room> (blk->data, blk->len, dhash);    ref<mud_obj> a = New refcounted<mud_obj> (get_name ());    location->leave (a);    cout << "here 1\n";    dhash->insert (location->ID (), location->bytes (), location->size (),		   wrap (this, &avatar::done_remove, next, cb), NULL, DHASH_NOAUTH);  } else {    cout << "avatar::done_move_lookup dhash err " << stat << "\n";    (*cb) (0);  } }voidavatar::done_remove (ref<room> next, av_cb_t cb, dhash_stat stat, ptr<insert_info> i){  if (stat == DHASH_OK) {    //location = next;    cout << "here 2\n";    warn << "next name: " << next->get_name () << "\n";    warn << "next ID: " << next->ID () << "\n";    dhash->retrieve (next->ID (), DHASH_NOAUTH, 		     wrap (this, &avatar::done_enter_lookup, cb));    cout << "here 2.3\n";  } else {    cout << "avatar::done_move_lookup dhash err " << stat << "\n";    (*cb) (0);  } }void avatar::done_enter_lookup (av_cb_t cb, dhash_stat stat, ptr<dhash_block> blk, 			   vec<chordID> path){  if (stat == DHASH_OK) {    cout << "here 2.5\n";    ref<room> next = New refcounted<room> (blk->data, blk->len, dhash);    ref<mud_obj> a = New refcounted<mud_obj> (get_name ());    next->enter (a);    location = next;    cout << "here 3\n";    dhash->insert (location->ID (), location->bytes (), location->size (),		   wrap (this, &avatar::done_enter, cb), NULL, DHASH_NOAUTH);  } else {    cout << "avatar::done_move_lookup dhash err " << stat << "\n";    (*cb) (0);  } } voidavatar::done_enter (av_cb_t cb, dhash_stat stat, ptr<insert_info> i){  if (stat == DHASH_OK) {    cout << "here 4\n";    dhash->insert (ID (), bytes (), size (),		   wrap (this, &avatar::done_enter_cb, cb), NULL, DHASH_NOAUTH);  }  else {    cout << "avatar::done_move_lookup dhash err " << stat << "\n";    (*cb) (0);  }}voidavatar::done_enter_cb (av_cb_t cb, dhash_stat stat, ptr<insert_info> i){  if (stat == DHASH_OK)    (*cb) (1);  else {    cout << "avatar::done_move_lookup dhash err " << stat << "\n";    (*cb) (0);  }}void avatar::get (str command){  char num[100];  strcpy (num, command.cstr () + 4);  int k = atoi (num);  cout << "Getting object " << k << ".\n";  //location->remove (); do a set delete operation on room and if success,  //then add thing to inventory.  }stravatar::read_input (){  cout << "\nType something: ";  char input[100];   cin.getline (input, 100);  str command(input);  return command;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -