// Process all monsters, so they // attack if there is a player here, in random order // move if random, in random direction, not up or down except very rarely! // todo: handle combat, deaths, etc #region MonsterStuff public IEnumerable <TootToSend> ProcessMonsters() { List <TootToSend> toReturn = new List <TootToSend>(); var dbMonsters = _db.GetCollection <Player>("monsters"); dbMonsters.EnsureIndex(x => x.Username); var monsters = dbMonsters.Find(r => r.IsActive && r.IsMonster); foreach (var monster in monsters) { TootToSend thisToot = new TootToSend(); // Is there a player in this room? var p = LoadPlayer(monster.X, monster.Y, monster.Z); if (p != null) { // Monster randomly attacks you if (_rnd.Next(5) == 3) { thisToot.Username = p.Username; thisToot.AccountId = p.AccountId; thisToot.Privacy = "direct"; thisToot.Content = Fight(monster, p); toReturn.Add(thisToot); } // Monster just looks at you else { thisToot.Username = p.Username; thisToot.AccountId = p.AccountId; thisToot.Privacy = "direct"; thisToot.Content = monster.Username + " looks at you nastily."; toReturn.Add(thisToot); } } // If not, let's maybe move else { if (_rnd.Next(10) < 5) { var ch = _rnd.Next(6); switch (ch) { case 0: if (_rooms[monster.X, monster.Y, monster.Z].ExitNorth < 3) { monster.Y--; UpsertMonster(monster); Console.WriteLine("--Monster " + monster.Username + " moved to " + monster.X + "," + monster.Y + "," + monster.Z); } break; case 1: if (_rooms[monster.X, monster.Y, monster.Z].ExitEast < 3) { monster.X++; UpsertMonster(monster); Console.WriteLine("--Monster " + monster.Username + " moved to " + monster.X + "," + monster.Y + "," + monster.Z); } break; case 2: if (_rooms[monster.X, monster.Y, monster.Z].ExitSouth < 3) { monster.Y++; UpsertMonster(monster); Console.WriteLine("--Monster " + monster.Username + " moved to " + monster.X + "," + monster.Y + "," + monster.Z); } break; case 3: if (_rooms[monster.X, monster.Y, monster.Z].ExitWest < 3) { monster.X--; UpsertMonster(monster); Console.WriteLine("--Monster " + monster.Username + " moved to " + monster.X + "," + monster.Y + "," + monster.Z); } break; case 4: if (_rooms[monster.X, monster.Y, monster.Z].ExitDown < 3 && _rnd.Next(20) == 1) { monster.Z++; UpsertMonster(monster); Console.WriteLine("--Monster " + monster.Username + " moved to " + monster.X + "," + monster.Y + "," + monster.Z); } break; case 5: if (_rooms[monster.X, monster.Y, monster.Z].ExitUp < 3 && _rnd.Next(20) == 1) { monster.Z--; UpsertMonster(monster); Console.WriteLine("--Monster " + monster.Username + " moved to " + monster.X + "," + monster.Y + "," + monster.Z); } break; } } // After move, is there a player here? If so, toot him the good news! var q = LoadPlayer(monster.X, monster.Y, monster.Z); if (q != null) { thisToot.Username = q.Username; thisToot.AccountId = q.AccountId; thisToot.Privacy = "direct"; thisToot.Content = monster.Username + " has entered the room."; toReturn.Add(thisToot); } } } return(toReturn); }
// --- PROCESSING LOGIC --- #region ProcessingLogic // Process this player's input, note upsert is done in main program after all processing public TootToSend Process(Player p, long statusId, string content) { TootToSend toReturn = new TootToSend(); toReturn.AccountId = 0; // We have this on calling func toReturn.Content = ""; toReturn.Username = p.Username; toReturn.Privacy = "direct"; // Not visible to anyone else // Update with last status update id to prevent re-doing same command if (!Program.Debug) { p.LastStatusId = statusId; // Only update this if we're not debugging } // parse string content = Program.TagRegex.Replace(content.Replace("<br />", "\n"), "").Trim(); //var words = content.Trim().Split(' '); var contentScan = "|" + content.Replace(" ", "| |") + "|"; contentScan = contentScan.Replace("?", ""); contentScan = contentScan.Replace("!", "").ToLower(); var punctuation = contentScan.Where(Char.IsPunctuation).Distinct().ToArray(); var words = contentScan.Split().Select(x => x.Trim(punctuation)); var showRoomDetails = true; // They're dead if (!p.IsActive) { toReturn.Content += "You are dead.\r\n Unfollow me and wait for a confirmatation message that you have left the game, then follow me to play again. \r\n"; return(toReturn); } // Directions if (words.Any("|north|".Contains)) { if (_rooms[p.X, p.Y, p.Z].ExitNorth == (int)ExitStates.Open) { p.Y--; toReturn.Content += "You exit north. \r\n"; } else { toReturn.Content += "You can't go that way.\r\n"; } } if (words.Any("|east|".Contains)) { if (_rooms[p.X, p.Y, p.Z].ExitEast == (int)ExitStates.Open) { p.X++; toReturn.Content += "You exit east. \r\n"; } else { toReturn.Content += "You can't go that way.\r\n"; } } if (words.Any("|south|".Contains)) { if (_rooms[p.X, p.Y, p.Z].ExitSouth == (int)ExitStates.Open) { p.Y++; toReturn.Content += "You exit south. \r\n"; } else { toReturn.Content += "You can't go that way.\r\n"; } } if (words.Any("|west|".Contains)) { if (_rooms[p.X, p.Y, p.Z].ExitWest == (int)ExitStates.Open) { p.X--; toReturn.Content += "You exit west. \r\n"; } else { toReturn.Content += "You can't go that way.\r\n"; } } if (words.Any("|up|".Contains)) { if (_rooms[p.X, p.Y, p.Z].ExitUp == (int)ExitStates.Open) { p.Z--; toReturn.Content += "You ascend to safer places in the dungeon - you go up to level " + p.Z + "... \r\n"; } else { toReturn.Content += "You can't go that way.\r\n"; } } if (words.Any("|down|".Contains)) { if (_rooms[p.X, p.Y, p.Z].ExitDown == (int)ExitStates.Open) { p.Z++; toReturn.Content += "You descend deeper into the dungeon - you go down to level " + p.Z + "... \r\n"; } else { toReturn.Content += "You can't go that way.\r\n"; } } // Look if (words.Any("|look|".Contains)) { toReturn.Content += "You look around...\r\n"; } // Help if (words.Any("|score|".Contains) || words.Any("|status|".Contains)) { toReturn.Content += "==Status==\r\n"; toReturn.Content += "Name: " + p.Username + "\r\n"; toReturn.Content += "Location co-ords (xyz): " + p.X + p.Y + p.Z + "\r\n"; toReturn.Content += "Level: " + p.Level + "\r\n"; toReturn.Content += "Health: " + p.Health + "\r\n"; toReturn.Content += "Strengh: " + p.Strength + "\r\n"; toReturn.Content += "Magic: " + p.Magic + "\r\n"; toReturn.Content += "Luck: " + p.Luck + "\r\n"; toReturn.Content += "Current weapons:(to do)\r\n"; toReturn.Content += "Wearing:(to do)\r\n"; return(toReturn); } // Help if (words.Any("|help|".Contains)) { toReturn.Content += "Commands so far are north, east, south, west, up, down, look, status and help.\r\n"; return(toReturn); } // Attack if (words.Any("|attack|".Contains) || words.Any("|kill|".Contains) || words.Any("|hit|".Contains)) { var monstersEnumerable = GetMonstersInRoom(p.X, p.Y, p.Z); if (monstersEnumerable != null) { toReturn.Content += Fight(p, monstersEnumerable.First()); toReturn.Content += Fight(monstersEnumerable.First(), p); } else { toReturn.Content += "Attack what? There's no one here.\r\n"; } return(toReturn); } // Don't understand if (toReturn.Content == "") { toReturn.Content = "I'm sorry, I didn't understand that. Try using simple words, type 'help' for a full list of commands I understand.\r\n"; } else { // Get info about room to append to this at end of every toot unless we say not to above if (showRoomDetails) { toReturn.Content += ExamineRoomExtras(p); } } return(toReturn); }