This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Hivemine.coffee
90 lines (72 loc) · 2.01 KB
/
Hivemine.coffee
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
# Hivemine.coffee
# Oversees all agents
###
General ideas:
Hivemine will store a server-by-server flatfile with important information such as locations, items, tasks, etc.
Agents will be able to:
- Query this flatfile
- Request items and tasks from other Agents
###
EventEmitter = require 'events'
BaseRole = require './roles/class'
fs = require 'fs'
path = require 'path'
class ServerInformation
constructor: (@Host, @Port = 25565, @Name) ->
Parts = @Host.split ':'
if Parts.length > 1
@Port = parseInt Parts[1] # Server port
@Host = Parts[0] # Server host
@Name = @Host if not @Name # Name for flatfile DB
class Hivemine extends EventEmitter # thanks wvffle!
constructor: ->
super()
@Agents = []
@Roles = {}
@readRoles()
inquire: (Key) =>
# Query the flatfile for a certain key
# Would return a forked future or something.
addAgent: (Agent) =>
if -1 == @Agents.indexOf Agent
Agent.hivemineInit @
@Agents.push Agent
findFreeAgent: =>
# TODO: take role into consideration
for Agent in @Agents
return Agent if Agent.Free
setServer: (@ServerInformation) =>
setAgentCount: (@AgentCount = 1) =>
loadRole: (C) =>
instance = new C()
if instance.Name
@Roles[instance.Name] = C
return true
false
readRoles: =>
# roles = path.join __dirname, 'roles'
fs.readdir './roles/', (err, files) =>
return err if err
for f in files
continue if f == 'class.coffee'
success = @loadRole require './roles/' + f
if not success
console.error 'Failed to load role:',f
fetchRole: (Name) => # Todo: Read ./roles/ and get the role
Role = @Roles[Name]
new Role() if Role
assignRole: (Agent, Role) => Agent.hivemineAssignedRole Role
requestRoleChange: (Agent, NewRole) =>
return if Agent.Role == NewRole
if Agent.Role
# Quit that role
Agent.Role.onExit ->
Agent.Role = undefined
@requestRoleChange Agent, NewRole
else
NewRole = @fetchRole NewRole
@assignRole Agent, NewRole if NewRole
###
requestItem
###
module.exports = new Hivemine()